src/joystick/iphoneos/SDLUIAccelerationDelegate.m
branchgsoc2008_iphone
changeset 2362 44fc2537ff9e
child 2427 32b9909db651
equal deleted inserted replaced
2361:ba39d5af12a7 2362:44fc2537ff9e
       
     1 //
       
     2 //  SDLUIAccelerationDelegate.m
       
     3 //  iPodSDL
       
     4 //
       
     5 //  Created by Holmes Futrell on 6/21/08.
       
     6 //  Copyright 2008 __MyCompanyName__. All rights reserved.
       
     7 //
       
     8 
       
     9 #import "SDLUIAccelerationDelegate.h"
       
    10 
       
    11 static SDLUIAccelerationDelegate *sharedDelegate=nil;
       
    12 
       
    13 @implementation SDLUIAccelerationDelegate
       
    14 
       
    15 +(SDLUIAccelerationDelegate *)sharedDelegate {
       
    16 	if (sharedDelegate == nil) {
       
    17 		sharedDelegate = [[SDLUIAccelerationDelegate alloc] init];
       
    18 	}
       
    19 	return sharedDelegate;
       
    20 }
       
    21 
       
    22 -(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
       
    23 		
       
    24 	x = acceleration.x;
       
    25 	y = acceleration.y;
       
    26 	z = acceleration.z;
       
    27 	
       
    28 	hasNewData = YES;
       
    29 	//timestamp = acceleration.timestamp;
       
    30 	
       
    31 }
       
    32 
       
    33 -(void)getLastOrientation:(Sint16 *)data {
       
    34 
       
    35 #define MAX_G_FORCE 5.0
       
    36 #define MAX_SINT16 0x7FFF	
       
    37 
       
    38 	if (x > MAX_G_FORCE) x = MAX_G_FORCE;
       
    39 	else if (x < -MAX_G_FORCE) x = -MAX_G_FORCE;
       
    40 
       
    41 	if (y > MAX_G_FORCE) y = MAX_G_FORCE;
       
    42 	else if (y < -MAX_G_FORCE) y = -MAX_G_FORCE;
       
    43 
       
    44 	if (z > MAX_G_FORCE) z = MAX_G_FORCE;
       
    45 	else if (z < -MAX_G_FORCE) z = -MAX_G_FORCE;
       
    46 	
       
    47 	data[0] = (x / MAX_G_FORCE) * MAX_SINT16;
       
    48 	data[1] = (y / MAX_G_FORCE) * MAX_SINT16;
       
    49 	data[2] = (z / MAX_G_FORCE) * MAX_SINT16;
       
    50 
       
    51 }
       
    52 
       
    53 -(id)init {
       
    54 
       
    55 	self = [super init];
       
    56 	x = y = z = 0.0;
       
    57 	return self;
       
    58 	
       
    59 }
       
    60 
       
    61 -(void)dealloc {
       
    62 	sharedDelegate = nil;
       
    63 	[super dealloc];
       
    64 }
       
    65 
       
    66 -(void)startup {
       
    67 	[UIAccelerometer sharedAccelerometer].delegate = self;
       
    68 	isRunning = YES;
       
    69 }
       
    70 
       
    71 -(void)shutdown {
       
    72 	[UIAccelerometer sharedAccelerometer].delegate = nil;
       
    73 	isRunning = NO;
       
    74 }
       
    75 
       
    76 -(BOOL)isRunning {
       
    77 	return isRunning;
       
    78 }
       
    79 
       
    80 -(BOOL)hasNewData {
       
    81 	return hasNewData;
       
    82 }
       
    83 
       
    84 -(void)setHasNewData:(BOOL)value {
       
    85 	hasNewData = value;
       
    86 }
       
    87 
       
    88 @end