Debian Codegraph @

tests/manual/qardboard/iosdeviceorientation_p.mm

raw
1 // Copyright (C) 2021 The Qt Company Ltd.
2 // Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).
3 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
4
5 #import <Foundation/Foundation.h>
6 #import <UIKit/UIKit.h>
7
8 #include "iosdeviceorientation.h"
9 #import "iosdeviceorientation_p.h"
10 #include <QtCore/qmath.h>
11
12 @interface QIOSMotionManager : NSObject {
13 }
14
15 + (CMMotionManager *)sharedManager;
16 @end
17
18 @interface iOSDeviceOrientationP()
19 {
20 BOOL active;
21 iOSDeviceOrientation* handler;
22 }
23
24 @property (strong) CMMotionManager *motionManager;
25 @end
26
27
28
29 @implementation iOSDeviceOrientationP
30
31 #define kUpdateFrequency 20.0
32
33
34 + (iOSDeviceOrientationP*)instance
35 {
36 static iOSDeviceOrientationP* _myInstance = 0;
37 if (0 == _myInstance)
38 _myInstance = [[iOSDeviceOrientationP alloc] init];
39 return _myInstance;
40 }
41
42 - (id)init
43 {
44 self = [super init];
45 if (self) {
46 self.motionManager = [QIOSMotionManager sharedManager]; // [[CMMotionManager alloc] init];
47 self.motionManager.deviceMotionUpdateInterval = 1. / kUpdateFrequency;
48
49 active = FALSE;
50 }
51 return self;
52 }
53
54 - (void)setHandler:(iOSDeviceOrientation*)h
55 {
56 handler = h;
57 }
58
59 - (BOOL)isActive
60 {
61 return active;
62 }
63
64 - (void)start
65 {
66 // Motion updates
67 [self.motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryCorrectedZVertical
68 toQueue:[NSOperationQueue currentQueue]
69 withHandler: ^(CMDeviceMotion *motion, NSError *error) {
70 //CMAttitude *attitude = motion.attitude;
71 //NSLog(@"rotation rate = [%f, %f, %f]", attitude.pitch, attitude.roll, attitude.yaw);
72 if (error)
73 NSLog(@"%@", [error description]);
74 else
75 [self performSelectorOnMainThread:@selector(handleDeviceMotion:) withObject:motion waitUntilDone:YES];
76 }];
77 active = TRUE;
78 }
79
80 - (void)stop
81 {
82 [_motionManager stopDeviceMotionUpdates];
83 active = FALSE;
84 }
85
86 - (void)handleDeviceMotion:(CMDeviceMotion*)motion
87 {
88 if (!active)
89 return;
90
91 if (motion.magneticField.accuracy == CMMagneticFieldCalibrationAccuracyUncalibrated)
92 return;
93
94 // X: A pitch is a rotation around a lateral axis that passes through the device from side to side.
95 // Y: A roll is a rotation around a longitudinal axis that passes through the device from its top to bottom.
96 // Z: A yaw is a rotation around an axis that runs vertically through the device. It is perpendicular to the
97 // body of the device, with its origin at the center of gravity and directed toward the bottom of the device.
98
99 CMAttitude *attitude = motion.attitude;
100 handler->setRoll(90 - qRadiansToDegrees(attitude.roll));
101 handler->setPitch(qRadiansToDegrees(attitude.pitch));
102 handler->setYaw(qRadiansToDegrees(attitude.yaw));
103 }
104
105 @end