summaryrefslogtreecommitdiffstats
path: root/src/qtmsensors/backendwrapper.cpp
blob: 5bba7c4cfc549be8b17ef7b4abd22f41951c7fc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "backendwrapper_p.h"
#include "qsensormanager_p.h"
#include <QDebug>

QT_BEGIN_NAMESPACE
QTM_BEGIN_NAMESPACE

class WrapperFactory : public QSensorPluginInterface,
                       public QSensorBackendFactory
{
public:
    void registerSensors()
    {
        QList<QByteArray> 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<QAmbientLightReading::LightLevel>(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<QOrientationReading::Orientation>(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<QTapReading::TapDirection>(sensor->reading()->tapDirection()));
                  m_reading.setDoubleTap(sensor->reading()->isDoubleTap());
                  newReadingAvailable();
                  })

#include "moc_backendwrapper_p.cpp"
QTM_END_NAMESPACE
QT_END_NAMESPACE