From ea7af7f00886f77941b81262e597c3b7f3477f48 Mon Sep 17 00:00:00 2001 From: Paul Olav Tvete Date: Fri, 8 Aug 2014 18:31:20 +0200 Subject: Compass sensor implementation for Android Change-Id: Ic13d2d1e400a92b2f0c3c83e279c58b799f9d06b Reviewed-by: Lorn Potter --- .../qtproject/qt5/android/sensors/QtSensors.java | 19 +++ src/plugins/sensors/android/src/androidcompass.cpp | 173 +++++++++++++++++++++ src/plugins/sensors/android/src/androidcompass.h | 76 +++++++++ .../sensors/android/src/androidjnisensors.cpp | 16 ++ .../sensors/android/src/androidjnisensors.h | 1 + src/plugins/sensors/android/src/main.cpp | 11 ++ src/plugins/sensors/android/src/src.pro | 2 + 7 files changed, 298 insertions(+) create mode 100644 src/plugins/sensors/android/src/androidcompass.cpp create mode 100644 src/plugins/sensors/android/src/androidcompass.h diff --git a/src/plugins/sensors/android/jar/src/org/qtproject/qt5/android/sensors/QtSensors.java b/src/plugins/sensors/android/jar/src/org/qtproject/qt5/android/sensors/QtSensors.java index efd1ff6..e7e309e 100644 --- a/src/plugins/sensors/android/jar/src/org/qtproject/qt5/android/sensors/QtSensors.java +++ b/src/plugins/sensors/android/jar/src/org/qtproject/qt5/android/sensors/QtSensors.java @@ -153,6 +153,25 @@ public class QtSensors implements SensorEventListener return angles; } + private static float[] mRotation = new float[9]; + private static float[] mOrientation = new float[3]; + private static float[] mAcc = new float[3]; + private static float[] mMag = new float[3]; + + private static float getCompassAzimuth(float a0, float a1, float a2, float m0, float m1, float m2) + { + mAcc[0] = a0; + mAcc[1] = a1; + mAcc[2] = a2; + mMag[0] = m0; + mMag[1] = m1; + mMag[2] = m2; + + SensorManager.getRotationMatrix(mRotation, null, mAcc, mMag); + SensorManager.getOrientation(mRotation, mOrientation); + return mOrientation[0]; + } + public static native void accuracyChanged(int sensorType, int accuracy); public static native void sensorChanged(int sensorType, long timestamp, float[] values); diff --git a/src/plugins/sensors/android/src/androidcompass.cpp b/src/plugins/sensors/android/src/androidcompass.cpp new file mode 100644 index 0000000..f2b4bd2 --- /dev/null +++ b/src/plugins/sensors/android/src/androidcompass.cpp @@ -0,0 +1,173 @@ +/**************************************************************************** +** +** Copyright (C) 2014 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 "androidcompass.h" + +#include +#include +#include "androidjnisensors.h" + + +class AndroidAccelerometerListener : public AndroidSensors::AndroidSensorsListenerInterface +{ +public: + + AndroidAccelerometerListener(AndroidCompass *parent) + : m_compass(parent) + { + } + + void start(int dataRate) + { + AndroidSensors::registerListener(AndroidSensors::TYPE_ACCELEROMETER, this, dataRate); + } + + void stop() + { + AndroidSensors::unregisterListener(AndroidSensors::TYPE_ACCELEROMETER, this); + } + + void onAccuracyChanged(jint accuracy) Q_DECL_OVERRIDE + { + Q_UNUSED(accuracy); + } + + void onSensorChanged(jlong /*timestamp*/, const jfloat *values, uint size) Q_DECL_OVERRIDE + { + if (size < 3) + return; + reading[0] = values[0]; + reading[1] = values[1]; + reading[2] = values[2]; + m_compass->testStuff(); + } + + jfloat reading[3]; + +private: + AndroidCompass *m_compass; +}; + +class AndroidMagnetometerListener : public AndroidSensors::AndroidSensorsListenerInterface +{ +public: + AndroidMagnetometerListener(AndroidCompass *parent) + :m_compass(parent) + { + + } + + void start(int dataRate) + { + AndroidSensors::registerListener(AndroidSensors::TYPE_MAGNETIC_FIELD, this, dataRate); + } + + void stop() + { + AndroidSensors::unregisterListener(AndroidSensors::TYPE_MAGNETIC_FIELD, this); + } + + void onAccuracyChanged(jint accuracy) Q_DECL_OVERRIDE + { + Q_UNUSED(accuracy); + } + + void onSensorChanged(jlong /*timestamp*/, const jfloat *values, uint size) Q_DECL_OVERRIDE + { + if (size < 3) + return; + reading[0] = values[0]; + reading[1] = values[1]; + reading[2] = values[2]; + m_compass->testStuff(); + } + + jfloat reading[3]; +private: + AndroidCompass *m_compass; +}; + +char const * const AndroidCompass::id("android.synthetic.compass"); + +AndroidCompass::AndroidCompass(QSensor *sensor) + : QSensorBackend(sensor), m_accelerometerListener(0), m_magnetometerListener(0), m_isStarted(false) +{ + setReading(&m_reading); + m_isStarted = false; +} + +AndroidCompass::~AndroidCompass() +{ + if (m_isStarted) + stop(); + delete m_accelerometerListener; + delete m_magnetometerListener; +} + +void AndroidCompass::start() +{ + if (!m_accelerometerListener) + m_accelerometerListener = new AndroidAccelerometerListener(this); + m_accelerometerListener->start(sensor()->dataRate()); + if (!m_magnetometerListener) + m_magnetometerListener = new AndroidMagnetometerListener(this); + m_magnetometerListener->start(sensor()->dataRate()); + + m_isStarted = true; +} + +void AndroidCompass::stop() +{ + if (m_isStarted) { + m_isStarted = false; + m_accelerometerListener->stop(); + m_magnetometerListener->stop(); + } +} + +void AndroidCompass::testStuff() +{ + qreal azimuth = AndroidSensors::getCompassAzimuth(m_accelerometerListener->reading, m_magnetometerListener->reading); + + azimuth = azimuth * 180.0 / M_PI; + m_reading.setAzimuth(azimuth); + newReadingAvailable(); +} diff --git a/src/plugins/sensors/android/src/androidcompass.h b/src/plugins/sensors/android/src/androidcompass.h new file mode 100644 index 0000000..897dd46 --- /dev/null +++ b/src/plugins/sensors/android/src/androidcompass.h @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2014 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 ANDROIDCOMPASS_H +#define ANDROIDCOMPASS_H +#include + +#include "androidcommonsensor.h" + +class AndroidAccelerometerListener; +class AndroidMagnetometerListener; + +class AndroidCompass : public QSensorBackend +{ + Q_OBJECT + +public: + static char const * const id; + + AndroidCompass(QSensor *sensor); + ~AndroidCompass(); + + void start() Q_DECL_OVERRIDE; + void stop() Q_DECL_OVERRIDE; + +private: + AndroidAccelerometerListener *m_accelerometerListener; + AndroidMagnetometerListener *m_magnetometerListener; + + QCompassReading m_reading; + bool m_isStarted; + +public Q_SLOTS: + void testStuff(); + +}; + +#endif // ANDROIDCOMPASS_H diff --git a/src/plugins/sensors/android/src/androidjnisensors.cpp b/src/plugins/sensors/android/src/androidjnisensors.cpp index 87889a8..9a7b671 100644 --- a/src/plugins/sensors/android/src/androidjnisensors.cpp +++ b/src/plugins/sensors/android/src/androidjnisensors.cpp @@ -54,6 +54,7 @@ static jmethodID registerSensorMethodId; static jmethodID unregisterSensorMethodId; static jmethodID getSensorDescriptionMethodId; static jmethodID getSensorMaximumRangeMethodId; +static jmethodID getCompassAzimuthId; static QHash > listenersHash; QReadWriteLock listenersLocker; @@ -166,6 +167,20 @@ namespace AndroidSensors } return true; } + + qreal getCompassAzimuth(jfloat *accelerometerReading, jfloat *magnetometerReading) + { + AttachedJNIEnv aenv; + if (!aenv.jniEnv) + return 0.0; + return aenv.jniEnv->CallStaticFloatMethod(sensorsClass, getCompassAzimuthId, + accelerometerReading[0], + accelerometerReading[1], + accelerometerReading[2], + magnetometerReading[0], + magnetometerReading[1], + magnetometerReading[2]); + } } static const char logTag[] = "Qt"; @@ -227,6 +242,7 @@ static bool registerNatives(JNIEnv *env) GET_AND_CHECK_STATIC_METHOD(unregisterSensorMethodId, sensorsClass, "unregisterSensor", "(I)Z"); GET_AND_CHECK_STATIC_METHOD(getSensorDescriptionMethodId, sensorsClass, "getSensorDescription", "(I)Ljava/lang/String;"); GET_AND_CHECK_STATIC_METHOD(getSensorMaximumRangeMethodId, sensorsClass, "getSensorMaximumRange", "(I)F"); + GET_AND_CHECK_STATIC_METHOD(getCompassAzimuthId, sensorsClass, "getCompassAzimuth", "(FFFFFF)F"); return true; } diff --git a/src/plugins/sensors/android/src/androidjnisensors.h b/src/plugins/sensors/android/src/androidjnisensors.h index 30aab6c..9d2ccf5 100644 --- a/src/plugins/sensors/android/src/androidjnisensors.h +++ b/src/plugins/sensors/android/src/androidjnisensors.h @@ -83,6 +83,7 @@ namespace AndroidSensors qreal sensorMaximumRange(AndroidSensorType sensor); bool registerListener(AndroidSensorType sensor, AndroidSensorsListenerInterface *listener, int dataRate = 0); bool unregisterListener(AndroidSensorType sensor, AndroidSensorsListenerInterface *listener); + qreal getCompassAzimuth(jfloat *accelerometerReading, jfloat *magnetometerReading); } #endif // ANDROIDJNISENSORS_H diff --git a/src/plugins/sensors/android/src/main.cpp b/src/plugins/sensors/android/src/main.cpp index 3d8604f..b8d1496 100644 --- a/src/plugins/sensors/android/src/main.cpp +++ b/src/plugins/sensors/android/src/main.cpp @@ -44,7 +44,9 @@ #include #include #include +#include #include "androidaccelerometer.h" +#include "androidcompass.h" #include "androidgyroscope.h" #include "androidlight.h" #include "androidmagnetometer.h" @@ -63,10 +65,13 @@ class AndroidSensorPlugin : public QObject, public QSensorPluginInterface, publi public: void registerSensors() { + bool accelerometer = false; + bool magnetometer = false; foreach (AndroidSensorType sensor, availableSensors()) { switch (sensor) { case TYPE_ACCELEROMETER: QSensorManager::registerBackend(QAccelerometer::type, QByteArray::number(sensor), this); + accelerometer = true; break; case TYPE_AMBIENT_TEMPERATURE: case TYPE_TEMPERATURE: @@ -84,6 +89,7 @@ public: break; // add the linear acceleration sensor backend case TYPE_MAGNETIC_FIELD: QSensorManager::registerBackend(QMagnetometer::type, QByteArray::number(sensor), this); + magnetometer = true; break; case TYPE_ORIENTATION: break; // add the orientation sensor backend @@ -106,10 +112,15 @@ public: break; // add backends for API level 18 sensors } } + if (accelerometer && magnetometer) + QSensorManager::registerBackend(QCompass::type, AndroidCompass::id, this); } QSensorBackend *createBackend(QSensor *sensor) { + if (sensor->identifier() == AndroidCompass::id) + return new AndroidCompass(sensor); + AndroidSensorType type = static_cast(sensor->identifier().toInt()); switch (type) { case TYPE_ACCELEROMETER: { diff --git a/src/plugins/sensors/android/src/src.pro b/src/plugins/sensors/android/src/src.pro index 21423ef..23a6bea 100644 --- a/src/plugins/sensors/android/src/src.pro +++ b/src/plugins/sensors/android/src/src.pro @@ -12,6 +12,7 @@ DEFINES += QT_STATICPLUGIN HEADERS = \ androidjnisensors.h \ androidaccelerometer.h \ + androidcompass.h \ androidcommonsensor.h \ androidgyroscope.h \ androidmagnetometer.h \ @@ -25,6 +26,7 @@ SOURCES = \ main.cpp \ androidjnisensors.cpp \ androidaccelerometer.cpp \ + androidcompass.cpp \ androidgyroscope.cpp \ androidmagnetometer.cpp \ androidpressure.cpp \ -- cgit v1.2.1