-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathGyroscope.m
105 lines (87 loc) · 3.59 KB
/
Gyroscope.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// Gyroscope.m
//
// Created by Patrick Williams in beautiful Seattle, WA.
//
#import <React/RCTBridge.h>
#import <React/RCTEventDispatcher.h>
#import "Gyroscope.h"
@implementation Gyroscope
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE();
- (id) init {
self = [super init];
NSLog(@"Gyroscope");
if (self) {
self->_motionManager = [[CMMotionManager alloc] init];
//Gyroscope
if([self->_motionManager isGyroAvailable])
{
NSLog(@"Gyroscope available");
/* Start the gyroscope if it is not active already */
if([self->_motionManager isGyroActive] == NO)
{
NSLog(@"Gyroscope active");
} else {
NSLog(@"Gyroscope not active");
}
}
else
{
NSLog(@"Gyroscope not Available!");
}
}
return self;
}
RCT_EXPORT_METHOD(setGyroUpdateInterval:(double) interval) {
NSLog(@"setGyroUpdateInterval: %f", interval);
[self->_motionManager setGyroUpdateInterval:interval];
}
RCT_EXPORT_METHOD(getGyroUpdateInterval:(RCTResponseSenderBlock) cb) {
double interval = self->_motionManager.gyroUpdateInterval;
NSLog(@"getGyroUpdateInterval: %f", interval);
cb(@[[NSNull null], [NSNumber numberWithDouble:interval]]);
}
RCT_EXPORT_METHOD(getGyroData:(RCTResponseSenderBlock) cb) {
double x = self->_motionManager.gyroData.rotationRate.x;
double y = self->_motionManager.gyroData.rotationRate.y;
double z = self->_motionManager.gyroData.rotationRate.z;
double timestamp = self->_motionManager.gyroData.timestamp;
NSLog(@"getGyroData: %f, %f, %f, %f", x, y, z, timestamp);
cb(@[[NSNull null], @{
@"rotationRate": @{
@"x" : [NSNumber numberWithDouble:x],
@"y" : [NSNumber numberWithDouble:y],
@"z" : [NSNumber numberWithDouble:z],
@"timestamp" : [NSNumber numberWithDouble:timestamp]
}
}]
);
}
RCT_EXPORT_METHOD(startGyroUpdates) {
NSLog(@"startGyroUpdates");
[self->_motionManager startGyroUpdates];
/* Receive the gyroscope data on this block */
[self->_motionManager startGyroUpdatesToQueue:[NSOperationQueue mainQueue]
withHandler:^(CMGyroData *gyroData, NSError *error)
{
double x = gyroData.rotationRate.x;
double y = gyroData.rotationRate.y;
double z = gyroData.rotationRate.z;
double timestamp = gyroData.timestamp;
NSLog(@"startGyroUpdates: %f, %f, %f, %f", x, y, z, timestamp);
[self.bridge.eventDispatcher sendDeviceEventWithName:@"GyroData" body:@{
@"rotationRate": @{
@"x" : [NSNumber numberWithDouble:x],
@"y" : [NSNumber numberWithDouble:y],
@"z" : [NSNumber numberWithDouble:z],
@"timestamp" : [NSNumber numberWithDouble:timestamp]
}
}];
}];
}
RCT_EXPORT_METHOD(stopGyroUpdates) {
NSLog(@"stopGyroUpdates");
[self->_motionManager stopGyroUpdates];
}
@end