Finally got AIR to retrieve data from the sensor

After series of failures from trying to make an AIR native extension to leverage my Thinkpad’s awesome accelerometer, I turned to an NativeProcess and got it working! NativeProcess enable AIR to start a native application, input and retrieve output from the native application. To keep things simple, I separate the codes in which are used to retrieve accelerometer data into 2 native applications: 1 to get x acceleration and 1 to get y acceleration. In the video, I’m only getting the y-acceleration.

[youtube http://www.youtube.com/watch?v=z65FlPBCej4&w=420&h=315]

Using native process is very simple, but I find that it needs some workarounds to make it works, which I’m going to blog about.

As an introduction, Jeff Swartz’s guide to native process Interacting with native process (link) is highly recommended. What I found complicated, due to my limited knowledge of C is how to create a stream of stdout. For this project, I’m using fwrite (C++ Reference link) function from stdio.h to create a stdout stream, then use fflush to flush the stream to make sure the data is sent out.

This method means that the data in which ever format it was have to be converted to a string in order to be sent out with fwrite. Now in VC++, this is easy because Microsoft’s stdlib.h header file actually includes a function to convert integer to a string itoa(), but this function is not standard, so in order to use one that is similar, you can use this function to make life lots easier. This is one of my working test source file that may demonstrate the use of itoa() and fwrite(), fflush() to create a stdout stream.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char buffer[10];
    int num = 999;

    itoa(num, buffer, 10);
    //printf("Printf: Number to string: %s.\n", buffer);
    fwrite(buffer, 3, 1, stdout);
    fflush(stdout);
    return 0;
}

Since the data is sent out as a string, in AS3, I then have to convert it back to a Number type. This is relatively easy, but I’ve found that this wouldn’t work.

Number(process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable));

You would have to create a string in AS3 to store the read UTF bytes, then another Number typed variable to convert the string to Number type.

A very frustrating final problem I found after packaging the source files into an AIR application with native installer, and installing it, is that the AIR application, even though claim to have its native process started, does not have its native process actually started. I might have missed out on something, but it would be great if I don’t have to copy the native application into the installation folder of the application.

Other than that, the result is quite exciting. Now I can make applications or use other people’s code to make applications that use Thinkpad’s accelerometer for more engaging experience.

You can have a try with the AIR file. Remember to copy the folder Native with SensorGetY_VC into the installation folder. You will need Lenovo’s Thinkpad APS Protection drivers and software for this to work. Link to download AIR installer (http://www.mediafire.com/?rrq3yq8ghqs4nak).

Note: This application has only been tested on a Windows XP x86 Thinkpad T60. Use on other OS at own risk.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s