From a16f81a365b7d9a6629b04bd298a3b45fc1b7628 Mon Sep 17 00:00:00 2001 From: Richard Moe Gustavsen Date: Wed, 20 Mar 2013 12:50:04 +0100 Subject: iOS: implement magnetometer Change-Id: I611e1ccafb0362b78737785e9cc7f4f48278063a Reviewed-by: Lorn Potter --- src/plugins/sensors/ios/ios.pro | 6 +- src/plugins/sensors/ios/iosmagnetometer.h | 73 +++++++++++++ src/plugins/sensors/ios/iosmagnetometer.mm | 155 ++++++++++++++++++++++++++++ src/plugins/sensors/ios/iosmotionmanager.mm | 1 + src/plugins/sensors/ios/main.mm | 5 + src/sensors/doc/src/compatmap.qdoc | 2 +- 6 files changed, 239 insertions(+), 3 deletions(-) create mode 100644 src/plugins/sensors/ios/iosmagnetometer.h create mode 100644 src/plugins/sensors/ios/iosmagnetometer.mm diff --git a/src/plugins/sensors/ios/ios.pro b/src/plugins/sensors/ios/ios.pro index 303c9741..0776d8cb 100644 --- a/src/plugins/sensors/ios/ios.pro +++ b/src/plugins/sensors/ios/ios.pro @@ -9,11 +9,13 @@ OTHER_FILES = plugin.json HEADERS += iosaccelerometer.h \ iosmotionmanager.h \ - iosgyroscope.h + iosgyroscope.h \ + iosmagnetometer.h OBJECTIVE_SOURCES += main.mm \ iosaccelerometer.mm \ iosmotionmanager.mm \ - iosgyroscope.mm + iosgyroscope.mm \ + iosmagnetometer.mm LIBS += -framework UIKit -framework CoreMotion diff --git a/src/plugins/sensors/ios/iosmagnetometer.h b/src/plugins/sensors/ios/iosmagnetometer.h new file mode 100644 index 00000000..72ceab6d --- /dev/null +++ b/src/plugins/sensors/ios/iosmagnetometer.h @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2013 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 IOSMAGNETOMETER_H +#define IOSMAGNETOMETER_H + +#include + +#include +#include + +QT_BEGIN_NAMESPACE + +class IOSMagnetometer : public QSensorBackend +{ +public: + static char const * const id; + + explicit IOSMagnetometer(QSensor *sensor); + ~IOSMagnetometer(); + + void start(); + void stop(); + + void startMagnetometer(); + void startDeviceMotion(); + +private: + NSOperationQueue *m_updateQueue; + QMagnetometerReading m_reading; +}; +QT_END_NAMESPACE + +#endif // IOSMAGNETOMETER_H + diff --git a/src/plugins/sensors/ios/iosmagnetometer.mm b/src/plugins/sensors/ios/iosmagnetometer.mm new file mode 100644 index 00000000..f46d7d5b --- /dev/null +++ b/src/plugins/sensors/ios/iosmagnetometer.mm @@ -0,0 +1,155 @@ +/**************************************************************************** +** +** Copyright (C) 2013 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 +#include + +#include "iosmotionmanager.h" +#include "iosmagnetometer.h" + +QT_BEGIN_NAMESPACE + +char const * const IOSMagnetometer::id("ios.magnetometer"); + +IOSMagnetometer::IOSMagnetometer(QSensor *sensor) + : QSensorBackend(sensor) + , m_updateQueue([[NSOperationQueue alloc] init]) +{ + setReading(&m_reading); + // Technical information about data rate is not found, but + // seems to be ~70Hz after testing on iPad4: + addDataRate(1, 70); + // Output range is +/- 2 gauss (0.0002 tesla) and can sense magnetic fields less than + // 100 microgauss (1e-08 tesla) Ref: "iOS Sensor Programming", Alasdair, 2012. + addOutputRange(-0.0002, 0.0002, 1e-08); +} + +IOSMagnetometer::~IOSMagnetometer() +{ + [m_updateQueue release]; +} + +void IOSMagnetometer::start() +{ + if (static_cast(sensor())->returnGeoValues()) + startDeviceMotion(); + else + startMagnetometer(); +} + +void IOSMagnetometer::startMagnetometer() +{ + CMMotionManager *motionManager = [QIOSMotionManager sharedManager]; + // Convert Hz to NSTimeInterval: + int hz = sensor()->dataRate(); + motionManager.magnetometerUpdateInterval = (hz == 0) ? 0 : 1. / hz; + + QPointer 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(); + } + }); + }]; +} + +void IOSMagnetometer::startDeviceMotion() +{ + CMMotionManager *motionManager = [QIOSMotionManager sharedManager]; + // Convert Hz to NSTimeInterval: + int hz = sensor()->dataRate(); + motionManager.deviceMotionUpdateInterval = (hz == 0) ? 0 : 1. / hz; + QPointer self = this; + + [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); + + 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; + } + + newReadingAvailable(); + } + }); + }]; +} + +void IOSMagnetometer::stop() +{ + [[QIOSMotionManager sharedManager] stopMagnetometerUpdates]; + [[QIOSMotionManager sharedManager] stopDeviceMotionUpdates]; +} + +QT_END_NAMESPACE diff --git a/src/plugins/sensors/ios/iosmotionmanager.mm b/src/plugins/sensors/ios/iosmotionmanager.mm index a367c49b..90362ab3 100644 --- a/src/plugins/sensors/ios/iosmotionmanager.mm +++ b/src/plugins/sensors/ios/iosmotionmanager.mm @@ -50,6 +50,7 @@ static CMMotionManager *sharedManager = nil; static dispatch_once_t staticToken; dispatch_once(&staticToken, ^{ sharedManager = [[CMMotionManager alloc] init]; + sharedManager.showsDeviceMovementDisplay = YES; }); return sharedManager; } diff --git a/src/plugins/sensors/ios/main.mm b/src/plugins/sensors/ios/main.mm index efa5370a..25b41b60 100644 --- a/src/plugins/sensors/ios/main.mm +++ b/src/plugins/sensors/ios/main.mm @@ -46,6 +46,7 @@ #include "iosmotionmanager.h" #include "iosaccelerometer.h" #include "iosgyroscope.h" +#include "iosmagnetometer.h" class IOSSensorPlugin : public QObject, public QSensorPluginInterface, public QSensorBackendFactory { @@ -58,6 +59,8 @@ public: QSensorManager::registerBackend(QAccelerometer::type, IOSAccelerometer::id, this); if ([QIOSMotionManager sharedManager].gyroAvailable) QSensorManager::registerBackend(QGyroscope::type, IOSGyroscope::id, this); + if ([QIOSMotionManager sharedManager].magnetometerAvailable) + QSensorManager::registerBackend(QMagnetometer::type, IOSMagnetometer::id, this); } QSensorBackend *createBackend(QSensor *sensor) @@ -66,6 +69,8 @@ public: return new IOSAccelerometer(sensor); if (sensor->identifier() == IOSGyroscope::id) return new IOSGyroscope(sensor); + if (sensor->identifier() == IOSMagnetometer::id) + return new IOSMagnetometer(sensor); return 0; } diff --git a/src/sensors/doc/src/compatmap.qdoc b/src/sensors/doc/src/compatmap.qdoc index 9bb21fea..7fbe3bb5 100644 --- a/src/sensors/doc/src/compatmap.qdoc +++ b/src/sensors/doc/src/compatmap.qdoc @@ -146,7 +146,7 @@ - + -- cgit v1.2.3