Sensor Scol plugin
Multi platform sensors for handled devices
SQSensor.cpp
1
2#include <map>
3#include <vector>
4#include <QGuiApplication>
5#include <QScreen>
6
7#include "SSensor.h"
8#include "SQSensor.h"
9#include "tools/SO3Util.h"
10#include <scolPlugin.h>
11
12#ifdef SCOL_STATIC
13# include <QtPlugin>
14
15# if defined(APPLE_IOS)
16Q_IMPORT_PLUGIN(IOSSensorPlugin)
17# elif defined(LINUX) || defined(RPI)
18Q_IMPORT_PLUGIN(LinuxSensorPlugin)
19# else
20Q_IMPORT_PLUGIN(genericSensorPlugin)
21# endif
22
23#endif
24
25/* * * * * * * * * * * * * EVENT CALLBACK * * * * * * * * * * * * */
26
27SSensorManager* SQSensorManager::mSingletonInstance;
28
29
30/* * * * * * * * * * * * * SENSOR * * * * * * * * * * * * */
31
32SQSensor::SQSensor() : QObject(QCoreApplication::instance()), SSensor()
33{
34 mQSensor = NULL;
35}
36
37SQSensor::SQSensor(SSensorType type) : QObject(QCoreApplication::instance()), SSensor(type)
38{
39 switch (type)
40 {
41 case SSensorType::SSENSOR_TYPE_ACCELEROMETER:
42 {
43 mQSensor = new QAccelerometer(this);
44 //static_cast<QAccelerometer*>(mQSensor)->setAccelerationMode(QAccelerometer::Combined);
45 break;
46 }
47
48 case SSensorType::SSENSOR_TYPE_GYROSCOPE:
49 {
50 mQSensor = new QGyroscope(this);
51 break;
52 }
53
54 case SSensorType::SSENSOR_TYPE_MAGNETIC_FIELD:
55 {
56 mQSensor = new QMagnetometer(this);
57 break;
58 }
59
60 case SSensorType::SSENSOR_TYPE_LIGHT:
61 {
62 mQSensor = new QLightSensor(this);
63 break;
64 }
65
66 case SSensorType::SSENSOR_TYPE_PROXIMITY:
67 {
68 mQSensor = new QProximitySensor(this);
69 break;
70 }
71
72 default:
73 mQSensor = NULL;
74 }
75
76 if (mQSensor)
77 {
78 connect(mQSensor, SIGNAL(readingChanged()), this, SLOT(updateData()));
79 mQSensor->connectToBackend();
80 }
81}
82
83SQSensor::~SQSensor()
84{
85 if (mQSensor)
86 mQSensor->stop();
87
88 SAFE_DELETE(mQSensor);
89}
90
91
92void SQSensor::updateData()
93{
94 if (mEnabled && mQSensor->isActive())
95 {
96 SSensorData formatedData;
97 memset(&formatedData, 0, sizeof(formatedData));
98 SSensorManager* manager = SSensorManager::GetInstance();
99
100 QSensorReading* reading = mQSensor->reading();
101 formatedData.delta = GetElapsedTime(reading->timestamp());
102
103 switch (mType)
104 {
105 case SSensorType::SSENSOR_TYPE_ACCELEROMETER:
106 {
107 QAccelerometerReading* reading = static_cast<QAccelerometerReading*>(reading);
108 formatedData.vec3.x = reading->x();
109 formatedData.vec3.y = reading->y();
110 formatedData.vec3.z = reading->z();
111
112 CallSensorCb(formatedData);
113 if (manager->IsEnabled(SSENSOR_TYPE_ORIENTATION))
114 {
115 if (OrientationTracker* orientationTracker = manager->GetOrientationTracker())
116 {
117 orientationTracker->processAcceleration(formatedData.vec3, GetTickCountNano());
118 }
119 }
120 break;
121 }
122 case SSensorType::SSENSOR_TYPE_GYROSCOPE:
123 {
124 QGyroscopeReading* reading = static_cast<QGyroscopeReading*>(reading);
125 //convert to radian
126 formatedData.vec3.x = reading->x() * DEG_TO_RAD;
127 formatedData.vec3.y = reading->y() * DEG_TO_RAD;
128 formatedData.vec3.z = reading->z() * DEG_TO_RAD;
129
130 CallSensorCb(formatedData);
131 //qDebug("Current device gyro: x=%f y=%f z=%f", formatedData.vec3.x, formatedData.vec3.y, formatedData.vec3.z);
132 if (manager->IsEnabled(SSENSOR_TYPE_ORIENTATION))
133 {
134 if (OrientationTracker* orientationTracker = manager->GetOrientationTracker())
135 {
136 orientationTracker->processGyro(formatedData.vec3, GetTickCountNano());
137 }
138 }
139 break;
140 }
141 case SSensorType::SSENSOR_TYPE_MAGNETIC_FIELD:
142 {
143 QMagnetometerReading* reading = static_cast<QMagnetometerReading*>(reading);
144 formatedData.vec3.x = reading->x();
145 formatedData.vec3.y = reading->y();
146 formatedData.vec3.z = reading->z();
147
148 CallSensorCb(formatedData);
149 /*if (manager->IsEnabled(SSENSOR_TYPE_ORIENTATION))
150 {
151 if (OrientationTracker* orientationTracker = manager->GetOrientationTracker())
152 {
153 orientationTracker->processMag(formatedData.vec3, GetTickCountNano());
154 }
155 }*/
156 break;
157 }
158 case SSensorType::SSENSOR_TYPE_LIGHT:
159 {
160 QLightReading* reading = static_cast<QLightReading*>(reading);
161 formatedData.fval = reading->lux();
162
163 CallSensorCb(formatedData);
164 break;
165 }
166 case SSensorType::SSENSOR_TYPE_PROXIMITY:
167 {
168 QProximityReading* reading = static_cast<QProximityReading*>(reading);
169 formatedData.fval = (reading->close()) ? 1.0f : 0.0f;
170
171 CallSensorCb(formatedData);
172 break;
173 }
174 default:
175 break;
176
177 // >>>>> ADD HERE NEW SENSOR TYPES TO BE SUPPORTED <<<<<
178 }
179 SetData(formatedData);
180 }
181}
182
183SSensorData SQSensor::GetData()
184{
185 return mData;
186}
187
188void SQSensor::SetData(SSensorData newData)
189{
190 mData = newData;
191}
192
193QSensor* SQSensor::GetQSensor()
194{
195 return mQSensor;
196}
197
198void SQSensor::SetQSensor(QSensor* aSensor)
199{
200 mQSensor = aSensor;
201}
202
203
204/* * * * * * * * * * * * * SENSOR MANAGER * * * * * * * * * * * * */
205
206SQSensorManager::SQSensorManager() : SSensorManager()
207{
208 mSensorList = std::set<SSensor*>();
209 QList<QByteArray> sensorTypes = QSensor::sensorTypes();
210
211 if (sensorTypes.contains(QAccelerometer::type))
212 {
213 MMechostr(MSKDEBUG, "SSensorManager() : an accelerometer has been found\n");
214 SQSensor* sensor = new SQSensor(SSensorType::SSENSOR_TYPE_ACCELEROMETER);
215 mSensorList.insert(sensor);
216 }
217
218 if (sensorTypes.contains(QGyroscope::type))
219 {
220 MMechostr(MSKDEBUG, "SSensorManager() : a gyrometer has been found\n");
221 SQSensor* sensor = new SQSensor(SSensorType::SSENSOR_TYPE_GYROSCOPE);
222 mSensorList.insert(sensor);
223 }
224
225 if (sensorTypes.contains(QMagnetometer::type))
226 {
227 MMechostr(MSKDEBUG, "SSensorManager() : a magnetometer has been found\n");
228 SQSensor* sensor = new SQSensor(SSensorType::SSENSOR_TYPE_MAGNETIC_FIELD);
229 mSensorList.insert(sensor);
230 }
231
232 if (sensorTypes.contains(QLightSensor::type))
233 {
234 MMechostr(MSKDEBUG, "SSensorManager() : a light sensor has been found\n");
235 SQSensor* sensor = new SQSensor(SSensorType::SSENSOR_TYPE_LIGHT);
236 mSensorList.insert(sensor);
237 }
238
239 if (sensorTypes.contains(QIRProximitySensor::type))
240 {
241 MMechostr(MSKDEBUG, "SSensorManager() : a proximity sensor has been found\n");
242 SQSensor* sensor = new SQSensor(SSensorType::SSENSOR_TYPE_PROXIMITY);
243 mSensorList.insert(sensor);
244 }
245}
246
247SQSensorManager::~SQSensorManager()
248{
249 for (std::set<SSensor*>::iterator it = mSensorList.begin(); it != mSensorList.end(); ++it)
250 {
251 SQSensor* sensor = (SQSensor*) (*it);
252 SAFE_DELETE(sensor);
253 }
254
255 mSingletonInstance = NULL;
256}
257
258int SQSensorManager::SetSensorEnable(SSensorType sensorType, bool state)
259{
260 boost::shared_lock< boost::shared_mutex > lock(mGlobalMutex);
261 // SOFTWARE SENSORS
262 if (sensorType == SSENSOR_TYPE_ORIENTATION && IsAvailable(SSENSOR_TYPE_ORIENTATION))
263 {
264 if (state)
265 {
266 int orient = ROTATION_0;
267 Qt::ScreenOrientation qrot = QGuiApplication::primaryScreen()->primaryOrientation();
268 switch(qrot)
269 {
270 case Qt::LandscapeOrientation:
271 orient = ROTATION_90;
272 break;
273
274 case Qt::InvertedPortraitOrientation:
275 orient = ROTATION_180;
276 break;
277
278 case Qt::InvertedLandscapeOrientation:
279 orient = ROTATION_270;
280 break;
281
282 default:
283 orient = ROTATION_0;
284 }
285
286 GetOrientationTracker()->startTracking((DeviceOrientation)orient);
287 }
288 else
289 GetOrientationTracker()->stopTracking();
290
291 SetSensorEnable(SSENSOR_TYPE_MAGNETIC_FIELD, state);
292 return SetSensorEnable(SSENSOR_TYPE_ACCELEROMETER, state) + SetSensorEnable(SSENSOR_TYPE_GYROSCOPE, state);
293 }
294 // PHYSICAL SENSORS
295 else
296 {
297 SSensor* sensor = GetSensorByType(sensorType);
298 if (sensor)
299 {
300 QSensor* aSensor = ((SQSensor*) sensor)->GetQSensor();
301 if (aSensor != NULL)
302 {
303 sensor->SetEnabled(state);
304 if (state)
305 {
306 aSensor->setDataRate(100); //hz
307 aSensor->start();
308 }
309 else
310 {
311 aSensor->stop();
312 }
313 return 1;
314 }
315 else
316 {
317 // ERROR: NO ASENSOR FOUND
318 return -1;
319 }
320 }
321 else
322 {
323 // ERROR: NO SENSOR FOUND
324 return -2;
325 }
326 }
327}
328
329void SQSensorManager::Vibrate(long millis)
330{
331}
332
333void SQSensorManager::StartVibration()
334{
335}
336
337void SQSensorManager::VibratePattern(std::vector<int> pattern, bool loop)
338{
339 //for (unsigned int i = 0; i < pattern.size(); i++)
340 //t[i] = (jlong)pattern[i];
341}
342
343void SQSensorManager::StopVibration()
344{
345}
346
347void SQSensorManager::StartLocationService()
348{
349
350}
351
352void SQSensorManager::StopLocationService()
353{
354
355}
356
357bool SQSensorManager::GetLocation(float &longitude, float &latitude, float &altitude)
358{
359 return false;
360}