Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Using Windows 7 Light Sensor in Your Application

, 17 Jul 2010
Rate this:
Please Sign up or sign in to vote.
One of the new features we got with Windows 7 is 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.

One of the new features we got with Windows 7 is 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 provide a Windows 7 compliant driver. Microsoft practically opened this market to competition, so we can expect a lot of these sensors in the coming future.

Ambient Light Sensor

One sensor type which becomes increasingly popular 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 lighting condition such as direct sun or no light.
  • Reduce screen brightness according to current lighting 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

The 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

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Share

About the Author

Arik Poznanski
Software Developer (Senior) Verint
Israel Israel
Arik Poznanski is a senior software developer at Verint. He completed two B.Sc. degrees in Mathematics & Computer Science, summa cum laude, from the Technion in Israel.
 
Arik has extensive knowledge and experience in many Microsoft technologies, including .NET with C#, WPF, Silverlight, WinForms, Interop, COM/ATL programming, C++ Win32 programming and reverse engineering (assembly, IL).
Follow on   Twitter   Google+

Comments and Discussions

 
Questionvirtuallightsensor.exe can't run Pinmemberbrilliant21822-Jan-13 22:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 18 Jul 2010
Article Copyright 2010 by Arik Poznanski
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid