This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
These files contain the specification for a class which receives upda…
…tes from the iPhone accelerometer. The class holds the accelerometer information, and is queried by SDL_sysjoystick.m.
- Loading branch information
Holmes Futrell
committed
Jul 18, 2008
1 parent
1a24d07
commit e516727
Showing
2 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// SDLUIAccelerationDelegate.h | ||
// iPodSDL | ||
// | ||
// Created by Holmes Futrell on 6/21/08. | ||
// Copyright 2008 __MyCompanyName__. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
#import "SDL_stdinc.h" | ||
|
||
@interface SDLUIAccelerationDelegate: NSObject <UIAccelerometerDelegate> { | ||
|
||
UIAccelerationValue x, y, z; | ||
//NSTimeInterval timestamp; | ||
BOOL isRunning; | ||
BOOL hasNewData; | ||
|
||
} | ||
|
||
+(SDLUIAccelerationDelegate *)sharedDelegate; | ||
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration; | ||
-(void)getLastOrientation:(Sint16 *)data; | ||
-(void)startup; | ||
-(void)shutdown; | ||
-(BOOL)isRunning; | ||
-(BOOL)hasNewData; | ||
-(void)setHasNewData:(BOOL)value; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// | ||
// SDLUIAccelerationDelegate.m | ||
// iPodSDL | ||
// | ||
// Created by Holmes Futrell on 6/21/08. | ||
// Copyright 2008 __MyCompanyName__. All rights reserved. | ||
// | ||
|
||
#import "SDLUIAccelerationDelegate.h" | ||
|
||
static SDLUIAccelerationDelegate *sharedDelegate=nil; | ||
|
||
@implementation SDLUIAccelerationDelegate | ||
|
||
+(SDLUIAccelerationDelegate *)sharedDelegate { | ||
if (sharedDelegate == nil) { | ||
sharedDelegate = [[SDLUIAccelerationDelegate alloc] init]; | ||
} | ||
return sharedDelegate; | ||
} | ||
|
||
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { | ||
|
||
x = acceleration.x; | ||
y = acceleration.y; | ||
z = acceleration.z; | ||
|
||
hasNewData = YES; | ||
//timestamp = acceleration.timestamp; | ||
|
||
} | ||
|
||
-(void)getLastOrientation:(Sint16 *)data { | ||
|
||
#define MAX_G_FORCE 5.0 | ||
#define MAX_SINT16 0x7FFF | ||
|
||
if (x > MAX_G_FORCE) x = MAX_G_FORCE; | ||
else if (x < -MAX_G_FORCE) x = -MAX_G_FORCE; | ||
|
||
if (y > MAX_G_FORCE) y = MAX_G_FORCE; | ||
else if (y < -MAX_G_FORCE) y = -MAX_G_FORCE; | ||
|
||
if (z > MAX_G_FORCE) z = MAX_G_FORCE; | ||
else if (z < -MAX_G_FORCE) z = -MAX_G_FORCE; | ||
|
||
data[0] = (x / MAX_G_FORCE) * MAX_SINT16; | ||
data[1] = (y / MAX_G_FORCE) * MAX_SINT16; | ||
data[2] = (z / MAX_G_FORCE) * MAX_SINT16; | ||
|
||
} | ||
|
||
-(id)init { | ||
|
||
self = [super init]; | ||
x = y = z = 0.0; | ||
return self; | ||
|
||
} | ||
|
||
-(void)dealloc { | ||
sharedDelegate = nil; | ||
[super dealloc]; | ||
} | ||
|
||
-(void)startup { | ||
[UIAccelerometer sharedAccelerometer].delegate = self; | ||
isRunning = YES; | ||
} | ||
|
||
-(void)shutdown { | ||
[UIAccelerometer sharedAccelerometer].delegate = nil; | ||
isRunning = NO; | ||
} | ||
|
||
-(BOOL)isRunning { | ||
return isRunning; | ||
} | ||
|
||
-(BOOL)hasNewData { | ||
return hasNewData; | ||
} | ||
|
||
-(void)setHasNewData:(BOOL)value { | ||
hasNewData = value; | ||
} | ||
|
||
@end |