summaryrefslogtreecommitdiff
path: root/src/plugins
diff options
context:
space:
mode:
authorRichard Moe Gustavsen <richard.gustavsen@digia.com>2013-03-21 16:44:12 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-04-14 11:31:08 +0200
commit00388a96f3324b540580d8171102b4ae1cf17f0e (patch)
treecbc2d1eb054a866e89b5f5f4ae4851a19aa13d82 /src/plugins
parent8c9b27b1d01aab98ed5e283499654a74a1551d92 (diff)
downloadqtsensors-00388a96f3324b540580d8171102b4ae1cf17f0e.tar.gz
iOS: refactor and simplify IOSAccelerometer code
The code as it stood made use of an extra obj-c class to listen for sensor readings. This is not needed with the block (lambda) API used by CMMotionManager. This will also make IOSGyroscope and (soon) IOSMagnetometer follow the same code pattern. Change-Id: If945f8616f4cdde98b31382fcd9c555c2b490c4a Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@digia.com> Reviewed-by: Lorn Potter <lorn.potter@jollamobile.com>
Diffstat (limited to 'src/plugins')
-rw-r--r--src/plugins/sensors/ios/iosaccelerometer.h5
-rw-r--r--src/plugins/sensors/ios/iosaccelerometer.mm91
2 files changed, 22 insertions, 74 deletions
diff --git a/src/plugins/sensors/ios/iosaccelerometer.h b/src/plugins/sensors/ios/iosaccelerometer.h
index 34cc3eb..5fcac19 100644
--- a/src/plugins/sensors/ios/iosaccelerometer.h
+++ b/src/plugins/sensors/ios/iosaccelerometer.h
@@ -42,6 +42,8 @@
#ifndef IOSACCELEROMETER_H
#define IOSACCELEROMETER_H
+#include <Foundation/Foundation.h>
+
#include <qsensorbackend.h>
#include <qaccelerometer.h>
@@ -59,10 +61,9 @@ public:
void start();
void stop();
- void readingsChanged(quint64,qreal,qreal,qreal);
private:
- QtIoAccelListener *m_listener;
+ NSOperationQueue *m_updateQueue;
QAccelerometerReading m_reading;
};
QT_END_NAMESPACE
diff --git a/src/plugins/sensors/ios/iosaccelerometer.mm b/src/plugins/sensors/ios/iosaccelerometer.mm
index a815c40..ab6e8c4 100644
--- a/src/plugins/sensors/ios/iosaccelerometer.mm
+++ b/src/plugins/sensors/ios/iosaccelerometer.mm
@@ -47,67 +47,11 @@
char const * const IOSAccelerometer::id("ios.accelerometer");
-@interface QtIoAccelListener : NSObject
-{
- IOSAccelerometer *m_qiosAccelerometer;
- NSOperationQueue *m_updateQueue;
-}
-@end
-
-@implementation QtIoAccelListener
-
--(id)initWithQIOSAccelerometer:(IOSAccelerometer *) qiosAccelerometer
-{
- self = [super init];
- if (self) {
- m_qiosAccelerometer = qiosAccelerometer;
- m_updateQueue = [[NSOperationQueue alloc] init];
- }
- return self;
-}
-
--(void)dealloc
-{
- [m_updateQueue release];
- [super dealloc];
-}
-
--(void)startAccelerometer
-{
- CMMotionManager *motionManager = [QIOSMotionManager sharedManager];
- [motionManager startAccelerometerUpdatesToQueue:m_updateQueue withHandler:^(CMAccelerometerData *data, NSError *error) {
- dispatch_async(dispatch_get_main_queue(), ^{
- Q_UNUSED(error);
- CMAcceleration acc = data.acceleration;
- // Convert from G to m/s2, and flip axes:
- const qreal G = 9.8066;
- qreal x = qreal(acc.x) * G * -1;
- qreal y = qreal(acc.y) * G * -1;
- qreal z = qreal(acc.z) * G * -1;
- // Convert from NSTimeInterval to microseconds:
- quint64 timestamp = quint64(data.timestamp * 1000000);
- m_qiosAccelerometer->readingsChanged(timestamp, x, y, z);
- });
- }];
-}
-
--(void)stopAccelerometer
-{
- [[QIOSMotionManager sharedManager] stopAccelerometerUpdates];
-}
-
--(void)setInterval:(NSTimeInterval) interval
-{
- [QIOSMotionManager sharedManager].accelerometerUpdateInterval = interval;
-}
-
-@end
-
QT_BEGIN_NAMESPACE
IOSAccelerometer::IOSAccelerometer(QSensor *sensor)
: QSensorBackend(sensor)
- , m_listener([[QtIoAccelListener alloc] initWithQIOSAccelerometer:this])
+ , m_updateQueue([[NSOperationQueue alloc] init])
{
setReading<QAccelerometerReading>(&m_reading);
addDataRate(1, 100); // 100Hz
@@ -116,31 +60,34 @@ IOSAccelerometer::IOSAccelerometer(QSensor *sensor)
IOSAccelerometer::~IOSAccelerometer()
{
- [m_listener dealloc];
+ [m_updateQueue release];
}
void IOSAccelerometer::start()
{
+ CMMotionManager *motionManager = [QIOSMotionManager sharedManager];
// Convert from Hz to NSTimeInterval:
int hz = sensor()->dataRate();
- NSTimeInterval interval = (hz == 0) ? 0 : 1. / hz;
- [m_listener setInterval:interval];
- [m_listener startAccelerometer];
-}
+ motionManager.accelerometerUpdateInterval = (hz == 0) ? 0 : 1. / hz;
-void IOSAccelerometer::stop()
-{
- [m_listener stopAccelerometer];
+ [motionManager startAccelerometerUpdatesToQueue:m_updateQueue withHandler:^(CMAccelerometerData *data, NSError *error) {
+ dispatch_async(dispatch_get_main_queue(), ^{
+ Q_UNUSED(error);
+ // Convert from NSTimeInterval to microseconds and G to m/s2, and flip axes:
+ CMAcceleration acc = data.acceleration;
+ const qreal G = 9.8066;
+ m_reading.setTimestamp(quint64(data.timestamp * 1000000));
+ m_reading.setX(qreal(acc.x) * G * -1);
+ m_reading.setY(qreal(acc.y) * G * -1);
+ m_reading.setZ(qreal(acc.z) * G * -1);
+ newReadingAvailable();
+ });
+ }];
}
-void IOSAccelerometer::readingsChanged(quint64 ts, qreal x, qreal y, qreal z)
+void IOSAccelerometer::stop()
{
- m_reading.setTimestamp(ts);
- m_reading.setX(x);
- m_reading.setY(y);
- m_reading.setZ(z);
-
- newReadingAvailable();
+ [[QIOSMotionManager sharedManager] stopAccelerometerUpdates];
}
QT_END_NAMESPACE