Comments

main
ce stuff
my shots
sample code
personal pages
programming
web toys
reviews

rss

Xbox Live Member: sm360
   Fun with the Diamond's tilt sensor  (7/2/2008 4:32 pm)
      Filed under: Windows Mobile


I spent a couple of sleepless hours last night writing a little Sensor Test for my new HTC Diamond. It's a small app that lets you move a circle around the screen by tilting the device.

Exciting, eh? Well, the fun is in getting it to work. I asked HTC if they provide a development kit (SDK) for the tilt sensor, and they said "No", so I had to figure it out myself by digging around (and trying to remember what I knew of ARM assembly). I'm just happy I managed to figure it out, and so others can write interesting games with it, I'm giving the source code to what I figured out away. Have fun, create something with it.

You can download Sensor Test here, it's the small app I describe, along with the project necessary to compile it. You'll need Visual Studio 2008 and the WM 6.0 SDK to compile it yourself, though if you're a Windows Mobile developer, I suspect you'll only need to see the top of the main code file and the first couple of functions to figure things out.

I'm going to work on a little game idea I have, but with the way I bounce from one project to the next, don't be surprised if it takes me a while.

Edit: 7/3/2008 7:51 am I added a bit more information to the struct of data, I still can't figure out one field


Time: 7/3/2008 3:11 am
From: HTC_GLOW
Comment:
Hey thanks very much, this will help loads in production of Haptic UI for Touchflow 3D. HTC_GLOW - XDA Developers

Time: 7/3/2008 4:35 am
From: Racer
Comment:
Hi! nice work! How good is the resolution of the values and the frequency they can be polled? keep on!

Time: 7/3/2008 7:55 am
From: Scott
Comment:
Glad others are finding this useful! As to the poll rate: Got me. So far it seems to be happy at at least 20 times a second. I haven't (yet) tried higher, though the API doesn't seem to respond well to being polled in a tight loop.

Time: 7/6/2008 10:04 am
From: Koush
Comment:
Amazing perseverence... I gave up after figuring out what the parameter to HTCSensorOpen was supposed to be, hahaha... Thanks again for a great job!

Time: 7/7/2008 12:16 am
From: Koush
Comment:
Hello again Scott! I've written a managed wrapper for the HTC Sensor SDK and published it at my blog (and linked back to yours of course!) Again, great work. http://blog.enterprisemobile.com/2008/07/using-htc-diamonds-sensor-sdk-from-managed-code/

Time: 7/8/2008 4:33 am
From: j0bro
Comment:
Hi Scott, I came across this blog of yours and I'm interested in whether this would work in c# as well? Or is your code stuck to C++? Thanks in advance!

Time: 7/8/2008 7:25 am
From: Scott
Comment:
My code is C/C++ only. Take a look at Koush's reply.

Time: 7/9/2008 6:08 am
From: Daniele
Comment:
Hi, 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?

Time: 7/9/2008 9:49 am
From: Scott
Comment:
That looks like VB code, which would mean you're using Koush's Managed solution. I'd suggest you contact him for help, or try posting on his blog, I'm afraid I really don't have the spare cycles to investigate issues in the managed solution.

Time: 7/9/2008 1:15 pm
From: Nuke
Comment:
Scott, do you know any way to recalibrate the tilt sensor?!

Time: 7/9/2008 3:07 pm
From: Daniele
Comment:
It's right, your unmanaged is working fine.... I go to study your version (some problem with C++ but ....) Thanks and sorry.

Time: 7/9/2008 6:01 pm
From: Scott
Comment:
Sorry, I haven't determined how to recalibrate the sensor, yet (or even if it's possible). I'll look into it, but no promises of when I'll be able to figure things out.

Time: 7/10/2008 10:52 am
From: Koush
Comment:
I had a post on xda-developers that it would be possible to recalibrate the sensor by creating a new HTCSensorSDK.DLL that acts as a pass through to the real DLL. It could then tweak the output values as it pleased. But HTCSensorSDK.dll is on ROM (I think) and note removable. The callibration program would need to be flashed onto the device via a new ROM.

Time: 7/10/2008 1:11 pm
From: Nuke
Comment:
Thanks Scott! mmmh i whould like use original ROMs. it could be nice if HTC release a patch.....

Time: 7/10/2008 4:05 pm
From: Scott
Comment:
Koush: That would work for some apps, but not for others, for instance, Opera will still be confused about which side of the device is "up" (assuming your device is far out of calibration), as it doesn't use the HTCSensorSDK.dll to get the sensor information. In the end, it'll be up to someone to discover a calibration API, or ideally, for HTC to release a real SDK letting us know exactly how to do that sort of thing.

Time: 7/11/2008 8:59 am
From: pocketMax
Comment:
Brilliant work Scott, Here is a simple game based on this - Lunar Lander, that I hacked together this afternoon http://www.pocketmax.net/smf/index.php?topic=6380.msg43257

Time: 7/15/2008 2:22 pm
From: ...
Comment:
The light sensor does not seem to work if the screen is off. My guess is that the HTCSensorQueryCapability (don't know the signature) would return false. Any ideas on how to get around this? I looked to see if I could figure out if there was a driver I could turn on, but didn't have much luck. Any thoughts?

Time: 7/15/2008 3:50 pm
From: Scott
Comment:
Sorry, I haven't yet had a change to look into the light sensor. I'm going to look into this, but I really don't know when I'll have a chance.

Time: 7/15/2008 4:04 pm
From: ...
Comment:
The same holds true for the accelerometer. You can't query when the screen is off. I think that either the driver is off or they are doing some power state check, but I don't know enough about disassembly to figure it out.

Time: 7/31/2008 9:02 am
From: Riley
Comment:
nice find there. Currently, i am holding samsung omnia. Similar to HTC, they are not releasing the accelerometer sdk either. I'm interested in reverse engineering the dll. could you point me to some direction/guide how should i go about do it? it would be great for developing motion sensing games that will work across more handset :D

Time: 8/4/2008 10:30 am
From: JD Barclay
Comment:
You are the man. Thanks! I'll be getting my HTC soon and start the play... For anybody there, look at this app built for IPhone: http://www.dynolicious.com/ It uses the iPhone g-sensor to detect car acceleration and calculate several values such as HP, speed, etc. Im not iPhone fan but really admire this app... Has anyone done something similar over WM platform?

Time: 8/13/2008 2:12 am
From: Dale Lane
Comment:
How specific is this to the Diamond? Could it also work with the HTC Advantage?

Time: 8/13/2008 10:01 pm
From: Scott
Comment:
Got me. If the Advantage has a tilt and motion sensor, it might work. That's all up to how exactly HTC implemented the SDK on the advantage. Someone with some development experience is going to need to try it out, and that can't be me since I don't have an Advantage to play with, sorry.

Time: 9/7/2008 6:00 am
From: Cosmo
Comment:
Scott, how did you figure out this API by the way, what techniques or tools helped. There are other API's to discover!

Time: 9/7/2008 10:25 am
From: Codemaster
Comment:
Hi, Nice app could you turn it to VB.NET ? Greats code master

Time: 9/9/2008 2:44 pm
From: Scott
Comment:
Sorry, no VB client. Koush has a C# client that he posted a link to above that might help you out. As for how I did this, maybe that's a good idea for a series of posts coming in the future.

Time: 9/10/2008 1:08 pm
From: Jamie
Comment:
How does Koush manage to get GetGVector x,y,z from the data in the SENSORDATA struct? SHORT TiltX; // From -1000 to 1000 (about), 0 is flat SHORT TiltY; // From -1000 to 1000 (about), 0 is flat SHORT Orientation; // From -1000 to 1000 (about) // 0 = Straight up, -1000 = Flat, 1000 = Upside down WORD Unknown1; // Always zero DWORD AngleY; // From 0 to 359 DWORD AngleX; // From 0 to 359 DWORD Unknown2; // Bit field?

Time: 9/10/2008 1:36 pm
From: Scott
Comment:
I'd try asking on his blog, I don't think he monitors my blog for questions :)

Time: 9/10/2008 2:21 pm
From: Jamie
Comment:
Good idea! His comment box wasn't so obvious, found it now though.