src/joystick/iphoneos/SDLUIAccelerationDelegate.m
branchgsoc2008_iphone
changeset 2362 44fc2537ff9e
child 2427 32b9909db651
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/joystick/iphoneos/SDLUIAccelerationDelegate.m	Fri Jul 18 17:46:17 2008 +0000
     1.3 @@ -0,0 +1,88 @@
     1.4 +//
     1.5 +//  SDLUIAccelerationDelegate.m
     1.6 +//  iPodSDL
     1.7 +//
     1.8 +//  Created by Holmes Futrell on 6/21/08.
     1.9 +//  Copyright 2008 __MyCompanyName__. All rights reserved.
    1.10 +//
    1.11 +
    1.12 +#import "SDLUIAccelerationDelegate.h"
    1.13 +
    1.14 +static SDLUIAccelerationDelegate *sharedDelegate=nil;
    1.15 +
    1.16 +@implementation SDLUIAccelerationDelegate
    1.17 +
    1.18 ++(SDLUIAccelerationDelegate *)sharedDelegate {
    1.19 +	if (sharedDelegate == nil) {
    1.20 +		sharedDelegate = [[SDLUIAccelerationDelegate alloc] init];
    1.21 +	}
    1.22 +	return sharedDelegate;
    1.23 +}
    1.24 +
    1.25 +-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    1.26 +		
    1.27 +	x = acceleration.x;
    1.28 +	y = acceleration.y;
    1.29 +	z = acceleration.z;
    1.30 +	
    1.31 +	hasNewData = YES;
    1.32 +	//timestamp = acceleration.timestamp;
    1.33 +	
    1.34 +}
    1.35 +
    1.36 +-(void)getLastOrientation:(Sint16 *)data {
    1.37 +
    1.38 +#define MAX_G_FORCE 5.0
    1.39 +#define MAX_SINT16 0x7FFF	
    1.40 +
    1.41 +	if (x > MAX_G_FORCE) x = MAX_G_FORCE;
    1.42 +	else if (x < -MAX_G_FORCE) x = -MAX_G_FORCE;
    1.43 +
    1.44 +	if (y > MAX_G_FORCE) y = MAX_G_FORCE;
    1.45 +	else if (y < -MAX_G_FORCE) y = -MAX_G_FORCE;
    1.46 +
    1.47 +	if (z > MAX_G_FORCE) z = MAX_G_FORCE;
    1.48 +	else if (z < -MAX_G_FORCE) z = -MAX_G_FORCE;
    1.49 +	
    1.50 +	data[0] = (x / MAX_G_FORCE) * MAX_SINT16;
    1.51 +	data[1] = (y / MAX_G_FORCE) * MAX_SINT16;
    1.52 +	data[2] = (z / MAX_G_FORCE) * MAX_SINT16;
    1.53 +
    1.54 +}
    1.55 +
    1.56 +-(id)init {
    1.57 +
    1.58 +	self = [super init];
    1.59 +	x = y = z = 0.0;
    1.60 +	return self;
    1.61 +	
    1.62 +}
    1.63 +
    1.64 +-(void)dealloc {
    1.65 +	sharedDelegate = nil;
    1.66 +	[super dealloc];
    1.67 +}
    1.68 +
    1.69 +-(void)startup {
    1.70 +	[UIAccelerometer sharedAccelerometer].delegate = self;
    1.71 +	isRunning = YES;
    1.72 +}
    1.73 +
    1.74 +-(void)shutdown {
    1.75 +	[UIAccelerometer sharedAccelerometer].delegate = nil;
    1.76 +	isRunning = NO;
    1.77 +}
    1.78 +
    1.79 +-(BOOL)isRunning {
    1.80 +	return isRunning;
    1.81 +}
    1.82 +
    1.83 +-(BOOL)hasNewData {
    1.84 +	return hasNewData;
    1.85 +}
    1.86 +
    1.87 +-(void)setHasNewData:(BOOL)value {
    1.88 +	hasNewData = value;
    1.89 +}
    1.90 +
    1.91 +@end