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