summaryrefslogtreecommitdiffstats
path: root/src/plugins/sensors/ios/iosgyroscope.mm
diff options
context:
space:
mode:
authorRichard Moe Gustavsen <richard.gustavsen@digia.com>2013-06-10 12:33:08 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-06-10 13:28:38 +0200
commitb01f928735a768fe676df60523be1fd718d45593 (patch)
tree1b9f6014281eecb92b46db6be149d9a97661ab84 /src/plugins/sensors/ios/iosgyroscope.mm
parentd487c74a7f6a18a106314d3dc58013c6cfc2e1ca (diff)
iOS: change gyro implementation to use Timer base polling
Ref change: 102bdf3 We need to change the implementation to use polling rather than callback to achieve full performance together with fine-grained QTimers. Change-Id: I895418996b53432642d37279855167d815261d92 Reviewed-by: Lorn Potter <lorn.potter@jollamobile.com>
Diffstat (limited to 'src/plugins/sensors/ios/iosgyroscope.mm')
-rw-r--r--src/plugins/sensors/ios/iosgyroscope.mm51
1 files changed, 19 insertions, 32 deletions
diff --git a/src/plugins/sensors/ios/iosgyroscope.mm b/src/plugins/sensors/ios/iosgyroscope.mm
index 700755de..8dfa3a4a 100644
--- a/src/plugins/sensors/ios/iosgyroscope.mm
+++ b/src/plugins/sensors/ios/iosgyroscope.mm
@@ -39,9 +39,6 @@
**
****************************************************************************/
-#include <CoreMotion/CMMotionManager.h>
-#include <QPointer>
-
#include "iosmotionmanager.h"
#include "iosgyroscope.h"
@@ -51,48 +48,38 @@ QT_BEGIN_NAMESPACE
IOSGyroscope::IOSGyroscope(QSensor *sensor)
: QSensorBackend(sensor)
- , m_updateQueue([[NSOperationQueue alloc] init])
+ , m_motionManager([QIOSMotionManager sharedManager])
+ , m_timer(0)
{
setReading<QGyroscopeReading>(&m_reading);
addDataRate(1, 100); // 100Hz is max it seems
addOutputRange(-360, 360, 0.01);
}
-IOSGyroscope::~IOSGyroscope()
-{
- [m_updateQueue release];
-}
-
void IOSGyroscope::start()
{
- CMMotionManager *motionManager = [QIOSMotionManager sharedManager];
- // Convert Hz to NSTimeInterval:
int hz = sensor()->dataRate();
- motionManager.gyroUpdateInterval = (hz == 0) ? 0 : 1. / hz;
-
- QPointer<QObject> self = this;
- [motionManager startGyroUpdatesToQueue:m_updateQueue withHandler:^(CMGyroData *data, NSError *error) {
- // NSOperationQueue is multi-threaded, so we process the data by queuing a callback to
- // the main application queue. By the time the callback executes, IOSAccelerometer might
- // have been deleted, so we need an extra QPointer check for that:
- dispatch_async(dispatch_get_main_queue(), ^{
- if (self) {
- Q_UNUSED(error);
- // Convert NSTimeInterval to microseconds and radians to degrees:
- CMRotationRate rate = data.rotationRate;
- m_reading.setTimestamp(quint64(data.timestamp * 1000000));
- m_reading.setX((qreal(rate.x) / M_PI) * 180);
- m_reading.setY((qreal(rate.y) / M_PI) * 180);
- m_reading.setZ((qreal(rate.z) / M_PI) * 180);
- newReadingAvailable();
- }
- });
- }];
+ m_timer = startTimer(1000 / (hz == 0 ? 60 : hz));
+ [m_motionManager startGyroUpdates];
}
void IOSGyroscope::stop()
{
- [[QIOSMotionManager sharedManager] stopGyroUpdates];
+ [m_motionManager stopGyroUpdates];
+ killTimer(m_timer);
+ m_timer = 0;
+}
+
+void IOSGyroscope::timerEvent(QTimerEvent *)
+{
+ // Convert NSTimeInterval to microseconds and radians to degrees:
+ CMGyroData *data = m_motionManager.gyroData;
+ CMRotationRate rate = data.rotationRate;
+ m_reading.setTimestamp(quint64(data.timestamp * 1e6));
+ m_reading.setX((qreal(rate.x) / M_PI) * 180);
+ m_reading.setY((qreal(rate.y) / M_PI) * 180);
+ m_reading.setZ((qreal(rate.z) / M_PI) * 180);
+ newReadingAvailable();
}
QT_END_NAMESPACE