5
// Created by mac on 3/8/13.
9
#import "SensorAccess.h"
11
@implementation SensorAccess
14
if ((self = [super init])) {
16
self.motionManager = [[[CMMotionManager alloc] init] autorelease];
17
self.queue = [[[NSOperationQueue alloc] init] autorelease];
19
self.locMngr = [[[CLLocationManager alloc] init] autorelease];
20
self.locMngr.delegate = self;
21
self.locMngr.desiredAccuracy = kCLLocationAccuracyBest;
22
self.locMngr.distanceFilter = 2;
34
self.locMngr.delegate = NULL;
38
- (int) setSensorType :(int) type
49
- (int) setSensorCallback:(SensorDataCallback*)callback :(void*)user
51
sensorCallbak = callback;
59
case SENSOR_ACCELEROMETER:
60
if ( ![[self motionManager] isAccelerometerAvailable] )
62
[[self motionManager] startAccelerometerUpdatesToQueue:[self queue] withHandler:^(CMAccelerometerData* data, NSError* error) {
63
if ( sensorCallbak ) {
65
sprintf(cdata, "%f;%f;%f;", data.acceleration.x, data.acceleration.y, data.acceleration.z);
66
sensorCallbak(userData, cdata);
72
case SENSOR_GYROSCOPE:
73
if ( ![[self motionManager] isGyroAvailable] )
75
[[self motionManager] startGyroUpdatesToQueue:[self queue] withHandler:^(CMGyroData* data, NSError* error) {
76
if ( sensorCallbak ) {
78
sprintf(cdata, "%f;%f;%f;", data.rotationRate.x, data.rotationRate.y, data.rotationRate.z);
79
sensorCallbak(userData, cdata);
85
case SENSOR_MAGNETOMETER:
86
if ( ![[self motionManager] isMagnetometerAvailable] )
88
[[self motionManager] startMagnetometerUpdatesToQueue:[self queue] withHandler:^(CMMagnetometerData* data, NSError* error) {
89
if ( sensorCallbak ) {
91
sprintf(cdata, "%f;%f;%f;", data.magneticField.x, data.magneticField.y, data.magneticField.z);
92
sensorCallbak(userData, cdata);
99
[self.locMngr startUpdatingLocation];
110
switch (sensorType) {
111
case SENSOR_ACCELEROMETER:
112
[[self motionManager] stopAccelerometerUpdates];
115
case SENSOR_GYROSCOPE:
116
[[self motionManager] stopGyroUpdates];
119
case SENSOR_MAGNETOMETER:
120
[[self motionManager] stopMagnetometerUpdates];
123
[self.locMngr stopUpdatingLocation];
131
- (int) isSensorStarted
133
switch (sensorType) {
134
case SENSOR_ACCELEROMETER:
135
return [[self motionManager] isAccelerometerActive];
137
case SENSOR_GYROSCOPE:
138
return [[self motionManager] isGyroActive];
140
case SENSOR_MAGNETOMETER:
141
return [[self motionManager] isMagnetometerActive];
149
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)location fromLocation:(CLLocation *)oldLocation {
151
sprintf(cdata, "%f;%f;%f;%f;%f;", (float)location.coordinate.latitude, (float)location.coordinate.longitude, (float)location.altitude,
152
(float)location.horizontalAccuracy, 0.f);
153
sensorCallbak(userData, cdata);
158
void* SENS_CreateInstance() {
159
void* obj = [[SensorAccess alloc] init];
163
int SENS_DestroyInstance(void** inst) {
169
int SENS_SetSensorType(void* inst, int type) {
173
return [(id)inst setSensorType:type];
176
int SENS_GetCurrentSensorType(void* inst) {
180
return [(id)inst getSensorType];
183
int SENS_Start(void* inst) {
187
return [(id)inst startSensor];
190
int SENS_Stop(void* inst) {
194
return [(id)inst stopSensor];
197
int SENS_IsStarted(void* inst) {
201
return [(id)inst isSensorStarted];
204
int SENS_SetCallback(void* inst, SensorDataCallback *callback, void* user) {
208
return [(id)inst setSensorCallback:callback :user];