Windows 7 and Sensor API
One of the coolest features in Windows 7 is the new Sensor (and Location) API. The APIs are completely native, but we can use it with the .NET Framework if we install the Windows API Code Pack 1.0 .
If we want to try testing the Sensor API, we can do this even without a real physical sensor. In the Windows 7 SDK (x86 and x64) you can find Virtual Light Sensor - a simple application that simulates Ambient Sensor.
The screenshot shows how the program looks.
1) Buy a sensor
If we want to test with a real sensor, you can purchase it on Freescale . Actually, this is how it looks: Freescale board has the following sensors:
- Light sensor
- 3D accelerometer
- Two touch bars
2) Firmware update
In order to use the device in Windows 7, we need to download the Windows 7 Sensor Development Kit and update the firmware using the instructions from the readme file. To update, you must connect the device to the system. To do this, following the instructions, we need to touch the E4 button before “Press E4 to enter bootloader.” Appears on the display.
In the following image, I highlighted the display and the E4 button, so I need to touch the button quickly. 3) Test the sensor in Windows 7 If we want to test all the sensors, we need to run the test program (SensorDevKitDiagnosticApp.exe) in the tools \ Diagnostic \ Binaries Tool directory with the Win 7 Development kit: And so, I tested the sensor, but how to have fun further ?
4) Install XNA Game Studio 3.0
With CodePlex, we can download some classes that can be used to extend one of the starter kit that comes with XNA Game Studio. I will not show the entire code, I will show only the minimum list of changes that should be made to control the machine using the accelerometer.
1. Install Microsoft XNA Game Studio 3.0 (62 MB)
2. Install Microsoft XNA Racing Game Starter Kit (127 MB)
3. Download Windows 7 Sensor XNA Racing Game (inside we will find two classes and a dll file).
Now we can create a simple racing Game Project with Visual Studio 2008: After that, we need to follow the instructions in the CodePlex example
- add Windows7.SensorAndLocation.dll, which contains the wrapper of the native API for .NET
- copy to the Shaders folder, PreScreenSkyCubeMapping.cs (this file is used to change the lighting and sky depending on the sensor readings)
- In the GameLogic folder, replace Input.cs. This file contains code that receives data from different devices: mouse, keyboard, accelerometer. We can look at the Initialize method, which initializes the sensor and provides acceleration along the X and Y axes, as shown in the code
The code gives three properties:
The first is used to check whether the device is connected, and the rest to return the values of the accelerometer. In my test, I saw a value from -1 to 1.
Now we have the values returned by the accelerometer, but how to use them in the game? First of all, we need to initialize the sensor so that the application knows what to use, then we need to control the acceleration / deceleration of the car, turn right / left.
- We go to Graphics \ BaseGame.cs and at the end of the BaseGame constructor, call Input.Initialize (), which must be initialized before the Accelerometer3D sensor.
- We go to GameLogic \ CarPhysics.cs and in the Update method we can, for example, control the X axis for left and right turns
I did some experiments to adjust the sensitivity, but you can change the 3.0f value for yourself.
In order for the car to accelerate and be able to drive forward or backward, I added this code to the same method:
You can download my classes from an example.
5) Enjoy the Windows 7 Sensor API!
The time has come when we can press F5 and plunge into the game while getting a thrill of driving from the accelerometer.
6) For the curious
If you want to know more about this API, watch the video that inspired me.