diff options
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/sensors/blackberry/bbpressuresensor.cpp | 2 | ||||
-rw-r--r-- | src/plugins/sensors/ios/ios.pro | 6 | ||||
-rw-r--r-- | src/plugins/sensors/ios/iosaccelerometer.h | 5 | ||||
-rw-r--r-- | src/plugins/sensors/ios/iosaccelerometer.mm | 105 | ||||
-rw-r--r-- | src/plugins/sensors/ios/iosgyroscope.h | 70 | ||||
-rw-r--r-- | src/plugins/sensors/ios/iosgyroscope.mm | 98 | ||||
-rw-r--r-- | src/plugins/sensors/ios/main.mm | 9 |
7 files changed, 213 insertions, 82 deletions
diff --git a/src/plugins/sensors/blackberry/bbpressuresensor.cpp b/src/plugins/sensors/blackberry/bbpressuresensor.cpp index 8cb9b1ea..7098dec9 100644 --- a/src/plugins/sensors/blackberry/bbpressuresensor.cpp +++ b/src/plugins/sensors/blackberry/bbpressuresensor.cpp @@ -53,8 +53,6 @@ QString BbPressureSensor::devicePath() bool BbPressureSensor::updateReadingFromEvent(const sensor_event_t &event, QPressureReading *reading) { - // TODO: I was unable to test this since the device I was testing this with did not have - // a pressure sensor. Verify that this works and check that the units are correct. reading->setPressure(event.pressure_s.pressure); return true; } diff --git a/src/plugins/sensors/ios/ios.pro b/src/plugins/sensors/ios/ios.pro index 4242cfca..303c9741 100644 --- a/src/plugins/sensors/ios/ios.pro +++ b/src/plugins/sensors/ios/ios.pro @@ -8,10 +8,12 @@ load(qt_plugin) OTHER_FILES = plugin.json HEADERS += iosaccelerometer.h \ - iosmotionmanager.h + iosmotionmanager.h \ + iosgyroscope.h OBJECTIVE_SOURCES += main.mm \ iosaccelerometer.mm \ - iosmotionmanager.mm + iosmotionmanager.mm \ + iosgyroscope.mm LIBS += -framework UIKit -framework CoreMotion diff --git a/src/plugins/sensors/ios/iosaccelerometer.h b/src/plugins/sensors/ios/iosaccelerometer.h index 34cc3ebd..5fcac19f 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 ef7d2a6d..b657b507 100644 --- a/src/plugins/sensors/ios/iosaccelerometer.mm +++ b/src/plugins/sensors/ios/iosaccelerometer.mm @@ -39,76 +39,20 @@ ** ****************************************************************************/ -#include "iosaccelerometer.h" -#include "iosmotionmanager.h" - #include <UIKit/UIAccelerometer.h> #include <CoreMotion/CMMotionManager.h> +#include <QPointer> -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]; - - if (motionManager.deviceMotionAvailable) { - [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; - m_qiosAccelerometer->readingsChanged(quint64(data.timestamp), x, y, z); - }); - }]; - } -} - --(void)stopAccelerometer -{ - [[QIOSMotionManager sharedManager] stopAccelerometerUpdates]; -} - --(void)setInterval:(NSTimeInterval) interval -{ - [QIOSMotionManager sharedManager].accelerometerUpdateInterval = interval; -} +#include "iosaccelerometer.h" +#include "iosmotionmanager.h" -@end +char const * const IOSAccelerometer::id("ios.accelerometer"); 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 @@ -117,27 +61,40 @@ IOSAccelerometer::IOSAccelerometer(QSensor *sensor) IOSAccelerometer::~IOSAccelerometer() { - [m_listener dealloc]; + [m_updateQueue release]; } void IOSAccelerometer::start() { - [m_listener startAccelerometer]; + 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(); + } + }); + }]; } void IOSAccelerometer::stop() { - [m_listener stopAccelerometer]; -} - -void IOSAccelerometer::readingsChanged(quint64 ts, qreal x, qreal y, qreal z) -{ - m_reading.setTimestamp(ts); - m_reading.setX(x); - m_reading.setY(y); - m_reading.setZ(z); - - newReadingAvailable(); + [[QIOSMotionManager sharedManager] stopAccelerometerUpdates]; } QT_END_NAMESPACE diff --git a/src/plugins/sensors/ios/iosgyroscope.h b/src/plugins/sensors/ios/iosgyroscope.h new file mode 100644 index 00000000..ed46241c --- /dev/null +++ b/src/plugins/sensors/ios/iosgyroscope.h @@ -0,0 +1,70 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef IOSGYROSCOPE_H +#define IOSGYROSCOPE_H + +#include <Foundation/Foundation.h> + +#include <qsensorbackend.h> +#include <qgyroscope.h> + +QT_BEGIN_NAMESPACE + +class IOSGyroscope : public QSensorBackend +{ +public: + static char const * const id; + + explicit IOSGyroscope(QSensor *sensor); + ~IOSGyroscope(); + + void start(); + void stop(); + +private: + NSOperationQueue *m_updateQueue; + QGyroscopeReading m_reading; +}; +QT_END_NAMESPACE + +#endif // IOSGYROSCOPE_H + diff --git a/src/plugins/sensors/ios/iosgyroscope.mm b/src/plugins/sensors/ios/iosgyroscope.mm new file mode 100644 index 00000000..700755de --- /dev/null +++ b/src/plugins/sensors/ios/iosgyroscope.mm @@ -0,0 +1,98 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <CoreMotion/CMMotionManager.h> +#include <QPointer> + +#include "iosmotionmanager.h" +#include "iosgyroscope.h" + +char const * const IOSGyroscope::id("ios.gyroscope"); + +QT_BEGIN_NAMESPACE + +IOSGyroscope::IOSGyroscope(QSensor *sensor) + : QSensorBackend(sensor) + , m_updateQueue([[NSOperationQueue alloc] init]) +{ + 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(); + } + }); + }]; +} + +void IOSGyroscope::stop() +{ + [[QIOSMotionManager sharedManager] stopGyroUpdates]; +} + +QT_END_NAMESPACE diff --git a/src/plugins/sensors/ios/main.mm b/src/plugins/sensors/ios/main.mm index 3c739393..efa5370a 100644 --- a/src/plugins/sensors/ios/main.mm +++ b/src/plugins/sensors/ios/main.mm @@ -43,7 +43,9 @@ #include <qsensorbackend.h> #include <qsensormanager.h> +#include "iosmotionmanager.h" #include "iosaccelerometer.h" +#include "iosgyroscope.h" class IOSSensorPlugin : public QObject, public QSensorPluginInterface, public QSensorBackendFactory { @@ -53,14 +55,17 @@ class IOSSensorPlugin : public QObject, public QSensorPluginInterface, public QS public: void registerSensors() { - if (!QSensorManager::isBackendRegistered(QAccelerometer::type, IOSAccelerometer::id)) - QSensorManager::registerBackend(QAccelerometer::type, IOSAccelerometer::id, this); + QSensorManager::registerBackend(QAccelerometer::type, IOSAccelerometer::id, this); + if ([QIOSMotionManager sharedManager].gyroAvailable) + QSensorManager::registerBackend(QGyroscope::type, IOSGyroscope::id, this); } QSensorBackend *createBackend(QSensor *sensor) { if (sensor->identifier() == IOSAccelerometer::id) return new IOSAccelerometer(sensor); + if (sensor->identifier() == IOSGyroscope::id) + return new IOSGyroscope(sensor); return 0; } |