From 4eff9a96845e3e87b12ea036bb5d32cbc05fd47a Mon Sep 17 00:00:00 2001 From: Gatis Paeglis Date: Mon, 2 Sep 2013 08:51:10 +0200 Subject: Introducing eAndroid sensor plugin Change-Id: I8d0bad4ca759cf6d4f6f1f0ef67e6251b3b0e42f Reviewed-by: Eirik Aavitsland --- src/plugins/sensors/eandroid/eandroid.pro | 30 ++++ .../sensors/eandroid/eandroidaccelerometer.cpp | 20 +++ .../sensors/eandroid/eandroidaccelerometer.h | 20 +++ .../eandroid/eandroidambientlightsensor.cpp | 29 ++++ .../sensors/eandroid/eandroidambientlightsensor.h | 20 +++ .../sensors/eandroid/eandroidbasesensor.cpp | 41 +++++ src/plugins/sensors/eandroid/eandroidbasesensor.h | 22 +++ src/plugins/sensors/eandroid/eandroidgyroscope.cpp | 22 +++ src/plugins/sensors/eandroid/eandroidgyroscope.h | 20 +++ src/plugins/sensors/eandroid/eandroidlight.cpp | 18 ++ src/plugins/sensors/eandroid/eandroidlight.h | 20 +++ .../sensors/eandroid/eandroidmagnetometer.cpp | 39 +++++ .../sensors/eandroid/eandroidmagnetometer.h | 20 +++ .../sensors/eandroid/eandroidrotationsensor.cpp | 24 +++ .../sensors/eandroid/eandroidrotationsensor.h | 20 +++ .../sensors/eandroid/eandroidsensordevice.cpp | 182 +++++++++++++++++++++ .../sensors/eandroid/eandroidsensordevice.h | 80 +++++++++ src/plugins/sensors/eandroid/main.cpp | 142 ++++++++++++++++ src/plugins/sensors/eandroid/plugin.json | 1 + 19 files changed, 770 insertions(+) create mode 100644 src/plugins/sensors/eandroid/eandroid.pro create mode 100644 src/plugins/sensors/eandroid/eandroidaccelerometer.cpp create mode 100644 src/plugins/sensors/eandroid/eandroidaccelerometer.h create mode 100644 src/plugins/sensors/eandroid/eandroidambientlightsensor.cpp create mode 100644 src/plugins/sensors/eandroid/eandroidambientlightsensor.h create mode 100644 src/plugins/sensors/eandroid/eandroidbasesensor.cpp create mode 100644 src/plugins/sensors/eandroid/eandroidbasesensor.h create mode 100644 src/plugins/sensors/eandroid/eandroidgyroscope.cpp create mode 100644 src/plugins/sensors/eandroid/eandroidgyroscope.h create mode 100644 src/plugins/sensors/eandroid/eandroidlight.cpp create mode 100644 src/plugins/sensors/eandroid/eandroidlight.h create mode 100644 src/plugins/sensors/eandroid/eandroidmagnetometer.cpp create mode 100644 src/plugins/sensors/eandroid/eandroidmagnetometer.h create mode 100644 src/plugins/sensors/eandroid/eandroidrotationsensor.cpp create mode 100644 src/plugins/sensors/eandroid/eandroidrotationsensor.h create mode 100644 src/plugins/sensors/eandroid/eandroidsensordevice.cpp create mode 100644 src/plugins/sensors/eandroid/eandroidsensordevice.h create mode 100644 src/plugins/sensors/eandroid/main.cpp create mode 100644 src/plugins/sensors/eandroid/plugin.json diff --git a/src/plugins/sensors/eandroid/eandroid.pro b/src/plugins/sensors/eandroid/eandroid.pro new file mode 100644 index 0000000..3b15f74 --- /dev/null +++ b/src/plugins/sensors/eandroid/eandroid.pro @@ -0,0 +1,30 @@ +TARGET = qtsensors_eandroid +QT = core sensors + +PLUGIN_TYPE = sensors +load(qt_plugin) + +LIBS += -lhardware + +HEADERS += \ + eandroidambientlightsensor.h \ + eandroidsensordevice.h \ + eandroidbasesensor.h \ + eandroidaccelerometer.h \ + eandroidlight.h \ + eandroidgyroscope.h \ + eandroidmagnetometer.h \ + eandroidrotationsensor.h + +SOURCES += \ + main.cpp \ + eandroidambientlightsensor.cpp \ + eandroidsensordevice.cpp \ + eandroidbasesensor.cpp \ + eandroidaccelerometer.cpp \ + eandroidlight.cpp \ + eandroidgyroscope.cpp \ + eandroidmagnetometer.cpp \ + eandroidrotationsensor.cpp + +OTHER_FILES = plugin.json diff --git a/src/plugins/sensors/eandroid/eandroidaccelerometer.cpp b/src/plugins/sensors/eandroid/eandroidaccelerometer.cpp new file mode 100644 index 0000000..1837990 --- /dev/null +++ b/src/plugins/sensors/eandroid/eandroidaccelerometer.cpp @@ -0,0 +1,20 @@ +#include + +EAndroidAccelerometer::EAndroidAccelerometer(int type, QSensor *sensor) + : EAndroidBaseSensor(type, sensor) +{ + setReading(&m_reading); +} + +EAndroidAccelerometer::~EAndroidAccelerometer() +{ +} + +void EAndroidAccelerometer::processEvent(sensors_event_t &event) +{ + m_reading.setTimestamp(event.timestamp / 1000); + m_reading.setX(event.data[0]); + m_reading.setY(event.data[1]); + m_reading.setZ(event.data[2]); + newReadingAvailable(); +} diff --git a/src/plugins/sensors/eandroid/eandroidaccelerometer.h b/src/plugins/sensors/eandroid/eandroidaccelerometer.h new file mode 100644 index 0000000..f3c618d --- /dev/null +++ b/src/plugins/sensors/eandroid/eandroidaccelerometer.h @@ -0,0 +1,20 @@ +#ifndef EANDROIDACCELEROMETER_H +#define EANDROIDACCELEROMETER_H + +#include + +#include + +class EAndroidAccelerometer : public EAndroidBaseSensor +{ + Q_OBJECT +public: + EAndroidAccelerometer(int type, QSensor *sensor); + ~EAndroidAccelerometer(); + void processEvent(sensors_event_t &event); + +private: + QAccelerometerReading m_reading; +}; + +#endif // EANDROIDACCELEROMETER_H diff --git a/src/plugins/sensors/eandroid/eandroidambientlightsensor.cpp b/src/plugins/sensors/eandroid/eandroidambientlightsensor.cpp new file mode 100644 index 0000000..212733c --- /dev/null +++ b/src/plugins/sensors/eandroid/eandroidambientlightsensor.cpp @@ -0,0 +1,29 @@ +#include "eandroidambientlightsensor.h" + +EAndroidAmbientLightSensor::EAndroidAmbientLightSensor(int type, QSensor *sensor) + : EAndroidBaseSensor(type, sensor) +{ + setReading(&m_reading); +} + +EAndroidAmbientLightSensor::~EAndroidAmbientLightSensor() +{ +} + +void EAndroidAmbientLightSensor::processEvent(sensors_event_t &event) +{ + const int lightLevelLux = event.light; + + if (lightLevelLux < 10) + m_reading.setLightLevel(QAmbientLightReading::Dark); + else if (lightLevelLux < 80) + m_reading.setLightLevel(QAmbientLightReading::Twilight); + else if (lightLevelLux < 400) + m_reading.setLightLevel(QAmbientLightReading::Light); + else if (lightLevelLux < 2500) + m_reading.setLightLevel(QAmbientLightReading::Bright); + else + m_reading.setLightLevel(QAmbientLightReading::Sunny); + + newReadingAvailable(); +} diff --git a/src/plugins/sensors/eandroid/eandroidambientlightsensor.h b/src/plugins/sensors/eandroid/eandroidambientlightsensor.h new file mode 100644 index 0000000..632164d --- /dev/null +++ b/src/plugins/sensors/eandroid/eandroidambientlightsensor.h @@ -0,0 +1,20 @@ +#ifndef EANDROIDAMBIENTLIGHTSENSOR_H +#define EANDROIDAMBIENTLIGHTSENSOR_H + +#include + +#include + +class EAndroidAmbientLightSensor : public EAndroidBaseSensor +{ + Q_OBJECT +public: + EAndroidAmbientLightSensor(int type, QSensor *sensor); + ~EAndroidAmbientLightSensor(); + void processEvent(sensors_event_t &event); + +private: + QAmbientLightReading m_reading; +}; + +#endif // EANDROIDAMBIENTLIGHTSENSOR_H diff --git a/src/plugins/sensors/eandroid/eandroidbasesensor.cpp b/src/plugins/sensors/eandroid/eandroidbasesensor.cpp new file mode 100644 index 0000000..f21961c --- /dev/null +++ b/src/plugins/sensors/eandroid/eandroidbasesensor.cpp @@ -0,0 +1,41 @@ +#include +#include + +EAndroidBaseSensor::EAndroidBaseSensor(int sensorType, QSensor *sensor) + : QSensorBackend(sensor) +{ + m_type = sensorType; + m_isStarted = false; + EAndroidSensorDevice *device = EAndroidSensorDevice::instance(); + setDescription(device->description(sensorType)); +#if 0 // #### TODO - QTBUG-33293 + qint32 maxRate = device->maxDataRate(sensorType); // convert to Hz + addDataRate(1, 70); + addOutputRange(1,55,6); +#endif +} + +EAndroidBaseSensor::~EAndroidBaseSensor() +{ + if (m_isStarted) + stop(); +} + +void EAndroidBaseSensor::start() +{ + EAndroidSensorDevice::instance()->registerListener(m_type, this, sensor()->dataRate()); + m_isStarted = true; +} + +void EAndroidBaseSensor::stop() +{ + if (m_isStarted) { + m_isStarted = false; + EAndroidSensorDevice::instance()->unregisterListener(m_type, this); + } +} + +void EAndroidBaseSensor::processEvent(sensors_event_t &event) +{ + Q_UNUSED(event) +} diff --git a/src/plugins/sensors/eandroid/eandroidbasesensor.h b/src/plugins/sensors/eandroid/eandroidbasesensor.h new file mode 100644 index 0000000..4e282df --- /dev/null +++ b/src/plugins/sensors/eandroid/eandroidbasesensor.h @@ -0,0 +1,22 @@ +#ifndef EANDROIDBASESENSOR_H +#define EANDROIDBASESENSOR_H + +#include +#include + +class EAndroidBaseSensor : public QSensorBackend +{ + Q_OBJECT +public: + EAndroidBaseSensor(int sensorType, QSensor *sensor); + virtual ~EAndroidBaseSensor(); + virtual void start(); + virtual void stop(); + virtual void processEvent(sensors_event_t &event); + +private: + int m_type; + bool m_isStarted; +}; + +#endif // EANDROIDBASESENSOR_H diff --git a/src/plugins/sensors/eandroid/eandroidgyroscope.cpp b/src/plugins/sensors/eandroid/eandroidgyroscope.cpp new file mode 100644 index 0000000..601415f --- /dev/null +++ b/src/plugins/sensors/eandroid/eandroidgyroscope.cpp @@ -0,0 +1,22 @@ +#include "eandroidgyroscope.h" +#include + +EAndroidGyroscope::EAndroidGyroscope(int type, QSensor *sensor) + : EAndroidBaseSensor(type, sensor) +{ + setReading(&m_reading); +} + +EAndroidGyroscope::~EAndroidGyroscope() +{ +} + +void EAndroidGyroscope::processEvent(sensors_event_t &event) +{ + m_reading.setTimestamp(event.timestamp / 1000); + m_reading.setX(event.gyro.x *180/M_PI); + m_reading.setY(event.gyro.y *180/M_PI); + m_reading.setZ(event.gyro.z *180/M_PI); + newReadingAvailable(); +} + diff --git a/src/plugins/sensors/eandroid/eandroidgyroscope.h b/src/plugins/sensors/eandroid/eandroidgyroscope.h new file mode 100644 index 0000000..65c531f --- /dev/null +++ b/src/plugins/sensors/eandroid/eandroidgyroscope.h @@ -0,0 +1,20 @@ +#ifndef EANDROIDGYROSCOPE_H +#define EANDROIDGYROSCOPE_H + +#include + +#include + +class EAndroidGyroscope : public EAndroidBaseSensor +{ + Q_OBJECT +public: + EAndroidGyroscope(int type, QSensor *sensor); + ~EAndroidGyroscope(); + void processEvent(sensors_event_t &event); + +private: + QGyroscopeReading m_reading; +}; + +#endif // EANDROIDGYROSCOPE_H diff --git a/src/plugins/sensors/eandroid/eandroidlight.cpp b/src/plugins/sensors/eandroid/eandroidlight.cpp new file mode 100644 index 0000000..f9cce98 --- /dev/null +++ b/src/plugins/sensors/eandroid/eandroidlight.cpp @@ -0,0 +1,18 @@ +#include + +EAndroidLight::EAndroidLight(int type, QSensor *sensor) + : EAndroidBaseSensor(type, sensor) +{ + setReading(&m_reading); +} + +EAndroidLight::~EAndroidLight() +{ +} + +void EAndroidLight::processEvent(sensors_event_t &event) +{ + m_reading.setTimestamp(event.timestamp / 1000); + m_reading.setLux(event.light); + newReadingAvailable(); +} diff --git a/src/plugins/sensors/eandroid/eandroidlight.h b/src/plugins/sensors/eandroid/eandroidlight.h new file mode 100644 index 0000000..3929dea --- /dev/null +++ b/src/plugins/sensors/eandroid/eandroidlight.h @@ -0,0 +1,20 @@ +#ifndef EANDROIDLIGHT_H +#define EANDROIDLIGHT_H + +#include + +#include + +class EAndroidLight : public EAndroidBaseSensor +{ + Q_OBJECT +public: + EAndroidLight(int type, QSensor *sensor); + ~EAndroidLight(); + void processEvent(sensors_event_t &ev); + +private: + QLightReading m_reading; +}; + +#endif // EANDROIDLIGHT_H diff --git a/src/plugins/sensors/eandroid/eandroidmagnetometer.cpp b/src/plugins/sensors/eandroid/eandroidmagnetometer.cpp new file mode 100644 index 0000000..8747d45 --- /dev/null +++ b/src/plugins/sensors/eandroid/eandroidmagnetometer.cpp @@ -0,0 +1,39 @@ +#include "eandroidmagnetometer.h" + +EAndroidMagnetometer::EAndroidMagnetometer(int type, QSensor *sensor) + : EAndroidBaseSensor(type, sensor) +{ + setReading(&m_reading); +} + +EAndroidMagnetometer::~EAndroidMagnetometer() +{ +} + +void EAndroidMagnetometer::processEvent(sensors_event_t &event) +{ + m_reading.setTimestamp(event.timestamp / 1000); + // convect micro-Tesla to tesla + m_reading.setX(event.magnetic.x / 1e6); + m_reading.setY(event.magnetic.y / 1e6); + m_reading.setZ(event.magnetic.z / 1e6); + + switch (event.magnetic.status) { + case SENSOR_STATUS_UNRELIABLE: + m_reading.setCalibrationLevel(0.0); + break; + case SENSOR_STATUS_ACCURACY_LOW: + m_reading.setCalibrationLevel(0.3); + break; + case SENSOR_STATUS_ACCURACY_MEDIUM: + m_reading.setCalibrationLevel(0.6); + break; + case SENSOR_STATUS_ACCURACY_HIGH: + m_reading.setCalibrationLevel(1.0); + break; + default: + break; + } + + newReadingAvailable(); +} diff --git a/src/plugins/sensors/eandroid/eandroidmagnetometer.h b/src/plugins/sensors/eandroid/eandroidmagnetometer.h new file mode 100644 index 0000000..5cb3842 --- /dev/null +++ b/src/plugins/sensors/eandroid/eandroidmagnetometer.h @@ -0,0 +1,20 @@ +#ifndef EANDROIDMAGNETOMETER_H +#define EANDROIDMAGNETOMETER_H + +#include + +#include + +class EAndroidMagnetometer : public EAndroidBaseSensor +{ + Q_OBJECT +public: + EAndroidMagnetometer(int type, QSensor *sensor); + ~EAndroidMagnetometer(); + void processEvent(sensors_event_t &event); + +private: + QMagnetometerReading m_reading; +}; + +#endif // EANDROIDMAGNETOMETER_H diff --git a/src/plugins/sensors/eandroid/eandroidrotationsensor.cpp b/src/plugins/sensors/eandroid/eandroidrotationsensor.cpp new file mode 100644 index 0000000..0d9bfde --- /dev/null +++ b/src/plugins/sensors/eandroid/eandroidrotationsensor.cpp @@ -0,0 +1,24 @@ +#include "eandroidrotationsensor.h" +#include + +EAndroidRotationSensor::EAndroidRotationSensor(int type, QSensor *sensor) + : EAndroidBaseSensor(type, sensor) +{ + setReading(&m_reading); +} + +EAndroidRotationSensor::~EAndroidRotationSensor() +{ +} + +void EAndroidRotationSensor::processEvent(sensors_event_t &event) +{ + m_reading.setTimestamp(event.timestamp / 1000); + + float rz = -event.data[0] * 180 / M_PI; + float rx = -event.data[1] * 180 / M_PI; + float ry = event.data[2] * 180 / M_PI; + + m_reading.setFromEuler(rx, ry, rz); + newReadingAvailable(); +} diff --git a/src/plugins/sensors/eandroid/eandroidrotationsensor.h b/src/plugins/sensors/eandroid/eandroidrotationsensor.h new file mode 100644 index 0000000..6139001 --- /dev/null +++ b/src/plugins/sensors/eandroid/eandroidrotationsensor.h @@ -0,0 +1,20 @@ +#ifndef EANDROIDROTATIONSENSOR_H +#define EANDROIDROTATIONSENSOR_H + +#include + +#include + +class EAndroidRotationSensor : public EAndroidBaseSensor +{ + Q_OBJECT +public: + EAndroidRotationSensor(int type, QSensor *sensor); + ~EAndroidRotationSensor(); + void processEvent(sensors_event_t &event); + +private: + QRotationReading m_reading; +}; + +#endif // EANDROIDROTATIONSENSOR_H diff --git a/src/plugins/sensors/eandroid/eandroidsensordevice.cpp b/src/plugins/sensors/eandroid/eandroidsensordevice.cpp new file mode 100644 index 0000000..178ef8f --- /dev/null +++ b/src/plugins/sensors/eandroid/eandroidsensordevice.cpp @@ -0,0 +1,182 @@ +#include + +EventReaderThread::EventReaderThread(EAndroidSensorDevice *device) : + m_device(device) +{ +} + +SensorEventArray* EventReaderThread::lock() +{ + m_mutex.lock(); + return &m_events; +} + +void EventReaderThread::run() +{ + static const size_t numEvents = 16; + sensors_event_t buffer[numEvents]; + int err = 0; + int n; + do { + n = m_device->m_sensorDevice->poll(m_device->m_sensorDevice, buffer, numEvents); + if (n < 0) { + qWarning("poll() failed (%s)\n", strerror(-err)); + break; + } + m_mutex.lock(); + for (int i = 0 ; i < n ; i++) { + sensors_event_t& event = buffer[i]; + if (event.version != sizeof(sensors_event_t)) { + qWarning("incorrect event version (version=%d, expected=%d", + event.version, sizeof(sensors_event_t)); + break; + } + m_events.append(event); + } + m_mutex.unlock(); + eventPending(); + } while (true); +} + +EAndroidSensorDevice::EAndroidSensorDevice() + : m_eventThread(0), + m_sensorModule(0), + m_availableSensorsList(0) +{ + initSensorDevice(); + m_eventThread = new EventReaderThread(this); + connect(m_eventThread, SIGNAL(eventPending()), this, + SLOT(processSensorEvents()), Qt::QueuedConnection); +} + +EAndroidSensorDevice* EAndroidSensorDevice::m_instance = 0; +EAndroidSensorDevice* EAndroidSensorDevice::instance() +{ + if (!m_instance) + m_instance = new EAndroidSensorDevice(); + return m_instance; +} + +void EAndroidSensorDevice::registerListener(int type, EAndroidBaseSensor *sensor, int dataRateHz) +{ + bool startReaderThread = m_listenersHash.isEmpty(); + bool enableSensor = m_listenersHash[type].isEmpty(); + m_listenersHash[type].push_back(sensor); + if (startReaderThread) + m_eventThread->start(); + if (enableSensor) { + setActive(type, true); + setDelay(type, dataRateHz); + } +} + +void EAndroidSensorDevice::unregisterListener(int type, EAndroidBaseSensor *sensor) +{ + m_listenersHash[type].removeOne(sensor); + bool disableSensor = m_listenersHash[type].isEmpty(); + if (disableSensor) + m_listenersHash.remove(type); + bool stopReaderThread = m_listenersHash.isEmpty(); + if (stopReaderThread) + m_eventThread->quit(); + if (disableSensor) + setActive(type, false); +} + +int EAndroidSensorDevice::indexForType(int type) const +{ + for (int i = 0; i < m_availableSensors; ++i) + if (m_availableSensorsList[i].type == type) + return i; + qWarning() << "invalid sensor type: " << type; + return -1; +} + +void EAndroidSensorDevice::setActive(int type, bool enable) +{ + for (int i = 0; i < m_availableSensors; i++) { + if (m_availableSensorsList[i].type == type) { + int err = m_sensorDevice->activate(m_sensorDevice, + m_availableSensorsList[i].handle, enable); + if (err != 0) + qWarning("activate() for '%s'failed (%s)\n", + m_availableSensorsList[i].name, strerror(-err)); + } + } +} + +void EAndroidSensorDevice::setDelay(int type, int dataRateHz) const +{ + qint64 ns; + // convert microseconds to nanoseconds + qint32 maxRateNs = maxDataRate(type) * 1000; + if (dataRateHz == 0) { + // if dataRateHz is not set, then we use maxRateNs + ns = maxRateNs; + } else { + // convert Hz to nanoseconds + ns = 1000000000LL / dataRateHz; + if (ns > maxRateNs) + ns = maxRateNs; + } + int index = indexForType(type); + if (index != -1) + m_sensorDevice->setDelay(m_sensorDevice, + m_availableSensorsList[index].handle, ns); + +} + +qint32 EAndroidSensorDevice::maxDataRate(int type) const +{ + // minDelay - minimum delay allowed between events in microseconds + int index = indexForType(type); + if (index != -1) + return m_availableSensorsList[index].minDelay; + return 0; +} + +QString EAndroidSensorDevice::description(int type) const +{ + int index = indexForType(type); + if (index != -1) { + QString desc; + desc.append(m_availableSensorsList[index].name); + desc.append(QString(" (Vendor:%1) ").arg(m_availableSensorsList[index].vendor)); + return desc; + } + return QString(); +} + +int EAndroidSensorDevice::availableSensors(sensor_t const** list) const +{ + return m_sensorModule->get_sensors_list(m_sensorModule, list); +} + +void EAndroidSensorDevice::initSensorDevice() +{ + int err = 0; + err = hw_get_module(SENSORS_HARDWARE_MODULE_ID, + (hw_module_t const**)&m_sensorModule); + if (err != 0) + qWarning("hw_get_module() failed (%s)\n", strerror(-err)); + + if (m_sensorModule) { + err = sensors_open(&m_sensorModule->common, &m_sensorDevice); + if (err != 0) + qWarning("sensors_open() failed (%s)\n", strerror(-err)); + } + m_availableSensors = m_sensorModule->get_sensors_list(m_sensorModule, + &m_availableSensorsList); +} + +void EAndroidSensorDevice::processSensorEvents() const +{ + SensorEventArray *eventqueue = m_eventThread->lock(); + for (int i = 0; i < eventqueue->size(); i++) { + sensors_event_t event = eventqueue->at(i); + foreach (EAndroidBaseSensor *sensorListener, m_listenersHash[event.type]) + sensorListener->processEvent(event); + } + eventqueue->clear(); + m_eventThread->unlock(); +} diff --git a/src/plugins/sensors/eandroid/eandroidsensordevice.h b/src/plugins/sensors/eandroid/eandroidsensordevice.h new file mode 100644 index 0000000..6d9a64b --- /dev/null +++ b/src/plugins/sensors/eandroid/eandroidsensordevice.h @@ -0,0 +1,80 @@ +#ifndef EANDROIDSENSORDEVICE_H +#define EANDROIDSENSORDEVICE_H + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +typedef QVarLengthArray SensorEventArray; +typedef QHash > ListenersHash; + +class EAndroidSensorDevice; + +class EventReaderThread : public QThread +{ + Q_OBJECT +public: + EventReaderThread(EAndroidSensorDevice *manager); + void run(); + SensorEventArray* lock(); + inline void unlock() { m_mutex.unlock(); } + +signals: + void eventPending(); + +private: + EAndroidSensorDevice *m_device; + SensorEventArray m_events; + QMutex m_mutex; +}; + +class EAndroidSensorDevice : public QObject +{ + Q_OBJECT +public: + static EAndroidSensorDevice* instance(); + + void registerListener(int type, EAndroidBaseSensor *sensor, int dataRateHz); + void unregisterListener(int type, EAndroidBaseSensor *sensor); + int availableSensors(sensor_t const** list) const; + qint32 maxDataRate(int type) const; + QString description(int type) const; + +protected: + void initSensorDevice(); + int indexForType(int type) const; + void setActive(int type, bool enable); + void setDelay(int type, int dataRateHz) const; + +public slots: + void processSensorEvents() const; + +private: + static EAndroidSensorDevice* m_instance; + EAndroidSensorDevice(); + EAndroidSensorDevice(const EAndroidSensorDevice &); + EAndroidSensorDevice& operator=(const EAndroidSensorDevice &); + + friend class EventReaderThread; + EventReaderThread *m_eventThread; + + sensors_module_t* m_sensorModule; + sensors_poll_device_t* m_sensorDevice; + sensor_t const* m_availableSensorsList; + int m_availableSensors; + + ListenersHash m_listenersHash; +}; + +#endif // EANDROIDSENSORDEVICE_H diff --git a/src/plugins/sensors/eandroid/main.cpp b/src/plugins/sensors/eandroid/main.cpp new file mode 100644 index 0000000..bea3345 --- /dev/null +++ b/src/plugins/sensors/eandroid/main.cpp @@ -0,0 +1,142 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include +#include +#include +#include + +#include + +class EAndroidSensorPlugin : public QObject, public QSensorPluginInterface, public QSensorBackendFactory +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "com.qt-project.Qt.QSensorPluginInterface/1.0" FILE "plugin.json") + Q_INTERFACES(QSensorPluginInterface) +public: + void registerSensors() + { +#define REGISTER_BACKEND(type, id) \ + QSensorManager::registerBackend(type, id, this); \ + + sensor_t const* sensors; + QByteArray id; + int count = EAndroidSensorDevice::instance()->availableSensors(&sensors); + for (int i = 0; i < count; i++) { + id = QByteArray::number(sensors[i].type); + switch (sensors[i].type) { + case SENSOR_TYPE_ACCELEROMETER: + REGISTER_BACKEND(QAccelerometer::type, id) + break; + case SENSOR_TYPE_MAGNETIC_FIELD: + REGISTER_BACKEND(QMagnetometer::type, id) + break; + case SENSOR_TYPE_ORIENTATION: + break; + case SENSOR_TYPE_GYROSCOPE: + REGISTER_BACKEND(QGyroscope::type, id) + break; + case SENSOR_TYPE_LIGHT: + REGISTER_BACKEND(QLightSensor::type, id); + REGISTER_BACKEND(QAmbientLightSensor::type, id); + break; + case SENSOR_TYPE_PRESSURE: + break; + case SENSOR_TYPE_TEMPERATURE: + break; + case SENSOR_TYPE_PROXIMITY: + break; + case SENSOR_TYPE_GRAVITY: + break; + case SENSOR_TYPE_LINEAR_ACCELERATION: + break; + case SENSOR_TYPE_ROTATION_VECTOR: + REGISTER_BACKEND(QRotationSensor::type, id); + break; + case SENSOR_TYPE_RELATIVE_HUMIDITY: + break; + case SENSOR_TYPE_AMBIENT_TEMPERATURE: + break; + } + } +#undef REGISTER_BACKEND + } + + QSensorBackend *createBackend(QSensor *sensor) + { + int sensorType = sensor->identifier().toInt(); + switch (sensorType) { + case SENSOR_TYPE_ACCELEROMETER: + return new EAndroidAccelerometer(sensorType, sensor); + case SENSOR_TYPE_MAGNETIC_FIELD: + return new EAndroidMagnetometer(sensorType, sensor); + case SENSOR_TYPE_ORIENTATION: + break; + case SENSOR_TYPE_GYROSCOPE: + return new EAndroidGyroscope(sensorType, sensor); + case SENSOR_TYPE_LIGHT: + if (sensor->type() == QAmbientLightSensor::type) + return new EAndroidAmbientLightSensor(sensorType, sensor); + else if (sensor->type() == QLightSensor::type) + return new EAndroidLight(sensorType, sensor); + case SENSOR_TYPE_PRESSURE: + break; + case SENSOR_TYPE_TEMPERATURE: + break; + case SENSOR_TYPE_PROXIMITY: + break; + case SENSOR_TYPE_GRAVITY: + break; + case SENSOR_TYPE_LINEAR_ACCELERATION: + break; + case SENSOR_TYPE_ROTATION_VECTOR: + return new EAndroidRotationSensor(sensorType, sensor); + case SENSOR_TYPE_RELATIVE_HUMIDITY: + break; + case SENSOR_TYPE_AMBIENT_TEMPERATURE: + break; + default: + break; + } + return 0; + } +}; + +#include "main.moc" diff --git a/src/plugins/sensors/eandroid/plugin.json b/src/plugins/sensors/eandroid/plugin.json new file mode 100644 index 0000000..8a55b3a --- /dev/null +++ b/src/plugins/sensors/eandroid/plugin.json @@ -0,0 +1 @@ +{ "Keys": [ "notused" ] } -- cgit v1.2.3