06 Jul
Posted by kdutta in Development, HTC, Windows Mobile
When I first got my HTC Touch Diamond a while ago, one of the first things I tried to do was reverse engineer the Sensor API found in HTCSensorSDK.dll. However, anyone who has tried to reverse engineer DLL arguments knows how tedious and painful it can be to create a dummy DLL to intercept valid arguments, parse through assembly, and inspect random memory pointers. Luckily, I did discover a registry key: HKEY_LOCAL_MACHINE\Software\HTC\HTCSensor\GSensor\EventChanged which let me figure out what the general orientation of the device was; and that was good enough for what I was trying to do.
However Scott, from scottandmichelle.net, successfully reverse engineered the HTCSensorSDK.dll. This allows developers to use the g-sensor that is available on the device. Very impressive work on the part of Scott!
Anyhow, I spent a portion of today writing a managed wrapper for HTC’s Sensor API. You can download it here. The code also includes a sample Teeter-esque type application which allows you to roll a ball around the screen.
The managed API exposes the HTCGSensor class which allows you to hook to query the state of the g-sensor on the device. It exposes the following methods, properties, and events:
GetGVector
Returns a vector that desribes the direction of gravity/acceleration in relation to the device screen.
The vector returned will have a length measured in the unit meters per second square.
Ideally the when the device is in a motionless state, the vector would be of length 9.8 (the gravitational constant). However, the sensor is not extremely accurate, so this almost never the case.
Orientation
Retrieves the current orientation of the device, returning one of the following enums: Landscape, ReverseLandscape, Portrait, ReversePortrait, FaceDown, FaceUp.
OrientationChanged
This event fires whenever the device’s orientation changes.
Enjoy, and let me know if you find any bugs!
Koushik Dutta
Software Engineer
www.koushikdutta.com
9 Responses
Jeroen Brosens
08|Jul|2008 1Hi Koushik,
Looks interesting, your Managed wrapper! Good work!
About the vector data; does this 9.8 have anything to do with ‘g’ (gravitational acceleration, being 9.8 m/s^2) or am I totally wrong here?
Next to that, I was browsing your code quickly but is there any chance that you can provide a non-ambiguous illustration of the coordinate system with the axes that you describe in relation to the device?
Thanks in advance,
– Jeroen
kdutta
08|Jul|2008 2HTC actually reports their sensor data back in the 3 axis with values between approximately -1000 and 1000. However, to make this more API device independent and give the numbers real world meaning, I converted those values into meters/second^2. So yes, in a stand still state, the device would return a vector of length 9.8, which is the gravitational constant.
I have also updated the blog post to further explain the coordinate system in relation to the device’s orientation.
I would also encourage you to inspect the sample application. Also you could create one of your own that displays the vector values as you twist and turn the device. It’s difficult to explain it via words and diagrams, but it all makes perfect sense when you play with it.
Daniele
09|Jul|2008 3Hi,
i’ve tried to use your code and I receive an error when execute this row
Dim htcGV As HTCGSensor = New HTCGSensor()
the error is:
InvalidProgramException
Can you help me?
kdutta
09|Jul|2008 4I updated the code to explictly pass the the HTC Sensor type as a uint. I doubt that was the problem though. It works fine on my phone still, obviously.
Let me know if the change works for you. I also emailed you on the address you provided in your comment; feel free to email me back with the results of the new code.
Svein Skogen
09|Jul|2008 5Wouldn’t it, using this sensor, be possible to add (albeit low-precision) inertia-navigation to augment the gps sensor?
The reason I’m asking is this:
GPS systems are infamous for their (lack of) capabilities in tunnels and garages. Using this three-axis-accelerometer, we should be able to detect speed changes, and to a certain amount turning, while we are missing the gps signals. If so, the gps functionality of this cellphone might suddenly rival the capabilities of many gps-units sold for car navigation today. And even better: It might give GPS-unit manufacturers a hint that such an “extra” chip may be a good idea to add to their gear.
Regards,
Svein Skogen
AJ
17|Jul|2008 6I’m thinking of an app that shows a keyboard if you tilt the device backwards, hides it when tilted forward.
The keyboard itself will be quite large but tilting left and right shows either the left or the right side.
kdutta
18|Jul|2008 7That’s a pretty good idea, but I don’t know if you can programatically show the SIP (using SipShowIM) in a process outside of your own. That would be something to look into.
Un SDK per l’accelerometro del HTC Touch Diamond « P@ssioneMobile
27|Jul|2008 8[...] si è adoperato per fare un reverse engineering dell’API presente HTCSensorSDK.dll. Enteprisemobile ha quindi realizzato un wrapper per la classe, esponendola e rendendola utilizzabile. Nel post è presente anche una guida [...]
wmpoweruser.com » Blog Archive » Sensory Overload - asteroids game for the HTC Diamond using G-sensor
10|Aug|2008 9[...] version of the venerable Asteroids game utilizes the G-Sensor, Nav Sensor, and the 3D hardware capabilities of the the HTC Touch Diamond and looks simply [...]
Leave a reply
Search
Archives
Categories
Windows Mobile Blogs
Copyright © 2008 - Enterprise Mobile
Proudly powered by WordPress - InSense 1.0 Theme by Design Disease.