Switching themes based on the ambient light? - Rudi Grobler
Thursday, August 20, 2009 3:30 PM rudi

Switching themes based on the ambient light?

This article demonstrates how to use the Windows Sensor and Location platform to dynamically change a applications theme based on the ambient light luminance intensity!

What we need

Windows Sensor and Location platform

The Windows Sensor and Location platform, which is new for Windows 7, enables your computer and applications to adapt to their current environment. With location sensors -- including GPS devices, WWAN radios, and even triangulation technology -- your applications and gadgets can know exactly where they are, enabling them to provide more locally relevant content and functionality. Ambient light sensors, for example, can allow your computer to automatically adjust your screen's brightness based on the current lighting conditions. They can also enable applications to optimize their content for readability, making your computer more useful in a range of operating environments than ever before.

The platform provides a standard way to integrate sensor and location devices into Windows, as well as a standard programming interface for applications to take advantage of these devices. On Windows 7, the user has control over how data from these devices is exposed to applications. Hardware manufacturers can learn how to write sensor and location drivers by installing the Windows 7 WDK. Developers can learn how to write location-aware and sensor-enabled applications by installing the Windows 7 SDK.

The Basics

The Windows 7 Sensor Development Kit is equipped with a ambient light sensor! To read the current ambient light luminance intensity, we will use the Windows® API Code Pack for Microsoft® .NET Framework

First, we need to find all the ambient light sensor available and then set it to automatically poll for new data reports

SensorList<AmbientLightSensor> als = SensorManager.GetSensorsByTypeId<AmbientLightSensor>();
foreach (var sensor in als)
{
    sensor.AutoUpdateDataReport = true;
    sensor.ReportInterval = 10;
    sensor.DataReportChanged += new DataReportChangedEventHandler(sensor_DataReportChanged);
}

All that is left to do then is to use the data report!

void sensor_DataReportChanged(Sensor sender, EventArgs e)
{
    AmbientLightSensor als = sender as AmbientLightSensor;

    // Read the als.CurrentLuminousIntensity.Intensity... 0~200            
}

And that is it!

Why should I care?

Imagine the following scenario: I have a application which has a normal theme and a high contrast theme for when there is direct sunlight and I want to switch between these based on the luminance intensity from the sensors?

For this scenario I took the WPF themes and extended the theme manager to support

Using the basics we just looked at and the magic off attached behaviors, this is how you would then use it

<Window x:Class="WPF.Themes.Demo.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:themes="clr-namespace:WPF.Themes;assembly=WPF.Themes">
    <themes:ThemeManager.ThemeSelector>
        <themes:AmbiendLighThemeSelector NormalTheme="ExpressionDark" HighContrastTheme="ExpressionLight" />
    </themes:ThemeManager.ThemeSelector>

    <!-- Add your UI stuff here... -->

</Window>

This is also easily portable to the Blend SDK behaviors!

If you found this article interesting or useful, please kick it on DotNetKicks.com

More Information

Freescale MCF51JM128 V1 ColdFire 32-bit microprocessor

Freescale MMA7260QT 3-axis accelerometer

Freescale MPR084 8-button touch sensor

Freescale MC34673 Li-ion battery charger

Comments

# Switching themes based on the ambient light

Thursday, August 20, 2009 3:35 PM by DotNetKicks.com

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# re: Switching themes based on the ambient light?

Thursday, August 20, 2009 4:56 PM by Colin E.

This sounds seriously cool - although you really need to shoot a video showing this in action.

Colin E.

# re: Switching themes based on the ambient light?

Friday, August 21, 2009 12:46 AM by Laurent

Nice idea. I imagine we could use that kind of code to control lighting in a house too, and modulate the colors depending on the level of sunlight in the room, to provide different feelings during the day. Love it.

Laurent

# re: Switching themes based on the ambient light?

Friday, August 21, 2009 7:15 AM by pieterg

absolutely awesome!

# Switch Array Sensor - Rudi Grobler

Friday, August 21, 2009 4:30 PM by Switch Array Sensor - Rudi Grobler

Pingback from  Switch Array Sensor - Rudi Grobler

# Switching themes based on the ambient light? - Rudi Grobler &laquo; Meanings of Words

Pingback from  Switching themes based on the ambient light? - Rudi Grobler &laquo;  Meanings of Words

# Twitter Trackbacks for Switching themes based on the ambient light? - Rudi Grobler [dotnet.org.za] on Topsy.com

Pingback from  Twitter Trackbacks for                 Switching themes based on the ambient light? - Rudi Grobler         [dotnet.org.za]        on Topsy.com

# Daily tech links for .net and related technologies - August 20-24, 2009

Sunday, August 23, 2009 5:21 PM by Sanjeev Agarwal

Daily tech links for .net and related technologies - August 20-24, 2009 Web Development ELMAH (Error

# Twitted by shell_beach

Monday, August 24, 2009 1:09 PM by Twitted by shell_beach

Pingback from  Twitted by shell_beach

# Windows API Code Pack for .NET Framework | {Programming} &amp; Life

Pingback from  Windows API Code Pack for .NET Framework | {Programming} &amp; Life

# This Week C9: VS 2010, Must-Have Developer Utilities, Upgrade your home

Saturday, September 05, 2009 3:00 AM by ComponentGear.com Feed

This week on Channel 9, Dan (back from paternity leave) and Brian (back from Brazil) discuss the week&#39;s