Topic: iPhone Accelerometer Simulator

I started to develop for iPhone, and I faced a problem, what I think is not only mine.
I had no device to test on it, but I wanted to work with the accelerometer.
I was looking around on the web for any simulator, without any success.

Finally I decided to create my own simulator. In the end it contains a small app and a UIAccelerometer replacement class.
The ideea is pretty simple, I made a small http server what serves the x,y,z acceleration values. I admit I was lazy to write the whole http server so I used the "simple http server", what is the base of the simulator. You can download it from: http://culturedcode.com/cocoa. Thanks for it.

The replacement class connects to the http server and instead of the accelerometer, it receives the acceleration values from the server app.
After that it simulates the behaviour of the UIAccelerometer class.

Anyone interested in it, may download the simulator, and the small class with what you should replace the class from the SDK.
iPhoneAccelerometerSimulator.zip
iPhoneAccelerometerSimulator.dmg


The usage is pretty simple, mostly you have to replace the UIAccelerometer references with zUIAccelerometer.

Here is a code snipett, based on the AccelerometerGraph app from the SDK for better understanding. If you do exactly the same changes what I did, the simulator should work.
First you have to download the AccelerometerGraph from here

In AppDelegate.h make the following change:

...
#import "GraphView.h"
/*accelerometer simulator*/
#import "zUIAccelerometer.h"
/*accelerometer simulator*/
...

In AppDelegate.m do the following changes:

...
@implementation AppDelegate
@synthesize window;
@synthesize graphView;
@synthesize toolbar;
/*accelerometer simulator*/
zUIAccelerometer *am;
/*accelerometer simulator*/
...
...
- (void)applicationDidFinishLaunching:(UIApplication*)application {
...
...
    // Configure and start the accelerometer
/*accelerometer simulator*/
    am = [zUIAccelerometer alloc];
    [am setDelegate:self];
    [am startFakeAccelerometer];    
    //[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / kAccelerometerFrequency)];
    //[[UIAccelerometer sharedAccelerometer] setDelegate:self];
/*accelerometer simulator*/
...
...
// UIAccelerometerDelegate method, called when the device accelerates.
/*accelerometer simulator*/
//- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
- (void)accelerometer:(zUIAccelerometer *)accelerometer didAccelerate:(zUIAcceleration *)acceleration {
/*accelerometer simulator*/
...

That's all. If you have any question don't hesitate.

Enjoy, and feel free to comment and any constructive suggestion is welcome.

Cheers,
Zotya

Re: iPhone Accelerometer Simulator

Thank you! It's simple and it works.

Re: iPhone Accelerometer Simulator

hi, thanks for posting this!  I have some basic questions about how it works.  I am still a newbie to ipod app development so please bear with me.

how do I use the zUIAcclerometer class files within the AccelerometerGraph project?  I opened the AccelerometerGraph project and dragged in zUIAccelerometer.h and zUIAccelerometer.m into the Classes folder, and I made most of the changes that you indicated in AppDelegate.h and AppDelegate.m of the AccelerometerGraph project.  However, I still don't have a UIAccelerometerDelegate method so I don't know where to put the change for the accelerometer didAccelerate method call. 

also, I don't understand how the zUIAccelerometer class takes inputs from the SimpleHttpServer class.  I don't see the method checkFakeAcc in zUIAccelerometer get called from anywhere.  I did build and run for the SimpleHttpServer, and I can get it to display webpages within its window, but I'm not sure what to do after that. 

Thanks a lot for any help!

Re: iPhone Accelerometer Simulator

Hi mjpablo23,

After you added zUIAccelerometer.h & zUIAccelerometer.m to the AccelerometerGraph app, import in the AppDelegate.h file the zUIAccelerometer.h:
#import "zUIAccelerometer.h"
In the AppDelegate.m file define the simulator:
zUIAccelerometer *am;
Also in this file you have to search for method "applicationDidFinishLaunching" and add the lines what I mentioned in the specification.
After that search for method "accelerometer"  and modify it as I specified.

When all is done, build & run your project.
You shouldn't see anything important.

You have to start the iphoneaccelerometersimulator app. It's an osx desktop application. It has 3 sliders where you can simulate X/Y/Z accelerations. Dragging these sliders you should see changes in your iphone application.

Regarding to the SimpleHttpServer. I think you misunderstood something (or I didn't understood what you are asking smile ). This class is used in the iphoneaccelerometersimulator app, to send somehow the X/Y/Z accelerations to the iphone app. It has not too much to do with webpages.

Cheers,
Zotya

Re: iPhone Accelerometer Simulator

ok thanks a lot!  haha I didn't know that I just had to open the iphone accelerometer simulator.  it was a lot simpler than I thought! 

just as a heads up to people in the future, I think the AccelerometerGraph code has been updated, so the code that was supposed to go into AppDelegate.h and .m regarding the zUIAccelerometer should go into MainViewController.h and .m instead.  Also, the declaration of graphView has been updated as well so it's now in MainViewController. 

Happy xcoding!

Re: iPhone Accelerometer Simulator

Thanks mjpablo23 for the observations.

I will check the AccelerometerGraph app, and maybe one day I will update the documentation.

Cheers,
Zotya

Re: iPhone Accelerometer Simulator

Hi Zotya,

Thank you for creating such a nice and handy tool. It is really useful for me.

I have a feature idea, if you can add it in future release, it could be more useful I think:

You can add some buttons that save several configuration. When clicking these buttons, the application can send different pre-configured x, y, z data to iphone simulator. For example, you can have three buttons in the bottom of the screen. User can set button 1 to send x, y, z data : (1.0, 0.5, -1), button 2 send (1.0, -0.5, 0) and button 3 send (-1.0, -0.5, 0). When user clicking button 1, the app send (1.0, 0.5, -1) once. User can click these buttons quickly to simulate real shaking behavior. I think if you can add this feature, the tool can be more helpful for developing.

Thank you again for creating such a nice tool. :-)

Re: iPhone Accelerometer Simulator

Hello ypyean,

Thanks for the positive feedback wink.

I was also thinking about something similar, but not exactly the same. I want  to record somehow a list of values, and when I want to play them.
There would be 2 ways to record these values:
1. Manually from the simulator
2. Directly from an iPhone
I think this would make the usage much easier.

I don't know when I'll have time to implement it, but immediately when I'm done I will post a message.

Cheers,
Zotya

Re: iPhone Accelerometer Simulator

First I changed the setting to iphone simulator. then
compile the AccelerometerGraph project, there are 3 errors.

Line Location MainViewController.m: error: 'UIAccessibilityLayoutChangedNotification' undeclared (first use in this function)


under [am SetDelegate:self]

there's warning: class 'MainViewController' does not implement the zUIAccelerometerdelegate ' protocol.

Last edited by lilzz (2009-11-17 02:04:37)

Re: iPhone Accelerometer Simulator

@zotya : it works great. pretty simple too thanks.  would you be able to release the source of the iPhoneAccelerometerSimulator simple http server app?

Re: iPhone Accelerometer Simulator

@zotay. I was made changes that you mentioned, but I get some errors like in AppDelegate.m:

no declaration of property 'graphView' found in the interface
no declaration of property 'toolbar' found in the interface