Using Windows 7 Light Sensor in Your Application

June 29, 2010

One of the new features we got with Windows 7 is a the Windows Sensor and Location Platform. This platform enables us, developers, to access a variety of sensors with a standard API. No more rewrite-entire-device-layer-because-we-changed-vendors issues. All that is required is that the hardware vendor will provide a Windows 7 compliant driver. Microsoft practically opened this market to competition, so we can expect allot of these sensors in the coming future.


Ambient Light Sensor


One sensor type which becomes increasingly poplar with laptops is the Ambient Light Sensor. This sensor type measures the amount of light in the computer surroundings.


There are numerous applications for such a sensor, just to name a few:



  • Change application theme to a darker one in case there’s almost no surrounding light, to avoid stress on the user eyes.

  • Use high contrast colors in extreme lightning condition such as direct sun or no light.

  • Reduce screen brightness according to current lightning conditions.

  • Reduce sound volume in applications when the light is low (maybe someone is sleeping).

Using Ambient Light Sensor in a .NET application


Using the light sensor is pretty simple in .NET with the help of Windows API Code Pack and another helper file from the Windows 7 Training Kit for Developers.


I’ll show you an example using a WPF application but the same will work with a WinForms application.


Installing Virtual Light Sensor


First step is to install the Windows 7 SDK. Among other things it contains a virtual light sensor you can use in case you don’t actually have a light sensor on your computer.


For installing the virtual light sensor follow these instructions.


In case you fail to install virtual light sensor, make sure you install the one which is suitable for your operating system. 64bit systems need to install the 64bit version of the virtual light sensor driver (the 64bit driver is stored in the Windows 7 SDK subfolder named x64).


After you have successfully installed the virtual light sensor driver you can control the simulated light sensor using the utility VirtualLightSensor.exe:


image


Adding Ambient Light Sensor to Our Application


Now we will open our WPF application, named LightSensorDemo.


Add a reference to Microsoft.WindowsAPICodePack.Sensors.dll


Add the file SensorHelper.cs to the project. This file appears in one of the examples of the Windows 7 Training Kit for Developers. It provides a convenient facade around the Windows API Code Pack Sensor API.


Note, this demo is not a great example for WPF best practices..


Add a label to your MainWindow.xaml:




<Window x:Class=”LightSensorDemo.MainWindow”
        xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
        xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
        Title=”MainWindow” Height=”150″ Width=”150″>
    <Grid>
        <Label Name=”_textBlock” />
    </Grid>
</Window>

Add the following code to your MainWindow.xaml.cs:




public partial class MainWindow : Window
{
    SensorHelper<AmbientLightSensor, LuminousIntensity> _lightSensor;

    public MainWindow()
    {
        InitializeComponent();

        try
        {
            _lightSensor = new SensorHelper<AmbientLightSensor,
                                            LuminousIntensity>();
            _lightSensor.Initialize();
            _lightSensor.PropertyChanged +=
                new PropertyChangedEventHandler(
                    _lightSensor_PropertyChanged);
        }
        catch (SensorPlatformException)
        {
            MessageBox.Show(“Error while loading sensor. \n” +
                      “Make sure you run the Virtual Light Sensor “ +
                      “before the application”, “Error”);
        }
    }

    void _lightSensor_PropertyChanged(object sender,
                                      PropertyChangedEventArgs e)
    {
        Dispatcher.BeginInvoke(new Action(
            delegate()
            {
                if (_lightSensor.Value.Intensity > 200)
                {
                    _textBlock.Content = “Light”;
                    _textBlock.Foreground = Brushes.Black;
                    _textBlock.Background = Brushes.White;
                }
                else
                {
                    _textBlock.Content = “Dark”;
                    _textBlock.Foreground = Brushes.White;
                    _textBlock.Background = Brushes.Black;
                }
            }));
    }
}

The end result:


image


image


You can find the source code for this demo application here.


That’s it for now,
Arik Poznanski.

kick it on DotNetKicks.com Shout it

Add comment
facebook linkedin twitter email

Leave a Reply

one comment

  1. pedror414June 25, 2011 ב 09:52

    I was not familiar with all this written in this blog. I came to learn a lot from this. Thanks