From 164e4f5813d8beb30ce8e1555f49f84b785c0d51 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 9 Jun 2011 18:31:09 +1000 Subject: Add dummy, generic and simulator backend The Simulator backend is not working/tested at this stage as the simulator library is not available for Qt5 at this stage. In addtion the sensor_explorer test app was added for simple sensor testing. --- src/plugins/sensors/dummy/dummy.pri | 9 ++ src/plugins/sensors/dummy/dummy.pro | 15 +++ src/plugins/sensors/dummy/dummyaccelerometer.cpp | 65 +++++++++++++ src/plugins/sensors/dummy/dummyaccelerometer.h | 61 ++++++++++++ src/plugins/sensors/dummy/dummycommon.cpp | 116 +++++++++++++++++++++++ src/plugins/sensors/dummy/dummycommon.h | 65 +++++++++++++ src/plugins/sensors/dummy/dummylightsensor.cpp | 65 +++++++++++++ src/plugins/sensors/dummy/dummylightsensor.h | 61 ++++++++++++ src/plugins/sensors/dummy/main.cpp | 79 +++++++++++++++ 9 files changed, 536 insertions(+) create mode 100644 src/plugins/sensors/dummy/dummy.pri create mode 100644 src/plugins/sensors/dummy/dummy.pro create mode 100644 src/plugins/sensors/dummy/dummyaccelerometer.cpp create mode 100644 src/plugins/sensors/dummy/dummyaccelerometer.h create mode 100644 src/plugins/sensors/dummy/dummycommon.cpp create mode 100644 src/plugins/sensors/dummy/dummycommon.h create mode 100644 src/plugins/sensors/dummy/dummylightsensor.cpp create mode 100644 src/plugins/sensors/dummy/dummylightsensor.h create mode 100644 src/plugins/sensors/dummy/main.cpp (limited to 'src/plugins/sensors/dummy') diff --git a/src/plugins/sensors/dummy/dummy.pri b/src/plugins/sensors/dummy/dummy.pri new file mode 100644 index 00000000..502081c7 --- /dev/null +++ b/src/plugins/sensors/dummy/dummy.pri @@ -0,0 +1,9 @@ +HEADERS += dummycommon.h\ + dummyaccelerometer.h\ + dummylightsensor.h\ + +SOURCES += dummycommon.cpp\ + dummyaccelerometer.cpp\ + dummylightsensor.cpp\ + main.cpp\ + diff --git a/src/plugins/sensors/dummy/dummy.pro b/src/plugins/sensors/dummy/dummy.pro new file mode 100644 index 00000000..3ccfad69 --- /dev/null +++ b/src/plugins/sensors/dummy/dummy.pro @@ -0,0 +1,15 @@ +load(qt_module) + +TARGET = qtsensors_dummy +QT += sensors core + +load(qt_plugin) + +DESTDIR = $$QT.sensors.plugins/sensors + +include(dummy.pri) + +unix:LIBS+=-lrt + +target.path += $$[QT_INSTALL_PLUGINS]/sensors +INSTALLS += target diff --git a/src/plugins/sensors/dummy/dummyaccelerometer.cpp b/src/plugins/sensors/dummy/dummyaccelerometer.cpp new file mode 100644 index 00000000..5db80f52 --- /dev/null +++ b/src/plugins/sensors/dummy/dummyaccelerometer.cpp @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "dummyaccelerometer.h" +#include +#include + +char const * const dummyaccelerometer::id("dummy.accelerometer"); + +dummyaccelerometer::dummyaccelerometer(QSensor *sensor) + : dummycommon(sensor) +{ + setReading(&m_reading); + addDataRate(100, 100); // 100Hz +} + +void dummyaccelerometer::poll() +{ + m_reading.setTimestamp(getTimestamp()); + // Your average desktop computer doesn't move :) + m_reading.setX(0); + m_reading.setY(9.8); // facing the user, gravity goes here + m_reading.setZ(0); + + newReadingAvailable(); +} + diff --git a/src/plugins/sensors/dummy/dummyaccelerometer.h b/src/plugins/sensors/dummy/dummyaccelerometer.h new file mode 100644 index 00000000..2293d53e --- /dev/null +++ b/src/plugins/sensors/dummy/dummyaccelerometer.h @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DUMMYACCELEROMETER_H +#define DUMMYACCELEROMETER_H + +#include "dummycommon.h" +#include + +class dummyaccelerometer : public dummycommon +{ +public: + static char const * const id; + + dummyaccelerometer(QSensor *sensor); + + void poll(); +private: + QAccelerometerReading m_reading; +}; + +#endif + diff --git a/src/plugins/sensors/dummy/dummycommon.cpp b/src/plugins/sensors/dummy/dummycommon.cpp new file mode 100644 index 00000000..643342d4 --- /dev/null +++ b/src/plugins/sensors/dummy/dummycommon.cpp @@ -0,0 +1,116 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "dummycommon.h" + +#ifdef Q_OS_WINCE +#include +// WINCE has but using clock() gives a link error because +// the function isn't actually implemented. +#else +#include +#endif + +dummycommon::dummycommon(QSensor *sensor) + : QSensorBackend(sensor) + , m_timerid(0) +{ +} + +void dummycommon::start() +{ + if (m_timerid) + return; + + int dataRate = sensor()->dataRate(); + if (dataRate == 0) { + if (sensor()->availableDataRates().count()) + // Use the first available rate when -1 is chosen + dataRate = sensor()->availableDataRates().first().first; + else + dataRate = 1; + } + + int interval = 1000 / dataRate; + + if (interval) + m_timerid = startTimer(interval); +} + +void dummycommon::stop() +{ + if (m_timerid) { + killTimer(m_timerid); + m_timerid = 0; + } +} + +void dummycommon::timerEvent(QTimerEvent * /*event*/) +{ + poll(); +} + +quint64 dummycommon::getTimestamp() +{ +#ifdef Q_OS_WINCE + // This implementation is based on code found here: + // http://social.msdn.microsoft.com/Forums/en/vssmartdevicesnative/thread/74870c6c-76c5-454c-8533-812cfca585f8 + HANDLE currentThread = GetCurrentThread(); + FILETIME creationTime, exitTime, kernalTime, userTime; + GetThreadTimes(currentThread, &creationTime, &exitTime, &kernalTime, &userTime); + + ULARGE_INTEGER uli; + uli.LowPart = userTime.dwLowDateTime; + uli.HighPart = userTime.dwHighDateTime; + ULONGLONG systemTimeInMS = uli.QuadPart/10000; + return static_cast(systemTimeInMS); +#else + struct timespec tv; + int ok; + + ok = clock_gettime(CLOCK_MONOTONIC, &tv); + Q_ASSERT(ok == 0); + + quint64 result = (tv.tv_sec * 1000000ULL) + (tv.tv_nsec * 0.001); // scale to microseconds + return result; +#endif +} + diff --git a/src/plugins/sensors/dummy/dummycommon.h b/src/plugins/sensors/dummy/dummycommon.h new file mode 100644 index 00000000..e0a0e025 --- /dev/null +++ b/src/plugins/sensors/dummy/dummycommon.h @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DUMMYCOMMON_H +#define DUMMYCOMMON_H + +#include + +class dummycommon : public QSensorBackend +{ +public: + dummycommon(QSensor *sensor); + + void start(); + void stop(); + virtual void poll() = 0; + void timerEvent(QTimerEvent * /*event*/); + +protected: + quint64 getTimestamp(); + +private: + int m_timerid; +}; + +#endif + diff --git a/src/plugins/sensors/dummy/dummylightsensor.cpp b/src/plugins/sensors/dummy/dummylightsensor.cpp new file mode 100644 index 00000000..a4bc6fe8 --- /dev/null +++ b/src/plugins/sensors/dummy/dummylightsensor.cpp @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "dummylightsensor.h" +#include +#include + +char const * const dummylightsensor::id("dummy.lightsensor"); + +dummylightsensor::dummylightsensor(QSensor *sensor) + : dummycommon(sensor) +{ + setReading(&m_reading); + addDataRate(100,100); +} + +void dummylightsensor::poll() +{ + m_reading.setTimestamp(getTimestamp()); + if ((qrand() % 100) == 0) + m_reading.setLightLevel(QAmbientLightReading::Dark); + else + m_reading.setLightLevel(QAmbientLightReading::Light); + + newReadingAvailable(); +} + diff --git a/src/plugins/sensors/dummy/dummylightsensor.h b/src/plugins/sensors/dummy/dummylightsensor.h new file mode 100644 index 00000000..ba5be132 --- /dev/null +++ b/src/plugins/sensors/dummy/dummylightsensor.h @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DUMMYLIGHTSENSOR_H +#define DUMMYLIGHTSENSOR_H + +#include "dummycommon.h" +#include + +class dummylightsensor : public dummycommon +{ +public: + static char const * const id; + + dummylightsensor(QSensor *sensor); + + void poll(); +private: + QAmbientLightReading m_reading; +}; + +#endif + diff --git a/src/plugins/sensors/dummy/main.cpp b/src/plugins/sensors/dummy/main.cpp new file mode 100644 index 00000000..b074a0eb --- /dev/null +++ b/src/plugins/sensors/dummy/main.cpp @@ -0,0 +1,79 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the Qt Mobility Components. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "dummyaccelerometer.h" +#include "dummylightsensor.h" +#include +#include +#include +#include +#include + +class dummySensorPlugin : public QObject, public QSensorPluginInterface, public QSensorBackendFactory +{ + Q_OBJECT + Q_INTERFACES(QSensorPluginInterface) +public: + void registerSensors() + { + qDebug() << "loaded the dummy plugin"; + QSensorManager::registerBackend(QAccelerometer::type, dummyaccelerometer::id, this); + QSensorManager::registerBackend(QAmbientLightSensor::type, dummylightsensor::id, this); + } + + QSensorBackend *createBackend(QSensor *sensor) + { + if (sensor->identifier() == dummyaccelerometer::id) { + return new dummyaccelerometer(sensor); + } + + if (sensor->identifier() == dummylightsensor::id) { + return new dummylightsensor(sensor); + } + + return 0; + } +}; + +Q_EXPORT_PLUGIN2(qtsensors_dummy, dummySensorPlugin) + +#include "main.moc" + -- cgit v1.2.3