Windows 7 Sensor and Location platform: Implementation of own sensor (Part 3/3)

Windows 7 +1Recently I have told about use Sensor and Location platform in the applications. We have seen, that use of this component set can be very convenient for the application and do not demand many efforts. Also we have an opportunity to work with set of devices in the unified style. Problem of use of this platform there is a presence of drivers for Windows 7 and presence of wrappers for Sensor API. Development of the driver for the device – a task of the manufacturer. And implementation of support in Sensor API can be made own forces.

As I already spoke an entry point is SensorManager class . By means of this class it is possible to get access to the necessary sensor controls and to work with them. This class has methods of getting of the list of all sensors, sensor getting by ID or on type, request to sensor use, and also event of change of quantity of sensors in system.

SensorManager

Each sensor has two main types of identifiers – SensorId and TypeId. TypeId identifies a separate class of devices. For example, by it it is possible to get all sensors of light in system, or any other types of devices. SensorId it is given it is unique to each device. For example, if in system three same sensors of movements everyone will have the unique identifier. Is also CategoryId which unites sensors in a category.

Each identifier represents GUID. They are set by manufacturers by developing of the device and drivers. Thus, it is possible to get a concrete sensor only knowing it ID. Each sensor is presented by Sensor class. It has the general information about a sensor and methods which data from the generalised collections in not typified kind allow to obtain. It is clear, that such data presentation is not so convenient for our applications. Therefore for each sensor it is accepted to implement a class-wrapper within Sensor API. It is implemented by inheritance from general class Sensor. In demonstration examples already there are two such realisations – for 3D accelerometer and for light sensor. However, at the device which we considered earlier there are also touch buttons which also can be used. Therefore let’s implement such class for this sensor.

We will define a new class which will be the inheritor of Sensor class. That it was recognised in Sensor API it it is necessary to mark with SensorDescription attribute in which to specify TypeId for this type of sensors. In base class Sensor there are two important things for us – DataReport property and DataReportChanged event. This property contains data from a sensor, and event fires at their change. The task of our class – to take advantage of these data and to deliver to their user of our class in a convenient kind. For this purpose we will create one more small class which will be engaged in analysis of the information from DataReport.

Experimental by we will find out, that by pressing of the button 1 the code 1 is generated, by pressing 2 – the code 2 is generated, by pressing 3 – the code 4 is generated, and by pressing 4 – the code 8 is generated. It is visible, that binary bits here are used. Also the code 0 in a case unpress of all buttons is generated. Thus, we can write the following code.

[SensorDescription("545C8BA5-B143-4545-868F-CA7FD986B4F6")]


public class SwitchArraySensor : Sensor


{


    public class SwitchArraySensorData


    {


        private static Guid KeyStateProperyId = new Guid(@"38564a7c-f2f2-49bb-9b2b-ba60f66a58df");


 


        public SwitchArraySensorData(SensorReport report)


        {


            uint state = (uint) report.Values[KeyStateProperyId][0];


            Button1Pressed = (state & 0x01) != 0;


            Button2Pressed = (state & 0x02) != 0;


            Button3Pressed = (state & 0x04) != 0;


            Button4Pressed = (state & 0x08) != 0;


        }


 


        public bool Button1Pressed { get; private set; }


        public bool Button2Pressed { get; private set; }


        public bool Button3Pressed { get; private set; }


        public bool Button4Pressed { get; private set; }


    }


 


    public SwitchArraySensorData Current


    {


        get { return new SwitchArraySensorData(DataReport); }


    }


 


    public event EventHandler StateChanged;


 


    public SwitchArraySensor()


    {


        DataReportChanged += SwitchArraySensor_DataReportChanged;


    }


 


    void SwitchArraySensor_DataReportChanged(Sensor sender, EventArgs e)


    {


        if (StateChanged != null)


        {


            StateChanged.Invoke(sender, e);


        }


    }


}

Actually, this class is a wrapper in Sensor API for the sensor necessary to us. For its use I should subscribe for StateChanged event and receive the information through Current property.

For getting of the list of accessible sensor of the some type it is possible to use GetSensorsByTypeId method of SensorManager class. Thus TypeId these sensor it will be defined on the basis of set SensorDescription attribute. Now, using these sensors we can subscribe on necessary event and obtain data in a kind convenient for the application. For example, we can display on the form a state of pressing of buttons.

private void Window_Loaded(object sender, RoutedEventArgs e)


{


    var sensors = SensorManager.GetSensorsByTypeId<SwitchArraySensor>();


    foreach (SwitchArraySensor sensor in sensors)


    {


        switch (sensor.FriendlyName)


        {


            case "Left Switch Array Sensor":


                sensor.StateChanged += delegate(object leftSensor, EventArgs arg)


                {


                    var buttons = ((SwitchArraySensor)leftSensor).Current;


                    SwitchState(LeftButton1, buttons.Button1Pressed);


                    SwitchState(LeftButton2, buttons.Button2Pressed);


                    SwitchState(LeftButton3, buttons.Button3Pressed);


                    SwitchState(LeftButton4, buttons.Button4Pressed);


                };


                break;


            case "Right Switch Array Sensor":


                sensor.StateChanged += delegate(object rightSensor, EventArgs arg)


                {


                    var buttons = ((SwitchArraySensor)rightSensor).Current;


                    SwitchState(RightButton1, buttons.Button1Pressed);


                    SwitchState(RightButton2, buttons.Button2Pressed);


                    SwitchState(RightButton3, buttons.Button3Pressed);


                    SwitchState(RightButton4, buttons.Button4Pressed);


                };


                break;


        }


 


    }


}





As a result we will receive the application which looks as follows.





Certainly, an example with realisation of a similar sensor synthetic enough. However, it obviously shows process of connection of the sensor to Sensor API.



I wish successes to you in creation of your context-aware applications!



Sample application:



Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>