src/joystick/iphoneos/SDLUIAccelerationDelegate.m
author Sam Lantinga <slouken@libsdl.org>
Fri, 11 Feb 2011 22:37:15 -0800
changeset 5262 b530ef003506
parent 2859 99210400e8b9
child 5535 96594ac5fd1a
permissions -rw-r--r--
Happy 2011! :)
slouken@2765
     1
/*
slouken@5262
     2
    SDL - Simple DirectMedia Layer
slouken@5262
     3
    Copyright (C) 1997-2011 Sam Lantinga
slouken@5262
     4
slouken@5262
     5
    This library is free software; you can redistribute it and/or
slouken@5262
     6
    modify it under the terms of the GNU Lesser General Public
slouken@5262
     7
    License as published by the Free Software Foundation; either
slouken@5262
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@5262
     9
slouken@5262
    10
    This library is distributed in the hope that it will be useful,
slouken@5262
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@5262
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@5262
    13
    Lesser General Public License for more details.
slouken@5262
    14
slouken@5262
    15
    You should have received a copy of the GNU Lesser General Public
slouken@5262
    16
    License along with this library; if not, write to the Free Software
slouken@5262
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@5262
    18
slouken@5262
    19
    Sam Lantinga
slouken@5262
    20
    slouken@libsdl.org
slouken@5262
    21
*/
slouken@2765
    22
slouken@2765
    23
#import "SDLUIAccelerationDelegate.h"
slouken@2765
    24
/* needed for SDL_IPHONE_MAX_GFORCE macro */
slouken@2765
    25
#import "../../../include/SDL_config_iphoneos.h"
slouken@2765
    26
slouken@2765
    27
static SDLUIAccelerationDelegate *sharedDelegate=nil;
slouken@2765
    28
slouken@2765
    29
@implementation SDLUIAccelerationDelegate
slouken@2765
    30
slouken@2765
    31
/*
slouken@2765
    32
	Returns a shared instance of the SDLUIAccelerationDelegate, creating the shared delegate if it doesn't exist yet.
slouken@2765
    33
*/
slouken@2765
    34
+(SDLUIAccelerationDelegate *)sharedDelegate {
slouken@2765
    35
	if (sharedDelegate == nil) {
slouken@2765
    36
		sharedDelegate = [[SDLUIAccelerationDelegate alloc] init];
slouken@2765
    37
	}
slouken@2765
    38
	return sharedDelegate;
slouken@2765
    39
}
slouken@2765
    40
/*
slouken@2765
    41
	UIAccelerometerDelegate delegate method.  Invoked by the UIAccelerometer instance when it has new data for us.
slouken@2765
    42
	We just take the data and mark that we have new data available so that the joystick system will pump it to the
slouken@2765
    43
	events system when SDL_SYS_JoystickUpdate is called.
slouken@2765
    44
*/	
slouken@2765
    45
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
slouken@2765
    46
		
slouken@2765
    47
	x = acceleration.x;
slouken@2765
    48
	y = acceleration.y;
slouken@2765
    49
	z = acceleration.z;
slouken@2765
    50
	
slouken@2765
    51
	hasNewData = YES;	
slouken@2765
    52
}
slouken@2765
    53
/* 
slouken@2765
    54
	getLastOrientation -- put last obtained accelerometer data into Sint16 array
slouken@2765
    55
 
slouken@2765
    56
	Called from the joystick system when it needs the accelerometer data.
slouken@2765
    57
	Function grabs the last data sent to the accelerometer and converts it 
slouken@2765
    58
	from floating point to Sint16, which is what the joystick system expects.
slouken@2765
    59
 
slouken@2765
    60
	To do the conversion, the data is first clamped onto the interval
slouken@2765
    61
	[-SDL_IPHONE_MAX_G_FORCE, SDL_IPHONE_MAX_G_FORCE], then the data is multiplied
slouken@2765
    62
	by MAX_SINT16 so that it is mapped to the full range of an Sint16.
slouken@2765
    63
 
slouken@2765
    64
	You can customize the clamped range of this function by modifying the 
slouken@2765
    65
	SDL_IPHONE_MAX_GFORCE macro in SDL_config_iphoneos.h.
slouken@2765
    66
 
slouken@2765
    67
	Once converted to Sint16, the accelerometer data no longer has coherent units.
slouken@2765
    68
	You can convert the data back to units of g-force by multiplying it 
slouken@2765
    69
	in your application's code by SDL_IPHONE_MAX_GFORCE / 0x7FFF.
slouken@2765
    70
 */
slouken@2765
    71
-(void)getLastOrientation:(Sint16 *)data {
slouken@2765
    72
slouken@2765
    73
	#define MAX_SINT16 0x7FFF	
slouken@2765
    74
slouken@2765
    75
	/* clamp the data */
slouken@2765
    76
	if (x > SDL_IPHONE_MAX_GFORCE) x = SDL_IPHONE_MAX_GFORCE;
slouken@2765
    77
	else if (x < -SDL_IPHONE_MAX_GFORCE) x = -SDL_IPHONE_MAX_GFORCE;
slouken@2765
    78
	if (y > SDL_IPHONE_MAX_GFORCE) y = SDL_IPHONE_MAX_GFORCE;
slouken@2765
    79
	else if (y < -SDL_IPHONE_MAX_GFORCE) y = -SDL_IPHONE_MAX_GFORCE;
slouken@2765
    80
	if (z > SDL_IPHONE_MAX_GFORCE) z = SDL_IPHONE_MAX_GFORCE;
slouken@2765
    81
	else if (z < -SDL_IPHONE_MAX_GFORCE) z = -SDL_IPHONE_MAX_GFORCE;
slouken@2765
    82
	
slouken@2765
    83
	/* pass in data mapped to range of SInt16 */
slouken@2765
    84
	data[0] = (x / SDL_IPHONE_MAX_GFORCE) * MAX_SINT16;
slouken@2765
    85
	data[1] = (y / SDL_IPHONE_MAX_GFORCE) * MAX_SINT16;
slouken@2765
    86
	data[2] = (z / SDL_IPHONE_MAX_GFORCE) * MAX_SINT16;
slouken@2765
    87
slouken@2765
    88
}
slouken@2765
    89
slouken@2765
    90
/*
slouken@2765
    91
	Initialize SDLUIAccelerationDelegate.  Since we don't have any data yet,
slouken@2765
    92
	just set our last received data to zero, and indicate we don't have any;
slouken@2765
    93
*/
slouken@2765
    94
-(id)init {
slouken@2765
    95
	self = [super init];
slouken@2765
    96
	x = y = z = 0.0;
slouken@2765
    97
	hasNewData = NO;
slouken@2765
    98
	return self;
slouken@2765
    99
}
slouken@2765
   100
slouken@2765
   101
-(void)dealloc {
slouken@2765
   102
	sharedDelegate = nil;
slouken@2765
   103
	[self shutdown];
slouken@2765
   104
	[super dealloc];
slouken@2765
   105
}
slouken@2765
   106
slouken@2765
   107
/*
slouken@2765
   108
	Lets our delegate start receiving accelerometer updates.
slouken@2765
   109
*/
slouken@2765
   110
-(void)startup {
slouken@2765
   111
	[UIAccelerometer sharedAccelerometer].delegate = self;
slouken@2765
   112
	isRunning = YES;
slouken@2765
   113
}
slouken@2765
   114
/*
slouken@2765
   115
	Stops our delegate from receiving accelerometer updates.
slouken@2765
   116
*/
slouken@2765
   117
-(void)shutdown {
slouken@2765
   118
	if ([UIAccelerometer sharedAccelerometer].delegate == self) {
slouken@2765
   119
		[UIAccelerometer sharedAccelerometer].delegate = nil;
slouken@2765
   120
	}
slouken@2765
   121
	isRunning = NO;
slouken@2765
   122
}
slouken@2765
   123
/*
slouken@2765
   124
	Our we currently receiving accelerometer updates?
slouken@2765
   125
*/
slouken@2765
   126
-(BOOL)isRunning {
slouken@2765
   127
	return isRunning;
slouken@2765
   128
}
slouken@2765
   129
/*
slouken@2765
   130
	Do we have any data that hasn't been pumped into SDL's event system?
slouken@2765
   131
*/
slouken@2765
   132
-(BOOL)hasNewData {
slouken@2765
   133
	return hasNewData;
slouken@2765
   134
}
slouken@2765
   135
/*
slouken@2765
   136
	When the joystick system grabs the new data, it sets this to NO.
slouken@2765
   137
*/
slouken@2765
   138
-(void)setHasNewData:(BOOL)value {
slouken@2765
   139
	hasNewData = value;
slouken@2765
   140
}
slouken@2765
   141
slouken@2765
   142
@end