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

Sensor API: Make your Win32 applications environment-aware in Windows 7

By , 28 Sep 2009
 

Introduction

The Sensor API is a simple Windows 7 ActiveX interface that allows you to communicate with sensors. Sensors can detect light, temperature, electricity, location (GPS), motion and so on. (The supported list of categories is available in MSDN).

You need

Where to Find Sensors?

In the SDK, there is a Virtual Light Sensor that can simulate a light sensor. As Windows 7 is released, more and more devices will support the Sensors abstraction API.

In my site, I've also created a GPS Sensor Driver that reads actual GPS data from a COM port. Read more here.

Overview

The sample here provides code for both full (explorer) sensor capabilities and also a simple SENSOR namespace that implements sensors in a library you can use. The steps for the full explorer are:

  • Call CoCreateInstance() to instantiate an ISensorManager.
  • Call ISensorManager::GetSensorsByCategory using the requested category (all categories in MSDN).
  • From the returned ISensorCollection, call GetCount() to get the number of sensors in that category, then call GetAt() to retrieve an ISensor.
  • Use ISensor methods to learn about the sensor properties, type, ID etc.
  • To get the supported data fields, call ISensor::GetSupportedDataFields().
  • Use GetCount() and GetAt() from the returned IPortableDeviceCollection to get the data fields.
  • Use ISensor::GetData() to retrieve the current data in a ISensorDataReport.
  • Use ISensorDataReport::GetSensorValue to get a specific value, use ISensorDataReport::GetTimestamp to return the time at which the data was collected.

Sensors are disabled by default, so you may have to call ISensorManager::RequestPermissions if you get an access denied error when retrieving the sensor state.

All this stuff is used in main.cpp.

Sensor Manager Events

The Sensor Manager can notify you when something occurs. Implement an ISensorManagerEvents, then pass it to ISensorManager::SetEventSink. Currently, this interface implements the OnSensorEnter() function which is called when a sensor is available.

Sensor Events

The Sensor can notify you when something occurs. Implement an ISensorEvents, then pass it to ISensor::SetEventSink, then call ISensor::SetEventInterest to indicate the types of notifications you want to receive. Currently, ISensorEvents implements four member functions which notify you when an event occurs, when data is changed, when state is changed, and when the sensor is disabled.

Using an Actual Sensor

The Virtual Light sensor is OK for testing purposes, but we need real data. I've created a small sample driver project, GPSDirect, which can read and parse actual GPS information from a COM port (Bluetooth, USB etc.). This driver currently supports parsing GGA and RMC sentences to provide position information.

SensorLib

In case you just want a simple interface, you can use my senslib* project and the SENSOR namespace. This currently implements a SENSOR and a LIGHTSENSOR class to be used as follows:

INT_PTR CALLBACK D_DP(HWND hh,UINT mm,WPARAM ww,LPARAM ll)
{
    HWND hT = GetDlgItem(hh,701);
    HWND hL2 = GetDlgItem(hh,902);
    static SENSOR::LIGHTSENSOR* ls = 0;
    switch(mm)
    {
        case WM_INITDIALOG:
        {
            SetClassLongPtr(hh,GCLP_HICONSM,(LONG_PTR)hIcon1);
            SetClassLongPtr(hh,GCLP_HICON,(LONG_PTR)hIcon1);
            if (!SENSOR::InitializeSensorManager())
            // Make sure we are running under Windows 7
            {
                EndDialog(hh,-1);
                break;
            }
            // Install the light sensor, giving it our HWND and a custom message
            ls = new SENSOR::LIGHTSENSOR(hh,WM_USER);
            break;
        }
        case WM_USER:
        {
            // Sent by LIGHTSENSOR.
            // ww == 0 -> ll == address of a SENSOR_DATA containing the sensor data
            // ww == 1 -> Sensor is disabled
            // ww == 2 -> State changed, ll = the new SensorState
            if (ww == 0)
            {
                SENSOR::SENSOR_DATA* data = (SENSOR::SENSOR_DATA*)ll;
                if (data)
                {
                    for(unsigned int i = 0 ; i < data->Num ; i++)
                    {
                        if (data->keys[i] == SENSOR_DATA_TYPE_LIGHT_LEVEL_LUX)
                        {
                            TCHAR x[100];
                            _stprintf(x,_T("Lux: %f"),data->data[i].fltVal);
                            SetDlgItemText(hh,701,x);
                        }
                    }
                }
            }
            return 0;
        }
        case WM_CLOSE:
        {
            delete ls;
            SENSOR::FreeSensorManager();
            EndDialog(hh,0);
            return 0;
        }
    }
    return 0;
}

To-do...

  • Add the Location API.

History

  • 28 - 9 - 2009: Library update and GPS Sensor.
  • 2 - 5 - 2009: First release.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Michael Chourdakis
Engineer
Greece Greece
Member
I'm working in C++, PHP , Flash and DSP Programming, currently experimenting with Windows 7 technologies and professional audio applications.
 
I 've a PhD in Digital Signal Processing.
 
My home page: http://www.michaelchourdakis.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
SuggestionGPS sensormemberFloppe29 Apr '13 - 21:24 
Hi,
 
Great tool you've created to help developers to get started with GPS sensors before they have any real hardware to test on. One feature which would be nice is to have report timestamp taken from GPS NMEA data instead of computer time.
 
Regards,
GeneralRe: GPS sensormemberMichael Chourdakis29 Apr '13 - 21:31 
You may try my GPSSensor for that.
http://www.michaelchourdakis.com/gps7
Michael Chourdakis - Music & DSP Engineer
http://www.turboirc.com - Software & Research

Questionviruses found in compressed file?memberJeffrey_YH5 Sep '10 - 20:03 
http://threatinfo.trendmicro.com/vinfo/virusencyclo/default5.asp?VName=PAK_Generic.001[^]
QuestionAre you answering questions concering your GSP sensor here too?memberneil young29 Oct '09 - 8:02 
Regards
AnswerRe: Are you answering questions concering your GSP sensor here too?memberMichael Chourdakis29 Oct '09 - 8:34 
Sure, what's the problem ?
GeneralRe: Are you answering questions concering your GSP sensor here too?memberneil young29 Oct '09 - 9:03 
Thanks.
Should we proceed via direct mail?
GeneralRe: Are you answering questions concering your GSP sensor here too?memberneil young29 Oct '09 - 10:29 
OK. I was launching TurboGPS and did connect it directly to COM3. There is a Bluetooth GPS mouse. Works. Sometimes it makes a very annoying sound (like ActiveSync). Then I switch to you Sensor API driver, which is installed and configured to use the same COM port, same speed. Doesn't work at all..
 
Regards
GeneralRe: Are you answering questions concering your GSP sensor here too?memberMichael Chourdakis29 Oct '09 - 11:00 
Does the sensor app itself shows NMEA data ?
 
When you install it (and perhaps enable the sensor in CPanel), it should show like http://www.turboirc.com/gps7/1.png[^]
 
and update the nmea data periodically. Is that true ?
GeneralRe: Are you answering questions concering your GSP sensor here too?memberneil young29 Oct '09 - 11:09 
The screenshot is here http://maps.alphadex.de/2009-10-29_2134.png[^]
But after a couple of seconds the program crashes.
 
The same crash happens while opening another third party driver.
GeneralRe: Are you answering questions concering your GSP sensor here too?memberMichael Chourdakis29 Oct '09 - 11:22 
What do you mean 'another third party driver' ?
 
So the driver installs and the sensor is only visible for a while, then the driver crashes ? Or the application crashes ?

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 28 Sep 2009
Article Copyright 2009 by Michael Chourdakis
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid