From fd6f05aa763538309c4d26223f2111be8ec0f82d Mon Sep 17 00:00:00 2001 From: Lincoln Ramsay Date: Tue, 23 Aug 2011 11:21:42 +1000 Subject: QtmSensors, legacy sensors library. QtSensors will not remain source-compatible with Mobility. QtmSensors exists to provide a source-compatible API. QtmSensors is implemented as a wrapper over the QtSensors library. Only front end functionality is supported. Backends must be ported to QtSensors. Change-Id: Ia7e45b719fb5f84637f899374990dbd680e259bd Reviewed-on: http://codereview.qt.nokia.com/3348 Reviewed-by: Qt Sanity Bot Reviewed-by: Zsolt Simon --- src/qtmsensors/backendwrapper.cpp | 191 ++++++ src/qtmsensors/backendwrapper_p.h | 113 ++++ src/qtmsensors/qaccelerometer.cpp | 196 ++++++ src/qtmsensors/qaccelerometer.h | 92 +++ src/qtmsensors/qaccelerometer_p.h | 78 +++ src/qtmsensors/qambientlightsensor.cpp | 172 ++++++ src/qtmsensors/qambientlightsensor.h | 94 +++ src/qtmsensors/qambientlightsensor_p.h | 74 +++ src/qtmsensors/qcompass.cpp | 182 ++++++ src/qtmsensors/qcompass.h | 88 +++ src/qtmsensors/qcompass_p.h | 76 +++ src/qtmsensors/qgyroscope.cpp | 194 ++++++ src/qtmsensors/qgyroscope.h | 92 +++ src/qtmsensors/qgyroscope_p.h | 78 +++ src/qtmsensors/qlightsensor.cpp | 154 +++++ src/qtmsensors/qlightsensor.h | 87 +++ src/qtmsensors/qlightsensor_p.h | 76 +++ src/qtmsensors/qmagnetometer.cpp | 245 ++++++++ src/qtmsensors/qmagnetometer.h | 99 ++++ src/qtmsensors/qmagnetometer_p.h | 80 +++ src/qtmsensors/qorientationsensor.cpp | 195 ++++++ src/qtmsensors/qorientationsensor.h | 95 +++ src/qtmsensors/qorientationsensor_p.h | 74 +++ src/qtmsensors/qproximitysensor.cpp | 151 +++++ src/qtmsensors/qproximitysensor.h | 84 +++ src/qtmsensors/qproximitysensor_p.h | 74 +++ src/qtmsensors/qrotationsensor.cpp | 240 ++++++++ src/qtmsensors/qrotationsensor.h | 95 +++ src/qtmsensors/qrotationsensor_p.h | 78 +++ src/qtmsensors/qsensor.cpp | 1017 ++++++++++++++++++++++++++++++++ src/qtmsensors/qsensor.h | 254 ++++++++ src/qtmsensors/qsensor_p.h | 124 ++++ src/qtmsensors/qsensorbackend.cpp | 328 ++++++++++ src/qtmsensors/qsensorbackend_p.h | 99 ++++ src/qtmsensors/qsensormanager.cpp | 453 ++++++++++++++ src/qtmsensors/qsensormanager_p.h | 107 ++++ src/qtmsensors/qtapsensor.cpp | 249 ++++++++ src/qtmsensors/qtapsensor.h | 108 ++++ src/qtmsensors/qtapsensor_p.h | 76 +++ src/qtmsensors/qtmsensors.pro | 52 ++ src/qtmsensors/qtmsensorsglobal.h | 53 ++ 41 files changed, 6467 insertions(+) create mode 100644 src/qtmsensors/backendwrapper.cpp create mode 100644 src/qtmsensors/backendwrapper_p.h create mode 100644 src/qtmsensors/qaccelerometer.cpp create mode 100644 src/qtmsensors/qaccelerometer.h create mode 100644 src/qtmsensors/qaccelerometer_p.h create mode 100644 src/qtmsensors/qambientlightsensor.cpp create mode 100644 src/qtmsensors/qambientlightsensor.h create mode 100644 src/qtmsensors/qambientlightsensor_p.h create mode 100644 src/qtmsensors/qcompass.cpp create mode 100644 src/qtmsensors/qcompass.h create mode 100644 src/qtmsensors/qcompass_p.h create mode 100644 src/qtmsensors/qgyroscope.cpp create mode 100644 src/qtmsensors/qgyroscope.h create mode 100644 src/qtmsensors/qgyroscope_p.h create mode 100644 src/qtmsensors/qlightsensor.cpp create mode 100644 src/qtmsensors/qlightsensor.h create mode 100644 src/qtmsensors/qlightsensor_p.h create mode 100644 src/qtmsensors/qmagnetometer.cpp create mode 100644 src/qtmsensors/qmagnetometer.h create mode 100644 src/qtmsensors/qmagnetometer_p.h create mode 100644 src/qtmsensors/qorientationsensor.cpp create mode 100644 src/qtmsensors/qorientationsensor.h create mode 100644 src/qtmsensors/qorientationsensor_p.h create mode 100644 src/qtmsensors/qproximitysensor.cpp create mode 100644 src/qtmsensors/qproximitysensor.h create mode 100644 src/qtmsensors/qproximitysensor_p.h create mode 100644 src/qtmsensors/qrotationsensor.cpp create mode 100644 src/qtmsensors/qrotationsensor.h create mode 100644 src/qtmsensors/qrotationsensor_p.h create mode 100644 src/qtmsensors/qsensor.cpp create mode 100644 src/qtmsensors/qsensor.h create mode 100644 src/qtmsensors/qsensor_p.h create mode 100644 src/qtmsensors/qsensorbackend.cpp create mode 100644 src/qtmsensors/qsensorbackend_p.h create mode 100644 src/qtmsensors/qsensormanager.cpp create mode 100644 src/qtmsensors/qsensormanager_p.h create mode 100644 src/qtmsensors/qtapsensor.cpp create mode 100644 src/qtmsensors/qtapsensor.h create mode 100644 src/qtmsensors/qtapsensor_p.h create mode 100644 src/qtmsensors/qtmsensors.pro create mode 100644 src/qtmsensors/qtmsensorsglobal.h (limited to 'src/qtmsensors') diff --git a/src/qtmsensors/backendwrapper.cpp b/src/qtmsensors/backendwrapper.cpp new file mode 100644 index 00000000..5bba7c4c --- /dev/null +++ b/src/qtmsensors/backendwrapper.cpp @@ -0,0 +1,191 @@ +#include "backendwrapper_p.h" +#include "qsensormanager_p.h" +#include + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +class WrapperFactory : public QSensorPluginInterface, + public QSensorBackendFactory +{ +public: + void registerSensors() + { + QList types; + types << QAccelerometer::type; + types << QAmbientLightSensor::type; + types << QCompass::type; + types << QGyroscope::type; + types << QLightSensor::type; + types << QMagnetometer::type; + types << QOrientationSensor::type; + types << QProximitySensor::type; + types << QRotationSensor::type; + types << QTapSensor::type; + foreach (const QByteArray &type, types) { + foreach (const QByteArray &identifier, NEW_NAMESPACE(QSensor)::sensorsForType(type)) { + QSensorManager::registerBackend(type, identifier, this); + } + } + } + + QSensorBackend *createBackend(QSensor *sensor) + { + if (sensor->type() == QAccelerometer::type) { + return new QAccelerometerWrapper(sensor); + } + if (sensor->type() == QAmbientLightSensor::type) { + return new QAmbientLightSensorWrapper(sensor); + } + if (sensor->type() == QCompass::type) { + return new QCompassWrapper(sensor); + } + if (sensor->type() == QGyroscope::type) { + return new QGyroscopeWrapper(sensor); + } + if (sensor->type() == QLightSensor::type) { + return new QLightSensorWrapper(sensor); + } + if (sensor->type() == QMagnetometer::type) { + return new QMagnetometerWrapper(sensor); + } + if (sensor->type() == QOrientationSensor::type) { + return new QOrientationSensorWrapper(sensor); + } + if (sensor->type() == QProximitySensor::type) { + return new QProximitySensorWrapper(sensor); + } + if (sensor->type() == QRotationSensor::type) { + return new QRotationSensorWrapper(sensor); + } + if (sensor->type() == QTapSensor::type) { + return new QTapSensorWrapper(sensor); + } + return 0; + } +}; + +REGISTER_STATIC_PLUGIN_V1(WrapperFactory) + +QSensorWrapper::QSensorWrapper(QSensor *sensor) + : QSensorBackend(sensor) +{ +} + +void QSensorWrapper::init(QObject *wsensor) +{ + connect(wsensor, SIGNAL(readingChanged()), this, SLOT(fetchData())); + connect(wsensor, SIGNAL(sensorError(int)), this, SLOT(reportSensorError(int))); + connect(wsensor, SIGNAL(busyChanged()), sensor(), SIGNAL(busyChanged())); + m_wsensor = wsensor; +} + +void QSensorWrapper::start() +{ + _start(); +} + +void QSensorWrapper::stop() +{ + _stop(); +} + +void QSensorWrapper::fetchData() +{ + _fetchData(); +} + +void QSensorWrapper::setProperty(const char *name, const QVariant &value) +{ + //qDebug() << "QSensorWrapper::setProperty" << name << value; + QObject::setProperty(name, value); + m_wsensor->setProperty(name, value); +} + +QVariant QSensorWrapper::property(const char *name) const +{ + //qDebug() << "QSensorWrapper::property"; + return m_wsensor->property(name); +} + +void QSensorWrapper::reportSensorError(int error) +{ + sensorError(error); +} + +IMPLEMENT_WRAPPER(QAccelerometer, QAccelerometerReading, { + m_reading.setTimestamp(sensor->reading()->timestamp()); + m_reading.setX(sensor->reading()->x()); + m_reading.setY(sensor->reading()->y()); + m_reading.setZ(sensor->reading()->z()); + newReadingAvailable(); + }) + +IMPLEMENT_WRAPPER(QAmbientLightSensor, QAmbientLightReading, { + m_reading.setTimestamp(sensor->reading()->timestamp()); + m_reading.setLightLevel(static_cast(sensor->reading()->lightLevel())); + newReadingAvailable(); + }) + +IMPLEMENT_WRAPPER(QCompass, QCompassReading, { + m_reading.setTimestamp(sensor->reading()->timestamp()); + m_reading.setAzimuth(sensor->reading()->azimuth()); + //m_reading.setCalibrationLevel(sensor->calibrationLevel()); + m_reading.setCalibrationLevel(sensor->reading()->calibrationLevel()); + newReadingAvailable(); + }) + +IMPLEMENT_WRAPPER(QGyroscope, QGyroscopeReading, { + m_reading.setTimestamp(sensor->reading()->timestamp()); + m_reading.setX(sensor->reading()->x()); + m_reading.setY(sensor->reading()->y()); + m_reading.setZ(sensor->reading()->z()); + newReadingAvailable(); + }) + +IMPLEMENT_WRAPPER(QLightSensor, QLightReading, { + m_reading.setTimestamp(sensor->reading()->timestamp()); + m_reading.setLux(sensor->reading()->lux()); + newReadingAvailable(); + }) + +IMPLEMENT_WRAPPER(QMagnetometer, QMagnetometerReading, { + m_reading.setTimestamp(sensor->reading()->timestamp()); + m_reading.setX(sensor->reading()->x()); + m_reading.setY(sensor->reading()->y()); + m_reading.setZ(sensor->reading()->z()); + //m_reading.setCalibrationLevel(sensor->calibrationLevel()); + m_reading.setCalibrationLevel(sensor->reading()->calibrationLevel()); + newReadingAvailable(); + }) + +IMPLEMENT_WRAPPER(QOrientationSensor, QOrientationReading, { + m_reading.setTimestamp(sensor->reading()->timestamp()); + m_reading.setOrientation(static_cast(sensor->reading()->orientation())); + newReadingAvailable(); + }) + +IMPLEMENT_WRAPPER(QProximitySensor, QProximityReading, { + m_reading.setTimestamp(sensor->reading()->timestamp()); + m_reading.setClose(sensor->reading()->close()); + newReadingAvailable(); + }) + +IMPLEMENT_WRAPPER(QRotationSensor, QRotationReading, { + m_reading.setTimestamp(sensor->reading()->timestamp()); + m_reading.setX(sensor->reading()->x()); + m_reading.setY(sensor->reading()->y()); + m_reading.setZ(sensor->reading()->z()); + newReadingAvailable(); + }) + +IMPLEMENT_WRAPPER(QTapSensor, QTapReading, { + m_reading.setTimestamp(sensor->reading()->timestamp()); + m_reading.setTapDirection(static_cast(sensor->reading()->tapDirection())); + m_reading.setDoubleTap(sensor->reading()->isDoubleTap()); + newReadingAvailable(); + }) + +#include "moc_backendwrapper_p.cpp" +QTM_END_NAMESPACE +QT_END_NAMESPACE diff --git a/src/qtmsensors/backendwrapper_p.h b/src/qtmsensors/backendwrapper_p.h new file mode 100644 index 00000000..164cfce3 --- /dev/null +++ b/src/qtmsensors/backendwrapper_p.h @@ -0,0 +1,113 @@ +#ifndef BACKENDWRAPPER_P_H +#define BACKENDWRAPPER_P_H + +#include "qsensorbackend_p.h" +#include "qaccelerometer.h" +#include +#include "qambientlightsensor.h" +#include +#include "qcompass.h" +#include +#include "qgyroscope.h" +#include +#include "qlightsensor.h" +#include +#include "qmagnetometer.h" +#include +#include "qorientationsensor.h" +#include +#include "qproximitysensor.h" +#include +#include "qrotationsensor.h" +#include +#include "qtapsensor.h" +#include + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +class QTM_SENSORS_EXPORT QSensorWrapper : public QSensorBackend +{ + Q_OBJECT +public: + explicit QSensorWrapper(QSensor *sensor); + + void start(); + void stop(); + + void setProperty(const char *name, const QVariant &value); + QVariant property(const char *name) const; + + virtual bool _connect() = 0; + +public slots: + void fetchData(); + void reportSensorError(int); + +protected: + void init(QObject *wsensor); + +private: + virtual void _start() = 0; + virtual void _stop() = 0; + virtual void _fetchData() = 0; +protected: + QObject *m_wsensor; +}; + +#define DECLARE_WRAPPER(sensorclass, readingclass)\ + class sensorclass ## Wrapper : public QSensorWrapper {\ + public:\ + explicit sensorclass ## Wrapper(QSensor *sensor);\ + void _start();\ + void _stop();\ + void _fetchData();\ + bool _connect();\ + readingclass m_reading;\ + }; + +#define IMPLEMENT_WRAPPER(sensorclass, readingclass, fetchfunc)\ + sensorclass ## Wrapper::sensorclass ## Wrapper(QSensor *wsensor) : QSensorWrapper(wsensor) {\ + setReading(&m_reading);\ + NEW_NAMESPACE(sensorclass) *sensor = new NEW_NAMESPACE(sensorclass)(this);\ + init(sensor);\ + }\ + bool sensorclass ## Wrapper::_connect() {\ + QSensor *wsensor = this->sensor();\ + NEW_NAMESPACE(sensorclass) *sensor = static_cast(m_wsensor);\ + sensor->setIdentifier(wsensor->identifier());\ + sensor->connectToBackend();\ + setDescription(sensor->description());\ + foreach (const NEW_NAMESPACE(qoutputrange) &range, sensor->outputRanges()) {\ + addOutputRange(range.minimum, range.maximum, range.accuracy);\ + }\ + foreach (const NEW_NAMESPACE(qrange) &range, sensor->availableDataRates()) {\ + addDataRate(range.first, range.second);\ + }\ + return sensor->isConnectedToBackend();\ + }\ + void sensorclass ## Wrapper::_start() {\ + NEW_NAMESPACE(sensorclass) *sensor = static_cast(m_wsensor);\ + if (!sensor->start()) {\ + if (sensor->isBusy()) sensorBusy();\ + sensorStopped();\ + }\ + }\ + void sensorclass ## Wrapper::_stop() { NEW_NAMESPACE(sensorclass) *sensor = static_cast(m_wsensor); sensor->stop(); }\ + void sensorclass ## Wrapper::_fetchData() { NEW_NAMESPACE(sensorclass) *sensor = static_cast(m_wsensor); fetchfunc } + +DECLARE_WRAPPER(QAccelerometer, QAccelerometerReading) +DECLARE_WRAPPER(QAmbientLightSensor, QAmbientLightReading) +DECLARE_WRAPPER(QCompass, QCompassReading) +DECLARE_WRAPPER(QGyroscope, QGyroscopeReading) +DECLARE_WRAPPER(QLightSensor, QLightReading) +DECLARE_WRAPPER(QMagnetometer, QMagnetometerReading) +DECLARE_WRAPPER(QOrientationSensor, QOrientationReading) +DECLARE_WRAPPER(QProximitySensor, QProximityReading) +DECLARE_WRAPPER(QRotationSensor, QRotationReading) +DECLARE_WRAPPER(QTapSensor, QTapReading) + +QTM_END_NAMESPACE +QT_END_NAMESPACE + +#endif diff --git a/src/qtmsensors/qaccelerometer.cpp b/src/qtmsensors/qaccelerometer.cpp new file mode 100644 index 00000000..0f0765ec --- /dev/null +++ b/src/qtmsensors/qaccelerometer.cpp @@ -0,0 +1,196 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qaccelerometer.h" +#include "qaccelerometer_p.h" + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +IMPLEMENT_READING(QAccelerometerReading) + +/*! + \class QAccelerometerReading + \ingroup sensors_reading + \inmodule QtSensors + \since 1.0 + + \brief The QAccelerometerReading class reports on linear acceleration + along the X, Y and Z axes. + + \section2 QAccelerometerReading Units + The scale of the values is meters per second squared. + The axes are arranged as follows. + + \image sensors-coordinates2.jpg + + A monoblock device sitting at rest, face up on a desk will experience + a force of approximately 9.8 on the Z axis (ie. towards the roof). + This is the proper acceleration the device experiences relative to + freefall. +*/ + +/*! + \property QAccelerometerReading::x + \brief the acceleration on the X axis. + + The scale of the values is meters per second squared. + \sa {QAccelerometerReading Units} +*/ + +qreal QAccelerometerReading::x() const +{ + return d->x; +} + +/*! + Sets the acceleration on the X axis to \a x. +*/ +void QAccelerometerReading::setX(qreal x) +{ + d->x = x; +} + +/*! + \property QAccelerometerReading::y + \brief the acceleration on the Y axis. + + The scale of the values is meters per second squared. + \sa {QAccelerometerReading Units} +*/ + +qreal QAccelerometerReading::y() const +{ + return d->y; +} + +/*! + Sets the acceleration on the Y axis to \a y. +*/ +void QAccelerometerReading::setY(qreal y) +{ + d->y = y; +} + +/*! + \property QAccelerometerReading::z + \brief the acceleration on the Z axis. + + The scale of the values is meters per second squared. + \sa {QAccelerometerReading Units} +*/ + +qreal QAccelerometerReading::z() const +{ + return d->z; +} + +/*! + Sets the acceleration on the Z axis to \a z. +*/ +void QAccelerometerReading::setZ(qreal z) +{ + d->z = z; +} + +// ===================================================================== + +/*! + \class QAccelerometerFilter + \ingroup sensors_filter + \inmodule QtSensors + + \brief The QAccelerometerFilter class is a convenience wrapper around QSensorFilter. + + The only difference is that the filter() method features a pointer to QAccelerometerReading + instead of QSensorReading. +*/ + +/*! + \fn QAccelerometerFilter::filter(QAccelerometerReading *reading) + + Called when \a reading changes. Returns false to prevent the reading from propagating. + + \sa QSensorFilter::filter() +*/ + +char const * const QAccelerometer::type("QAccelerometer"); + +/*! + \class QAccelerometer + \ingroup sensors_type + \inmodule QtSensors + + \brief The QAccelerometer class is a convenience wrapper around QSensor. + + The only behavioural difference is that this class sets the type properly. + + This class also features a reading() function that returns a QAccelerometerReading instead of a QSensorReading. + + For details about how the sensor works, see \l QAccelerometerReading. + + \sa QAccelerometerReading +*/ + +/*! + \fn QAccelerometer::QAccelerometer(QObject *parent) + + Construct the sensor as a child of \a parent. +*/ + +/*! + \fn QAccelerometer::~QAccelerometer() + + Destroy the sensor. Stops the sensor if it has not already been stopped. +*/ + +/*! + \fn QAccelerometer::reading() const + + Returns the reading class for this sensor. + + \sa QSensor::reading() +*/ + +#include "moc_qaccelerometer.cpp" +QTM_END_NAMESPACE +QT_END_NAMESPACE + diff --git a/src/qtmsensors/qaccelerometer.h b/src/qtmsensors/qaccelerometer.h new file mode 100644 index 00000000..7c1c55a2 --- /dev/null +++ b/src/qtmsensors/qaccelerometer.h @@ -0,0 +1,92 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTM_QACCELEROMETER_H +#define QTM_QACCELEROMETER_H + +#include "qsensor.h" + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +class QAccelerometerReadingPrivate; + +class QTM_SENSORS_EXPORT QAccelerometerReading : public QSensorReading +{ + Q_OBJECT + Q_PROPERTY(qreal x READ x) + Q_PROPERTY(qreal y READ y) + Q_PROPERTY(qreal z READ z) + DECLARE_READING(QAccelerometerReading) +public: + qreal x() const; + void setX(qreal x); + + qreal y() const; + void setY(qreal y); + + qreal z() const; + void setZ(qreal z); +}; + +class QTM_SENSORS_EXPORT QAccelerometerFilter : public QSensorFilter +{ +public: + virtual bool filter(QAccelerometerReading *reading) = 0; +private: + bool filter(QSensorReading *reading) { return filter(static_cast(reading)); } +}; + +class QTM_SENSORS_EXPORT QAccelerometer : public QSensor +{ + Q_OBJECT +public: + explicit QAccelerometer(QObject *parent = 0) : QSensor(QAccelerometer::type, parent) {} + virtual ~QAccelerometer() {} + QAccelerometerReading *reading() const { return static_cast(QSensor::reading()); } + static char const * const type; +}; + +QTM_END_NAMESPACE +QT_END_NAMESPACE + +#endif + diff --git a/src/qtmsensors/qaccelerometer_p.h b/src/qtmsensors/qaccelerometer_p.h new file mode 100644 index 00000000..eef64396 --- /dev/null +++ b/src/qtmsensors/qaccelerometer_p.h @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTM_QACCELEROMETER_P_H +#define QTM_QACCELEROMETER_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +class QAccelerometerReadingPrivate +{ +public: + QAccelerometerReadingPrivate() + : x(0) + , y(0) + , z(0) + { + } + + qreal x; + qreal y; + qreal z; +}; + +QTM_END_NAMESPACE +QT_END_NAMESPACE + +#endif + diff --git a/src/qtmsensors/qambientlightsensor.cpp b/src/qtmsensors/qambientlightsensor.cpp new file mode 100644 index 00000000..dbddc018 --- /dev/null +++ b/src/qtmsensors/qambientlightsensor.cpp @@ -0,0 +1,172 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qambientlightsensor.h" +#include "qambientlightsensor_p.h" + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +IMPLEMENT_READING(QAmbientLightReading) + +/*! + \class QAmbientLightReading + \ingroup sensors_reading + \inmodule QtSensors + \since 1.0 + + \brief The QAmbientLightReading class represents one reading from the + ambient light sensor. + + \section2 QAmbientLightReading Units + The ambient light sensor returns the intensity of the ambient light + using the pre-defined values found in the QAmbientLightReading::LightLevel + enum. +*/ + +/*! + \enum QAmbientLightReading::LightLevel + + This enum represents the ambient light level. + + \value Undefined The light level is unknown. + \value Dark It is dark. + \value Twilight It is moderately dark. + \value Light It is light (eg. internal lights). + \value Bright It is bright (eg. shade). + \value Sunny It is very bright (eg. direct sunlight). +*/ + +/*! + \property QAmbientLightReading::lightLevel + \brief the ambient light level. + + The value represents the ambient light and comes from QAmbientLightReading::LightLevel. + \sa {QAmbientLightReading Units} +*/ + +QAmbientLightReading::LightLevel QAmbientLightReading::lightLevel() const +{ + return static_cast(d->lightLevel); +} + +/*! + Sets the ambient light level to \a lightLevel. +*/ +void QAmbientLightReading::setLightLevel(QAmbientLightReading::LightLevel lightLevel) +{ + switch (lightLevel) { + case Dark: + case Twilight: + case Light: + case Bright: + case Sunny: + d->lightLevel = lightLevel; + break; + default: + d->lightLevel = Undefined; + break; + } +} + +// ===================================================================== + +/*! + \class QAmbientLightFilter + \ingroup sensors_filter + \inmodule QtSensors + + \brief The QAmbientLightFilter class is a convenience wrapper around QSensorFilter. + + The only difference is that the filter() method features a pointer to QAmbientLightReading + instead of QSensorReading. +*/ + +/*! + \fn QAmbientLightFilter::filter(QAmbientLightReading *reading) + + Called when \a reading changes. Returns false to prevent the reading from propagating. + + \sa QSensorFilter::filter() +*/ + +char const * const QAmbientLightSensor::type("QAmbientLightSensor"); + +/*! + \class QAmbientLightSensor + \ingroup sensors_type + \inmodule QtSensors + + \brief The QAmbientLightSensor class is a convenience wrapper around QSensor. + + The only behavioural difference is that this class sets the type properly. + + This class also features a reading() function that returns a QAmbientLightReading instead of a QSensorReading. + + For details about how the sensor works, see \l QAmbientLightReading. + + \sa QAmbientLightReading +*/ + +/*! + \fn QAmbientLightSensor::QAmbientLightSensor(QObject *parent) + + Construct the sensor as a child of \a parent. +*/ + +/*! + \fn QAmbientLightSensor::~QAmbientLightSensor() + + Destroy the sensor. Stops the sensor if it has not already been stopped. +*/ + +/*! + \fn QAmbientLightSensor::reading() const + + Returns the reading class for this sensor. + + \sa QSensor::reading() +*/ + +#include "moc_qambientlightsensor.cpp" +QTM_END_NAMESPACE +QT_END_NAMESPACE + diff --git a/src/qtmsensors/qambientlightsensor.h b/src/qtmsensors/qambientlightsensor.h new file mode 100644 index 00000000..05381bef --- /dev/null +++ b/src/qtmsensors/qambientlightsensor.h @@ -0,0 +1,94 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTM_QAMBIENTLIGHTSENSOR_H +#define QTM_QAMBIENTLIGHTSENSOR_H + +#include "qsensor.h" + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +class QAmbientLightReadingPrivate; + +class QTM_SENSORS_EXPORT QAmbientLightReading : public QSensorReading +{ + Q_OBJECT + Q_ENUMS(LightLevel) + Q_PROPERTY(LightLevel lightLevel READ lightLevel) + DECLARE_READING(QAmbientLightReading) +public: + enum LightLevel { + Undefined = 0, + Dark, + Twilight, + Light, + Bright, + Sunny + }; + + LightLevel lightLevel() const; + void setLightLevel(LightLevel lightLevel); +}; + +class QTM_SENSORS_EXPORT QAmbientLightFilter : public QSensorFilter +{ +public: + virtual bool filter(QAmbientLightReading *reading) = 0; +private: + bool filter(QSensorReading *reading) { return filter(static_cast(reading)); } +}; + +class QTM_SENSORS_EXPORT QAmbientLightSensor : public QSensor +{ + Q_OBJECT +public: + explicit QAmbientLightSensor(QObject *parent = 0) : QSensor(QAmbientLightSensor::type, parent) {} + virtual ~QAmbientLightSensor() {} + QAmbientLightReading *reading() const { return static_cast(QSensor::reading()); } + static char const * const type; +}; + +QTM_END_NAMESPACE +QT_END_NAMESPACE + +#endif + diff --git a/src/qtmsensors/qambientlightsensor_p.h b/src/qtmsensors/qambientlightsensor_p.h new file mode 100644 index 00000000..85b6272e --- /dev/null +++ b/src/qtmsensors/qambientlightsensor_p.h @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTM_QAMBIENTLIGHTSENSOR_P_H +#define QTM_QAMBIENTLIGHTSENSOR_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +class QAmbientLightReadingPrivate +{ +public: + QAmbientLightReadingPrivate() + : lightLevel(0) + { + } + + int lightLevel; +}; + +QTM_END_NAMESPACE +QT_END_NAMESPACE + +#endif + diff --git a/src/qtmsensors/qcompass.cpp b/src/qtmsensors/qcompass.cpp new file mode 100644 index 00000000..711d8238 --- /dev/null +++ b/src/qtmsensors/qcompass.cpp @@ -0,0 +1,182 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qcompass.h" +#include "qcompass_p.h" + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +IMPLEMENT_READING(QCompassReading) + +/*! + \class QCompassReading + \ingroup sensors_reading + \inmodule QtSensors + \since 1.0 + + \brief The QCompassReading class represents one reading from a + compass. + + \section2 QCompassReading Units + The compass returns the azimuth of the device as degrees from + magnetic north in a clockwise direction based on the top of the UI. + There is also a value to indicate the calibration status of the device. + If the device is not calibrated the azimuth may not be accurate. + + Digital compasses are susceptible to magnetic interference and may need + calibration after being placed near anything that emits a magnetic force. + Accuracy of the compass can be affected by any ferrous materials that are nearby. + + The calibration status of the device is measured as a number from 0 to 1. + A value of 1 is the highest level that the device can support and 0 is + the worst. + \sa {http://wiki.forum.nokia.com/index.php/CS001671_-_Calibrating_the_magnetometer_sensor}{CS001671 - Calibrating the magnetometer sensor} +*/ + +/*! + \property QCompassReading::azimuth + \brief the azimuth of the device. + + Measured in degrees from magnetic north in a clockwise direction based + the top of the UI. + \sa {QCompassReading Units} +*/ + +qreal QCompassReading::azimuth() const +{ + return d->azimuth; +} + +/*! + Sets the \a azimuth of the device. + + \sa {QCompassReading Units} +*/ +void QCompassReading::setAzimuth(qreal azimuth) +{ + d->azimuth = azimuth; +} + +/*! + \property QCompassReading::calibrationLevel + \brief the calibration level of the reading. + + Measured as a value from 0 to 1 with higher values being better. + \sa {QCompassReading Units}, {http://wiki.forum.nokia.com/index.php/CS001671_-_Calibrating_the_magnetometer_sensor}{CS001671 - Calibrating the magnetometer sensor} +*/ + +qreal QCompassReading::calibrationLevel() const +{ + return d->calibrationLevel; +} + +/*! + Sets the calibration level of the reading to \a calibrationLevel. +*/ +void QCompassReading::setCalibrationLevel(qreal calibrationLevel) +{ + d->calibrationLevel = calibrationLevel; +} + +// ===================================================================== + +/*! + \class QCompassFilter + \ingroup sensors_filter + \inmodule QtSensors + + \brief The QCompassFilter class is a convenience wrapper around QSensorFilter. + + The only difference is that the filter() method features a pointer to QCompassReading + instead of QSensorReading. +*/ + +/*! + \fn QCompassFilter::filter(QCompassReading *reading) + + Called when \a reading changes. Returns false to prevent the reading from propagating. + + \sa QSensorFilter::filter() +*/ + +char const * const QCompass::type("QCompass"); + +/*! + \class QCompass + \ingroup sensors_type + \inmodule QtSensors + + \brief The QCompass class is a convenience wrapper around QSensor. + + The only behavioural difference is that this class sets the type properly. + + This class also features a reading() function that returns a QCompassReading instead of a QSensorReading. + + For details about how the sensor works, see \l QCompassReading. + + \sa QCompassReading +*/ + +/*! + \fn QCompass::QCompass(QObject *parent) + + Construct the sensor as a child of \a parent. +*/ + +/*! + \fn QCompass::~QCompass() + + Destroy the sensor. Stops the sensor if it has not already been stopped. +*/ + +/*! + \fn QCompass::reading() const + + Returns the reading class for this sensor. + + \sa QSensor::reading() +*/ + +#include "moc_qcompass.cpp" +QTM_END_NAMESPACE +QT_END_NAMESPACE + diff --git a/src/qtmsensors/qcompass.h b/src/qtmsensors/qcompass.h new file mode 100644 index 00000000..212a8b6d --- /dev/null +++ b/src/qtmsensors/qcompass.h @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTM_QCOMPASS_H +#define QTM_QCOMPASS_H + +#include "qsensor.h" + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +class QCompassReadingPrivate; + +class QTM_SENSORS_EXPORT QCompassReading : public QSensorReading +{ + Q_OBJECT + Q_PROPERTY(qreal azimuth READ azimuth) + Q_PROPERTY(qreal calibrationLevel READ calibrationLevel) + DECLARE_READING(QCompassReading) +public: + qreal azimuth() const; + void setAzimuth(qreal azimuth); + + qreal calibrationLevel() const; + void setCalibrationLevel(qreal calibrationLevel); +}; + +class QTM_SENSORS_EXPORT QCompassFilter : public QSensorFilter +{ +public: + virtual bool filter(QCompassReading *reading) = 0; +private: + bool filter(QSensorReading *reading) { return filter(static_cast(reading)); } +}; + +class QTM_SENSORS_EXPORT QCompass : public QSensor +{ + Q_OBJECT +public: + explicit QCompass(QObject *parent = 0) : QSensor(QCompass::type, parent) {} + virtual ~QCompass() {} + QCompassReading *reading() const { return static_cast(QSensor::reading()); } + static char const * const type; +}; + +QTM_END_NAMESPACE +QT_END_NAMESPACE + +#endif + diff --git a/src/qtmsensors/qcompass_p.h b/src/qtmsensors/qcompass_p.h new file mode 100644 index 00000000..bf069176 --- /dev/null +++ b/src/qtmsensors/qcompass_p.h @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTM_QCOMPASS_P_H +#define QTM_QCOMPASS_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +class QCompassReadingPrivate +{ +public: + QCompassReadingPrivate() + : azimuth(0) + , calibrationLevel(0) + { + } + + qreal azimuth; + qreal calibrationLevel; +}; + +QTM_END_NAMESPACE +QT_END_NAMESPACE + +#endif + diff --git a/src/qtmsensors/qgyroscope.cpp b/src/qtmsensors/qgyroscope.cpp new file mode 100644 index 00000000..c43eb4b0 --- /dev/null +++ b/src/qtmsensors/qgyroscope.cpp @@ -0,0 +1,194 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qgyroscope.h" +#include "qgyroscope_p.h" + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +IMPLEMENT_READING(QGyroscopeReading) + +/*! + \class QGyroscopeReading + \ingroup sensors_reading + \inmodule QtSensors + \since 1.2 + + \brief The QGyroscopeReading class represents one reading from the + gyroscope sensor. + + \section2 QGyroscopeReading Units + + The reading contains 3 values, measured in degrees per second that define + the movement of the device around the x, y and z axes. Unlike QRotationReading, + the values represent the current angular velocity rather than a fixed rotation. + The measurements are in degrees per second. + + \image sensors-coordinates3.jpg +*/ + +/*! + \property QGyroscopeReading::x + \brief the angular velocity around the x axis. + + Measured as degrees per second. + \sa {QGyroscopeReading Units} +*/ + +qreal QGyroscopeReading::x() const +{ + return d->x; +} + +/*! + Sets the angular velocity around the x axis to \a x. +*/ +void QGyroscopeReading::setX(qreal x) +{ + d->x = x; +} + +/*! + \property QGyroscopeReading::y + \brief the angular velocity around the y axis. + + Measured as degrees per second. + \sa {QGyroscopeReading Units} +*/ + +qreal QGyroscopeReading::y() const +{ + return d->y; +} + +/*! + Sets the angular velocity around the y axis to \a y. +*/ +void QGyroscopeReading::setY(qreal y) +{ + d->y = y; +} + +/*! + \property QGyroscopeReading::z + \brief the angular velocity around the z axis. + + Measured as degrees per second. + \sa {QGyroscopeReading Units} +*/ + +qreal QGyroscopeReading::z() const +{ + return d->z; +} + +/*! + Sets the angular velocity around the z axis to \a z. +*/ +void QGyroscopeReading::setZ(qreal z) +{ + d->z = z; +} + +// ===================================================================== + +/*! + \class QGyroscopeFilter + \ingroup sensors_filter + \inmodule QtSensors + + \brief The QGyroscopeFilter class is a convenience wrapper around QSensorFilter. + + The only difference is that the filter() method features a pointer to QGyroscopeReading + instead of QSensorReading. +*/ + +/*! + \fn QGyroscopeFilter::filter(QGyroscopeReading *reading) + + Called when \a reading changes. Returns false to prevent the reading from propagating. + + \sa QSensorFilter::filter() +*/ + +char const * const QGyroscope::type("QGyroscope"); + +/*! + \class QGyroscope + \ingroup sensors_type + \inmodule QtSensors + + \brief The QGyroscope class is a convenience wrapper around QSensor. + + The only behavioural difference is that this class sets the type properly. + + This class also features a reading() function that returns a QGyroscopeReading instead of a QSensorReading. + + For details about how the sensor works, see \l QGyroscopeReading. + + \sa QGyroscopeReading +*/ + +/*! + \fn QGyroscope::QGyroscope(QObject *parent) + + Construct the sensor as a child of \a parent. +*/ + +/*! + \fn QGyroscope::~QGyroscope() + + Destroy the sensor. Stops the sensor if it has not already been stopped. +*/ + +/*! + \fn QGyroscope::reading() const + + Returns the reading class for this sensor. + + \sa QSensor::reading() +*/ + +#include "moc_qgyroscope.cpp" +QTM_END_NAMESPACE +QT_END_NAMESPACE + diff --git a/src/qtmsensors/qgyroscope.h b/src/qtmsensors/qgyroscope.h new file mode 100644 index 00000000..d0683dcd --- /dev/null +++ b/src/qtmsensors/qgyroscope.h @@ -0,0 +1,92 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTM_QGYROSCOPE_H +#define QTM_QGYROSCOPE_H + +#include "qsensor.h" + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +class QGyroscopeReadingPrivate; + +class QTM_SENSORS_EXPORT QGyroscopeReading : public QSensorReading +{ + Q_OBJECT + Q_PROPERTY(qreal x READ x) + Q_PROPERTY(qreal y READ y) + Q_PROPERTY(qreal z READ z) + DECLARE_READING(QGyroscopeReading) +public: + qreal x() const; + void setX(qreal x); + + qreal y() const; + void setY(qreal y); + + qreal z() const; + void setZ(qreal z); +}; + +class QTM_SENSORS_EXPORT QGyroscopeFilter : public QSensorFilter +{ +public: + virtual bool filter(QGyroscopeReading *reading) = 0; +private: + bool filter(QSensorReading *reading) { return filter(static_cast(reading)); } +}; + +class QTM_SENSORS_EXPORT QGyroscope : public QSensor +{ + Q_OBJECT +public: + explicit QGyroscope(QObject *parent = 0) : QSensor(QGyroscope::type, parent) {} + virtual ~QGyroscope() {} + QGyroscopeReading *reading() const { return static_cast(QSensor::reading()); } + static char const * const type; +}; + +QTM_END_NAMESPACE +QT_END_NAMESPACE + +#endif + diff --git a/src/qtmsensors/qgyroscope_p.h b/src/qtmsensors/qgyroscope_p.h new file mode 100644 index 00000000..560909f3 --- /dev/null +++ b/src/qtmsensors/qgyroscope_p.h @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTM_QGYROSCOPE_P_H +#define QTM_QGYROSCOPE_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +class QGyroscopeReadingPrivate +{ +public: + QGyroscopeReadingPrivate() + : x(0) + , y(0) + , z(0) + { + } + + qreal x; + qreal y; + qreal z; +}; + +QTM_END_NAMESPACE +QT_END_NAMESPACE + +#endif + diff --git a/src/qtmsensors/qlightsensor.cpp b/src/qtmsensors/qlightsensor.cpp new file mode 100644 index 00000000..355bbb7b --- /dev/null +++ b/src/qtmsensors/qlightsensor.cpp @@ -0,0 +1,154 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qlightsensor.h" +#include "qlightsensor_p.h" + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +IMPLEMENT_READING(QLightReading) + +/*! + \class QLightReading + \ingroup sensors_reading + + \since 1.2 + + \preliminary + \brief The QLightReading class represents one reading from the + light sensor. + + \section2 QLightReading Units + The light sensor returns the intensity of the light in lux. +*/ + +/*! + \property QLightReading::lux + \brief the light level. + + Measured as lux. + \sa {QLightReading Units} +*/ + +qreal QLightReading::lux() const +{ + return d->lux; +} + +/*! + Sets the light level to \a lux. +*/ +void QLightReading::setLux(qreal lux) +{ + d->lux = lux; +} + +// ===================================================================== + +/*! + \class QLightFilter + \ingroup sensors_filter + + \preliminary + \brief The QLightFilter class is a convenience wrapper around QSensorFilter. + + The only difference is that the filter() method features a pointer to QLightReading + instead of QSensorReading. +*/ + +/*! + \fn QLightFilter::filter(QLightReading *reading) + + Called when \a reading changes. Returns false to prevent the reading from propagating. + + \sa QSensorFilter::filter() +*/ + +char const * const QLightSensor::type("QLightSensor"); + +/*! + \class QLightSensor + \ingroup sensors_type + + \preliminary + \brief The QLightSensor class is a convenience wrapper around QSensor. + + The only behavioural difference is that this class sets the type properly. + + This class also features a reading() function that returns a QLightReading instead of a QSensorReading. + + For details about how the sensor works, see \l QLightReading. + + \sa QLightReading +*/ + +/*! + \fn QLightSensor::QLightSensor(QObject *parent) + + Construct the sensor as a child of \a parent. +*/ + +/*! + \fn QLightSensor::~QLightSensor() + + Destroy the sensor. Stops the sensor if it has not already been stopped. +*/ + +/*! + \fn QLightSensor::reading() const + + Returns the reading class for this sensor. + + \sa QSensor::reading() +*/ + +/*! + \property QLightSensor::fieldOfView + \brief a value indicating the field of view. + + This is an angle that represents the field of view of the sensor. +*/ + +#include "moc_qlightsensor.cpp" +QTM_END_NAMESPACE +QT_END_NAMESPACE + diff --git a/src/qtmsensors/qlightsensor.h b/src/qtmsensors/qlightsensor.h new file mode 100644 index 00000000..34c2e316 --- /dev/null +++ b/src/qtmsensors/qlightsensor.h @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTM_QLIGHTSENSOR_H +#define QTM_QLIGHTSENSOR_H + +#include "qsensor.h" + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +class QLightReadingPrivate; + +class QTM_SENSORS_EXPORT QLightReading : public QSensorReading +{ + Q_OBJECT + Q_PROPERTY(qreal lux READ lux) + DECLARE_READING(QLightReading) +public: + qreal lux() const; + void setLux(qreal lux); +}; + +class QTM_SENSORS_EXPORT QLightFilter : public QSensorFilter +{ +public: + virtual bool filter(QLightReading *reading) = 0; +private: + bool filter(QSensorReading *reading) { return filter(static_cast(reading)); } +}; + +class QTM_SENSORS_EXPORT QLightSensor : public QSensor +{ + Q_OBJECT +#ifdef Q_QDOC + Q_PROPERTY(qreal fieldOfView) +#endif +public: + explicit QLightSensor(QObject *parent = 0) : QSensor(QLightSensor::type, parent) {} + virtual ~QLightSensor() {} + QLightReading *reading() const { return static_cast(QSensor::reading()); } + static char const * const type; +}; + +QTM_END_NAMESPACE +QT_END_NAMESPACE + +#endif + diff --git a/src/qtmsensors/qlightsensor_p.h b/src/qtmsensors/qlightsensor_p.h new file mode 100644 index 00000000..00e447da --- /dev/null +++ b/src/qtmsensors/qlightsensor_p.h @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTM_QLIGHTSENSOR_P_H +#define QTM_QLIGHTSENSOR_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "qsensor_p.h" + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +class QLightReadingPrivate : public QSensorReadingPrivate +{ +public: + QLightReadingPrivate() + : lux(0) + { + } + + qreal lux; +}; + +QTM_END_NAMESPACE +QT_END_NAMESPACE + +#endif + diff --git a/src/qtmsensors/qmagnetometer.cpp b/src/qtmsensors/qmagnetometer.cpp new file mode 100644 index 00000000..9d18c8e6 --- /dev/null +++ b/src/qtmsensors/qmagnetometer.cpp @@ -0,0 +1,245 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qmagnetometer.h" +#include "qmagnetometer_p.h" + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +IMPLEMENT_READING(QMagnetometerReading) + +/*! + \class QMagnetometerReading + \ingroup sensors_reading + \inmodule QtSensors + \since 1.0 + + \brief The QMagnetometerReading class represents one reading from the + magnetometer. + + \section2 QMagnetometerReading Units + The magnetometer returns magnetic flux density values along 3 axes. + The scale of the values is teslas. The axes are arranged as follows. + + \image sensors-coordinates2.jpg + + The magnetometer can report on either raw magnetic flux values or geomagnetic flux values. + By default it returns raw magnetic flux values. The QMagnetometer::returnGeoValues property + must be set to return geomagnetic flux values. + + The primary difference between raw and geomagnetic values is that extra processing + is done to eliminate local magnetic interference from the geomagnetic values so they + represent only the effect of the Earth's magnetic field. This process is not perfect + and the accuracy of each reading may change. + + The image below shows the difference between geomagnetic (on the left) and raw (on the right) + readings for a phone that is being subjected to magnetic interference. + + \image sensors-geo-vs-raw-magnetism.jpg + + The accuracy of each reading is measured as a number from 0 to 1. + A value of 1 is the highest level that the device can support and 0 is + the worst. + \sa {http://wiki.forum.nokia.com/index.php/CS001671_-_Calibrating_the_magnetometer_sensor}{CS001671 - Calibrating the magnetometer sensor} +*/ + +/*! + \property QMagnetometerReading::x + \brief the raw magnetic flux density on the X axis. + + Measured as telsas. + \sa {QMagnetometerReading Units} +*/ + +qreal QMagnetometerReading::x() const +{ + return d->x; +} + +/*! + Sets the raw magnetic flux density on the X axis to \a x. +*/ +void QMagnetometerReading::setX(qreal x) +{ + d->x = x; +} + +/*! + \property QMagnetometerReading::y + \brief the raw magnetic flux density on the Y axis. + + Measured as telsas. + \sa {QMagnetometerReading Units} +*/ + +qreal QMagnetometerReading::y() const +{ + return d->y; +} + +/*! + Sets the raw magnetic flux density on the Y axis to \a y. +*/ +void QMagnetometerReading::setY(qreal y) +{ + d->y = y; +} + +/*! + \property QMagnetometerReading::z + \brief the raw magnetic flux density on the Z axis. + + Measured as telsas. + \sa {QMagnetometerReading Units} +*/ + +qreal QMagnetometerReading::z() const +{ + return d->z; +} + +/*! + Sets the raw magnetic flux density on the Z axis to \a z. +*/ +void QMagnetometerReading::setZ(qreal z) +{ + d->z = z; +} + +/*! + \property QMagnetometerReading::calibrationLevel + \brief the accuracy of the reading. + + Measured as a value from 0 to 1 with higher values being better. + + Note that this only changes when measuring geomagnetic flux density. + Raw magnetic flux readings will always have a value of 1. + \sa {QMagnetometerReading Units}, {http://wiki.forum.nokia.com/index.php/CS001671_-_Calibrating_the_magnetometer_sensor}{CS001671 - Calibrating the magnetometer sensor} +*/ + +qreal QMagnetometerReading::calibrationLevel() const +{ + return d->calibrationLevel; +} + +/*! + Sets the accuracy of the reading to \a calibrationLevel. +*/ +void QMagnetometerReading::setCalibrationLevel(qreal calibrationLevel) +{ + d->calibrationLevel = calibrationLevel; +} + +// ===================================================================== + +/*! + \class QMagnetometerFilter + \ingroup sensors_filter + \inmodule QtSensors + + \brief The QMagnetometerFilter class is a convenience wrapper around QSensorFilter. + + The only difference is that the filter() method features a pointer to QMagnetometerReading + instead of QSensorReading. +*/ + +/*! + \fn QMagnetometerFilter::filter(QMagnetometerReading *reading) + + Called when \a reading changes. Returns false to prevent the reading from propagating. + + \sa QSensorFilter::filter() +*/ + +char const * const QMagnetometer::type("QMagnetometer"); + +/*! + \class QMagnetometer + \ingroup sensors_type + \inmodule QtSensors + + \brief The QMagnetometer class is a convenience wrapper around QSensor. + + The only behavioural difference is that this class sets the type properly. + + This class also features a reading() function that returns a QMagnetometerReading instead of a QSensorReading. + + For details about how the sensor works, see \l QMagnetometerReading. + + \sa QMagnetometerReading +*/ + +/*! + \fn QMagnetometer::QMagnetometer(QObject *parent) + + Construct the sensor as a child of \a parent. +*/ + +/*! + \fn QMagnetometer::~QMagnetometer() + + Destroy the sensor. Stops the sensor if it has not already been stopped. +*/ + +/*! + \fn QMagnetometer::reading() const + + Returns the reading class for this sensor. + + \sa QSensor::reading() +*/ + +/*! + \property QMagnetometer::returnGeoValues + \brief a value indicating if geomagnetic values should be returned. + + Set to true to return geomagnetic flux density. + Set to false (the default) to return raw magnetic flux density. + + Note that you must access this property via QObject::property() and QObject::setProperty(). + The property must be set before calling start(). +*/ + +#include "moc_qmagnetometer.cpp" +QTM_END_NAMESPACE +QT_END_NAMESPACE + diff --git a/src/qtmsensors/qmagnetometer.h b/src/qtmsensors/qmagnetometer.h new file mode 100644 index 00000000..821da3c6 --- /dev/null +++ b/src/qtmsensors/qmagnetometer.h @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTM_QMAGNETOMETER_H +#define QTM_QMAGNETOMETER_H + +#include "qsensor.h" + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +class QMagnetometerReadingPrivate; + +class QTM_SENSORS_EXPORT QMagnetometerReading : public QSensorReading +{ + Q_OBJECT + Q_PROPERTY(qreal x READ x) + Q_PROPERTY(qreal y READ y) + Q_PROPERTY(qreal z READ z) + Q_PROPERTY(qreal calibrationLevel READ calibrationLevel) + DECLARE_READING(QMagnetometerReading) +public: + qreal x() const; + void setX(qreal x); + + qreal y() const; + void setY(qreal y); + + qreal z() const; + void setZ(qreal z); + + qreal calibrationLevel() const; + void setCalibrationLevel(qreal calibrationLevel); +}; + +class QTM_SENSORS_EXPORT QMagnetometerFilter : public QSensorFilter +{ +public: + virtual bool filter(QMagnetometerReading *reading) = 0; +private: + bool filter(QSensorReading *reading) { return filter(static_cast(reading)); } +}; + +class QTM_SENSORS_EXPORT QMagnetometer : public QSensor +{ + Q_OBJECT +#ifdef Q_QDOC + Q_PROPERTY(bool returnGeoValues) +#endif +public: + explicit QMagnetometer(QObject *parent = 0) : QSensor(QMagnetometer::type, parent) {} + virtual ~QMagnetometer() {} + QMagnetometerReading *reading() const { return static_cast(QSensor::reading()); } + static char const * const type; +}; + +QTM_END_NAMESPACE +QT_END_NAMESPACE + +#endif + diff --git a/src/qtmsensors/qmagnetometer_p.h b/src/qtmsensors/qmagnetometer_p.h new file mode 100644 index 00000000..89938a19 --- /dev/null +++ b/src/qtmsensors/qmagnetometer_p.h @@ -0,0 +1,80 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTM_QMAGNETOMETER_P_H +#define QTM_QMAGNETOMETER_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +class QMagnetometerReadingPrivate +{ +public: + QMagnetometerReadingPrivate() + : x(0) + , y(0) + , z(0) + , calibrationLevel(0) + { + } + + qreal x; + qreal y; + qreal z; + qreal calibrationLevel; +}; + +QTM_END_NAMESPACE +QT_END_NAMESPACE + +#endif + diff --git a/src/qtmsensors/qorientationsensor.cpp b/src/qtmsensors/qorientationsensor.cpp new file mode 100644 index 00000000..6381b029 --- /dev/null +++ b/src/qtmsensors/qorientationsensor.cpp @@ -0,0 +1,195 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qorientationsensor.h" +#include "qorientationsensor_p.h" + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +IMPLEMENT_READING(QOrientationReading) + +/*! + \class QOrientationReading + \ingroup sensors_reading + \inmodule QtSensors + \since 1.0 + + \brief The QOrientationReading class represents one reading from the + orientation sensor. + + The orientation sensor reports the orientation of the device. As it operates below + the UI level it does not report on or even know how the UI is rotated. Most importantly + this means that this sensor cannot be used to detect if a device is in portrait or + landscape mode. + + This sensor is useful to detect that a particular side of the device is pointing up. + + \section2 QOrientationReading Units + The orientation sensor returns the orientation of the device using + the pre-defined values found in the QOrientationReading::Orientation + enum. +*/ + +/*! + \enum QOrientationReading::Orientation + + This enum represents the orientation of the device. + + To explain the meaning of each value it is helpful to refer to the following diagram. + + \image sensors-sides.jpg + + The orientations are shown here in order: TopUp, TopDown, LeftUp, RightUp, FaceUp, FaceDown. + + \image sensors-orientation.jpg + + \value Undefined The orientation is unknown. + \value TopUp The Top edge of the device is pointing up. + \value TopDown The Top edge of the device is pointing down. + \value LeftUp The Left edge of the device is pointing up. + \value RightUp The Right edge of the device is pointing up. + \value FaceUp The Face of the device is pointing up. + \value FaceDown The Face of the device is pointing down. + + It should be noted that the orientation sensor reports the orientation of the device + and not the UI. The orientation of the device will not change just because the UI is + rotated. This means this sensor cannot be used to detect if a device is in portrait + or landscape mode. +*/ + +/*! + \property QOrientationReading::orientation + \brief the orientation of the device. + + The unit is an enumeration describing the orientation of the device. + + \sa {QOrientationReading Units} +*/ + +QOrientationReading::Orientation QOrientationReading::orientation() const +{ + return static_cast(d->orientation); +} + +/*! + Sets the \a orientation for the reading. +*/ +void QOrientationReading::setOrientation(QOrientationReading::Orientation orientation) +{ + switch (orientation) { + case TopUp: + case TopDown: + case LeftUp: + case RightUp: + case FaceUp: + case FaceDown: + d->orientation = orientation; + break; + default: + d->orientation = Undefined; + break; + } +} + +// ===================================================================== + +/*! + \class QOrientationFilter + \ingroup sensors_filter + \inmodule QtSensors + + \brief The QOrientationFilter class is a convenience wrapper around QSensorFilter. + + The only difference is that the filter() method features a pointer to QOrientationReading + instead of QSensorReading. +*/ + +/*! + \fn QOrientationFilter::filter(QOrientationReading *reading) + + Called when \a reading changes. Returns false to prevent the reading from propagating. + + \sa QSensorFilter::filter() +*/ + +char const * const QOrientationSensor::type("QOrientationSensor"); + +/*! + \class QOrientationSensor + \ingroup sensors_type + \inmodule QtSensors + + \brief The QOrientationSensor class is a convenience wrapper around QSensor. + + The only behavioural difference is that this class sets the type properly. + + This class also features a reading() function that returns a QOrientationReading instead of a QSensorReading. + + For details about how the sensor works, see \l QOrientationReading. + + \sa QOrientationReading +*/ + +/*! + \fn QOrientationSensor::QOrientationSensor(QObject *parent) + + Construct the sensor as a child of \a parent. +*/ + +/*! + \fn QOrientationSensor::~QOrientationSensor() + + Destroy the sensor. Stops the sensor if it has not already been stopped. +*/ + +/*! + \fn QOrientationSensor::reading() const + + Returns the reading class for this sensor. + + \sa QSensor::reading() +*/ + +#include "moc_qorientationsensor.cpp" +QTM_END_NAMESPACE +QT_END_NAMESPACE + diff --git a/src/qtmsensors/qorientationsensor.h b/src/qtmsensors/qorientationsensor.h new file mode 100644 index 00000000..c3fc779a --- /dev/null +++ b/src/qtmsensors/qorientationsensor.h @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTM_QORIENTATIONSENSOR_H +#define QTM_QORIENTATIONSENSOR_H + +#include "qsensor.h" + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +class QOrientationReadingPrivate; + +class QTM_SENSORS_EXPORT QOrientationReading : public QSensorReading +{ + Q_OBJECT + Q_ENUMS(Orientation) + Q_PROPERTY(Orientation orientation READ orientation) + DECLARE_READING(QOrientationReading) +public: + enum Orientation { + Undefined = 0, + TopUp, + TopDown, + LeftUp, + RightUp, + FaceUp, + FaceDown, + }; + + Orientation orientation() const; + void setOrientation(Orientation orientation); +}; + +class QTM_SENSORS_EXPORT QOrientationFilter : public QSensorFilter +{ +public: + virtual bool filter(QOrientationReading *reading) = 0; +private: + bool filter(QSensorReading *reading) { return filter(static_cast(reading)); } +}; + +class QTM_SENSORS_EXPORT QOrientationSensor : public QSensor +{ + Q_OBJECT +public: + explicit QOrientationSensor(QObject *parent = 0) : QSensor(QOrientationSensor::type, parent) {} + virtual ~QOrientationSensor() {} + QOrientationReading *reading() const { return static_cast(QSensor::reading()); } + static char const * const type; +}; + +QTM_END_NAMESPACE +QT_END_NAMESPACE + +#endif + diff --git a/src/qtmsensors/qorientationsensor_p.h b/src/qtmsensors/qorientationsensor_p.h new file mode 100644 index 00000000..bace7ac4 --- /dev/null +++ b/src/qtmsensors/qorientationsensor_p.h @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTM_QORIENTATIONSENSOR_P_H +#define QTM_QORIENTATIONSENSOR_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +class QOrientationReadingPrivate +{ +public: + QOrientationReadingPrivate() + : orientation(0) + { + } + + int orientation; +}; + +QTM_END_NAMESPACE +QT_END_NAMESPACE + +#endif + diff --git a/src/qtmsensors/qproximitysensor.cpp b/src/qtmsensors/qproximitysensor.cpp new file mode 100644 index 00000000..0705a8aa --- /dev/null +++ b/src/qtmsensors/qproximitysensor.cpp @@ -0,0 +1,151 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qproximitysensor.h" +#include "qproximitysensor_p.h" + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +IMPLEMENT_READING(QProximityReading) + +/*! + \class QProximityReading + \ingroup sensors_reading + \inmodule QtSensors + \since 1.0 + + \brief The QProximityReading class represents one reading from the + proximity sensor. + + \target QProximityReading_Units + The proximity sensor can only indicate if an object is close or not. + + The distance at which an object is considered close is device-specific. This + distance may be available in the QSensor::outputRanges property. +*/ + +/*! + \property QProximityReading::close + \brief a value indicating if something is close. + + Set to true if something is close. + Set to false is nothing is close. + + \sa QProximityReading_Units +*/ + +bool QProximityReading::close() const +{ + return d->close; +} + +/*! + Sets the close value to \a close. +*/ +void QProximityReading::setClose(bool close) +{ + d->close = close; +} + +// ===================================================================== + +/*! + \class QProximityFilter + \ingroup sensors_filter + \inmodule QtSensors + + \brief The QProximityFilter class is a convenience wrapper around QSensorFilter. + + The only difference is that the filter() method features a pointer to QProximityReading + instead of QSensorReading. +*/ + +/*! + \fn QProximityFilter::filter(QProximityReading *reading) + + Called when \a reading changes. Returns false to prevent the reading from propagating. + + \sa QSensorFilter::filter() +*/ + +char const * const QProximitySensor::type("QProximitySensor"); + +/*! + \class QProximitySensor + \ingroup sensors_type + \inmodule QtSensors + + \brief The QProximitySensor class is a convenience wrapper around QSensor. + + The only behavioural difference is that this class sets the type properly. + + This class also features a reading() function that returns a QProximityReading instead of a QSensorReading. + + For details about how the sensor works, see \l QProximityReading. + + \sa QProximityReading +*/ + +/*! + \fn QProximitySensor::QProximitySensor(QObject *parent) + + Construct the sensor as a child of \a parent. +*/ + +/*! + \fn QProximitySensor::~QProximitySensor() + + Destroy the sensor. Stops the sensor if it has not already been stopped. +*/ + +/*! + \fn QProximitySensor::reading() const + + Returns the reading class for this sensor. + + \sa QSensor::reading() +*/ + +#include "moc_qproximitysensor.cpp" +QTM_END_NAMESPACE +QT_END_NAMESPACE + diff --git a/src/qtmsensors/qproximitysensor.h b/src/qtmsensors/qproximitysensor.h new file mode 100644 index 00000000..86e6bfdb --- /dev/null +++ b/src/qtmsensors/qproximitysensor.h @@ -0,0 +1,84 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTM_QPROXIMITYSENSOR_H +#define QTM_QPROXIMITYSENSOR_H + +#include "qsensor.h" + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +class QProximityReadingPrivate; + +class QTM_SENSORS_EXPORT QProximityReading : public QSensorReading +{ + Q_OBJECT + Q_PROPERTY(bool close READ close) + DECLARE_READING(QProximityReading) +public: + bool close() const; + void setClose(bool close); +}; + +class QTM_SENSORS_EXPORT QProximityFilter : public QSensorFilter +{ +public: + virtual bool filter(QProximityReading *reading) = 0; +private: + bool filter(QSensorReading *reading) { return filter(static_cast(reading)); } +}; + +class QTM_SENSORS_EXPORT QProximitySensor : public QSensor +{ + Q_OBJECT +public: + explicit QProximitySensor(QObject *parent = 0) : QSensor(QProximitySensor::type, parent) {} + virtual ~QProximitySensor() {} + QProximityReading *reading() const { return static_cast(QSensor::reading()); } + static char const * const type; +}; + +QTM_END_NAMESPACE +QT_END_NAMESPACE + +#endif + diff --git a/src/qtmsensors/qproximitysensor_p.h b/src/qtmsensors/qproximitysensor_p.h new file mode 100644 index 00000000..6cb031b2 --- /dev/null +++ b/src/qtmsensors/qproximitysensor_p.h @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTM_QPROXIMITYSENSOR_P_H +#define QTM_QPROXIMITYSENSOR_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +class QProximityReadingPrivate +{ +public: + QProximityReadingPrivate() + : close(false) + { + } + + bool close; +}; + +QTM_END_NAMESPACE +QT_END_NAMESPACE + +#endif + diff --git a/src/qtmsensors/qrotationsensor.cpp b/src/qtmsensors/qrotationsensor.cpp new file mode 100644 index 00000000..eba121f8 --- /dev/null +++ b/src/qtmsensors/qrotationsensor.cpp @@ -0,0 +1,240 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qrotationsensor.h" +#include "qrotationsensor_p.h" + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +IMPLEMENT_READING(QRotationReading) + +/*! + \class QRotationReading + \ingroup sensors_reading + \inmodule QtSensors + \since 1.0 + + \brief The QRotationReading class represents one reading from the + rotation sensor. + + \section2 QRotationReading Units + + The rotation reading contains 3 angles, measured in degrees that define + the orientation of the device in three-dimensional space. These angles + are similar to yaw, pitch and roll but are defined using only right hand + rotation with axes as defined by the right hand cartesian coordinate system. + + \image sensors-rotation.jpg + + The three angles are applied to the device in the following order. + + \list + \o Right-handed rotation z (-180, 180]. Starting from the y-axis and + incrementing in the counter-clockwise direction. + \o Right-handed rotation x [-90, 90]. Starting from the new + (once-rotated) y-axis and incrementing towards the z-axis. + \o Right-handed rotation y (-180, 180]. Starting from the new + (twice-rotated) z-axis and incrementing towards the x-axis. + \endlist + + Here is a visualization showing the order in which angles are applied. + + \image sensors-rotation-anim.gif + + The 0 point for the z angle is defined as a fixed, external entity and + is device-specific. While magnetic North is typically used as this + reference point it may not be. Do not attempt to compare values + for the z angle between devices or even on the same device if it has + moved a significant distance. + + If the device cannot detect a fixed, external entity the z angle will + always be 0 and the QRotationSensor::hasZ property will be set to false. + + The 0 point for the x and y angles are defined as when the x and y axes + of the device are oriented towards the horizon. Here is an example of + how the x value will change with device movement. + + \image sensors-rotation2.jpg + + Here is an example of how the y value will change with device movement. + + \image sensors-rotation3.jpg + + Note that when x is 90 or -90, values for z and y achieve rotation around + the same axis (due to the order of operations). In this case the y + rotation will be 0. +*/ + +/*! + \property QRotationReading::x + \brief the rotation around the x axis. + + Measured as degrees. + \sa {QRotationReading Units} +*/ + +qreal QRotationReading::x() const +{ + return d->x; +} + +/*! + Sets the rotation around the x axis to \a x. +*/ +void QRotationReading::setX(qreal x) +{ + d->x = x; +} + +/*! + \property QRotationReading::y + \brief the rotation around the y axis. + + Measured as degrees. + \sa {QRotationReading Units} +*/ + +qreal QRotationReading::y() const +{ + return d->y; +} + +/*! + Sets the rotation around the y axis to \a y. +*/ +void QRotationReading::setY(qreal y) +{ + d->y = y; +} + +/*! + \property QRotationReading::z + \brief the rotation around the z axis. + + Measured as degrees. + \sa {QRotationReading Units} +*/ + +qreal QRotationReading::z() const +{ + return d->z; +} + +/*! + Sets the rotation around the z axis to \a z. +*/ +void QRotationReading::setZ(qreal z) +{ + d->z = z; +} + +// ===================================================================== + +/*! + \class QRotationFilter + \ingroup sensors_filter + \inmodule QtSensors + + \brief The QRotationFilter class is a convenience wrapper around QSensorFilter. + + The only difference is that the filter() method features a pointer to QRotationReading + instead of QSensorReading. +*/ + +/*! + \fn QRotationFilter::filter(QRotationReading *reading) + + Called when \a reading changes. Returns false to prevent the reading from propagating. + + \sa QSensorFilter::filter() +*/ + +char const * const QRotationSensor::type("QRotationSensor"); + +/*! + \class QRotationSensor + \ingroup sensors_type + \inmodule QtSensors + + \brief The QRotationSensor class is a convenience wrapper around QSensor. + + The only behavioural difference is that this class sets the type properly. + + This class also features a reading() function that returns a QRotationReading instead of a QSensorReading. + + For details about how the sensor works, see \l QRotationReading. + + \sa QRotationReading +*/ + +/*! + \fn QRotationSensor::QRotationSensor(QObject *parent) + + Construct the sensor as a child of \a parent. +*/ + +/*! + \fn QRotationSensor::~QRotationSensor() + + Destroy the sensor. Stops the sensor if it has not already been stopped. +*/ + +/*! + \fn QRotationSensor::reading() const + + Returns the reading class for this sensor. + + \sa QSensor::reading() +*/ + +/*! + \property QRotationSensor::hasZ + \brief a value indicating if the z angle is available. + + Returns true if z is available. + Returns false if z is not available. +*/ + +#include "moc_qrotationsensor.cpp" +QTM_END_NAMESPACE +QT_END_NAMESPACE + diff --git a/src/qtmsensors/qrotationsensor.h b/src/qtmsensors/qrotationsensor.h new file mode 100644 index 00000000..c89f2b2c --- /dev/null +++ b/src/qtmsensors/qrotationsensor.h @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTM_QROTATIONSENSOR_H +#define QTM_QROTATIONSENSOR_H + +#include "qsensor.h" + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +class QRotationReadingPrivate; + +class QTM_SENSORS_EXPORT QRotationReading : public QSensorReading +{ + Q_OBJECT + Q_PROPERTY(qreal x READ x) + Q_PROPERTY(qreal y READ y) + Q_PROPERTY(qreal z READ z) + DECLARE_READING(QRotationReading) +public: + qreal x() const; + void setX(qreal x); + + qreal y() const; + void setY(qreal y); + + qreal z() const; + void setZ(qreal z); +}; + +class QTM_SENSORS_EXPORT QRotationFilter : public QSensorFilter +{ +public: + virtual bool filter(QRotationReading *reading) = 0; +private: + bool filter(QSensorReading *reading) { return filter(static_cast(reading)); } +}; + +class QTM_SENSORS_EXPORT QRotationSensor : public QSensor +{ + Q_OBJECT +#ifdef Q_QDOC + Q_PROPERTY(bool hasZ) +#endif +public: + explicit QRotationSensor(QObject *parent = 0) : QSensor(QRotationSensor::type, parent) {} + virtual ~QRotationSensor() {} + QRotationReading *reading() const { return static_cast(QSensor::reading()); } + static char const * const type; +}; + +QTM_END_NAMESPACE +QT_END_NAMESPACE + +#endif + diff --git a/src/qtmsensors/qrotationsensor_p.h b/src/qtmsensors/qrotationsensor_p.h new file mode 100644 index 00000000..23b9dcc8 --- /dev/null +++ b/src/qtmsensors/qrotationsensor_p.h @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTM_QROTATIONSENSOR_P_H +#define QTM_QROTATIONSENSOR_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +class QRotationReadingPrivate +{ +public: + QRotationReadingPrivate() + : x(0) + , y(0) + , z(0) + { + } + + qreal x; + qreal y; + qreal z; +}; + +QTM_END_NAMESPACE +QT_END_NAMESPACE + +#endif + diff --git a/src/qtmsensors/qsensor.cpp b/src/qtmsensors/qsensor.cpp new file mode 100644 index 00000000..acdaec2f --- /dev/null +++ b/src/qtmsensors/qsensor.cpp @@ -0,0 +1,1017 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qsensor.h" +#include "qsensor_p.h" +#include +#include +#include +#include "qsensorbackend_p.h" +#include "qsensormanager_p.h" +#include "backendwrapper_p.h" + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +/*! + \typedef qrange + \relates QSensor + + This type is defined as a QPair. + + \code + typedef QPair qrange; + \endcode + + \sa QPair, qrangelist, QSensor::availableDataRates +*/ + +/*! + \typedef qrangelist + \relates QSensor + + This type is defined as a list of qrange values. + + \code + typedef QList qrangelist; + \endcode + + \sa QList, qrange, QSensor::availableDataRates +*/ + +/*! + \class qoutputrange + \relates QSensor + \brief The qoutputrange class holds the specifics of an output range. + + The class is defined as a simple struct. + + \code + struct qoutputrange + { + qreal maximum; + qreal minimum; + qreal accuracy; + }; + \endcode + + Each output range specifies a minimum and maximum value as well as an accuracy value. + The accuracy value represents the resolution of the sensor. It is the smallest change + the sensor can detect and is expressed using the same units as the minimum and maximum. + + Sensors must often trade off range for accuracy. To allow the user to determine which of + these are more important the sensor may offer several output ranges. One output + range may have reduced minimum and maximum values and increased sensitivity. Another output + range may have higher minimum and maximum values with reduced sensitivity. Note that higher + sensitivities will be represented by smaller accuracy values. + + An example of this tradeoff can be seen by examining the LIS302DL accelerometer. It has only + 256 possible values to report with. These values are scaled so that they can represent either + -2G to +2G (with an accuracy value of 0.015G) or -8G to +8G (with an accuracy value of 0.06G). + + \sa qoutputrangelist, QSensor::outputRanges +*/ + +/*! + \variable qoutputrange::maximum + + This is the maximum value for this output range. + The units are defined by the sensor. +*/ + +/*! + \variable qoutputrange::minimum + + This is the minimum value for this output range. + The units are defined by the sensor. +*/ + +/*! + \variable qoutputrange::accuracy + + The accuracy value represents the resolution of the sensor. It is the smallest change + the sensor can detect and is expressed using the same units as the minimum and maximum. +*/ + +/*! + \typedef qoutputrangelist + \relates QSensor + + This type is defined as a list of qoutputrange values. + + \code + typedef QList qoutputrangelist; + \endcode + + \sa QList, qoutputrange, QSensor::outputRanges +*/ + +#if 0 +// A bit of a hack to call qRegisterMetaType when the library is loaded. +static int qrange_id = qRegisterMetaType("qrange"); +static int qrangelist_id = qRegisterMetaType("qrangelist"); +static int qoutputrangelist_id = qRegisterMetaType("qoutputrangelist"); +#endif + +// ===================================================================== + +/*! + \class QSensor + \ingroup sensors_main + \inmodule QtSensors + + \brief The QSensor class represents a single hardware sensor. + + The life cycle of a sensor is typically: + + \list + \o Create a sub-class of QSensor on the stack or heap. + \o Setup as required by the application. + \o Start receiving values. + \o Sensor data is used by the application. + \o Stop receiving values. + \endlist + + The sensor data is delivered via QSensorData and its sub-classes. + + \sa QSensorReading +*/ + +/*! + Construct the \a type sensor as a child of \a parent. +*/ +QSensor::QSensor(const QByteArray &type, QObject *parent) + : QObject(parent) + , d(new QSensorPrivate) +{ + d->type = type; + registerInstance(); // so the availableSensorsChanged() signal works +} + +/*! + Destroy the sensor. Stops the sensor if it has not already been stopped. +*/ +QSensor::~QSensor() +{ + stop(); + Q_FOREACH (QSensorFilter *filter, d->filters) + filter->setSensor(0); + delete d->backend; + d->backend = 0; + // owned by the backend + d->device_reading = 0; + d->filter_reading = 0; + d->cache_reading = 0; +} + +/*! + \property QSensor::connectedToBackend + \brief a value indicating if the sensor has connected to a backend. + + A sensor that has not been connected to a backend cannot do anything useful. + + Call the connectToBackend() method to force the sensor to connect to a backend + immediately. This is automatically called if you call start() so you only need + to do this if you need access to sensor properties (ie. to poll the sensor's + meta-data before you use it). +*/ + +bool QSensor::isConnectedToBackend() const +{ + return (d->backend != 0); +} + +/*! + \property QSensor::sensorid + \brief the backend identifier for the sensor. + + Note that the identifier is filled out automatically + when the sensor is connected to a backend. If you want + to connect a specific backend, you should call + setIdentifier() before connectToBackend(). +*/ + +QByteArray QSensor::identifier() const +{ + return d->identifier; +} + +void QSensor::setIdentifier(const QByteArray &identifier) +{ + if (isConnectedToBackend()) { + qWarning() << "ERROR: Cannot call QSensor::setIdentifier while connected to a backend!"; + return; + } + d->identifier = identifier; +} + +/*! + \property QSensor::type + \brief the type of the sensor. +*/ + +QByteArray QSensor::type() const +{ + return d->type; +} + +/*! + Try to connect to a sensor backend. + + Returns true if a suitable backend could be found, false otherwise. + + The type must be set before calling this method if you are using QSensor directly. + + \sa isConnectedToBackend() +*/ +bool QSensor::connectToBackend() +{ + if (isConnectedToBackend()) + return true; + + d->backend = QSensorManager::createBackend(this); + + // Reset the properties to their default values and re-set them now so + // that the logic we've put into the setters gets called. + if (d->dataRate != 0) { + int tmp = d->dataRate; + d->dataRate = 0; + setDataRate(tmp); + } + if (d->outputRange != -1) { + int tmp = d->outputRange; + d->outputRange = -1; + setOutputRange(tmp); + } + + QSensorWrapper *wrapper = qobject_cast(d->backend); + if (wrapper) { + if (d->pendingProperties.count()) { + foreach (const PendingProperty &pair, d->pendingProperties) { + setProperty(pair.first.toLocal8Bit().constData(), pair.second); + } + d->pendingProperties.clear(); + } + bool ok = wrapper->_connect(); + Q_ASSERT(ok); + } + + return isConnectedToBackend(); +} + +/*! + \property QSensor::busy + \brief a value to indicate if the sensor is busy. + + Some sensors may be on the system but unavailable for use. + This function will return true if the sensor is busy. You + will not be able to start() the sensor. + + Note that this function does not return true if you + are using the sensor, only if another process is using + the sensor. + + \sa busyChanged() +*/ + +bool QSensor::isBusy() const +{ + return d->busy; +} + +/*! + \fn QSensor::busyChanged() + + This signal is emitted when the sensor is no longer busy. + This can be used to grab a sensor when it becomes available. + + \code + sensor.start(); + if (sensor.isBusy()) { + // need to wait for busyChanged signal and try again + } + \endcode +*/ + +/*! + \property QSensor::active + \brief a value to indicate if the sensor is active. + + This is true if the sensor is active (returning values). This is false otherwise. + + Note that setting this value to true will not have an immediate effect. Instead, + the sensor will be started once the event loop has been reached. +*/ +void QSensor::setActive(bool active) +{ + if (active == isActive()) + return; + + if (active) + QTimer::singleShot(0, this, SLOT(start())); // delay ensures all properties have been set if using QML + else + stop(); +} + +bool QSensor::isActive() const +{ + return d->active; +} + +/*! + \property QSensor::availableDataRates + \brief the data rates that the sensor supports. + + This is a list of the data rates that the sensor supports. + Measured in Hertz. + + Entries in the list can represent discrete rates or a + continuous range of rates. + A discrete rate is noted by having both values the same. + + See the sensor_explorer example for an example of how to interpret and use + this information. + + Note that this information is not mandatory as not all sensors have a rate at which + they run. In such cases, the list will be empty. + + \sa QSensor::dataRate, qrangelist +*/ + +qrangelist QSensor::availableDataRates() const +{ + return d->availableDataRates; +} + +/*! + \property QSensor::dataRate + \brief the data rate that the sensor should be run at. + + Measured in Hertz. + + The data rate is the maximum frequency at which the sensor can detect changes. + + Setting this property is not portable and can cause conflicts with other + applications. Check with the sensor backend and platform documentation for + any policy regarding multiple applications requesting a data rate. + + The default value (0) means that the app does not care what the data rate is. + Applications should consider using a timer-based poll of the current value or + ensure that the code that processes values can run very quickly as the platform + may provide updates hundreds of times each second. + + This should be set before calling start() because the sensor may not + notice changes to this value while it is running. + + Note that there is no mechanism to determine the current data rate in use by the + platform. + + \sa QSensor::availableDataRates +*/ + +int QSensor::dataRate() const +{ + return d->dataRate; +} + +void QSensor::setDataRate(int rate) +{ + if (rate == 0 || !isConnectedToBackend()) { + d->dataRate = rate; + return; + } + bool warn = true; + Q_FOREACH (const qrange &range, d->availableDataRates) { + if (rate >= range.first && rate <= range.second) { + warn = false; + d->dataRate = rate; + break; + } + } + if (warn) { + qWarning() << "setDataRate:" << rate << "is not supported by the sensor."; + } +} + +/*! + Start retrieving values from the sensor. + Returns true if the sensor was started, false otherwise. + + The sensor may fail to start for several reasons. + + Once an application has started a sensor it must wait until the sensor receives a + new value before it can query the sensor's values. This is due to how the sensor + receives values from the system. Sensors do not (in general) poll for new values, + rather new values are pushed to the sensors as they happen. + + For example, this code will not work as intended. + + \badcode + sensor->start(); + sensor->reading()->x(); // no data available + \endcode + + To work correctly, the code that accesses the reading should ensure the + readingChanged() signal has been emitted. + + \code + connect(sensor, SIGNAL(readingChanged()), this, SLOT(checkReading())); + sensor->start(); + } + void MyClass::checkReading() { + sensor->reading()->x(); + \endcode + + \sa QSensor::busy +*/ +bool QSensor::start() +{ + if (isActive()) + return true; + if (!connectToBackend()) + return false; + // Set these flags to their defaults + d->active = true; + d->busy = false; + // Backend will update the flags appropriately + d->backend->start(); + Q_EMIT activeChanged(); + return isActive(); +} + +/*! + Stop retrieving values from the sensor. + + This releases the sensor so that other processes can use it. + + \sa QSensor::busy +*/ +void QSensor::stop() +{ + if (!isConnectedToBackend() || !isActive()) + return; + d->active = false; + d->backend->stop(); + Q_EMIT activeChanged(); +} + +/*! + \property QSensor::reading + \brief the reading class. + + The reading class provides access to sensor readings. The reading object + is a volatile cache of the most recent sensor reading that has been received + so the application should process readings immediately or save the values + somewhere for later processing. + + Note that this will return 0 until a sensor backend is connected to a backend. + + Also note that readings are not immediately available after start() is called. + Applications must wait for the readingChanged() signal to be emitted. + + \sa isConnectedToBackend(), start() +*/ + +QSensorReading *QSensor::reading() const +{ + return d->cache_reading; +} + +/*! + Add a \a filter to the sensor. + + The sensor does not take ownership of the filter. + QSensorFilter will inform the sensor if it is destroyed. + + \sa QSensorFilter +*/ +void QSensor::addFilter(QSensorFilter *filter) +{ + if (!filter) { + qWarning() << "addFilter: passed a null filter!"; + return; + } + filter->setSensor(this); + d->filters << filter; +} + +/*! + Remove \a filter from the sensor. + + \sa QSensorFilter +*/ +void QSensor::removeFilter(QSensorFilter *filter) +{ + if (!filter) { + qWarning() << "removeFilter: passed a null filter!"; + return; + } + d->filters.removeOne(filter); + filter->setSensor(0); +} + +/*! + Returns the filters currently attached to the sensor. + + \sa QSensorFilter +*/ +QList QSensor::filters() const +{ + return d->filters; +} + +/*! + \fn QSensor::d_func() const + \internal +*/ + +/*! + \fn QSensor::readingChanged() + + This signal is emitted when a new sensor reading is received. + + The sensor reading can be found in the QSensor::reading property. Note that the + reading object is a volatile cache of the most recent sensor reading that has + been received so the application should process the reading immediately or + save the values somewhere for later processing. + + Before this signal has been emitted for the first time, the reading object will + have uninitialized data. + + \sa start() +*/ + +/*! + \fn QSensor::activeChanged() + + This signal is emitted when the QSensor::active property has changed. + + \sa QSensor::active +*/ + +/*! + \property QSensor::outputRanges + \brief a list of output ranges the sensor supports. + + A sensor may have more than one output range. Typically this is done + to give a greater measurement range at the cost of lowering accuracy. + + Note that this information is not mandatory. This information is typically only + available for sensors that have selectable output ranges (such as typical + accelerometers). + + \sa QSensor::outputRange, qoutputrangelist +*/ + +qoutputrangelist QSensor::outputRanges() const +{ + return d->outputRanges; +} + +/*! + \property QSensor::outputRange + \brief the output range in use by the sensor. + + This value represents the index in the QSensor::outputRanges list to use. + + Setting this property is not portable and can cause conflicts with other + applications. Check with the sensor backend and platform documentation for + any policy regarding multiple applications requesting an output range. + + The default value (-1) means that the app does not care what the output range is. + + Note that there is no mechanism to determine the current output range in use by the + platform. + + \sa QSensor::outputRanges +*/ + +int QSensor::outputRange() const +{ + return d->outputRange; +} + +void QSensor::setOutputRange(int index) +{ + if (index == -1 || !isConnectedToBackend()) { + d->outputRange = index; + return; + } + bool warn = true; + if (index >= 0 && index < d->outputRanges.count()) { + warn = false; + d->outputRange = index; + } + if (warn) { + qWarning() << "setOutputRange:" << index << "is not supported by the sensor."; + } +} + +/*! + \property QSensor::description + \brief a descriptive string for the sensor. +*/ + +QString QSensor::description() const +{ + return d->description; +} + +/*! + \property QSensor::error + \brief the last error code set on the sensor. + + Note that error codes are sensor-specific. +*/ + +int QSensor::error() const +{ + return d->error; +} + +/*! + \fn QSensor::sensorError(int error) + + This signal is emitted when an \a error code is set on the sensor. + Note that some errors will cause the sensor to stop working. + You should call isActive() to determine if the sensor is still running. +*/ + +/*! + \fn QSensor::availableSensorsChanged() + + This signal is emitted when the list of available sensors has changed. + The sensors available to a program will not generally change over time + however some of the avilable sensors may represent hardware that is not + permanently connected. For example, a game controller that is connected + via bluetooth would become available when it was on and would become + unavailable when it was off. + + \sa QSensor::sensorTypes(), QSensor::sensorsForType() +*/ + +/*! + \property QSensor::maxBufferSize + + The property holds the maximum buffer size. + + Note that this may be undefined, in which case the sensor does not support any form of buffering. + + \sa QSensor::bufferSize, QSensor::efficientBufferSize +*/ + +/*! + \property QSensor::efficientBufferSize + + The property holds the most efficient buffer size. Normally this is 1 (which means + no particular size is most efficient). Some sensor drivers have a FIFO buffer which + makes it more efficient to deliver the FIFO's size worth of readings at one time. + + Note that this may be undefined, in which case the sensor does not support any form of buffering. + + \sa QSensor::bufferSize, QSensor::maxBufferSize +*/ + +/*! + \property QSensor::bufferSize + + This property holds the size of the buffer. By default (and if the property + is left undefined), the buffer size is 1, which means no buffering. + If the maximum buffer size is 1 (or undefined), then buffering is not supported + by the sensor. + + Setting bufferSize greater than maxBufferSize will cause maxBufferSize to be used. + + Buffering is turned on when bufferSize is greater than 1. The sensor will collect + the requested number of samples and deliver them all to the application at one time. + They will be delivered to the application as a burst of changed readings so it is + particularly important that the application processes each reading immediately or + saves the values somewhere else. + + If stop() is called when buffering is on-going, the partial buffer is not delivered. + + When the sensor is started with buffering option, values are collected from that + moment onwards. There is no pre-existing buffer that can be utilized. + + The buffer size can only be changed while the sensor is not active. + + \sa QSensor::maxBufferSize, QSensor::efficientBufferSize +*/ + +/*! + \internal +*/ +void QSensor::setProperty(const char *name, const QVariant &value) +{ + //qDebug() << "QSensor::setProperty" << name << value; + QObject::setProperty(name, value); + QSensorWrapper *wrapper = qobject_cast(d->backend); + if (wrapper) { + wrapper->setProperty(name, value); + } else if (!d->backend) { + d->pendingProperties << PendingProperty(QString::fromLocal8Bit(name), value); + } +} + +/*! + \internal +*/ +QVariant QSensor::property(const char *name) const +{ + //qDebug() << "QSensor::property"; + QSensorWrapper *wrapper = qobject_cast(d->backend); + if (wrapper) { + return wrapper->property(name); + } + return QObject::property(name); +} + +// ===================================================================== + +/*! + \class QSensorFilter + \ingroup sensors_main + \inmodule QtSensors + + \brief The QSensorFilter class provides an efficient + callback facility for asynchronous notifications of + sensor changes. + + Some sensors (eg. the accelerometer) are often accessed very frequently. + This may be slowed down by the use of signals and slots. + The QSensorFilter interface provides a more efficient way for the + sensor to notify your class that the sensor has changed. + + Additionally, multiple filters can be added to a sensor. They are called + in order and each filter has the option to modify the values in the reading + or to suppress the reading altogether. + + Note that the values in the class returned by QSensor::reading() will + not be updated until after the filters have been run. + + \sa filter() +*/ + +/*! + \internal +*/ +QSensorFilter::QSensorFilter() + : m_sensor(0) +{ +} + +/*! + Notifies the attached sensor (if any) that the filter is being destroyed. +*/ +QSensorFilter::~QSensorFilter() +{ + if (m_sensor) + m_sensor->removeFilter(this); +} + +/*! + \fn QSensorFilter::filter(QSensorReading *reading) + + This function is called when the sensor \a reading changes. + + The filter can modify the reading. + + Returns true to allow the next filter to receive the value. + If this is the last filter, returning true causes the signal + to be emitted and the value is stored in the sensor. + + Returns false to drop the reading. +*/ + +/*! + \internal +*/ +void QSensorFilter::setSensor(QSensor *sensor) +{ + m_sensor = sensor; +} + +// ===================================================================== + +/*! + \class QSensorReading + \ingroup sensors_main + \inmodule QtSensors + + \brief The QSensorReading class holds the readings from the sensor. + + Note that QSensorReading is not particularly useful by itself. The interesting + data for each sensor is defined in a sub-class of QSensorReading. +*/ + +/*! + \internal +*/ +QSensorReading::QSensorReading(QObject *parent, QSensorReadingPrivate *_d) + : QObject(parent) + , d(_d?_d:new QSensorReadingPrivate) +{ +} + +/*! + \internal +*/ +QSensorReading::~QSensorReading() +{ +} + +/*! + \property QSensorReading::timestamp + \brief the timestamp of the reading. + + Timestamps values are microseconds since a fixed point. + You can use timestamps to see how far apart two sensor readings are. + + Note that sensor timestamps from different sensors may not be directly + comparable (as they may choose different fixed points for their reference). + + \bold{Note that some platforms do not deliver timestamps correctly}. + Applications should be prepared for occasional issues that cause timestamps to jump + forwards or backwards. The \l{sensors-api.html#platform-notes}{platform notes} have + more details. +*/ + +/*! + Returns the timestamp of the reading. +*/ +quint64 QSensorReading::timestamp() const +{ + return d->timestamp; +} + +/*! + Sets the \a timestamp of the reading. +*/ +void QSensorReading::setTimestamp(quint64 timestamp) +{ + d->timestamp = timestamp; +} + +/*! + Returns the number of extra properties that the reading has. + + Note that this does not count properties declared in QSensorReading. + + As an example, this returns 3 for QAccelerometerReading because + there are 3 properties defined in that class. +*/ +int QSensorReading::valueCount() const +{ + const QMetaObject *mo = metaObject(); + return mo->propertyCount() - mo->propertyOffset(); +} + +/*! + Returns the value of the property at \a index. + + Note that this function is slower than calling the data function directly. + + Here is an example of getting a property via the different mechanisms available. + + Accessing directly provides the best performance but requires compile-time knowledge + of the data you are accessing. + + \code + QAccelerometerReading *reading = ...; + qreal x = reading->x(); + \endcode + + You can also access a property by name. To do this you must call QObject::property(). + + \code + qreal x = reading->property("x").value(); + \endcode + + Finally, you can access values via numeric index. + + \code + qreal x = reading->value(0).value(); + \endcode + + Note that value() can only access properties declared with Q_PROPERTY() in sub-classes + of QSensorReading. + + \sa valueCount(), QObject::property() +*/ +QVariant QSensorReading::value(int index) const +{ + // get them meta-object + const QMetaObject *mo = metaObject(); + + // determine the index of the property we want + index += mo->propertyOffset(); + + // get the meta-property + QMetaProperty property = mo->property(index); + + // read the property + return property.read(this); +} + +/*! + \fn QSensorReading::copyValuesFrom(QSensorReading *other) + \internal + + Copy values from other into this reading. Implemented by sub-classes + using the DECLARE_READING() and IMPLEMENT_READING() macros. + + Note that this method should only be called by QSensorBackend. +*/ +void QSensorReading::copyValuesFrom(QSensorReading *other) +{ + QSensorReadingPrivate *my_ptr = d.data(); + QSensorReadingPrivate *other_ptr = other->d.data(); + /* Do a direct copy of the private class */ + *(my_ptr) = *(other_ptr); +} + +/*! + \fn QSensorReading::d_ptr() + \internal + No longer used. Exists to keep the winscw build happy. +*/ + +/*! + \macro DECLARE_READING(classname) + \relates QSensorReading + \brief The DECLARE_READING macro adds some required methods to a reading class. + + This macro should be used for all reading classes. Pass the \a classname of your reading class. + + \code + class MyReading : public QSensorReading + { + Q_OBJECT + Q_PROPERTY(qreal myprop READ myprop) + DECLARE_READING(MyReading) + public: + qreal myprop() const; + vod setMyprop(qreal myprop); + }; + \endcode + + \sa IMPLEMENT_READING() +*/ + +/*! + \macro IMPLEMENT_READING(classname) + \relates QSensorReading + \brief The IMPLEMENT_READING macro implements the required methods for a reading class. + + This macro should be used for all reading classes. It should be placed into a single compilation + unit (source file), not into a header file. Pass the \a classname of your reading class. + + \code + IMPLEMENT_READING(MyReading) + \endcode + + \sa DECLARE_READING() +*/ + +#include "moc_qsensor.cpp" +QTM_END_NAMESPACE +QT_END_NAMESPACE + diff --git a/src/qtmsensors/qsensor.h b/src/qtmsensors/qsensor.h new file mode 100644 index 00000000..b8a0291a --- /dev/null +++ b/src/qtmsensors/qsensor.h @@ -0,0 +1,254 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTM_QSENSOR_H +#define QTM_QSENSOR_H + +#include +#include +#include +#include +#include + +#include "qtmsensorsglobal.h" + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +class QSensorPrivate; +class QSensorReading; +class QSensorReadingPrivate; +class QSensorFilter; + +// This type is no longer used in the API but third party apps may be using it +typedef quint64 qtimestamp; + +typedef QPair qrange; +typedef QList qrangelist; +struct qoutputrange +{ + qreal minimum; + qreal maximum; + qreal accuracy; +}; +typedef QList qoutputrangelist; + +class QTM_SENSORS_EXPORT QSensor : public QObject +{ + friend class QSensorBackend; + + Q_OBJECT + Q_PROPERTY(QByteArray sensorid READ identifier WRITE setIdentifier) + Q_PROPERTY(QByteArray type READ type) + Q_PROPERTY(bool connectedToBackend READ isConnectedToBackend) + Q_PROPERTY(qrangelist availableDataRates READ availableDataRates) + Q_PROPERTY(int dataRate READ dataRate WRITE setDataRate) + Q_PROPERTY(QSensorReading* reading READ reading NOTIFY readingChanged) + Q_PROPERTY(bool busy READ isBusy) + Q_PROPERTY(bool active READ isActive WRITE setActive NOTIFY activeChanged) + Q_PROPERTY(qoutputrangelist outputRanges READ outputRanges) + Q_PROPERTY(int outputRange READ outputRange WRITE setOutputRange) + Q_PROPERTY(QString description READ description) + Q_PROPERTY(int error READ error NOTIFY sensorError) +#ifdef Q_QDOC + Q_PROPERTY(int maxBufferSize) + Q_PROPERTY(int efficientBufferSize) + Q_PROPERTY(int bufferSize) +#endif +public: + explicit QSensor(const QByteArray &type, QObject *parent = 0); + virtual ~QSensor(); + + QByteArray identifier() const; + void setIdentifier(const QByteArray &identifier); + + QByteArray type() const; + + Q_INVOKABLE bool connectToBackend(); + bool isConnectedToBackend() const; + + bool isBusy() const; + + void setActive(bool active); + bool isActive() const; + + qrangelist availableDataRates() const; + int dataRate() const; + void setDataRate(int rate); + + qoutputrangelist outputRanges() const; + int outputRange() const; + void setOutputRange(int index); + + QString description() const; + int error() const; + + // Filters modify the reading + void addFilter(QSensorFilter *filter); + void removeFilter(QSensorFilter *filter); + QList filters() const; + + // The readings are exposed via this object + QSensorReading *reading() const; + + // Information about available sensors + // These functions are implemented in qsensormanager.cpp + static QList sensorTypes(); + static QList sensorsForType(const QByteArray &type); + static QByteArray defaultSensorForType(const QByteArray &type); + + // These are here so we can catch dynamic properties and share + // them with the non-legacy sensor that is being wrapped + void setProperty(const char *name, const QVariant &value); + QVariant property(const char *name) const; + +public Q_SLOTS: + // Start receiving values from the sensor + bool start(); + + // Stop receiving values from the sensor + void stop(); + +Q_SIGNALS: + void busyChanged(); + void activeChanged(); + void readingChanged(); + void sensorError(int error); + void availableSensorsChanged(); + +protected: + // called by the back end + QSensorPrivate *d_func() const { return d.data(); } + +private: + void registerInstance(); + + QScopedPointer d; + Q_DISABLE_COPY(QSensor) +}; + +class QTM_SENSORS_EXPORT QSensorFilter +{ + friend class QSensor; +public: + virtual bool filter(QSensorReading *reading) = 0; +protected: + QSensorFilter(); + ~QSensorFilter(); + virtual void setSensor(QSensor *sensor); + QSensor *m_sensor; +}; + +class QTM_SENSORS_EXPORT QSensorReading : public QObject +{ + friend class QSensorBackend; + + Q_OBJECT + Q_PROPERTY(quint64 timestamp READ timestamp) +public: + virtual ~QSensorReading(); + + quint64 timestamp() const; + void setTimestamp(quint64 timestamp); + + // Access properties of sub-classes by numeric index + // For name-based access use QObject::property() + int valueCount() const; + QVariant value(int index) const; + +protected: + explicit QSensorReading(QObject *parent, QSensorReadingPrivate *d); + QScopedPointer *d_ptr() { return &d; } + virtual void copyValuesFrom(QSensorReading *other); + +private: + QScopedPointer d; + Q_DISABLE_COPY(QSensorReading) +}; + +#undef DECLARE_READING +#undef DECLARE_READING_D +#undef IMPLEMENT_READING +#undef IMPLEMENT_READING_D + +#define DECLARE_READING(classname)\ + DECLARE_READING_D(classname, classname ## Private) + +#define DECLARE_READING_D(classname, pclassname)\ + public:\ + classname(QObject *parent = 0);\ + virtual ~classname();\ + void copyValuesFrom(QSensorReading *other);\ + private:\ + QScopedPointer d; + +#define IMPLEMENT_READING(classname)\ + IMPLEMENT_READING_D(classname, classname ## Private) + +#define IMPLEMENT_READING_D(classname, pclassname)\ + classname::classname(QObject *parent)\ + : QSensorReading(parent, 0)\ + , d(new pclassname)\ + {}\ + classname::~classname() {}\ + void classname::copyValuesFrom(QSensorReading *_other)\ + {\ + /* No need to verify types, only called by QSensorBackend */\ + classname *other = static_cast(_other);\ + pclassname *my_ptr = d.data();\ + pclassname *other_ptr = other->d.data();\ + /* Do a direct copy of the private class */\ + *(my_ptr) = *(other_ptr);\ + /* We need to copy the parent too */\ + QSensorReading::copyValuesFrom(_other);\ + } + +QTM_END_NAMESPACE +QT_END_NAMESPACE + +#if 0 +Q_DECLARE_METATYPE(QTM_PREPEND_NAMESPACE(qrange)) +Q_DECLARE_METATYPE(QTM_PREPEND_NAMESPACE(qrangelist)) +Q_DECLARE_METATYPE(QTM_PREPEND_NAMESPACE(qoutputrangelist)) +#endif + +#endif + diff --git a/src/qtmsensors/qsensor_p.h b/src/qtmsensors/qsensor_p.h new file mode 100644 index 00000000..ec75d793 --- /dev/null +++ b/src/qtmsensors/qsensor_p.h @@ -0,0 +1,124 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTM_QSENSOR_P_H +#define QTM_QSENSOR_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "qsensor.h" + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +typedef QList QFilterList; +typedef QPair PendingProperty; +class QSensorBackend; + +class QSensorPrivate +{ +public: + QSensorPrivate() + : identifier() + , type() + , outputRange(-1) + , dataRate(0) + , backend(0) + , active(false) + , busy(false) + , device_reading(0) + , filter_reading(0) + , cache_reading(0) + , error(0) + { + } + + // meta-data + QByteArray identifier; + QByteArray type; + + QString description; + + qoutputrangelist outputRanges; + int outputRange; + + // policy + qrangelist availableDataRates; + int dataRate; + + QSensorBackend *backend; + QFilterList filters; + bool active; + bool busy; + QSensorReading *device_reading; + QSensorReading *filter_reading; + QSensorReading *cache_reading; + + int error; + QList pendingProperties; +}; + +class QSensorReadingPrivate +{ +public: + QSensorReadingPrivate() + : timestamp(0) + { + } + + // sensor data cache + quint64 timestamp; +}; + +QTM_END_NAMESPACE +QT_END_NAMESPACE + +#endif + diff --git a/src/qtmsensors/qsensorbackend.cpp b/src/qtmsensors/qsensorbackend.cpp new file mode 100644 index 00000000..27a3ac2f --- /dev/null +++ b/src/qtmsensors/qsensorbackend.cpp @@ -0,0 +1,328 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qsensorbackend_p.h" +#include "qsensor_p.h" +#include + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +/*! + \class QSensorBackend + \ingroup sensors_backend + \inmodule QtSensors + \since 1.0 + + \brief The QSensorBackend class is a sensor implementation. + + Sensors on a device will be represented by sub-classes of + QSensorBackend. +*/ + +/*! + \internal +*/ +QSensorBackend::QSensorBackend(QSensor *sensor) + : m_sensor(sensor) +{ +} + +/*! + \internal +*/ +QSensorBackend::~QSensorBackend() +{ +} + +/*! + Notify the QSensor class that a new reading is available. +*/ +void QSensorBackend::newReadingAvailable() +{ + QSensorPrivate *d = m_sensor->d_func(); + + // Copy the values from the device reading to the filter reading + d->filter_reading->copyValuesFrom(d->device_reading); + + for (QFilterList::const_iterator it = d->filters.constBegin(); it != d->filters.constEnd(); ++it) { + QSensorFilter *filter = (*it); + if (!filter->filter(d->filter_reading)) + return; + } + + // Copy the values from the filter reading to the cached reading + d->cache_reading->copyValuesFrom(d->filter_reading); + + Q_EMIT m_sensor->readingChanged(); +} + +/*! + \fn QSensorBackend::start() + + Start reporting values. +*/ + +/*! + \fn QSensorBackend::stop() + + Stop reporting values. +*/ + +/*! + If the backend has lost its reference to the reading + it can call this method to get the address. + + Note that you will need to down-cast to the appropriate + type. + + \sa setReading() +*/ +QSensorReading *QSensorBackend::reading() const +{ + QSensorPrivate *d = m_sensor->d_func(); + return d->device_reading; +} + +/*! + \fn QSensorBackend::sensor() const + + Returns the sensor front end associated with this backend. +*/ + +/*! + \fn QSensorBackend::setReading(T *reading) + + This function is called to initialize the \a reading + classes used for a sensor. + + If your backend has already allocated a reading you + should pass the address of this to the function. + Otherwise you should pass 0 and the function will + return the address of the reading your backend + should use when it wants to notify the sensor API + of new readings. + + Note that this is a template function so it should + be called with the appropriate type. + + \code + class MyBackend : public QSensorBackend + { + QAccelerometerReading m_reading; + public: + MyBackend(QSensor *sensor) + : QSensorBackend(sensor) + { + setReading(&m_reading); + } + + ... + \endcode + + Note that this function must be called or you will + not be able to send readings to the front end. + + If you do not wish to store the address of the reading + you may use the reading() method to get it again later. + + \code + class MyBackend : public QSensorBackend + { + public: + MyBackend(QSensor *sensor) + : QSensorBackend(sensor) + { + setReading(0); + } + + void poll() + { + quint64 timestamp; + qreal x, y, z; + ... + QAccelerometerReading *reading = static_cast(reading()); + reading->setTimestamp(timestamp); + reading->setX(x); + reading->setY(y); + reading->setZ(z); + } + + ... + \endcode + + \sa reading() +*/ + +/*! + \internal +*/ +void QSensorBackend::setReadings(QSensorReading *device, QSensorReading *filter, QSensorReading *cache) +{ + QSensorPrivate *d = m_sensor->d_func(); + d->device_reading = device; + d->filter_reading = filter; + d->cache_reading = cache; +} + +/*! + Add a data rate (consisting of \a min and \a max values) for the sensor. + + Note that this function should be called from the constructor so that the information + is available immediately. + + \sa QSensor::availableDataRates +*/ +void QSensorBackend::addDataRate(qreal min, qreal max) +{ + QSensorPrivate *d = m_sensor->d_func(); + d->availableDataRates << qrange(min, max); +} + +/*! + Set the data rates for the sensor based on \a otherSensor. + + This is designed for sensors that are based on other sensors. + + \code + setDataRates(otherSensor); + \endcode + + Note that this function should be called from the constructor so that the information + is available immediately. + + \sa QSensor::availableDataRates, addDataRate() +*/ +void QSensorBackend::setDataRates(const QSensor *otherSensor) +{ + if (!otherSensor) { + qWarning() << "ERROR: Cannot call QSensorBackend::setDataRates with 0"; + return; + } + if (otherSensor->identifier().isEmpty()) { + qWarning() << "ERROR: Cannot call QSensorBackend::setDataRates with an invalid sensor"; + return; + } + QSensorPrivate *d = m_sensor->d_func(); + d->availableDataRates = otherSensor->availableDataRates(); + d->dataRate = otherSensor->dataRate(); +} + +/*! + Add an output range (consisting of \a min, \a max values and \a accuracy) for the sensor. + + Note that this function should be called from the constructor so that the information + is available immediately. + + \sa QSensor::outputRange, QSensor::outputRanges +*/ +void QSensorBackend::addOutputRange(qreal min, qreal max, qreal accuracy) +{ + QSensorPrivate *d = m_sensor->d_func(); + + qoutputrange details = {min, max, accuracy}; + + d->outputRanges << details; +} + +/*! + Set the \a description for the sensor. + + Note that this function should be called from the constructor so that the information + is available immediately. +*/ +void QSensorBackend::setDescription(const QString &description) +{ + QSensorPrivate *d = m_sensor->d_func(); + d->description = description; +} + +/*! + Inform the front end that the sensor has stopped. + This can be due to start() failing or for some + unexpected reason (eg. hardware failure). + + Note that the front end must call QSensor::isActive() to see if + the sensor has stopped. If the sensor has stopped due to an error + the sensorError() function should be called to notify the class + of the error condition. +*/ +void QSensorBackend::sensorStopped() +{ + QSensorPrivate *d = m_sensor->d_func(); + d->active = false; +} + +/*! + Inform the front end that the sensor is busy. + This implicitly calls sensorStopped() and + is typically called from start(). + + Note that the front end must call QSensor::isBusy() to see if + the sensor is busy. If the sensor has stopped due to an error + the sensorError() function should be called to notify the class + of the error condition. +*/ +void QSensorBackend::sensorBusy() +{ + QSensorPrivate *d = m_sensor->d_func(); + d->active = false; + d->busy = true; +} + +/*! + Inform the front end that a sensor error occurred. + Note that this only reports an \a error code. It does + not stop the sensor. + + \sa sensorStopped() +*/ +void QSensorBackend::sensorError(int error) +{ + QSensorPrivate *d = m_sensor->d_func(); + d->error = error; + Q_EMIT m_sensor->sensorError(error); +} + +#include "moc_qsensorbackend_p.cpp" +QTM_END_NAMESPACE +QT_END_NAMESPACE + diff --git a/src/qtmsensors/qsensorbackend_p.h b/src/qtmsensors/qsensorbackend_p.h new file mode 100644 index 00000000..a2d8b457 --- /dev/null +++ b/src/qtmsensors/qsensorbackend_p.h @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTM_QSENSORBACKEND_P_H +#define QTM_QSENSORBACKEND_P_H + +#include +#include "qtmsensorsglobal.h" + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +class QSensor; +class QSensorReading; + +class QTM_SENSORS_EXPORT QSensorBackend : public QObject +{ + Q_OBJECT +public: + QSensorBackend(QSensor *sensor); + virtual ~QSensorBackend(); + + virtual void start() = 0; + virtual void stop() = 0; + + // used by the backend to set metadata properties + void addDataRate(qreal min, qreal max); + void setDataRates(const QSensor *otherSensor); + void addOutputRange(qreal min, qreal max, qreal accuracy); + void setDescription(const QString &description); + + template + T *setReading(T *reading) + { + if (!reading) + reading = new T(this); + setReadings(reading, new T(this), new T(this)); + return reading; + } + + QSensorReading *reading() const; + QSensor *sensor() const { return m_sensor; } + + // used by the backend to inform us of events + void newReadingAvailable(); + void sensorStopped(); + void sensorBusy(); + void sensorError(int error); + +private: + void setReadings(QSensorReading *device, QSensorReading *filter, QSensorReading *cache); + + QSensor *m_sensor; + Q_DISABLE_COPY(QSensorBackend) +}; + +QTM_END_NAMESPACE +QT_END_NAMESPACE + +#endif + diff --git a/src/qtmsensors/qsensormanager.cpp b/src/qtmsensors/qsensormanager.cpp new file mode 100644 index 00000000..e829d78d --- /dev/null +++ b/src/qtmsensors/qsensormanager.cpp @@ -0,0 +1,453 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qsensormanager_p.h" +#include +#include +#include +#include +#include "backendwrapper_p.h" + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +typedef QHash FactoryForIdentifierMap; +typedef QHash BackendIdentifiersForTypeMap; + +class QSensorManagerPrivate : public QObject +{ + friend class QSensorManager; + + Q_OBJECT +public: + enum PluginLoadingState { + NotLoaded, + Loading, + Loaded + }; + QSensorManagerPrivate() + : pluginLoadingState(NotLoaded) + , sensorsChanged(false) + { + } + + PluginLoadingState pluginLoadingState; + void loadPlugins(); + + // Here for the unit test + QList staticRegistrations; + + // Holds a mapping from type to available identifiers (and from there to the factory) + BackendIdentifiersForTypeMap backendsByType; + + // Holds the first identifier for each type + QHash firstIdentifierForType; + + bool sensorsChanged; + +Q_SIGNALS: + void availableSensorsChanged(); + +public Q_SLOTS: + void emitSensorsChanged() + { + static bool alreadyRunning = false; + if (pluginLoadingState != QSensorManagerPrivate::Loaded || alreadyRunning) { + // We're busy. + // Just note that a registration changed and exit. + // Someone up the call stack will deal with this. + sensorsChanged = true; + return; + } + + // Notify the client of the changes + Q_EMIT availableSensorsChanged(); + } +}; + +Q_GLOBAL_STATIC(QSensorManagerPrivate, sensorManagerPrivate) + +// The unit test needs to change the behaviour of the library. It does this +// through an exported but undocumented function. +static void initPlugin(QObject *plugin); +static QSettings::Scope settings_scope = QSettings::SystemScope; +QTM_SENSORS_EXPORT void qtm_sensors_unit_test_hook(int index) +{ + QSensorManagerPrivate *d = sensorManagerPrivate(); + + switch (index) { + case 0: + Q_ASSERT(d->pluginLoadingState == QSensorManagerPrivate::NotLoaded); + settings_scope = QSettings::UserScope; + break; + default: + break; + } +} + +static void initPlugin(QObject *o) +{ + if (!o) return; + + QSensorPluginInterface *plugin = qobject_cast(o); + if (plugin) + plugin->registerSensors(); +} + +void QSensorManagerPrivate::loadPlugins() +{ + QSensorManagerPrivate *d = this; + if (d->pluginLoadingState != QSensorManagerPrivate::NotLoaded) return; + d->pluginLoadingState = QSensorManagerPrivate::Loading; + + SENSORLOG() << "initializing legacy static plugins"; + // Legacy static plugins + Q_FOREACH (CreatePluginFunc func, d->staticRegistrations) { + QObject *plugin = func(); + initPlugin(plugin); + } + + d->pluginLoadingState = QSensorManagerPrivate::Loaded; + + if (d->sensorsChanged) { + // Notify the app that the available sensor list has changed. + // This may cause recursive calls! + d->emitSensorsChanged(); + } +} + +// ===================================================================== + +/*! + \class QSensorManager + \ingroup sensors_backend + \inmodule QtSensors + \since 1.0 + + \brief The QSensorManager class handles registration and creation of sensor backends. + + Sensor plugins register backends using the registerBackend() function. + + When QSensor::connectToBackend() is called, the createBackend() function will be called. +*/ + +/*! + Register a sensor for \a type. The \a identifier must be unique. + + The \a factory will be asked to create instances of the backend. +*/ +void QSensorManager::registerBackend(const QByteArray &type, const QByteArray &identifier, QSensorBackendFactory *factory) +{ + Q_ASSERT(type.count()); + Q_ASSERT(identifier.count()); + Q_ASSERT(factory); + QSensorManagerPrivate *d = sensorManagerPrivate(); + if (!d->backendsByType.contains(type)) { + (void)d->backendsByType[type]; + d->firstIdentifierForType[type] = identifier; + } else if (d->firstIdentifierForType[type].startsWith("generic.")) { + // Don't let a generic backend be the default when some other backend exists! + d->firstIdentifierForType[type] = identifier; + } + FactoryForIdentifierMap &factoryByIdentifier = d->backendsByType[type]; + if (factoryByIdentifier.contains(identifier)) { + qWarning() << "A backend with type" << type << "and identifier" << identifier << "has already been registered!"; + return; + } + SENSORLOG() << "registering backend for type" << type << "identifier" << identifier;// << "factory" << QString().sprintf("0x%08x", (unsigned int)factory); + factoryByIdentifier[identifier] = factory; + + // Notify the app that the available sensor list has changed. + // This may cause recursive calls! + d->emitSensorsChanged(); +} + +/*! + Unregister the backend for \a type with \a identifier. + + Note that this only prevents new instance of the backend from being created. It does not + invalidate the existing instances of the backend. The backend code should handle the + disappearance of the underlying hardware itself. +*/ +void QSensorManager::unregisterBackend(const QByteArray &type, const QByteArray &identifier) +{ + QSensorManagerPrivate *d = sensorManagerPrivate(); + if (!d->backendsByType.contains(type)) { + qWarning() << "No backends of type" << type << "are registered"; + return; + } + FactoryForIdentifierMap &factoryByIdentifier = d->backendsByType[type]; + if (!factoryByIdentifier.contains(identifier)) { + qWarning() << "Identifier" << identifier << "is not registered"; + return; + } + + (void)factoryByIdentifier.take(identifier); // we don't own this pointer anyway + if (d->firstIdentifierForType[type] == identifier) { + if (factoryByIdentifier.count()) { + d->firstIdentifierForType[type] = factoryByIdentifier.begin().key(); + if (d->firstIdentifierForType[type].startsWith("generic.")) { + // Don't let a generic backend be the default when some other backend exists! + for (FactoryForIdentifierMap::const_iterator it = factoryByIdentifier.begin()++; it != factoryByIdentifier.end(); it++) { + const QByteArray &identifier(it.key()); + if (!identifier.startsWith("generic.")) { + d->firstIdentifierForType[type] = identifier; + break; + } + } + } + } else { + (void)d->firstIdentifierForType.take(type); + } + } + if (!factoryByIdentifier.count()) + (void)d->backendsByType.take(type); + + // Notify the app that the available sensor list has changed. + // This may cause recursive calls! + d->emitSensorsChanged(); +} + +/*! + \internal +*/ +void QSensorManager::registerStaticPlugin(CreatePluginFunc func) +{ + QSensorManagerPrivate *d = sensorManagerPrivate(); + d->staticRegistrations.append(func); +} + +/*! + Create a backend for \a sensor. Returns null if no suitable backend exists. +*/ +QSensorBackend *QSensorManager::createBackend(QSensor *sensor) +{ + Q_ASSERT(sensor); + + QSensorManagerPrivate *d = sensorManagerPrivate(); + d->loadPlugins(); + + SENSORLOG() << "QSensorManager::createBackend" << "type" << sensor->type() << "identifier" << sensor->identifier(); + + if (!d->backendsByType.contains(sensor->type())) { + SENSORLOG() << "no backends of type" << sensor->type() << "have been registered."; + return 0; + } + + const FactoryForIdentifierMap &factoryByIdentifier = d->backendsByType[sensor->type()]; + QSensorBackendFactory *factory; + QSensorBackend *backend; + + if (sensor->identifier().isEmpty()) { + QByteArray defaultIdentifier = QSensor::defaultSensorForType(sensor->type()); + SENSORLOG() << "Trying the default" << defaultIdentifier; + // No identifier set, try the default + factory = factoryByIdentifier[defaultIdentifier]; + //SENSORLOG() << "factory" << QString().sprintf("0x%08x", (unsigned int)factory); + sensor->setIdentifier(defaultIdentifier); // the factory requires this + backend = factory->createBackend(sensor); + if (backend) return backend; // Got it! + + // The default failed to instantiate so try any other registered sensors for this type + Q_FOREACH (const QByteArray &identifier, factoryByIdentifier.keys()) { + SENSORLOG() << "Trying" << identifier; + if (identifier == defaultIdentifier) continue; // Don't do the default one again + factory = factoryByIdentifier[identifier]; + //SENSORLOG() << "factory" << QString().sprintf("0x%08x", (unsigned int)factory); + sensor->setIdentifier(identifier); // the factory requires this + backend = factory->createBackend(sensor); + if (backend) return backend; // Got it! + } + SENSORLOG() << "FAILED"; + sensor->setIdentifier(QByteArray()); // clear the identifier + } else { + if (!factoryByIdentifier.contains(sensor->identifier())) { + SENSORLOG() << "no backend with identifier" << sensor->identifier() << "for type" << sensor->type(); + return 0; + } + + // We were given an explicit identifier so don't substitute other backends if it fails to instantiate + factory = factoryByIdentifier[sensor->identifier()]; + //SENSORLOG() << "factory" << QString().sprintf("0x%08x", (unsigned int)factory); + backend = factory->createBackend(sensor); + if (backend) return backend; // Got it! + } + + SENSORLOG() << "no suitable backend found for requested identifier" << sensor->identifier() << "and type" << sensor->type(); + return 0; +} + +/*! + Returns true if the backend identified by \a type and \a identifier is registered. + + This is a convenience method that helps out plugins doing dynamic registration. +*/ +bool QSensorManager::isBackendRegistered(const QByteArray &type, const QByteArray &identifier) +{ + QSensorManagerPrivate *d = sensorManagerPrivate(); + d->loadPlugins(); + + if (!d->backendsByType.contains(type)) + return false; + + const FactoryForIdentifierMap &factoryByIdentifier = d->backendsByType[type]; + if (!factoryByIdentifier.contains(identifier)) + return false; + + return true; +} + +// ===================================================================== + +/*! + Returns a list of all sensor types. +*/ +QList QSensor::sensorTypes() +{ + QSensorManagerPrivate *d = sensorManagerPrivate(); + d->loadPlugins(); + + return d->backendsByType.keys(); +} + +/*! + Returns a list of ids for each of the sensors for \a type. + If there are no sensors of that type available the list will be empty. +*/ +QList QSensor::sensorsForType(const QByteArray &type) +{ + QSensorManagerPrivate *d = sensorManagerPrivate(); + d->loadPlugins(); + + // no sensors of that type exist + if (!d->backendsByType.contains(type)) + return QList(); + + return d->backendsByType[type].keys(); +} + +/*! + Returns the default sensor identifier for \a type. + This is set in a config file and can be overridden if required. + If no default is available the system will return the first registered + sensor for \a type. + + Note that there is special case logic to prevent the generic plugin's backends from becoming the + default when another backend is registered for the same type. This logic means that a backend + identifier starting with \c{generic.} will only be the default if no other backends have been + registered for that type or if it is specified in \c{Sensors.conf}. + + \sa {Determining the default sensor for a type} +*/ +QByteArray QSensor::defaultSensorForType(const QByteArray &type) +{ + QSensorManagerPrivate *d = sensorManagerPrivate(); + d->loadPlugins(); + + // no sensors of that type exist + if (!d->backendsByType.contains(type)) + return QByteArray(); + + // The unit test needs to modify Sensors.conf but it can't access the system directory + QSettings settings(settings_scope, QLatin1String("Nokia"), QLatin1String("Sensors")); + QVariant value = settings.value(QString(QLatin1String("Default/%1")).arg(QString::fromLatin1(type))); + if (!value.isNull()) { + QByteArray defaultIdentifier = value.toByteArray(); + if (d->backendsByType[type].contains(defaultIdentifier)) // Don't return a value that we can't use! + return defaultIdentifier; + } + + // This is our fallback + return d->firstIdentifierForType[type]; +} + +void QSensor::registerInstance() +{ + QSensorManagerPrivate *d = sensorManagerPrivate(); + connect(d, SIGNAL(availableSensorsChanged()), this, SIGNAL(availableSensorsChanged())); +} + +// ===================================================================== + +/*! + \class QSensorBackendFactory + \ingroup sensors_backend + \inmodule QtSensors + + \brief The QSensorBackendFactory class instantiates instances of + QSensorBackend. + + This interface must be implemented in order to register a sensor backend. + + \sa {Creating a sensor plugin} +*/ + +/*! + \fn QSensorBackendFactory::~QSensorBackendFactory() + \internal +*/ + +/*! + \fn QSensorBackendFactory::createBackend(QSensor *sensor) + + Instantiate a backend. If the factory handles multiple identifiers + it should check with the \a sensor to see which one is requested. + + If the factory cannot create a backend it should return 0. +*/ + +/*! + \macro REGISTER_STATIC_PLUGIN(pluginname) + \relates QSensorManager + + Registers a static plugin, \a pluginname. + + Note that this macro relies on static initialization so it may not be appropriate + for use in a library and may not work on all platforms. + + \sa {Creating a sensor plugin} +*/ + +#include "qsensormanager.moc" +#include "moc_qsensormanager_p.cpp" +QTM_END_NAMESPACE +QT_END_NAMESPACE + diff --git a/src/qtmsensors/qsensormanager_p.h b/src/qtmsensors/qsensormanager_p.h new file mode 100644 index 00000000..24121412 --- /dev/null +++ b/src/qtmsensors/qsensormanager_p.h @@ -0,0 +1,107 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTM_QSENSORMANAGER_P_H +#define QTM_QSENSORMANAGER_P_H + +#include "qsensor.h" + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +class QSensorBackend; +class QSensorBackendFactory; + +typedef QObject *(*CreatePluginFunc)(); + +class QTM_SENSORS_EXPORT QSensorManager +{ +public: + // Register a backend (call this from a plugin) + static void registerBackend(const QByteArray &type, const QByteArray &identifier, QSensorBackendFactory *factory); + static void unregisterBackend(const QByteArray &type, const QByteArray &identifier); + + static bool isBackendRegistered(const QByteArray &type, const QByteArray &identifier); + + // Create a backend (uses the type and identifier set in the sensor) + static QSensorBackend *createBackend(QSensor *sensor); + + // For the unit test + static void registerStaticPlugin(CreatePluginFunc func); +}; + +class QTM_SENSORS_EXPORT QSensorPluginInterface : public QObject +{ + Q_OBJECT +public: + virtual void registerSensors() = 0; +protected: + ~QSensorPluginInterface() {} +}; + +class QTM_SENSORS_EXPORT QSensorBackendFactory +{ +public: + virtual QSensorBackend *createBackend(QSensor *sensor) = 0; +protected: + ~QSensorBackendFactory() {} +}; + +// Legacy static plugins have their own registration methods. +// They can only register types. They cannot use the changes interface. +#define REGISTER_STATIC_PLUGIN_V1(pluginname) \ + static QObject *create_static_plugin_ ## pluginname()\ + {\ + return new pluginname;\ + }\ + static bool side_effect_sensor_backend_ ## pluginname ()\ + {\ + QSensorManager::registerStaticPlugin(create_static_plugin_ ## pluginname);\ + return false;\ + }\ + /* This assignment calls the function above */\ + static bool dummy_sensor_backend_ ## pluginname = side_effect_sensor_backend_ ## pluginname(); + +QTM_END_NAMESPACE +QT_END_NAMESPACE + +#endif + diff --git a/src/qtmsensors/qtapsensor.cpp b/src/qtmsensors/qtapsensor.cpp new file mode 100644 index 00000000..fded7b36 --- /dev/null +++ b/src/qtmsensors/qtapsensor.cpp @@ -0,0 +1,249 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qtapsensor.h" +#include "qtapsensor_p.h" + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +IMPLEMENT_READING(QTapReading) + +/*! + \class QTapReading + \ingroup sensors_reading + \inmodule QtSensors + \since 1.0 + + \brief The QTapReading class represents one reading from the + tap sensor. + + \section2 QTapReading Units + The tap sensor registers tap events along the 3 axes that originate from the phone. + The axes are arranged as follows. + + \image sensors-coordinates2.jpg + + By default it returns only double tap events. The QTapSensor::returnDoubleTapEvents property + must be set to false to return individual tap events. +*/ + +/*! + \enum QTapReading::TapDirection + + The tap direction is indicated using flags. Applications should check for the presence of + a particular flag as multiple flags may be set at once. + + The X, Y and Z flags allow an app to check for taps along an axis without caring about the + direction. + + \code + if (reading->tapDirection()&QTapReading::X) { + ... + } + \endcode + + The *_Pos and *_Neg flags allow checking for taps in a specific direction. Note that some + devices cannot determine the direction of a tap and will set both the _Pos and _Neg flag for + the detected axis. Previous versions of the API did not allow this. Applications that check + for the _Pos and _Neg flags as values should be updated so they can work with all devices. + + \oldcode + if (reading->tapDirection() == QTapReading::X_Pos) { + ... + } + \newcode + if (reading->tapDirection()&QTapReading::X_Pos) { + ... + } + \endcode + + \value Undefined This value means that the direction is unknown. + \value X This flag is set if the tap was along the X axis. + \value Y This flag is set if the tap was along the Y axis. + \value Z This flag is set if the tap was along the Z axis. + \value X_Pos This flag is set if the tap was towards the positive X direction. + \value Y_Pos This flag is set if the tap was towards the positive Y direction. + \value Z_Pos This flag is set if the tap was towards the positive Z direction. + \value X_Neg This flag is set if the tap was towards the negative X direction. + \value Y_Neg This flag is set if the tap was towards the negative Y direction. + \value Z_Neg This flag is set if the tap was towards the negative Z direction. + \value X_Both Equivalent to \c{X_Pos|X_Neg}. Returned by devices that cannot detect the direction of a tap. + \value Y_Both Equivalent to \c{Y_Pos|Y_Neg}. Returned by devices that cannot detect the direction of a tap. + \value Z_Both Equivalent to \c{Z_Pos|Z_Neg}. Returned by devices that cannot detect the direction of a tap. +*/ + +/*! + \property QTapReading::tapDirection + \brief the direction of the tap. + + \sa {QTapReading Units} +*/ + +QTapReading::TapDirection QTapReading::tapDirection() const +{ + return static_cast(d->tapDirection); +} + +/*! + Sets the tap direction to \a tapDirection. +*/ +void QTapReading::setTapDirection(QTapReading::TapDirection tapDirection) +{ + switch (tapDirection) { + case X_Pos: + case Y_Pos: + case Z_Pos: + case X_Neg: + case Y_Neg: + case Z_Neg: + case X_Both: + case Y_Both: + case Z_Both: + d->tapDirection = tapDirection; + break; + default: + d->tapDirection = Undefined; + break; + } +} + +/*! + \property QTapReading::doubleTap + \brief a value indicating if there was a single or double tap. + + \list + \o true - double tap + \o false - single tap + \endlist + \sa {QTapReading Units} +*/ + +bool QTapReading::isDoubleTap() const +{ + return d->doubleTap; +} + +/*! + Sets the double tap status of the reading to \a doubleTap. +*/ +void QTapReading::setDoubleTap(bool doubleTap) +{ + d->doubleTap = doubleTap; +} + +// ===================================================================== + +/*! + \class QTapFilter + \ingroup sensors_filter + \inmodule QtSensors + + \brief The QTapFilter class is a convenience wrapper around QSensorFilter. + + The only difference is that the filter() method features a pointer to QTapReading + instead of QSensorReading. +*/ + +/*! + \fn QTapFilter::filter(QTapReading *reading) + + Called when \a reading changes. Returns false to prevent the reading from propagating. + + \sa QSensorFilter::filter() +*/ + +char const * const QTapSensor::type("QTapSensor"); + +/*! + \class QTapSensor + \ingroup sensors_type + \inmodule QtSensors + + \brief The QTapSensor class is a convenience wrapper around QSensor. + + The only behavioural difference is that this class sets the type properly. + + This class also features a reading() function that returns a QTapReading instead of a QSensorReading. + + For details about how the sensor works, see \l QTapReading. + + \sa QTapReading +*/ + +/*! + \fn QTapSensor::QTapSensor(QObject *parent) + + Construct the sensor as a child of \a parent. +*/ + +/*! + \fn QTapSensor::~QTapSensor() + + Destroy the sensor. Stops the sensor if it has not already been stopped. +*/ + +/*! + \fn QTapSensor::reading() const + + Returns the reading class for this sensor. + + \sa QSensor::reading() +*/ + +/*! + \property QTapSensor::returnDoubleTapEvents + \brief a value indicating if double tap events should be reported. + + Set to true (the default) to have the sensor report only on double tap events. + Set to false to have the sensor report only on individual tap events. + + It is not possible to have the sensor report both single and double tap events. + If both are needed the app should create 2 sensor objects. + + Note that you must access this property via QObject::property() and QObject::setProperty(). + The property must be set before calling start(). +*/ + +#include "moc_qtapsensor.cpp" +QTM_END_NAMESPACE +QT_END_NAMESPACE + diff --git a/src/qtmsensors/qtapsensor.h b/src/qtmsensors/qtapsensor.h new file mode 100644 index 00000000..82453a40 --- /dev/null +++ b/src/qtmsensors/qtapsensor.h @@ -0,0 +1,108 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTM_QTAPSENSOR_H +#define QTM_QTAPSENSOR_H + +#include "qsensor.h" + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +class QTapReadingPrivate; + +class QTM_SENSORS_EXPORT QTapReading : public QSensorReading +{ + Q_OBJECT + Q_ENUMS(TapDirection) + Q_PROPERTY(TapDirection tapDirection READ tapDirection) + Q_PROPERTY(bool doubleTap READ isDoubleTap) + DECLARE_READING(QTapReading) +public: + enum TapDirection { + Undefined = 0, + X = 0x0001, + Y = 0x0002, + Z = 0x0004, + X_Pos = 0x0011, + Y_Pos = 0x0022, + Z_Pos = 0x0044, + X_Neg = 0x0101, + Y_Neg = 0x0202, + Z_Neg = 0x0404, + X_Both = 0x0111, + Y_Both = 0x0222, + Z_Both = 0x0444 + }; + + TapDirection tapDirection() const; + void setTapDirection(TapDirection tapDirection); + + bool isDoubleTap() const; + void setDoubleTap(bool doubleTap); +}; + +class QTM_SENSORS_EXPORT QTapFilter : public QSensorFilter +{ +public: + virtual bool filter(QTapReading *reading) = 0; +private: + bool filter(QSensorReading *reading) { return filter(static_cast(reading)); } +}; + +class QTM_SENSORS_EXPORT QTapSensor : public QSensor +{ + Q_OBJECT +#ifdef Q_QDOC + Q_PROPERTY(bool returnDoubleTapEvents) +#endif +public: + explicit QTapSensor(QObject *parent = 0) : QSensor(QTapSensor::type, parent) {} + virtual ~QTapSensor() {} + QTapReading *reading() const { return static_cast(QSensor::reading()); } + static char const * const type; +}; + +QTM_END_NAMESPACE +QT_END_NAMESPACE + +#endif + diff --git a/src/qtmsensors/qtapsensor_p.h b/src/qtmsensors/qtapsensor_p.h new file mode 100644 index 00000000..7296f55a --- /dev/null +++ b/src/qtmsensors/qtapsensor_p.h @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia 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. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QTM_QTAPSENSOR_P_H +#define QTM_QTAPSENSOR_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +QT_BEGIN_NAMESPACE +QTM_BEGIN_NAMESPACE + +class QTapReadingPrivate +{ +public: + QTapReadingPrivate() + : tapDirection(0) + , doubleTap(false) + { + } + + int tapDirection; + bool doubleTap; +}; + +QTM_END_NAMESPACE +QT_END_NAMESPACE + +#endif + diff --git a/src/qtmsensors/qtmsensors.pro b/src/qtmsensors/qtmsensors.pro new file mode 100644 index 00000000..6f556f2a --- /dev/null +++ b/src/qtmsensors/qtmsensors.pro @@ -0,0 +1,52 @@ +load(qt_module) + +TARGET = QtmSensors +QPRO_PWD = $$PWD + +CONFIG += module +CONFIG += strict_flags +#CONFIG(debug,debug|release): +DEFINES += ENABLE_RUNTIME_SENSORLOG + +MODULE_PRI = ../../modules/qt_sensors.pri + +QT = core sensors-private + +DEFINES += QTM_BUILD_SENSORS_LIB QT_MAKEDLL + +load(qt_module_config) + +HEADERS += \ + qtmsensorsversion.h\ + qtmsensorsglobal.h\ + qsensorbackend.h\ + qsensormanager_p.h\ + qsensorbackend_p.h\ + backendwrapper_p.h + +SOURCES += \ + qsensorbackend.cpp\ + qsensormanager.cpp\ + backendwrapper.cpp + +# 3 files per sensor (including QSensor) +SENSORS=\ + qsensor\ + qaccelerometer\ + qambientlightsensor\ + qcompass\ + qlightsensor\ + qmagnetometer\ + qorientationsensor\ + qproximitysensor\ + qrotationsensor\ + qtapsensor\ + qgyroscope + +for(s,SENSORS) { + # Client API + HEADERS += $${s}.h + SOURCES += $${s}.cpp + # Private header + HEADERS += $${s}_p.h +} diff --git a/src/qtmsensors/qtmsensorsglobal.h b/src/qtmsensors/qtmsensorsglobal.h new file mode 100644 index 00000000..078ac897 --- /dev/null +++ b/src/qtmsensors/qtmsensorsglobal.h @@ -0,0 +1,53 @@ +#ifndef QTMSENSORSGLOBAL_H +#define QTMSENSORSGLOBAL_H + +#include + +#if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN) +# if defined(QT_NODLL) +# undef QT_MAKEDLL +# undef QT_DLL +# elif defined(QT_MAKEDLL) /* create a Qt DLL library */ +# if defined(QT_DLL) +# undef QT_DLL +# endif +# if defined(QT_BUILD_SENSORS1_LIB) +# define QTM_SENSORS_EXPORT Q_DECL_EXPORT +# else +# define QTM_SENSORS_EXPORT Q_DECL_IMPORT +# endif +# define Q_TEMPLATEDLL +# elif defined(QT_DLL) /* use a Qt DLL library */ +# define QTM_SENSORS_EXPORT Q_DECL_IMPORT +# define Q_TEMPLATEDLL +# endif +# define Q_NO_DECLARED_NOT_DEFINED +#else +# if defined(Q_OS_LINUX) && defined(Q_CC_BOR) +# define Q_TEMPLATEDLL +# define Q_NO_DECLARED_NOT_DEFINED +# endif +# undef QT_MAKEDLL /* ignore these for other platforms */ +# undef QT_DLL +#endif + +#if !defined(QTM_SENSORS_EXPORT) +# if defined(QT_SHARED) +# define QTM_SENSORS_EXPORT Q_DECL_EXPORT +# else +# define QTM_SENSORS_EXPORT +# endif +#endif + +#define QTM_BEGIN_NAMESPACE namespace QtMobility { +#define QTM_END_NAMESPACE } +#define QTM_USE_NAMESPACE using namespace QtMobility; +#define QTM_PREPEND_NAMESPACE(x) QtMobility::x + +#if QT_PREPEND_NAMESPACE +#define NEW_NAMESPACE(x) QT_PREPEND_NAMESPACE(x) +#else +#define NEW_NAMESPACE(x) ::x +#endif + +#endif -- cgit v1.2.3