-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrixsensorbase.cpp
245 lines (218 loc) · 6.57 KB
/
matrixsensorbase.cpp
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright (C) 2015, 2016 Canonical Ltd
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License version 3 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Inspired by qtmatrix module
#include "matrixsensorbase.h"
#include "qsensors-matrixpluginprivate_p.h"
#include <QFile>
#include <QStandardPaths>
#include <QSensorBackend>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#define RADIANS_TO_DEGREES 57.2957795
#define STANDARD_GRAVITY 9.80665
#define RADIANS_TO_DEGREES 57.2957795
static const int MAX_READ_ATTEMPTS = 5;
//Q_LOGGING_CATEGORY(qMatrix, "sensor.matrix")
quint64 produceTimestamp()
{
struct timespec tv;
int ok;
#ifdef CLOCK_MONOTONIC_RAW
ok = clock_gettime(CLOCK_MONOTONIC_RAW, &tv);
if (ok != 0)
#endif
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;
}
QMatrixSensorsPrivate::QMatrixSensorsPrivate(MatrixSensorBase *q_ptr)
: q(q_ptr),
matrixIOBus(Q_NULLPTR),
imuInited(false),
humidityInited(false),
pressureInited(false),
temperatureFromHumidity(true)
{
}
QMatrixSensorsPrivate::~QMatrixSensorsPrivate()
{
delete matrixIOBus;
matrixIOBus = 0;
}
bool QMatrixSensorsPrivate::open()
{
if (!matrixIOBus) {
matrixIOBus = new matrix_hal::MatrixIOBus();
}
if (matrixIOBus && matrixIOBus->Init()) {
switch (q->sensorFlag) {
case MatrixSensorBase::Pressure:
case MatrixSensorBase::Altimeter:
pressureSensor.Setup(matrixIOBus);
break;
case MatrixSensorBase::Temperature:
humiditySensor.Setup(matrixIOBus);
break;
case MatrixSensorBase::Uv:
uvSensor.Setup(matrixIOBus);
break;
default:
imuSensor.Setup(matrixIOBus);
break;
};
} else {
qWarning() << "Error SpiInit";
return false;
}
return true;
}
void QMatrixSensorsPrivate::update(MatrixSensorBase::UpdateFlag what)
{
switch (what) {
case MatrixSensorBase::Pressure:
{
pressureSensor.Read(&pressureData);
qreal pascals = (qreal)pressureData.pressure * 0.01;
if (pressure.pressure() != pascals) {
pressure.setTimestamp(produceTimestamp());
pressure.setPressure(pascals);
emit q->pressureChanged(pressure);
}
}
break;
case MatrixSensorBase::Temperature:
{
humiditySensor.Read(&humidityData);
if (temperature.temperature() != (qreal)humidityData.temperature) {
temperature.setTemperature((qreal)humidityData.temperature);
temperature.setTimestamp(produceTimestamp());
emit q->temperatureChanged(temperature);
}
}
break;
case MatrixSensorBase::Altimeter:
{
pressureSensor.Read(&pressureData);
if (altitude.altitude() != (qreal)pressureData.altitude) {
altitude.setAltitude((qreal)pressureData.altitude);
altitude.setTimestamp(produceTimestamp());
emit q->altitudeChanged(altitude);
}
}
break;
case MatrixSensorBase::Acceleration:
{
imuSensor.Read(&imuData);
acceleration.setTimestamp(produceTimestamp());
acceleration.setX((qreal)imuData.accel_x * STANDARD_GRAVITY);
acceleration.setY((qreal)imuData.accel_y * STANDARD_GRAVITY);
acceleration.setZ((qreal)imuData.accel_z * STANDARD_GRAVITY);
emit q->accelerationChanged(acceleration);
}
break;
case MatrixSensorBase::Gyro:
{
imuSensor.Read(&imuData);
gyro.setTimestamp(produceTimestamp());
gyro.setX((qreal)imuData.gyro_x * RADIANS_TO_DEGREES);
gyro.setY((qreal)imuData.gyro_y * RADIANS_TO_DEGREES);
gyro.setZ((qreal)imuData.gyro_z * RADIANS_TO_DEGREES);
emit q->gyroChanged(gyro);
}
break;
case MatrixSensorBase::Magnetometer:
{
imuSensor.Read(&imuData);
mag.setTimestamp(produceTimestamp());
mag.setX((qreal)imuData.mag_x * .000001);
mag.setY((qreal)imuData.mag_y * .000001);
mag.setZ((qreal)imuData.mag_z * .000001);
emit q->magnetometerChanged(mag);
}
break;
case MatrixSensorBase::Uv:
{
uvSensor.Read(&uvData);
lux.setTimestamp(produceTimestamp());
lux.setLux((qreal)uvData.uv);
emit q->uvChanged(lux);
}
break;
default:
break;
};
}
static inline qreal toDeg360(qreal rad)
{
const qreal deg = rad * RADIANS_TO_DEGREES;
return deg < 0 ? deg + 360 : deg;
}
MatrixSensorBase::MatrixSensorBase(QSensor *sensor)
: QSensorBackend(sensor),
d_ptr(new QMatrixSensorsPrivate(this))
{
}
MatrixSensorBase::~MatrixSensorBase()
{
}
bool MatrixSensorBase::needsImu(MatrixSensorBase::UpdateFlag sensorFlag)
{
bool isImu = false;
switch (sensorFlag) {
case MatrixSensorBase::Gyro:
case MatrixSensorBase::Acceleration:
case MatrixSensorBase::Compass:
case MatrixSensorBase::Orientation:
case MatrixSensorBase::Magnetometer:
isImu = true;
break;
default:
break;
};
return isImu;
}
void MatrixSensorBase::start()
{
d_ptr->imuInited = needsImu(sensorFlag);
if (d_ptr->open()) {
if (d_ptr->pollInterval > 100)
d_ptr->pollTimer.setInterval(50);
else
d_ptr->pollTimer.setInterval(d_ptr->pollInterval);
connect(&d_ptr->pollTimer, &QTimer::timeout, [this] { d_ptr->update(sensorFlag); });
d_ptr->update(sensorFlag);
} else {
qWarning() << Q_FUNC_INFO << "Sensor not opened";
sensorError(-ENODEV);
stop();
return;
}
d_ptr->pollTimer.start();
}
void MatrixSensorBase::stop()
{
if (d_ptr->imuInited)
d_ptr->pollTimer.stop();
sensorStopped();
}
bool MatrixSensorBase::isFeatureSupported(QSensor::Feature /*feature*/) const
{
return false;
}
void MatrixSensorBase::poll(MatrixSensorBase::UpdateFlag sensorFlag)
{
d_ptr->update(sensorFlag);
}