Windows 7: Sensor Platform

sensor_boardPart of Windows 7 is a completely new platform for Location and Sensors. This is a framework that tries to remove the old legacy of COM-ports for communication with external devices. Even a lot of today's USB devices, still rely on the COM protocol to communicate with the PC. The Location and Sensor Platform in Windows 7 improves this with a modern architecture for arbitrary hardware sensory input.

One of the most important advances in this platform is the ability for multiple applications to use the same sensors at the same time, this has always been some of a problem with GPS, where only one instance of the application could read the GPS coordinates.

The first use of this platform will probably with in the laptops, which already have ambient light sensor and sometimes 3D accelerometers built into them. When these devices starts appearing, you can extend the application you’re developing to benefit and utilize these new features.

Getting Started

Since Windows 7 was just released and it takes a while for hardware manufacturers to take use of the new software capabilities, you’re only option to learn the new platform APIs is to buy a development board like the one below.

The above board is a demo board from Freescale that I have connected to my desktop computer using a USB cable. Board has sensors for ambient light, 3D accelerometer and there are 8 touch buttons on the back. You can buy the device from here: http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=JMBADGE&tab=Buy_Parametric_Tab&fromSearch=false

Troubleshooting Driver Installation

When you first connect the board to your Windows 7 computer, it will recognize the device and install in the device manager. This does unfortunately not work, at least not if you’re running 64-bit version of Windows.

 device_manager_1

 

The sensory board will be installed as a HID-compliant device and does not appear in the Sensory Control Panel. The best way to find the Sensory Control Panel is to type “sensor” in the search field.

control_panel_search

When a sensory device is installed it has to be enabled in this control panel, but without manually configuring the correct driver nothing will appear in the above control panel.

To enable the correct drivers for the board, you need to download the “Sensor Development Kit for Windows 7” from the Windows Sensor and Location Platforms on MSDN Code Gallery. Inside this file you will find a folder called Drivers and within that a folder called Binaries. Depending on your bit platform, you should choose the adm64 if you have a 64-bit version of Windows or the x86 if you have a 32-bit version. This is the location at which we will find the right driver for our device.

Going back to the Device Manager, right-click on the HID-compliant device and choose “Update Driver Software…”. Choose the second option in the dialog, the “Browse my computer…”. Pick the second option again, which says “Let me pick from a list…”. This is because we need to “force” Windows to accept a different driver.

In the select the device driver dialog, click the “Have disk…” button and locate the correct driver. Eg. C:\Users\Sondre\Downloads\Windows 7 SDKs\Sensor_Development_Kit\Driver\Binaries\amd64.

Update_Driver

As you can see in this screenshot, the driver is not digitally signed so you will see a warning when you try to install this. Accept the warning and the device will be installed properly.

device_manager_2

The device should now appear under a separate node called Sensors, if installed correctly.

Note: There is a more correct way of installing the device, though I’ve never successfully managed to do it on my computers. The process is to run the SensorKitDriverInstaller.exe that comes with the Development Kit BEFORE you connect the device to your computer.

Enabling Your Sensor Device

After a successful installation of your device board you can now enable it from the control panel.

Control_Panel_Sensor

By default, everyone has access to the device. Restricting access to the device can be done using the Change user settings.

Control_Panel_Sensor_Access

That’s all to it, there are no other features for the sensor device in the control panel. There are some extra features for location devices such as GPS, which I’m not covering in this article.

Reading Sensory Input

Included with the SDK is a simple diagnostic tool (Tools\DiagnosticTool\Binaries\SensorDevKitDiagnosticApp.exe) which you can run to validate that everything is installed and configured correctly. Make sure this application works properly before you start programming against the device.

Sensor_Diagnostic

This application shows the current position of the board in a 3D space, how much light it’s seeing and whether any of the touch areas on the back are on, as you can see the bottom right is currently pressed.

SDK includes documentation, source and examples on how to do to different features with this new functionality, so I won’t cover everything in this post. Instead I will give a quick explanation on how you can start reading sensor data.

Programming Sensory Input

Create a new Visual Studio project, I called my project WhereAmI as the purpose of the application is to figure out if I’m sleeping, working or outside in the sun. Choose any type of project, I’m using Windows Forms for this example. The form will have nothing but two label controls, which will display a status message and the value.

When the project is created, right click on the solution and add existing project. Located and add the project called Windows7.SensorAndLocation, which has the API wrappers for the sensor and location. Add a reference to the Windows7.SensorAndLocation from your own project.

There are many ways you can get the list of available sensors, in our example application we will hook up an event handlers to the form load event and find one instance of the AmbientLightSensor. Alternatively you could look up the TypeId or the CategoryId, for simplicity we’ll use the FriendlyName. The freescale board has 4 different sensors, they are named Accelerometer Sensor, Ambient Light Sensor, Left Switch Array Sensor, Right Switch Array Sensor.

DataUpdated is the event you can hook up to on a specific sensor to receive messages when the data of a sensor changes, remember that the event handlers are called from a separate thread than the UI-thread so you need to ensure that you call Invoke properly.

LightStatus 

The final result, it will display 3 different statuses depending on the light level where the sensor board is. See the full source at the bottom of this post.

Windows 7 for Developers

This is the second of many posts I’m writing about Windows 7 for developers, some of these posts will be featured in my technical presentations that I do at events such as the Norwegian .NET User Group and MSDN Live.

1: Windows 7: Virtual Windows XP
2: Windows 7: Sensor Platform

Source Code

Here follows the full source code for the sample.

Note: If you don’t have a card connected to the computer, the application won’t be able to get any sensors but instead crash, this is probably a bug in the code from Microsoft and will be fixed in the future. Until then, I suggest wrapping the GetSensors call inside a try/catch.

namespace WhereAmI
{
    using System;
    using System.Windows.Forms;
    using Windows7.Sensors.Sensors.Light;
    using Windows7.Sensors;

    public partial class MainForm : Form
    {
        AmbientLightSensor _sensor;
        float _illuminanceLux;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            AmbientLightSensor[] sensors = SensorManager.GetSensorsByType<AmbientLightSensor>();

            if (sensors.Length == 0)
            {
                label1.Text = "No Light Sensor Detected.";
            }
            else
            {
                _sensor = sensors[0];
                _sensor.DataUpdated += new SensorDataUpdatedEventHandler(_sensor_DataUpdated);
                AmbientLightSensorDataReport report = (AmbientLightSensorDataReport)_sensor.GetDataReport();
                _illuminanceLux = report.IlluminanceLux;
                UpdateStatus();
            }
        }

        private void _sensor_DataUpdated(Sensor sensor, SensorDataReport dataReport)
        {
            AmbientLightSensorDataReport report = (AmbientLightSensorDataReport)dataReport;
            _illuminanceLux = report.IlluminanceLux;

            UpdateStatus();
        }

        private void UpdateStatus()
        {
            if (InvokeRequired)
            {
                Invoke(new MethodInvoker(UpdateStatus));
                return;
            }

            if (_illuminanceLux < 10)
            {
                label1.Text = "You are sleeping in your bed.";
            }
            else if (_illuminanceLux > 300)
            {
                label1.Text = "You are outside in the sun.";
            }
            else
            {
                label1.Text = "You're busy working.";
            }

            label2.Text = "Value: " + _illuminanceLux.ToString();
        }
    }
}


Comments

Comments are closed

Pages

Search

Tags

None

    Recent posts

    Recent comments

    Blogroll

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.