📜 ⬆️ ⬇️

Using the accelerometer in the iPhone simulator

We already know how to develop applications for the iPhone using an accelerometer . Now it remains to learn how to test it in the simulator.

This need may arise for several reasons: there is no iPhone, or you have not yet joined the iPhone Developer Program , so there is no possibility to run it on a real device.

To use the accelerometer in the simulator, we need:
  1. Mac with a built-in accelerometer (MacBook, MacBook Pro, MacBook Air), the readings of which will be transmitted to the simulator
  2. Unimotion - a program that removes readings from an accelerometer built into Mac
  3. Accelerometer Simulator - an application that sends readings from a real iPhone accelerometer to a Mac. (It consists of 2 parts: an application installed on the iPhone; and a class that connects to the application under test.)
  4. The sendaccsim.py script that converts readings from Unimotion for use in the Accelerometer Simulator:

import sys, socket, time, traceback

kCFAbsoluteTimeIntervalSince1970 = 978307200.0 #from CFDate.c

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(( '' ,0))
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)

while 1:
try :
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 coordinate system, from Sudden Motion Sensor to UIAcceleration format

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))

except ( ValueError, KeyboardInterrupt ):
sock.close()
sys.exit(-1)
except:
traceback.print_exc()



First you need to build Unimotion. C sourceforge.net download and unpack the archive with the program. Go to the unpacked folder and execute make , after which we have a bin folder containing motion and libUniMotion.dylib . There we copy sendaccsim.py and run it:
$ ./motion -f 17 | python sendaccsim.py

From Accelerometer Simulator we take AccelerometerSimulation.h and AccelerometerSimulation.m and connect to our project in AppDelegate.h :
#import "AccelerometerSimulation.h"

')
Run the application in the simulator, and emulate the accelerometer iPhone, tilting the Mac left-right :).

-
Free translation: Use the Mac's accelerometer in the iPhone Simulator .

For example, you can take the game Tweejump and play it directly on the Mac. It was found in the open spaces of GitHub and was chosen only because it uses an accelerometer to control the player.

Source: https://habr.com/ru/post/65363/


All Articles