diff options
Diffstat (limited to 'src/plugins/sensors/blackberry')
19 files changed, 373 insertions, 192 deletions
diff --git a/src/plugins/sensors/blackberry/bbaccelerometer.cpp b/src/plugins/sensors/blackberry/bbaccelerometer.cpp index dcbd7fcc..0826cf7a 100644 --- a/src/plugins/sensors/blackberry/bbaccelerometer.cpp +++ b/src/plugins/sensors/blackberry/bbaccelerometer.cpp @@ -44,6 +44,12 @@ BbAccelerometer::BbAccelerometer(QSensor *sensor) : BbSensorBackend<QAccelerometerReading>(devicePath(), SENSOR_TYPE_ACCELEROMETER, sensor) { setDescription(QLatin1String("X, Y, and Z axes accelerations in m/s^2")); + + QAccelerometer * const accelerometer = qobject_cast<QAccelerometer *>(sensor); + if (accelerometer) { + connect(accelerometer, SIGNAL(accelerationModeChanged(AccelerationMode)), + this, SLOT(applyAccelerationMode())); + } } bool BbAccelerometer::updateReadingFromEvent(const sensor_event_t &event, QAccelerometerReading *reading) @@ -58,7 +64,39 @@ bool BbAccelerometer::updateReadingFromEvent(const sensor_event_t &event, QAccel return true; } +void BbAccelerometer::start() +{ + applyAccelerationMode(); + BbSensorBackend<QAccelerometerReading>::start(); +} + QString BbAccelerometer::devicePath() { return QLatin1String("/dev/sensor/accel"); } + +void BbAccelerometer::applyAccelerationMode() +{ + const QAccelerometer * const accelerometer = qobject_cast<QAccelerometer *>(sensor()); + if (accelerometer) { + QString fileName; + sensor_type_e sensorType; + switch (accelerometer->accelerationMode()) { + case QAccelerometer::Gravity: + fileName = QLatin1String("/dev/sensor/gravity"); + sensorType = SENSOR_TYPE_GRAVITY; + break; + case QAccelerometer::User: + fileName = QLatin1String("/dev/sensor/linAccel"); + sensorType = SENSOR_TYPE_LINEAR_ACCEL; + break; + default: + case QAccelerometer::Combined: + fileName = devicePath(); + sensorType = SENSOR_TYPE_ACCELEROMETER; + break; + } + + setDevice(fileName, sensorType); + } +} diff --git a/src/plugins/sensors/blackberry/bbaccelerometer.h b/src/plugins/sensors/blackberry/bbaccelerometer.h index aa47873c..3071c801 100644 --- a/src/plugins/sensors/blackberry/bbaccelerometer.h +++ b/src/plugins/sensors/blackberry/bbaccelerometer.h @@ -51,10 +51,14 @@ class BbAccelerometer : public BbSensorBackend<QAccelerometerReading> public: explicit BbAccelerometer(QSensor *sensor); + void start() Q_DECL_OVERRIDE; static QString devicePath(); protected: bool updateReadingFromEvent(const sensor_event_t &event, QAccelerometerReading *reading) Q_DECL_OVERRIDE; + +private Q_SLOTS: + void applyAccelerationMode(); }; #endif diff --git a/src/plugins/sensors/blackberry/bbaltimeter.cpp b/src/plugins/sensors/blackberry/bbaltimeter.cpp index 07aa2646..3d21ef32 100644 --- a/src/plugins/sensors/blackberry/bbaltimeter.cpp +++ b/src/plugins/sensors/blackberry/bbaltimeter.cpp @@ -40,36 +40,13 @@ ****************************************************************************/ #include "bbaltimeter.h" -class BbAltimeterReadingPrivate -{ -public: - BbAltimeterReadingPrivate() - : altitude(0) - { - } - - qreal altitude; -}; - -IMPLEMENT_READING(BbAltimeterReading) - -qreal BbAltimeterReading::altitude() const -{ - return d->altitude; -} - -void BbAltimeterReading::setAltitude(qreal altitude) -{ - d->altitude = altitude; -} - BbAltimeter::BbAltimeter(QSensor *sensor) - : BbSensorBackend<BbAltimeterReading>(devicePath(), SENSOR_TYPE_ALTIMETER, sensor) + : BbSensorBackend<QAltimeterReading>(devicePath(), SENSOR_TYPE_ALTIMETER, sensor) { setDescription(QLatin1String("Altitude in meters relative to mean sea level")); } -bool BbAltimeter::updateReadingFromEvent(const sensor_event_t &event, BbAltimeterReading *reading) +bool BbAltimeter::updateReadingFromEvent(const sensor_event_t &event, QAltimeterReading *reading) { reading->setAltitude(event.altitude_s.altitude); return true; diff --git a/src/plugins/sensors/blackberry/bbaltimeter.h b/src/plugins/sensors/blackberry/bbaltimeter.h index 4744a3ff..b369c6cb 100644 --- a/src/plugins/sensors/blackberry/bbaltimeter.h +++ b/src/plugins/sensors/blackberry/bbaltimeter.h @@ -42,20 +42,9 @@ #define BBALTIMETER_H #include "bbsensorbackend.h" +#include <qaltimeter.h> -class BbAltimeterReadingPrivate; - -class BbAltimeterReading : public QSensorReading -{ - Q_OBJECT - Q_PROPERTY(qreal altitude READ altitude) - DECLARE_READING(BbAltimeterReading) -public: - qreal altitude() const; - void setAltitude(qreal altitude); -}; - -class BbAltimeter : public BbSensorBackend<BbAltimeterReading> +class BbAltimeter : public BbSensorBackend<QAltimeterReading> { Q_OBJECT @@ -65,7 +54,7 @@ public: static QString devicePath(); protected: - bool updateReadingFromEvent(const sensor_event_t &event, BbAltimeterReading *reading) Q_DECL_OVERRIDE; + bool updateReadingFromEvent(const sensor_event_t &event, QAltimeterReading *reading) Q_DECL_OVERRIDE; }; #endif diff --git a/src/plugins/sensors/blackberry/bbcompass.cpp b/src/plugins/sensors/blackberry/bbcompass.cpp index 6e33d1a3..df8cc2e1 100644 --- a/src/plugins/sensors/blackberry/bbcompass.cpp +++ b/src/plugins/sensors/blackberry/bbcompass.cpp @@ -55,19 +55,28 @@ BbCompass::BbCompass(QSensor *sensor) bool BbCompass::updateReadingFromEvent(const sensor_event_t &event, QCompassReading *reading) { + float azimuth; #ifdef HAVE_COMPASS_SENSOR - reading->setAzimuth(event.compass_s.azimuth); + azimuth = event.compass_s.azimuth; #else float xRad, yRad, zRad; matrixToEulerZXY(event.rotation_matrix, xRad, yRad, zRad); - float azimuth = radiansToDegrees(zRad); + azimuth = radiansToDegrees(zRad); if (azimuth < 0) azimuth = -azimuth; else azimuth = 360.0f - azimuth; - reading->setAzimuth(azimuth); #endif + if (isAutoAxisRemappingEnabled()) { + azimuth += orientationForRemapping(); + if (azimuth >= 360.0f) + azimuth -= 360.0f; + } + + reading->setAzimuth(azimuth); + + switch (event.accuracy) { case SENSOR_ACCURACY_UNRELIABLE: reading->setCalibrationLevel(0.0f); diff --git a/src/plugins/sensors/blackberry/bbholstersensor.cpp b/src/plugins/sensors/blackberry/bbholstersensor.cpp new file mode 100644 index 00000000..53888f58 --- /dev/null +++ b/src/plugins/sensors/blackberry/bbholstersensor.cpp @@ -0,0 +1,58 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Research In Motion +** 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 "bbholstersensor.h" + +BbHolsterSensor::BbHolsterSensor(QSensor *sensor) + : BbSensorBackend<QHolsterReading>(devicePath(), SENSOR_TYPE_HOLSTER, sensor) +{ + setDescription(QLatin1String("Whether the device is holstered or not")); +} + +bool BbHolsterSensor::updateReadingFromEvent(const sensor_event_t &event, QHolsterReading *reading) +{ + reading->setHolstered(event.holster_s.holstered == 1); + return true; +} + +QString BbHolsterSensor::devicePath() +{ + return QLatin1String("/dev/sensor/holster"); +} diff --git a/src/plugins/sensors/blackberry/bbholstersensor.h b/src/plugins/sensors/blackberry/bbholstersensor.h new file mode 100644 index 00000000..b33b391d --- /dev/null +++ b/src/plugins/sensors/blackberry/bbholstersensor.h @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Research In Motion +** 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 BBHOLSTERSENSOR_H +#define BBHOLSTERSENSOR_H + +#include "bbsensorbackend.h" +#include <qholstersensor.h> + +class BbHolsterSensor : public BbSensorBackend<QHolsterReading> +{ + Q_OBJECT + +public: + explicit BbHolsterSensor(QSensor *sensor); + + static QString devicePath(); + +protected: + bool updateReadingFromEvent(const sensor_event_t &event, QHolsterReading *reading) Q_DECL_OVERRIDE; +}; + +#endif diff --git a/src/plugins/sensors/blackberry/bbmagnetometer.cpp b/src/plugins/sensors/blackberry/bbmagnetometer.cpp index 489a0481..45fd49b3 100644 --- a/src/plugins/sensors/blackberry/bbmagnetometer.cpp +++ b/src/plugins/sensors/blackberry/bbmagnetometer.cpp @@ -53,17 +53,11 @@ QString BbMagnetometer::devicePath() bool BbMagnetometer::updateReadingFromEvent(const sensor_event_t &event, QMagnetometerReading *reading) { - // TODO: In the future, support returnGeoValues here. Right now, /dev/sensors/mag has no - // geomagnatic mode, but will gain it in the future. - float x = convertValue(event.motion.dsp.x); - float y = convertValue(event.motion.dsp.y); - float z = convertValue(event.motion.dsp.z); - remapAxes(&x, &y, &z); - reading->setX(x); - reading->setY(y); - reading->setZ(z); + float x, y, z; - const bool returnGeoValues = sensor()->property("returnGeoValues").toBool(); + QMagnetometer * const magnetometer = qobject_cast<QMagnetometer *>(sensor()); + Q_ASSERT(magnetometer); + const bool returnGeoValues = magnetometer->returnGeoValues(); if (returnGeoValues) { switch (event.accuracy) { case SENSOR_ACCURACY_UNRELIABLE: reading->setCalibrationLevel(0.0f); break; @@ -76,10 +70,30 @@ bool BbMagnetometer::updateReadingFromEvent(const sensor_event_t &event, QMagnet case SENSOR_ACCURACY_MEDIUM: reading->setCalibrationLevel(1.0f); break; case SENSOR_ACCURACY_HIGH: reading->setCalibrationLevel(1.0f); break; } + + x = convertValue(event.motion.dsp.x); + y = convertValue(event.motion.dsp.y); + z = convertValue(event.motion.dsp.z); + } else { reading->setCalibrationLevel(1.0f); + +#ifndef Q_OS_BLACKBERRY_TABLET + x = convertValue(event.motion.raw.x); + y = convertValue(event.motion.raw.y); + z = convertValue(event.motion.raw.z); +#else + // Blackberry Tablet OS does not support raw reading values + x = convertValue(event.motion.dsp.x); + y = convertValue(event.motion.dsp.y); + z = convertValue(event.motion.dsp.z); +#endif } + remapAxes(&x, &y, &z); + reading->setX(x); + reading->setY(y); + reading->setZ(z); return true; } diff --git a/src/plugins/sensors/blackberry/bborientationsensor.cpp b/src/plugins/sensors/blackberry/bborientationsensor.cpp index 68ed735b..3d9d120c 100644 --- a/src/plugins/sensors/blackberry/bborientationsensor.cpp +++ b/src/plugins/sensors/blackberry/bborientationsensor.cpp @@ -44,6 +44,9 @@ BbOrientationSensor::BbOrientationSensor(QSensor *sensor) : BbSensorBackend<QOrientationReading>(devicePath(), SENSOR_TYPE_ORIENTATION, sensor) { setDescription(QLatin1String("Device orientation")); + + // Orientation rarely changes, so enable skipping of duplicates by default + sensor->setSkipDuplicates(true); } QString BbOrientationSensor::devicePath() @@ -51,19 +54,6 @@ QString BbOrientationSensor::devicePath() return QLatin1String("/dev/sensor/orientation"); } -void BbOrientationSensor::start() -{ - BbSensorBackend<QOrientationReading>::start(); - - // Orientation rarely changes, so enable skiping of duplicates - sensor_devctl_skipdupevent_u deviceSkip; - deviceSkip.tx.enable = 1; - const int result = devctl(deviceFile().handle(), DCMD_SENSOR_SKIPDUPEVENT, &deviceSkip, - sizeof(deviceSkip), NULL); - if (result != EOK) - perror("Enabling duplicate skipping for orientation sensor failed"); -} - void BbOrientationSensor::additionalDeviceInit() { // When querying the OS service for the range, it gives us the angles, which we don't need. diff --git a/src/plugins/sensors/blackberry/bborientationsensor.h b/src/plugins/sensors/blackberry/bborientationsensor.h index 07dc3d0f..85c7bd67 100644 --- a/src/plugins/sensors/blackberry/bborientationsensor.h +++ b/src/plugins/sensors/blackberry/bborientationsensor.h @@ -53,7 +53,6 @@ public: static QString devicePath(); - void start() Q_DECL_OVERRIDE; void additionalDeviceInit() Q_DECL_OVERRIDE; bool addDefaultRange() Q_DECL_OVERRIDE; diff --git a/src/plugins/sensors/blackberry/bbpressuresensor.cpp b/src/plugins/sensors/blackberry/bbpressuresensor.cpp index 01a2493b..8cb9b1ea 100644 --- a/src/plugins/sensors/blackberry/bbpressuresensor.cpp +++ b/src/plugins/sensors/blackberry/bbpressuresensor.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2012 Research In Motion +** Copyright (C) 2013 Research In Motion ** Contact: http://www.qt-project.org/legal ** ** This file is part of the QtSensors module of the Qt Toolkit. @@ -40,31 +40,8 @@ ****************************************************************************/ #include "bbpressuresensor.h" -class BbPressureReadingPrivate -{ -public: - BbPressureReadingPrivate() - : pressure(0) - { - } - - qreal pressure; -}; - -IMPLEMENT_READING(BbPressureReading) - -qreal BbPressureReading::pressure() const -{ - return d->pressure; -} - -void BbPressureReading::setPressure(qreal pressure) -{ - d->pressure = pressure; -} - BbPressureSensor::BbPressureSensor(QSensor *sensor) - : BbSensorBackend<BbPressureReading>(devicePath(), SENSOR_TYPE_PRESSURE, sensor) + : BbSensorBackend<QPressureReading>(devicePath(), SENSOR_TYPE_PRESSURE, sensor) { setDescription(QLatin1String("Pressure in Pascals")); } @@ -74,7 +51,7 @@ QString BbPressureSensor::devicePath() return QLatin1String("/dev/sensor/pressure"); } -bool BbPressureSensor::updateReadingFromEvent(const sensor_event_t &event, BbPressureReading *reading) +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. diff --git a/src/plugins/sensors/blackberry/bbpressuresensor.h b/src/plugins/sensors/blackberry/bbpressuresensor.h index 833832c4..7531bd68 100644 --- a/src/plugins/sensors/blackberry/bbpressuresensor.h +++ b/src/plugins/sensors/blackberry/bbpressuresensor.h @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2012 Research In Motion +** Copyright (C) 2013 Research In Motion ** Contact: http://www.qt-project.org/legal ** ** This file is part of the QtSensors module of the Qt Toolkit. @@ -42,20 +42,9 @@ #define BBPRESSURESENSOR_H #include "bbsensorbackend.h" +#include <qpressuresensor.h> -class BbPressureReadingPrivate; - -class BbPressureReading : public QSensorReading -{ - Q_OBJECT - Q_PROPERTY(qreal pressure READ pressure) - DECLARE_READING(BbPressureReading) -public: - qreal pressure() const; - void setPressure(qreal pressure); -}; - -class BbPressureSensor : public BbSensorBackend<BbPressureReading> +class BbPressureSensor : public BbSensorBackend<QPressureReading> { Q_OBJECT @@ -65,7 +54,7 @@ public: static QString devicePath(); protected: - bool updateReadingFromEvent(const sensor_event_t &event, BbPressureReading *reading) Q_DECL_OVERRIDE; + bool updateReadingFromEvent(const sensor_event_t &event, QPressureReading *reading) Q_DECL_OVERRIDE; }; #endif diff --git a/src/plugins/sensors/blackberry/bbproximitysensor.cpp b/src/plugins/sensors/blackberry/bbproximitysensor.cpp index 79f3834a..1177f744 100644 --- a/src/plugins/sensors/blackberry/bbproximitysensor.cpp +++ b/src/plugins/sensors/blackberry/bbproximitysensor.cpp @@ -53,9 +53,6 @@ QString BbProximitySensor::devicePath() bool BbProximitySensor::updateReadingFromEvent(const sensor_event_t &event, QProximityReading *reading) { - // TODO: I was unable to test this since the device I was testing this with did not have - // a proximity sensor. Verify that this works, check that the units are correct - // and that the threshold makes sense. const qreal minProximity = sensor()->outputRanges().first().minimum; reading->setClose(event.proximity_s.distance <= minProximity); return true; diff --git a/src/plugins/sensors/blackberry/bbsensorbackend.cpp b/src/plugins/sensors/blackberry/bbsensorbackend.cpp index fa8c89be..6b614a8a 100644 --- a/src/plugins/sensors/blackberry/bbsensorbackend.cpp +++ b/src/plugins/sensors/blackberry/bbsensorbackend.cpp @@ -81,15 +81,17 @@ static void remapMatrix(const float inputMatrix[3*3], BbSensorBackendBase::BbSensorBackendBase(const QString &devicePath, sensor_type_e sensorType, QSensor *sensor) : QSensorBackend(sensor), m_deviceFile(devicePath), m_sensorType(sensorType), m_guiHelper(0), - m_started(false) + m_started(false), m_applyingBufferSize(false) { m_mappingMatrix[0] = m_mappingMatrix[3] = 1; m_mappingMatrix[1] = m_mappingMatrix[2] = 0; connect(sensor, SIGNAL(alwaysOnChanged()), this, SLOT(applyAlwaysOnProperty())); + connect(sensor, SIGNAL(bufferSizeChanged(int)), this, SLOT(applyBuffering())); + connect(sensor, SIGNAL(userOrientationChanged(int)), this, SLOT(updateOrientation())); // Set some sensible default values - sensor->setProperty("efficientBufferSize", defaultBufferSize); - sensor->setProperty("maxBufferSize", defaultBufferSize); + sensor->setEfficientBufferSize(defaultBufferSize); + sensor->setMaxBufferSize(defaultBufferSize); } BbGuiHelper *BbSensorBackendBase::guiHelper() const @@ -107,12 +109,40 @@ sensor_type_e BbSensorBackendBase::sensorType() const return m_sensorType; } +void BbSensorBackendBase::setDevice(const QString &deviceFile, sensor_type_e sensorType) +{ + if (deviceFile != m_deviceFile.fileName()) { + setPaused(true); + delete m_socketNotifier.take(); + m_deviceFile.close(); + + m_sensorType = sensorType; + m_deviceFile.setFileName(deviceFile); + initSensorInfo(); + if (m_started) + start(); // restart with new device file + } +} + void BbSensorBackendBase::initSensorInfo() { if (!m_deviceFile.open(QFile::ReadOnly | QFile::Unbuffered)) { qDebug() << "Failed to open sensor" << m_deviceFile.fileName() << ":" << m_deviceFile.errorString(); } else { + + // Explicitly switch to non-blocking mode, otherwise read() will wait until new sensor + // data is available, and we have no way to check if there is more data or not (bytesAvailable() + // does not work for unbuffered mode) + const int oldFlags = fcntl(m_deviceFile.handle(), F_GETFL); + if (fcntl(m_deviceFile.handle(), F_SETFL, oldFlags | O_NONBLOCK) == -1) { + perror(QString::fromLatin1("Starting sensor %1 failed, fcntl() returned -1") + .arg(m_deviceFile.fileName()).toLocal8Bit()); + sensorError(errno); + stop(); + return; + } + sensor_devctl_info_u deviceInfo; const int result = devctl(m_deviceFile.handle(), DCMD_SENSOR_INFO, &deviceInfo, sizeof(deviceInfo), NULL); @@ -139,6 +169,7 @@ void BbSensorBackendBase::initSensorInfo() setPaused(true); m_socketNotifier.reset(new QSocketNotifier(m_deviceFile.handle(), QSocketNotifier::Read)); + m_socketNotifier->setEnabled(false); connect(m_socketNotifier.data(), SIGNAL(activated(int)), this, SLOT(dataAvailable())); } } @@ -168,12 +199,13 @@ qreal BbSensorBackendBase::convertValue(float bbValue) bool BbSensorBackendBase::isAutoAxisRemappingEnabled() const { - return sensor()->property("automaticAxisRemapping").toBool(); + return sensor()->isFeatureSupported(QSensor::AxesOrientation) && + sensor()->axesOrientationMode() != QSensor::FixedOrientation; } void BbSensorBackendBase::remapMatrix(const float inputMatrix[], float outputMatrix[]) { - if (!isAutoAxisRemappingEnabled() || m_guiHelper->currentOrientation() == 0) { + if (!isAutoAxisRemappingEnabled() || orientationForRemapping() == 0) { memcpy(outputMatrix, inputMatrix, sizeof(float) * 9); return; } @@ -184,10 +216,10 @@ void BbSensorBackendBase::remapMatrix(const float inputMatrix[], float outputMat void BbSensorBackendBase::remapAxes(float *x, float *y, float *z) { Q_ASSERT(x && y && z); - if (!isAutoAxisRemappingEnabled() || m_guiHelper->currentOrientation() == 0) + if (!isAutoAxisRemappingEnabled() || orientationForRemapping() == 0) return; - const int angle = m_guiHelper->currentOrientation(); + const int angle = orientationForRemapping(); const float oldX = *x; const float oldY = *y; @@ -239,36 +271,17 @@ void BbSensorBackendBase::start() } } - // Explicitly switch to non-blocking mode, otherwise read() will wait until new sensor - // data is available, and we have no way to check if there is more data or not (bytesAvailable() - // does not work for unbuffered mode) - const int oldFlags = fcntl(m_deviceFile.handle(), F_GETFL); - if (fcntl(m_deviceFile.handle(), F_SETFL, oldFlags | O_NONBLOCK) == -1) { - perror(QString::fromLatin1("Starting sensor %1 failed, fcntl() returned -1") - .arg(m_deviceFile.fileName()).toLocal8Bit()); - sensorError(errno); - stop(); - return; - } - - // Activate event queuing if needed - bool ok = false; - const int requestedBufferSize = sensor()->property("bufferSize").toInt(&ok); - if (ok && requestedBufferSize > 1) { - sensor_devctl_queue_u queueControl; - queueControl.tx.enable = 1; - const int result = devctl(m_deviceFile.handle(), DCMD_SENSOR_QUEUE, &queueControl, sizeof(queueControl), NULL); - if (result != EOK) { - perror(QString::fromLatin1("Enabling sensor queuing for %1 failed") - .arg(m_deviceFile.fileName()).toLocal8Bit()); - } - - const int actualBufferSize = queueControl.rx.size; - sensor()->setProperty("bufferSize", actualBufferSize); - sensor()->setProperty("efficientBufferSize", actualBufferSize); - sensor()->setProperty("maxBufferSize", actualBufferSize); + // Enable/disable duplicate skipping + sensor_devctl_skipdupevent_u deviceSkip; + deviceSkip.tx.enable = sensor()->skipDuplicates(); + const int result = devctl(deviceFile().handle(), DCMD_SENSOR_SKIPDUPEVENT, &deviceSkip, + sizeof(deviceSkip), NULL); + if (result != EOK) { + perror(QString::fromLatin1("Setting duplicate skipping for %1 failed") + .arg(m_deviceFile.fileName()).toLocal8Bit()); } + applyBuffering(); applyAlwaysOnProperty(); } @@ -281,11 +294,23 @@ void BbSensorBackendBase::stop() bool BbSensorBackendBase::isFeatureSupported(QSensor::Feature feature) const { switch (feature) { + case QSensor::AxesOrientation: + return (sensorType() == SENSOR_TYPE_ACCELEROMETER || sensorType() == SENSOR_TYPE_MAGNETOMETER || + sensorType() == SENSOR_TYPE_GYROSCOPE || sensorType() == SENSOR_TYPE_GRAVITY || + sensorType() == SENSOR_TYPE_LINEAR_ACCEL || sensorType() == SENSOR_TYPE_ROTATION_VECTOR || + sensorType() == SENSOR_TYPE_ROTATION_MATRIX || sensorType() == SENSOR_TYPE_AZIMUTH_PITCH_ROLL); case QSensor::AlwaysOn: case QSensor::Buffering: + case QSensor::AccelerationMode: + case QSensor::SkipDuplicates: return true; - case QSensor::Reserved: case QSensor::GeoValues: +#ifndef Q_OS_BLACKBERRY_TABLET + return (sensorType() == SENSOR_TYPE_MAGNETOMETER); +#else + return false; +#endif + case QSensor::Reserved: case QSensor::FieldOfView: break; } @@ -295,8 +320,12 @@ bool BbSensorBackendBase::isFeatureSupported(QSensor::Feature feature) const void BbSensorBackendBase::dataAvailable() { - if (!m_started) + if (!m_started) { + // Spurious dataAvailable() call, drain the device file of data. We also disable + // the socket notifier for this, so this is just added safety here. + m_deviceFile.readAll(); return; + } Q_FOREVER { sensor_event_t event; @@ -331,6 +360,46 @@ void BbSensorBackendBase::applyAlwaysOnProperty() updatePauseState(); } +void BbSensorBackendBase::applyBuffering() +{ + if (!m_deviceFile.isOpen() || !m_started || m_applyingBufferSize) + return; + + // Flag to prevent recursion. We call setBufferSize() below, and because of the changed signal, + // we might end up in this slot again. + // The call to setBufferSize() is needed since the requested buffer size is most likely different + // from the actual buffer size that will be used. + m_applyingBufferSize = true; + + const bool enableBuffering = sensor()->bufferSize() > 1; + sensor_devctl_queue_u queueControl; + queueControl.tx.enable = enableBuffering ? 1 : 0; + const int result = devctl(m_deviceFile.handle(), DCMD_SENSOR_QUEUE, &queueControl, sizeof(queueControl), NULL); + if (result != EOK) { + perror(QString::fromLatin1("Enabling sensor queuing for %1 failed") + .arg(m_deviceFile.fileName()).toLocal8Bit()); + } else { + if (enableBuffering) { + int actualBufferSize = queueControl.rx.size; + + // Some firmware versions don't report the buffer size correctly. Simply pretend the + // buffer size is the same as the requested buffer size, as setting the buffer size to + // 1 here would seem as if buffering were disabled. + if (actualBufferSize == 1) + actualBufferSize = sensor()->bufferSize(); + + sensor()->setBufferSize(actualBufferSize); + sensor()->setEfficientBufferSize(actualBufferSize); + sensor()->setMaxBufferSize(actualBufferSize); + } else { + sensor()->setBufferSize(1); + sensor()->setEfficientBufferSize(defaultBufferSize); + sensor()->setMaxBufferSize(defaultBufferSize); + } + } + m_applyingBufferSize = false; +} + bool BbSensorBackendBase::setPaused(bool paused) { if (!m_deviceFile.isOpen()) @@ -339,6 +408,9 @@ bool BbSensorBackendBase::setPaused(bool paused) sensor_devctl_enable_u enableState; enableState.tx.enable = paused ? 0 : 1; + if (m_socketNotifier) + m_socketNotifier->setEnabled(!paused); + const int result = devctl(m_deviceFile.handle(), DCMD_SENSOR_ENABLE, &enableState, sizeof(enableState), NULL); if (result != EOK) { perror(QString::fromLatin1("Setting sensor enabled (%1) for %2 failed") @@ -360,12 +432,26 @@ void BbSensorBackendBase::updatePauseState() void BbSensorBackendBase::updateOrientation() { - // ### I can't really test this, the rotation matrix has too many glitches and drifts over time, - // making any measurement quite hard - const int rotationAngle = guiHelper()->currentOrientation(); + const int rotationAngle = orientationForRemapping(); m_mappingMatrix[0] = cos(rotationAngle*M_PI/180); m_mappingMatrix[1] = sin(rotationAngle*M_PI/180); m_mappingMatrix[2] = -sin(rotationAngle*M_PI/180); m_mappingMatrix[3] = cos(rotationAngle*M_PI/180); + + if (sensor()->isFeatureSupported(QSensor::AxesOrientation)) + sensor()->setCurrentOrientation(rotationAngle); +} + +int BbSensorBackendBase::orientationForRemapping() const +{ + if (!sensor()->isFeatureSupported(QSensor::AxesOrientation)) + return 0; + + switch (sensor()->axesOrientationMode()) { + default: + case QSensor::FixedOrientation: return 0; + case QSensor::AutomaticOrientation: return guiHelper()->currentOrientation(); + case QSensor::UserOrientation: return sensor()->userOrientation(); + } } diff --git a/src/plugins/sensors/blackberry/bbsensorbackend.h b/src/plugins/sensors/blackberry/bbsensorbackend.h index 7e2ad0ec..4e7b8101 100644 --- a/src/plugins/sensors/blackberry/bbsensorbackend.h +++ b/src/plugins/sensors/blackberry/bbsensorbackend.h @@ -76,6 +76,9 @@ protected: BbGuiHelper *guiHelper() const; QFile& deviceFile(); sensor_type_e sensorType() const; + int orientationForRemapping() const; + + void setDevice(const QString &deviceFile, sensor_type_e sensorType); // This is called while the device file is open during initalization and gives a subclass // an opportunity to do additional initalization. @@ -104,6 +107,7 @@ protected: private slots: void dataAvailable(); void applyAlwaysOnProperty(); + void applyBuffering(); bool setPaused(bool paused); void updatePauseState(); void updateOrientation(); @@ -115,6 +119,7 @@ private: BbGuiHelper *m_guiHelper; float m_mappingMatrix[4]; bool m_started; + bool m_applyingBufferSize; }; template<class SensorReading> diff --git a/src/plugins/sensors/blackberry/bbtemperaturesensor.cpp b/src/plugins/sensors/blackberry/bbtemperaturesensor.cpp index f3ec6342..b38fba4d 100644 --- a/src/plugins/sensors/blackberry/bbtemperaturesensor.cpp +++ b/src/plugins/sensors/blackberry/bbtemperaturesensor.cpp @@ -40,31 +40,8 @@ ****************************************************************************/ #include "bbtemperaturesensor.h" -class BbTemperatureReadingPrivate -{ -public: - BbTemperatureReadingPrivate() - : temperature(0) - { - } - - qreal temperature; -}; - -IMPLEMENT_READING(BbTemperatureReading) - -qreal BbTemperatureReading::temperature() const -{ - return d->temperature; -} - -void BbTemperatureReading::setTemperature(qreal temperature) -{ - d->temperature = temperature; -} - BbTemperatureSensor::BbTemperatureSensor(QSensor *sensor) - : BbSensorBackend<BbTemperatureReading>(devicePath(), SENSOR_TYPE_TEMPERATURE, sensor) + : BbSensorBackend<QAmbientTemperatureReading>(devicePath(), SENSOR_TYPE_TEMPERATURE, sensor) { setDescription(QLatin1String("Temperature in degrees Celsius")); } @@ -74,7 +51,7 @@ QString BbTemperatureSensor::devicePath() return QLatin1String("/dev/sensor/temp"); } -bool BbTemperatureSensor::updateReadingFromEvent(const sensor_event_t &event, BbTemperatureReading *reading) +bool BbTemperatureSensor::updateReadingFromEvent(const sensor_event_t &event, QAmbientTemperatureReading *reading) { // TODO: I was unable to test this since the device I was testing this with did not have // a temperature sensor. Verify that this works and check that the units are correct. diff --git a/src/plugins/sensors/blackberry/bbtemperaturesensor.h b/src/plugins/sensors/blackberry/bbtemperaturesensor.h index 1b9ea1bf..a24c67e7 100644 --- a/src/plugins/sensors/blackberry/bbtemperaturesensor.h +++ b/src/plugins/sensors/blackberry/bbtemperaturesensor.h @@ -42,20 +42,9 @@ #define BBTEMPERATURESENSOR_H #include "bbsensorbackend.h" +#include <qambienttemperaturesensor.h> -class BbTemperatureReadingPrivate; - -class BbTemperatureReading : public QSensorReading -{ - Q_OBJECT - Q_PROPERTY(qreal temperature READ temperature) - DECLARE_READING(BbTemperatureReading) -public: - qreal temperature() const; - void setTemperature(qreal temperature); -}; - -class BbTemperatureSensor : public BbSensorBackend<BbTemperatureReading> +class BbTemperatureSensor : public BbSensorBackend<QAmbientTemperatureReading> { Q_OBJECT @@ -65,7 +54,7 @@ public: static QString devicePath(); protected: - bool updateReadingFromEvent(const sensor_event_t &event, BbTemperatureReading *reading); + bool updateReadingFromEvent(const sensor_event_t &event, QAmbientTemperatureReading *reading); }; #endif diff --git a/src/plugins/sensors/blackberry/blackberry.pro b/src/plugins/sensors/blackberry/blackberry.pro index 73027c20..902239c4 100644 --- a/src/plugins/sensors/blackberry/blackberry.pro +++ b/src/plugins/sensors/blackberry/blackberry.pro @@ -12,6 +12,10 @@ config_bbsensor_compass { DEFINES += HAVE_COMPASS_SENSOR } +config_bbsensor_holster { + DEFINES += HAVE_HOLSTER_SENSOR +} + HEADERS += bbsensorbackend.h \ bbaccelerometer.h \ bbaltimeter.h \ @@ -47,4 +51,9 @@ SOURCES += bbsensorbackend.cpp \ bbutil.cpp \ main.cpp +config_bbsensor_holster { + HEADERS += bbholstersensor.h + SOURCES += bbholstersensor.cpp +} + OTHER_FILES = plugin.json diff --git a/src/plugins/sensors/blackberry/main.cpp b/src/plugins/sensors/blackberry/main.cpp index 7c2d5dbc..61f9aa08 100644 --- a/src/plugins/sensors/blackberry/main.cpp +++ b/src/plugins/sensors/blackberry/main.cpp @@ -43,6 +43,9 @@ #include "bbambientlightsensor.h" #include "bbcompass.h" #include "bbgyroscope.h" +#ifdef HAVE_HOLSTER_SENSOR +#include "bbholstersensor.h" +#endif #include "bbirproximitysensor.h" #include "bblightsensor.h" #include "bbmagnetometer.h" @@ -61,6 +64,9 @@ static const char *bbAltitmeterId = "bbAltimeter"; static const char *bbAmbientLightSensorId = "bbAmbientLightSensor"; static const char *bbCompassId = "bbCompass"; static const char *bbGyroscopeId = "bbGyroscope"; +#ifdef HAVE_HOLSTER_SENSOR +static const char *bbHolsterSensorId = "bbHolsterSensor"; +#endif static const char *bbIRProximitySensorId = "bbIRProximitySensor"; static const char *bbLightSensorId = "bbLightSensor"; static const char *bbMagnetometerId = "bbMagnetometer"; @@ -82,13 +88,17 @@ public: if (sensorSupported(BbAccelerometer::devicePath())) QSensorManager::registerBackend(QAccelerometer::type, bbAccelerometerId, this); if (sensorSupported(BbAltimeter::devicePath())) - QSensorManager::registerBackend("BbAltimeter", bbAltitmeterId, this); + QSensorManager::registerBackend(QAltimeter::type, bbAltitmeterId, this); if (sensorSupported(BbAmbientLightSensor::devicePath())) QSensorManager::registerBackend(QAmbientLightSensor::type, bbAmbientLightSensorId, this); if (sensorSupported(BbCompass::devicePath())) QSensorManager::registerBackend(QCompass::type, bbCompassId, this); if (sensorSupported(BbGyroscope::devicePath())) QSensorManager::registerBackend(QGyroscope::type, bbGyroscopeId, this); +#ifdef HAVE_HOLSTER_SENSOR + if (sensorSupported(BbHolsterSensor::devicePath())) + QSensorManager::registerBackend(QHolsterSensor::type, bbHolsterSensorId, this); +#endif if (sensorSupported(BbIRProximitySensor::devicePath())) QSensorManager::registerBackend(QIRProximitySensor::type, bbIRProximitySensorId, this); if (sensorSupported(BbLightSensor::devicePath())) @@ -98,13 +108,13 @@ public: if (sensorSupported(BbOrientationSensor::devicePath())) QSensorManager::registerBackend(QOrientationSensor::type, bbOrientationSensorId, this); if (sensorSupported(BbPressureSensor::devicePath())) - QSensorManager::registerBackend("BbPressureSensor", bbPressureSensorId, this); + QSensorManager::registerBackend(QPressureSensor::type, bbPressureSensorId, this); if (sensorSupported(BbProximitySensor::devicePath())) QSensorManager::registerBackend(QProximitySensor::type, bbProximitySensorId, this); if (sensorSupported(BbRotationSensor::devicePath())) QSensorManager::registerBackend(QRotationSensor::type, bbRotationSensorId, this); if (sensorSupported(BbTemperatureSensor::devicePath())) - QSensorManager::registerBackend("BbTemperatureSensor", bbTemperatureSensorId, this); + QSensorManager::registerBackend(QAmbientTemperatureSensor::type, bbTemperatureSensorId, this); } QSensorBackend *createBackend(QSensor *sensor) Q_DECL_OVERRIDE @@ -120,6 +130,10 @@ public: backend = new BbCompass(sensor); if (sensor->identifier() == bbGyroscopeId) backend = new BbGyroscope(sensor); +#ifdef HAVE_HOLSTER_SENSOR + if (sensor->identifier() == bbHolsterSensorId) + backend = new BbHolsterSensor(sensor); +#endif if (sensor->identifier() == bbIRProximitySensorId) backend = new BbIRProximitySensor(sensor); if (sensor->identifier() == bbLightSensorId) |