/
EXSensorsManager.m
378 lines (333 loc) · 12.9 KB
/
EXSensorsManager.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Copyright 2015-present 650 Industries. All rights reserved.
#import <EXSensors/EXSensorsManager.h>
#import <CoreMotion/CoreMotion.h>
@interface EXSensorsManager ()
@property (nonatomic, strong) CMMotionManager *manager;
@property (nonatomic, strong) CMAltimeter *altimeter;
@property (nonatomic, strong) NSMutableDictionary *accelerometerHandlers;
@property (nonatomic, strong) NSMutableDictionary *barometerHandlers;
@property (nonatomic, strong) NSMutableDictionary *deviceMotionHandlers;
@property (nonatomic, strong) NSMutableDictionary *gyroscopeHandlers;
@property (nonatomic, strong) NSMutableDictionary *magnetometerHandlers;
@property (nonatomic, strong) NSMutableDictionary *magnetometerUncalibratedHandlers;
@end
@implementation EXSensorsManager
UM_REGISTER_MODULE();
+ (const NSArray<Protocol *> *)exportedInterfaces
{
return @[@protocol(UMAccelerometerInterface), @protocol(UMBarometerInterface), @protocol(UMDeviceMotionInterface), @protocol(UMGyroscopeInterface), @protocol(UMMagnetometerInterface), @protocol(UMMagnetometerUncalibratedInterface)];
}
- (instancetype)init
{
if (self = [super init]) {
_accelerometerHandlers = [[NSMutableDictionary alloc] init];
_barometerHandlers = [[NSMutableDictionary alloc] init];
_deviceMotionHandlers = [[NSMutableDictionary alloc] init];
_gyroscopeHandlers = [[NSMutableDictionary alloc] init];
_magnetometerHandlers = [[NSMutableDictionary alloc] init];
_magnetometerUncalibratedHandlers = [[NSMutableDictionary alloc] init];
}
return self;
}
- (CMMotionManager *)manager
{
if (!_manager) {
_manager = [[CMMotionManager alloc] init];
}
return _manager;
}
- (CMAltimeter *)altimeter
{
if (!_altimeter) {
_altimeter = [[CMAltimeter alloc] init];
}
return _altimeter;
}
- (void)dealloc
{
[[self manager] stopAccelerometerUpdates];
[[self manager] stopDeviceMotionUpdates];
[[self manager] stopGyroUpdates];
[[self manager] stopMagnetometerUpdates];
[self.altimeter stopRelativeAltitudeUpdates];
}
- (void)sensorModuleDidSubscribeForAccelerometerUpdates:(id)scopedSensorModule
withHandler:(void (^)(NSDictionary *event))handlerBlock
{
if ([[self manager] isAccelerometerAvailable]) {
_accelerometerHandlers[scopedSensorModule] = handlerBlock;
}
if (![[self manager] isAccelerometerActive]) {
[[self manager] setAccelerometerUpdateInterval:0.1f];
__weak EXSensorsManager *weakSelf = self;
[[self manager] startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData *data, NSError *error) {
__strong EXSensorsManager *strongSelf = weakSelf;
if (strongSelf) {
for (void (^handler)(NSDictionary *) in strongSelf.accelerometerHandlers.allValues) {
handler(@{
@"x": [NSNumber numberWithDouble:data.acceleration.x],
@"y": [NSNumber numberWithDouble:data.acceleration.y],
@"z": [NSNumber numberWithDouble:data.acceleration.z]
});
}
}
}];
}
}
- (void)sensorModuleDidUnsubscribeForAccelerometerUpdates:(id)scopedSensorModule
{
[_accelerometerHandlers removeObjectForKey:scopedSensorModule];
if (_accelerometerHandlers.count == 0) {
[[self manager] stopAccelerometerUpdates];
}
}
- (void)setAccelerometerUpdateInterval:(NSTimeInterval)intervalMs
{
[[self manager] setAccelerometerUpdateInterval:intervalMs];
}
- (BOOL)isAccelerometerAvailable
{
return [[self manager] isAccelerometerAvailable];
}
- (void)sensorModuleDidSubscribeForBarometerUpdates:(id)scopedSensorModule
withHandler:(void (^)(NSDictionary *event))handlerBlock
{
if ([self isBarometerAvailable]) {
_barometerHandlers[scopedSensorModule] = handlerBlock;
}
__weak EXSensorsManager *weakSelf = self;
[[self altimeter] startRelativeAltitudeUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAltitudeData * _Nullable data, NSError * _Nullable error) {
__strong EXSensorsManager *strongSelf = weakSelf;
if (strongSelf && data) {
for (void (^handler)(NSDictionary *) in strongSelf.barometerHandlers.allValues) {
handler(@{
@"pressure": @([data.pressure intValue] * 10), // conversion from kPa to hPa
@"relativeAltitude": data.relativeAltitude,
});
}
}
}];
}
- (void)sensorModuleDidUnsubscribeForBarometerUpdates:(id)scopedSensorModule
{
[_barometerHandlers removeObjectForKey:scopedSensorModule];
if (_barometerHandlers.count == 0) {
[[self altimeter] stopRelativeAltitudeUpdates];
}
}
- (void)setBarometerUpdateInterval:(NSTimeInterval)intervalMs
{
// Do nothing
}
- (BOOL)isBarometerAvailable
{
return [CMAltimeter isRelativeAltitudeAvailable];
}
- (void)sensorModuleDidSubscribeForDeviceMotionUpdates:(id)scopedSensorModule
withHandler:(void (^)(NSDictionary *event))handlerBlock
{
if ([[self manager] isDeviceMotionAvailable]) {
_deviceMotionHandlers[scopedSensorModule] = handlerBlock;
}
if (![[self manager] isDeviceMotionActive]) {
[self activateDeviceMotionUpdates];
}
}
- (void)sensorModuleDidUnsubscribeForDeviceMotionUpdates:(id)scopedSensorModule
{
[_deviceMotionHandlers removeObjectForKey:scopedSensorModule];
if (_deviceMotionHandlers.count == 0 && _magnetometerHandlers.count == 0) {
[[self manager] stopDeviceMotionUpdates];
}
}
- (void)setDeviceMotionUpdateInterval:(NSTimeInterval)intervalMs
{
[[self manager] setDeviceMotionUpdateInterval:intervalMs];
}
- (BOOL)isDeviceMotionAvailable
{
return [[self manager] isDeviceMotionAvailable];
}
- (void)sensorModuleDidSubscribeForGyroscopeUpdates:(id)scopedSensorModule
withHandler:(void (^)(NSDictionary *event))handlerBlock
{
if ([[self manager] isGyroAvailable]) {
_gyroscopeHandlers[scopedSensorModule] = handlerBlock;
}
if (![[self manager] isGyroActive]) {
[[self manager] setGyroUpdateInterval:0.1f];
__weak EXSensorsManager *weakSelf = self;
[[self manager] startGyroUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMGyroData *data, NSError *error) {
__strong EXSensorsManager *strongSelf = weakSelf;
if (strongSelf) {
for (void (^handler)(NSDictionary *) in strongSelf.gyroscopeHandlers.allValues) {
// https://docs-assets.developer.apple.com/published/96e9d46b41/ab00c9d5-4f3d-475b-8020-95066068a18d.png
handler(@{
@"x": [NSNumber numberWithDouble:data.rotationRate.x],
@"y": [NSNumber numberWithDouble:data.rotationRate.y],
@"z": [NSNumber numberWithDouble:data.rotationRate.z]
});
}
}
}];
}
}
- (void)sensorModuleDidUnsubscribeForGyroscopeUpdates:(id)scopedSensorModule
{
[_gyroscopeHandlers removeObjectForKey:scopedSensorModule];
if (_gyroscopeHandlers.count == 0) {
[[self manager] stopGyroUpdates];
}
}
- (void)setGyroscopeUpdateInterval:(NSTimeInterval)intervalMs
{
[[self manager] setGyroUpdateInterval:intervalMs];
}
- (BOOL)isGyroAvailable
{
return [[self manager] isGyroAvailable];
}
- (void)sensorModuleDidSubscribeForMagnetometerUpdates:(id)scopedSensorModule
withHandler:(void (^)(NSDictionary *event))handlerBlock
{
if ([[self manager] isDeviceMotionAvailable]) {
_magnetometerHandlers[scopedSensorModule] = handlerBlock;
}
if (![[self manager] isDeviceMotionActive]) {
[self activateDeviceMotionUpdates];
}
}
- (void)sensorModuleDidUnsubscribeForMagnetometerUpdates:(id)scopedSensorModule
{
[_magnetometerHandlers removeObjectForKey:scopedSensorModule];
if (_deviceMotionHandlers.count == 0 && _magnetometerHandlers.count == 0) {
[[self manager] stopDeviceMotionUpdates];
}
}
- (void)setMagnetometerUpdateInterval:(NSTimeInterval)intervalMs
{
[[self manager] setDeviceMotionUpdateInterval:intervalMs];
}
- (BOOL)isMagnetometerAvailable
{
return [self isDeviceMotionAvailable];
}
- (void)sensorModuleDidSubscribeForMagnetometerUncalibratedUpdates:(id)scopedSensorModule
withHandler:(void (^)(NSDictionary *event))handlerBlock
{
if ([[self manager] isMagnetometerAvailable]) {
_magnetometerUncalibratedHandlers[scopedSensorModule] = handlerBlock;
}
if (![[self manager] isMagnetometerActive]) {
[[self manager] setMagnetometerUpdateInterval:0.1f];
__weak EXSensorsManager *weakSelf = self;
[[self manager] startMagnetometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMMagnetometerData *data, NSError *error) {
__strong EXSensorsManager *strongSelf = weakSelf;
if (strongSelf) {
for (void (^handler)(NSDictionary *) in strongSelf.magnetometerUncalibratedHandlers.allValues) {
handler(@{
@"x": [NSNumber numberWithDouble:data.magneticField.x],
@"y": [NSNumber numberWithDouble:data.magneticField.y],
@"z": [NSNumber numberWithDouble:data.magneticField.z]
});
}
}
}];
}
}
- (void)sensorModuleDidUnsubscribeForMagnetometerUncalibratedUpdates:(id)scopedSensorModule
{
[_magnetometerUncalibratedHandlers removeObjectForKey:scopedSensorModule];
if (_magnetometerUncalibratedHandlers.count == 0) {
[[self manager] stopMagnetometerUpdates];
}
}
- (void)setMagnetometerUncalibratedUpdateInterval:(NSTimeInterval)intervalMs
{
[[self manager] setMagnetometerUpdateInterval:intervalMs];
}
- (BOOL)isMagnetometerUncalibratedAvailable
{
return [[self manager] isMagnetometerAvailable];
}
- (float)getGravity
{
return EXGravity;
}
- (float)radiansToDegrees:(double)radians
{
return radians * 180 / M_PI;
}
// Match https://www.w3.org/TR/orientation-event/
- (void)activateDeviceMotionUpdates
{
float interval = 1.0f / 60.0f;
[[self manager] setDeviceMotionUpdateInterval:interval];
__weak EXSensorsManager *weakSelf = self;
[[self manager] startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryCorrectedZVertical toQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion *data, NSError *error) {
__strong EXSensorsManager *strongSelf = weakSelf;
if (!strongSelf) {
return;
}
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
int orientationDegrees;
switch (orientation) {
case UIDeviceOrientationPortrait:
orientationDegrees = 0;
break;
case UIDeviceOrientationLandscapeLeft:
orientationDegrees = -90;
break;
case UIDeviceOrientationLandscapeRight:
orientationDegrees = 90;
break;
case UIDeviceOrientationPortraitUpsideDown:
orientationDegrees = 180;
break;
default:
orientationDegrees = 0;
break;
}
NSMutableDictionary *result = [NSMutableDictionary dictionaryWithDictionary:@{
@"acceleration": [NSNull null],
@"accelerationIncludingGravity": @{
@"x": @((data.userAcceleration.x + data.gravity.x) * EXGravity),
@"y": @((data.userAcceleration.y + data.gravity.y) * EXGravity),
@"z": @((data.userAcceleration.z + data.gravity.z) * EXGravity)
},
@"rotation": @{
@"alpha": @(data.attitude.yaw),
@"beta": @(data.attitude.pitch),
@"gamma": @(data.attitude.roll),
},
@"rotationRate": [NSNull null],
@"orientation": @(orientationDegrees),
@"interval": @(interval),
}];
if ([[strongSelf manager] isGyroAvailable]) {
result[@"acceleration"] = @{
@"x": @(data.userAcceleration.x * EXGravity),
@"y": @(data.userAcceleration.y * EXGravity),
@"z": @(data.userAcceleration.z * EXGravity)
};
// Rate of rotation of the hosting device in space expressed in degrees per second.
result[@"rotationRate"] = @{
@"alpha": @([strongSelf radiansToDegrees:data.rotationRate.x]),
@"beta": @([strongSelf radiansToDegrees:data.rotationRate.y]),
@"gamma": @([strongSelf radiansToDegrees:data.rotationRate.z])
};
}
// DeviceMotionUpdates handle DeviceMotion data as well as magnetic field
for (void (^handler)(NSDictionary *) in strongSelf.deviceMotionHandlers.allValues) {
handler(result);
}
for (void (^handler)(NSDictionary *) in strongSelf.magnetometerHandlers.allValues) {
handler(@{
@"x": [NSNumber numberWithDouble:data.magneticField.field.x],
@"y": [NSNumber numberWithDouble:data.magneticField.field.y],
@"z": [NSNumber numberWithDouble:data.magneticField.field.z]
});
}
}];
}
@end