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. gsoc2008_iphone
authorHolmes Futrell <hfutrell@umail.ucsb.edu>
Fri, 18 Jul 2008 17:46:17 +0000
branchgsoc2008_iphone
changeset 236244fc2537ff9e
parent 2361 ba39d5af12a7
child 2363 49b243db2e04
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.
src/joystick/iphoneos/SDLUIAccelerationDelegate.h
src/joystick/iphoneos/SDLUIAccelerationDelegate.m
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/joystick/iphoneos/SDLUIAccelerationDelegate.h	Fri Jul 18 17:46:17 2008 +0000
     1.3 @@ -0,0 +1,30 @@
     1.4 +//
     1.5 +//  SDLUIAccelerationDelegate.h
     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 <UIKit/UIKit.h>
    1.13 +#import "SDL_stdinc.h"
    1.14 +
    1.15 +@interface SDLUIAccelerationDelegate: NSObject <UIAccelerometerDelegate> {
    1.16 +
    1.17 +	UIAccelerationValue x, y, z;
    1.18 +	//NSTimeInterval timestamp;
    1.19 +	BOOL isRunning;
    1.20 +	BOOL hasNewData;
    1.21 +	
    1.22 +}
    1.23 +
    1.24 ++(SDLUIAccelerationDelegate *)sharedDelegate;
    1.25 +-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration;
    1.26 +-(void)getLastOrientation:(Sint16 *)data;
    1.27 +-(void)startup;
    1.28 +-(void)shutdown;
    1.29 +-(BOOL)isRunning;
    1.30 +-(BOOL)hasNewData;
    1.31 +-(void)setHasNewData:(BOOL)value;
    1.32 +
    1.33 +@end
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/joystick/iphoneos/SDLUIAccelerationDelegate.m	Fri Jul 18 17:46:17 2008 +0000
     2.3 @@ -0,0 +1,88 @@
     2.4 +//
     2.5 +//  SDLUIAccelerationDelegate.m
     2.6 +//  iPodSDL
     2.7 +//
     2.8 +//  Created by Holmes Futrell on 6/21/08.
     2.9 +//  Copyright 2008 __MyCompanyName__. All rights reserved.
    2.10 +//
    2.11 +
    2.12 +#import "SDLUIAccelerationDelegate.h"
    2.13 +
    2.14 +static SDLUIAccelerationDelegate *sharedDelegate=nil;
    2.15 +
    2.16 +@implementation SDLUIAccelerationDelegate
    2.17 +
    2.18 ++(SDLUIAccelerationDelegate *)sharedDelegate {
    2.19 +	if (sharedDelegate == nil) {
    2.20 +		sharedDelegate = [[SDLUIAccelerationDelegate alloc] init];
    2.21 +	}
    2.22 +	return sharedDelegate;
    2.23 +}
    2.24 +
    2.25 +-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    2.26 +		
    2.27 +	x = acceleration.x;
    2.28 +	y = acceleration.y;
    2.29 +	z = acceleration.z;
    2.30 +	
    2.31 +	hasNewData = YES;
    2.32 +	//timestamp = acceleration.timestamp;
    2.33 +	
    2.34 +}
    2.35 +
    2.36 +-(void)getLastOrientation:(Sint16 *)data {
    2.37 +
    2.38 +#define MAX_G_FORCE 5.0
    2.39 +#define MAX_SINT16 0x7FFF	
    2.40 +
    2.41 +	if (x > MAX_G_FORCE) x = MAX_G_FORCE;
    2.42 +	else if (x < -MAX_G_FORCE) x = -MAX_G_FORCE;
    2.43 +
    2.44 +	if (y > MAX_G_FORCE) y = MAX_G_FORCE;
    2.45 +	else if (y < -MAX_G_FORCE) y = -MAX_G_FORCE;
    2.46 +
    2.47 +	if (z > MAX_G_FORCE) z = MAX_G_FORCE;
    2.48 +	else if (z < -MAX_G_FORCE) z = -MAX_G_FORCE;
    2.49 +	
    2.50 +	data[0] = (x / MAX_G_FORCE) * MAX_SINT16;
    2.51 +	data[1] = (y / MAX_G_FORCE) * MAX_SINT16;
    2.52 +	data[2] = (z / MAX_G_FORCE) * MAX_SINT16;
    2.53 +
    2.54 +}
    2.55 +
    2.56 +-(id)init {
    2.57 +
    2.58 +	self = [super init];
    2.59 +	x = y = z = 0.0;
    2.60 +	return self;
    2.61 +	
    2.62 +}
    2.63 +
    2.64 +-(void)dealloc {
    2.65 +	sharedDelegate = nil;
    2.66 +	[super dealloc];
    2.67 +}
    2.68 +
    2.69 +-(void)startup {
    2.70 +	[UIAccelerometer sharedAccelerometer].delegate = self;
    2.71 +	isRunning = YES;
    2.72 +}
    2.73 +
    2.74 +-(void)shutdown {
    2.75 +	[UIAccelerometer sharedAccelerometer].delegate = nil;
    2.76 +	isRunning = NO;
    2.77 +}
    2.78 +
    2.79 +-(BOOL)isRunning {
    2.80 +	return isRunning;
    2.81 +}
    2.82 +
    2.83 +-(BOOL)hasNewData {
    2.84 +	return hasNewData;
    2.85 +}
    2.86 +
    2.87 +-(void)setHasNewData:(BOOL)value {
    2.88 +	hasNewData = value;
    2.89 +}
    2.90 +
    2.91 +@end