Sample Application: Sensor Explorer Whitepaper

By Dale Taylor

Download Article

Sensor Explorer [PDF 287KB]

This document explains how to access the sensor information in Win8 using the Desktop C++ interface.  Here, I’ll walk you through a fully functioning sample application you can use as a reference, modify, and build at your convenience.  You’ll see exactly how to assemble all of the components into a working program.

To use the Sensor Explorer sample, download the sample files and unzip them into a folder.  Included in the sample is a se32.sln file, which will open up the project in Visual Studio* 2012.  This enables you to build the Win8 version of the program.  We will be looking through the code contained in the main.cpp file and discussing its structure.

Included in the main.cpp file are links to the MSDN documentation as noted in comments.  These links contain snippets and fragments that were assembled, filled in, deciphered, and completed to build this application.

Structures are to enable COM in the application

The communication method with the OS in this program is COM.  In the source code (main.cpp) right after the globals and a few const definitions, you will see this code:

This is the class that defines the Sensor Manager Events.  This class is a COM object that is used to register the Sensor Manager.  The main thing we accomplish with this class is at the bottom with the OnSensorEnter event.  It is in this event that we send the WM_USER+1 Message to the main window.  This enables us to handle the events and do what is necessary inside of our program.

Immediately following the Sensor Manager Event Definitions is the COM structure used to define the sensor event handlers for ISensorEvents.

This is where we define the interface for Windows to call when Sensor events happen.  The routine where the bulk of the work happens is OnDataUpdated.  This is what gets called when sensor data is updated.

The format of these classes is defined by COM and needs to be as shown.  Here is the link to the examples on the MSDN site where Microsoft discusses the COM setup structures:

Main Message Loop

The WinMain routine is similar to any traditional Desktop app and is used to create the window and display it. It also has the message handling code. .

The logic of this program runs like this:

WinMain is the routine called when the app is run.

                A window is created.

                A message pump is created.

                Window messages are then handled.

                When the close window message is received, the app is terminated.

The WM_CREATE message is issued when the main window is created.  In the WM_CREATE handler is where the list view is filled in, the event handlers are set, and the initial sensor information is then loaded into the list as seen here:

Once the initial window is in place, events start to arrive via the OnDataUpdated event, which then triggers an update to the lists so that the data is refreshed and displayed for our viewing pleasure.

Registering the COM event handlers

In the Load Sensors routine, when loading the local copy of the sensor data, I also set up the event handlers so that the OnDataUpdated event will be called.  This snippet shows where that happens.

Following the SetEventSink, we need to tell the OS which event types we are interested in responding to.  This is accomplished with the SetEventInterest call as shown next.

At this point our sensor event handlers are called and we begin receiving updates.

Local data for ease of display

Inside this program, I make a local copy of the sensor data and store it in a structure, making sure I am consistent when accessing this data.  The 3 int values i, j, and k are always used for accessing this information.  The i value is the index into the sensors by category.  The j value is the index into the sensors in a category.  And the k value is the index into the individual sensor data for the sensor j points to at that time. 

The structures holding the data are shown below.

When an UpdateData event is received, the structure is traversed looking for the sensor that the update applies to.  When it is found, that individual data is updated and then the line in the table is also updated.  This code snippet shows where we traverse through the sensor data.

This sample program provides a template you can use for accessing Sensor data in a Desktop program.  The code is well documented and working.  Anywhere you see List items used, those are used for displaying the data on screen.  The list is not a required item in your app, but simply used to present the sensor data here.

For more information, leave a comment and I can reply which enables future readers to see the replies too.

License Notices

Intel and the Intel logo are trademarks of Intel Corporation in the US and/or other countries.

Copyright © 2012 Intel Corporation. All rights reserved.

*Other names and brands may be claimed as the property of others.

Para obtener información más completa sobre las optimizaciones del compilador, consulte nuestro Aviso de optimización.
Icono PDF sensorexplorer.pdf286.84 KB