The accelerometer is not simulated in the current iPhone SDK (for iPhone OS 2.2).
Otto Chrons has posted a neat package that sends accelerometer data from an iPhone or iPod Touch to an application running in the iPhone simulator. This package is in two parts: a ‘server’ that runs on your iPhone hardware and a ‘client’ in the form of source code that you can drop into your project (adding just an #import statement).
This works really well, except when:
1) you don’t have an iPhone or iPod Touch, or
2) you’re not registered in the Standard or Enterprise iPhone Development programme; I wasn’t registered at the time, and so I had no way to push Otto’s server application to my iPod Touch.
But then it occurred to me that my MacBook did have an accelerometer. If only that could serve data to the fake accelerometer in the simulator... So I looked, and providentially enough, UniMotion hove into view.
So I browsed the source for the two packages for a bit, with a view to nefarious smash-and-grab, but by more happy chance it turned out that the relevant pieces in both were modular enough to fit as they were, with a little python for glue.
Otto’s client expects a string with timestamp and x,y,z readings over UDP, and UniMotion’s motion is kind enough to spew x, y and z readings to standard output, though they’re not oriented in the same directions as those in the iPhone SDK’s UIAcceleration.
On my late 2006 MacBook, motion emits x,y,z with a positive magnitude to the left, towards me and downwards, respectively. UIAcceleration expects readings to be positive to the right, away from me and upwards, respectively.
So all I needed to do was to grab motion’s output (with the -f flag for acceleration in units of G), multipy each value by -1, add a timestamp and broadcast over UDP on the given port in the format that Otto’s client expects. I did need to run calibrate in the UniMotion suite first.
Here’s my python glue:
import sys, socket, time, traceback
kCFAbsoluteTimeIntervalSince1970 = 978307200.0 # from CFDate.c
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
line = sys.stdin.readline()[:-1] # read line and strip EOL
fields = line.split() # split around space character
floatfields = map(float, fields) # convert to floats
# transform co-ordinate system, from SMS- to UIAcceleration-format,
# for my macbook (late 2006)
x, y, z = map(lambda x: -1 * x, floatfields)
# change epoch to be compatible with CFAbsoluteTimeGetCurrent()
currentTime = time.time() - kCFAbsoluteTimeIntervalSince1970
accdata = ','.join(map(str,('ACC: 0',currentTime,x,y,z)))
sock.sendto(accdata, ('<broadcast>', 10552))
# print '% .2f % .2f % .2f' % (x, y, z)
except (ValueError, KeyboardInterrupt):
I call this sendaccsim.py and run it as:
$ ./motion -f 17 | python sendaccsim.py
It’s just a bit awkward swinging the Mac around like a tea tray.