summaryrefslogtreecommitdiffstats
path: root/src/plugins/sensors/ios
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/sensors/ios')
-rw-r--r--src/plugins/sensors/ios/iosaccelerometer.h9
-rw-r--r--src/plugins/sensors/ios/iosaccelerometer.mm52
-rw-r--r--src/plugins/sensors/ios/iosgyroscope.h7
-rw-r--r--src/plugins/sensors/ios/iosgyroscope.mm51
-rw-r--r--src/plugins/sensors/ios/iosmagnetometer.h8
-rw-r--r--src/plugins/sensors/ios/iosmagnetometer.mm129
6 files changed, 101 insertions, 155 deletions
diff --git a/src/plugins/sensors/ios/iosaccelerometer.h b/src/plugins/sensors/ios/iosaccelerometer.h
index 5fcac19f..1a6560b1 100644
--- a/src/plugins/sensors/ios/iosaccelerometer.h
+++ b/src/plugins/sensors/ios/iosaccelerometer.h
@@ -42,29 +42,28 @@
#ifndef IOSACCELEROMETER_H
#define IOSACCELEROMETER_H
-#include <Foundation/Foundation.h>
+#include <CoreMotion/CMMotionManager.h>
#include <qsensorbackend.h>
#include <qaccelerometer.h>
QT_BEGIN_NAMESPACE
-@class QtIoAccelListener;
-
class IOSAccelerometer : public QSensorBackend
{
public:
static char const * const id;
explicit IOSAccelerometer(QSensor *sensor);
- ~IOSAccelerometer();
+ void timerEvent(QTimerEvent *);
void start();
void stop();
private:
- NSOperationQueue *m_updateQueue;
+ CMMotionManager *m_motionManager;
QAccelerometerReading m_reading;
+ int m_timer;
};
QT_END_NAMESPACE
diff --git a/src/plugins/sensors/ios/iosaccelerometer.mm b/src/plugins/sensors/ios/iosaccelerometer.mm
index b657b507..5f9c0f16 100644
--- a/src/plugins/sensors/ios/iosaccelerometer.mm
+++ b/src/plugins/sensors/ios/iosaccelerometer.mm
@@ -40,8 +40,6 @@
****************************************************************************/
#include <UIKit/UIAccelerometer.h>
-#include <CoreMotion/CMMotionManager.h>
-#include <QPointer>
#include "iosaccelerometer.h"
#include "iosmotionmanager.h"
@@ -52,49 +50,39 @@ QT_BEGIN_NAMESPACE
IOSAccelerometer::IOSAccelerometer(QSensor *sensor)
: QSensorBackend(sensor)
- , m_updateQueue([[NSOperationQueue alloc] init])
+ , m_motionManager([QIOSMotionManager sharedManager])
+ , m_timer(0)
{
setReading<QAccelerometerReading>(&m_reading);
addDataRate(1, 100); // 100Hz
addOutputRange(-22.418, 22.418, 0.17651); // 2G
}
-IOSAccelerometer::~IOSAccelerometer()
-{
- [m_updateQueue release];
-}
-
void IOSAccelerometer::start()
{
- CMMotionManager *motionManager = [QIOSMotionManager sharedManager];
- // Convert from Hz to NSTimeInterval:
int hz = sensor()->dataRate();
- motionManager.accelerometerUpdateInterval = (hz == 0) ? 0 : 1. / hz;
-
- QPointer<QObject> self = this;
- [motionManager startAccelerometerUpdatesToQueue:m_updateQueue withHandler:^(CMAccelerometerData *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 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();
- }
- });
- }];
+ m_timer = startTimer(1000 / (hz == 0 ? 60 : hz));
+ [m_motionManager startAccelerometerUpdates];
}
void IOSAccelerometer::stop()
{
- [[QIOSMotionManager sharedManager] stopAccelerometerUpdates];
+ [m_motionManager stopAccelerometerUpdates];
+ killTimer(m_timer);
+ m_timer = 0;
+}
+
+void IOSAccelerometer::timerEvent(QTimerEvent *)
+{
+ // Convert from NSTimeInterval to microseconds and G to m/s2, and flip axes:
+ CMAccelerometerData *data = m_motionManager.accelerometerData;
+ CMAcceleration acc = data.acceleration;
+ static const qreal G = 9.8066;
+ m_reading.setTimestamp(quint64(data.timestamp * 1e6));
+ 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();
}
QT_END_NAMESPACE
diff --git a/src/plugins/sensors/ios/iosgyroscope.h b/src/plugins/sensors/ios/iosgyroscope.h
index ed46241c..4f4c399a 100644
--- a/src/plugins/sensors/ios/iosgyroscope.h
+++ b/src/plugins/sensors/ios/iosgyroscope.h
@@ -42,7 +42,7 @@
#ifndef IOSGYROSCOPE_H
#define IOSGYROSCOPE_H
-#include <Foundation/Foundation.h>
+#include <CoreMotion/CMMotionManager.h>
#include <qsensorbackend.h>
#include <qgyroscope.h>
@@ -55,14 +55,15 @@ public:
static char const * const id;
explicit IOSGyroscope(QSensor *sensor);
- ~IOSGyroscope();
+ void timerEvent(QTimerEvent *);
void start();
void stop();
private:
- NSOperationQueue *m_updateQueue;
+ CMMotionManager *m_motionManager;
QGyroscopeReading m_reading;
+ int m_timer;
};
QT_END_NAMESPACE
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
diff --git a/src/plugins/sensors/ios/iosmagnetometer.h b/src/plugins/sensors/ios/iosmagnetometer.h
index 72ceab6d..b0dc848a 100644
--- a/src/plugins/sensors/ios/iosmagnetometer.h
+++ b/src/plugins/sensors/ios/iosmagnetometer.h
@@ -42,7 +42,7 @@
#ifndef IOSMAGNETOMETER_H
#define IOSMAGNETOMETER_H
-#include <Foundation/Foundation.h>
+#include <CoreMotion/CMMotionManager.h>
#include <qsensorbackend.h>
#include <qmagnetometer.h>
@@ -55,7 +55,7 @@ public:
static char const * const id;
explicit IOSMagnetometer(QSensor *sensor);
- ~IOSMagnetometer();
+ void timerEvent(QTimerEvent *);
void start();
void stop();
@@ -64,8 +64,10 @@ public:
void startDeviceMotion();
private:
- NSOperationQueue *m_updateQueue;
+ CMMotionManager *m_motionManager;
QMagnetometerReading m_reading;
+ int m_timer;
+ bool m_returnGeoValues;
};
QT_END_NAMESPACE
diff --git a/src/plugins/sensors/ios/iosmagnetometer.mm b/src/plugins/sensors/ios/iosmagnetometer.mm
index f46d7d5b..95f85ae1 100644
--- a/src/plugins/sensors/ios/iosmagnetometer.mm
+++ b/src/plugins/sensors/ios/iosmagnetometer.mm
@@ -39,9 +39,6 @@
**
****************************************************************************/
-#include <CoreMotion/CMMotionManager.h>
-#include <QPointer>
-
#include "iosmotionmanager.h"
#include "iosmagnetometer.h"
@@ -51,7 +48,9 @@ char const * const IOSMagnetometer::id("ios.magnetometer");
IOSMagnetometer::IOSMagnetometer(QSensor *sensor)
: QSensorBackend(sensor)
- , m_updateQueue([[NSOperationQueue alloc] init])
+ , m_motionManager([QIOSMotionManager sharedManager])
+ , m_timer(0)
+ , m_returnGeoValues(false)
{
setReading<QMagnetometerReading>(&m_reading);
// Technical information about data rate is not found, but
@@ -62,94 +61,64 @@ IOSMagnetometer::IOSMagnetometer(QSensor *sensor)
addOutputRange(-0.0002, 0.0002, 1e-08);
}
-IOSMagnetometer::~IOSMagnetometer()
-{
- [m_updateQueue release];
-}
-
void IOSMagnetometer::start()
{
- if (static_cast<QMagnetometer *>(sensor())->returnGeoValues())
- startDeviceMotion();
+ int hz = sensor()->dataRate();
+ m_timer = startTimer(1000 / (hz == 0 ? 60 : hz));
+ m_returnGeoValues = static_cast<QMagnetometer *>(sensor())->returnGeoValues();
+
+ if (m_returnGeoValues)
+ [m_motionManager startDeviceMotionUpdates];
else
- startMagnetometer();
+ [m_motionManager startMagnetometerUpdates];
}
-void IOSMagnetometer::startMagnetometer()
+void IOSMagnetometer::stop()
{
- CMMotionManager *motionManager = [QIOSMotionManager sharedManager];
- // Convert Hz to NSTimeInterval:
- int hz = sensor()->dataRate();
- motionManager.magnetometerUpdateInterval = (hz == 0) ? 0 : 1. / hz;
-
- QPointer<QObject> self = this;
- [motionManager startMagnetometerUpdatesToQueue:m_updateQueue withHandler:^(CMMagnetometerData *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, IOSMagnetometer might
- // have been deleted, so we need an extra QPointer check for that:
- dispatch_async(dispatch_get_main_queue(), ^{
- if (self) {
- Q_UNUSED(error);
- CMMagneticField field = data.magneticField;
- // Convert NSTimeInterval to microseconds and microtesla to tesla:
- m_reading.setTimestamp(quint64(data.timestamp * 1e6));
- m_reading.setX(qreal(field.x) / 1e6);
- m_reading.setY(qreal(field.y) / 1e6);
- m_reading.setZ(qreal(field.z) / 1e6);
- m_reading.setCalibrationLevel(1.0);
- newReadingAvailable();
- }
- });
- }];
+ if (m_returnGeoValues)
+ [m_motionManager stopDeviceMotionUpdates];
+ else
+ [m_motionManager stopMagnetometerUpdates];
+ killTimer(m_timer);
+ m_timer = 0;
}
-void IOSMagnetometer::startDeviceMotion()
+void IOSMagnetometer::timerEvent(QTimerEvent *)
{
- CMMotionManager *motionManager = [QIOSMotionManager sharedManager];
- // Convert Hz to NSTimeInterval:
- int hz = sensor()->dataRate();
- motionManager.deviceMotionUpdateInterval = (hz == 0) ? 0 : 1. / hz;
- QPointer<QObject> self = this;
+ CMMagneticField field;
- [motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryCorrectedZVertical
- toQueue:m_updateQueue withHandler:^(CMDeviceMotion *data, NSError *error) {
- dispatch_async(dispatch_get_main_queue(), ^{
- if (self) {
- Q_UNUSED(error);
- CMCalibratedMagneticField calibratedField = data.magneticField;
- CMMagneticField field = calibratedField.field;
- field = motionManager.deviceMotion.magneticField.field;
- // Convert NSTimeInterval to microseconds and microtesla to tesla:
- m_reading.setTimestamp(quint64(data.timestamp * 1e6));
- m_reading.setX(qreal(field.x) / 1e6);
- m_reading.setY(qreal(field.y) / 1e6);
- m_reading.setZ(qreal(field.z) / 1e6);
+ if (m_returnGeoValues) {
+ CMDeviceMotion *deviceMotion = m_motionManager.deviceMotion;
+ CMCalibratedMagneticField calibratedField = deviceMotion.magneticField;
+ field = calibratedField.field;
+ m_reading.setTimestamp(quint64(deviceMotion.timestamp * 1e6));
- switch (calibratedField.accuracy) {
- case CMMagneticFieldCalibrationAccuracyUncalibrated:
- m_reading.setCalibrationLevel(0.0);
- break;
- case CMMagneticFieldCalibrationAccuracyLow:
- m_reading.setCalibrationLevel(0.3);
- break;
- case CMMagneticFieldCalibrationAccuracyMedium:
- m_reading.setCalibrationLevel(0.6);
- break;
- case CMMagneticFieldCalibrationAccuracyHigh:
- m_reading.setCalibrationLevel(1.0);
- break;
- }
+ switch (calibratedField.accuracy) {
+ case CMMagneticFieldCalibrationAccuracyUncalibrated:
+ m_reading.setCalibrationLevel(0.0);
+ break;
+ case CMMagneticFieldCalibrationAccuracyLow:
+ m_reading.setCalibrationLevel(0.3);
+ break;
+ case CMMagneticFieldCalibrationAccuracyMedium:
+ m_reading.setCalibrationLevel(0.6);
+ break;
+ case CMMagneticFieldCalibrationAccuracyHigh:
+ m_reading.setCalibrationLevel(1.0);
+ break;
+ }
+ } else {
+ CMMagnetometerData *data = m_motionManager.magnetometerData;
+ field = data.magneticField;
+ m_reading.setTimestamp(quint64(data.timestamp * 1e6));
+ m_reading.setCalibrationLevel(1.0);
+ }
- newReadingAvailable();
- }
- });
- }];
-}
-
-void IOSMagnetometer::stop()
-{
- [[QIOSMotionManager sharedManager] stopMagnetometerUpdates];
- [[QIOSMotionManager sharedManager] stopDeviceMotionUpdates];
+ // Convert NSTimeInterval to microseconds and microtesla to tesla:
+ m_reading.setX(qreal(field.x) / 1e6);
+ m_reading.setY(qreal(field.y) / 1e6);
+ m_reading.setZ(qreal(field.z) / 1e6);
+ newReadingAvailable();
}
QT_END_NAMESPACE