Merge lp:~gerboland/qtubuntu-sensors/add-gyro-magnet-sensors into lp:qtubuntu-sensors

Proposed by Gerry Boland
Status: Approved
Approved by: Lorn Potter
Approved revision: 91
Proposed branch: lp:~gerboland/qtubuntu-sensors/add-gyro-magnet-sensors
Merge into: lp:qtubuntu-sensors
Diff against target: 761 lines (+655/-4)
12 files modified
data/Sensors.conf (+2/-0)
plugins/sensors/CMakeLists.txt (+8/-0)
plugins/sensors/core_gyroscope.cpp (+70/-0)
plugins/sensors/core_gyroscope.h (+49/-0)
plugins/sensors/core_magnetometer.cpp (+70/-0)
plugins/sensors/core_magnetometer.h (+49/-0)
plugins/sensors/core_sensor_plugins.cpp (+18/-0)
plugins/sensors/core_sensor_plugins.h (+0/-4)
plugins/sensors/core_shared_gyroscope.cpp (+131/-0)
plugins/sensors/core_shared_gyroscope.h (+64/-0)
plugins/sensors/core_shared_magnetometer.cpp (+130/-0)
plugins/sensors/core_shared_magnetometer.h (+64/-0)
To merge this branch: bzr merge lp:~gerboland/qtubuntu-sensors/add-gyro-magnet-sensors
Reviewer Review Type Date Requested Status
Lorn Potter (community) Approve
Review via email: mp+282033@code.launchpad.net

Commit message

Enable Gyroscope and Magnetic sensors

To post a comment you must log in.
Revision history for this message
Lorn Potter (lorn-potter) wrote :

\0/

Revision history for this message
Lorn Potter (lorn-potter) wrote :

You will want to add the new sensors to QSensors.conf in data/ in order for these to be available to Qt/QML.

review: Needs Fixing
90. By Gerry Boland

Use lowercase for magnetometer plugin id, match other sensor ids

91. By Gerry Boland

Declare magnetometer & gyroscope sensors as being implemented in the plugin

Revision history for this message
Gerry Boland (gerboland) wrote :

So very sorry about that.

Revision history for this message
Lorn Potter (lorn-potter) wrote :

lgtm

review: Approve
92. By Gerry Boland

Fix typo

93. By Gerry Boland

Typo typo

94. By Gerry Boland

Fix signal name and thus build

95. By Gerry Boland

Fix signal name error

Revision history for this message
Timo Jyrinki (timo-jyrinki) wrote :

Unmerged revisions

95. By Gerry Boland

Fix signal name error

94. By Gerry Boland

Fix signal name and thus build

93. By Gerry Boland

Typo typo

92. By Gerry Boland

Fix typo

91. By Gerry Boland

Declare magnetometer & gyroscope sensors as being implemented in the plugin

90. By Gerry Boland

Use lowercase for magnetometer plugin id, match other sensor ids

89. By Gerry Boland

Merge trunk

88. By Gerry Boland

Enable Gyroscope and Magnetic sensors

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'data/Sensors.conf'
2--- data/Sensors.conf 2014-01-07 07:58:10 +0000
3+++ data/Sensors.conf 2016-03-22 17:02:47 +0000
4@@ -1,3 +1,5 @@
5 [Default]
6 QAccelerometer = core.accelerometer
7 QOrientationSensor = core.orientationsensor
8+QGyroscope = core.gyroscope
9+QMagnetometer = core.magnetometer
10\ No newline at end of file
11
12=== modified file 'plugins/sensors/CMakeLists.txt'
13--- plugins/sensors/CMakeLists.txt 2014-01-10 16:51:27 +0000
14+++ plugins/sensors/CMakeLists.txt 2016-03-22 17:02:47 +0000
15@@ -10,14 +10,22 @@
16 qtubuntu_sensors_plugins SHARED
17
18 core_accelerometer.h
19+ core_gyroscope.h
20+ core_magnetometer.h
21 core_orientation_sensor.h
22 core_sensor_plugins.h
23 core_shared_accelerometer.h
24+ core_shared_gyroscope.h
25+ core_shared_magnetometer.h
26
27 core_accelerometer.cpp
28+ core_gyroscope.cpp
29+ core_magnetometer.cpp
30 core_orientation_sensor.cpp
31 core_sensor_plugins.cpp
32 core_shared_accelerometer.cpp
33+ core_shared_gyroscope.cpp
34+ core_shared_magnetometer.cpp
35
36 ${SENSORS_RESOURCES}
37 )
38
39=== added file 'plugins/sensors/core_gyroscope.cpp'
40--- plugins/sensors/core_gyroscope.cpp 1970-01-01 00:00:00 +0000
41+++ plugins/sensors/core_gyroscope.cpp 2016-03-22 17:02:47 +0000
42@@ -0,0 +1,70 @@
43+/*
44+ * Copyright (C) 2015 Canonical, Ltd.
45+ *
46+ * This program is free software; you can redistribute it and/or modify
47+ * it under the terms of the GNU Lesser General Public License as published by
48+ * the Free Software Foundation; version 3.
49+ *
50+ * This program is distributed in the hope that it will be useful,
51+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
52+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
53+ * GNU Lesser General Public License for more details.
54+ *
55+ * You should have received a copy of the GNU Lesser General Public License
56+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
57+ */
58+
59+#include "core_gyroscope.h"
60+#include "core_shared_gyroscope.h"
61+
62+#include <QDebug>
63+
64+core::Gyroscope::Gyroscope(QSensor *sensor)
65+ : QSensorBackend(sensor)
66+{
67+ // Register the reading instance with the parent
68+ setReading<QGyroscopeReading>(&m_reading);
69+
70+ const qreal minDelay = core::SharedGyroscope::instance().getMinDelay();
71+ if (minDelay > -1)
72+ {
73+ // Min and max sensor sampling frequencies, in Hz
74+ addDataRate(minDelay, minDelay * 10);
75+ }
76+ addOutputRange(core::SharedGyroscope::instance().getMinValue(),
77+ core::SharedGyroscope::instance().getMaxValue(),
78+ core::SharedGyroscope::instance().getResolution());
79+
80+ // Connect to the Gyroscope's readingChanged signal
81+ // This has to be a queued connection as the sensor callback
82+ // from the platform API can happen on an arbitrary thread.
83+ connect(
84+ &core::SharedGyroscope::instance(),
85+ SIGNAL(gyroscopeReadingChanged(QSharedPointer<QGyroscopeReading>)),
86+ this,
87+ SLOT(onGyroscopeReadingChanged(QSharedPointer<QGyroscopeReading>)),
88+ Qt::QueuedConnection);
89+
90+ setDescription(QLatin1String("Gyroscope Sensor"));
91+}
92+
93+void core::Gyroscope::start()
94+{
95+ core::SharedGyroscope::instance().start();
96+}
97+
98+void core::Gyroscope::stop()
99+{
100+ core::SharedGyroscope::instance().stop();
101+}
102+
103+void core::Gyroscope::onGyroscopeReadingChanged(QSharedPointer<QGyroscopeReading> reading)
104+{
105+ // Capture the coordinates from the Gyroscope device
106+ m_reading.setX(reading->x());
107+ m_reading.setY(reading->y());
108+ m_reading.setZ(reading->z());
109+ m_reading.setTimestamp(reading->timestamp());
110+
111+ newReadingAvailable();
112+}
113
114=== added file 'plugins/sensors/core_gyroscope.h'
115--- plugins/sensors/core_gyroscope.h 1970-01-01 00:00:00 +0000
116+++ plugins/sensors/core_gyroscope.h 2016-03-22 17:02:47 +0000
117@@ -0,0 +1,49 @@
118+/*
119+ * Copyright (C) 2015 Canonical, Ltd.
120+ *
121+ * This program is free software; you can redistribute it and/or modify
122+ * it under the terms of the GNU Lesser General Public License as published by
123+ * the Free Software Foundation; version 3.
124+ *
125+ * This program is distributed in the hope that it will be useful,
126+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
127+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128+ * GNU Lesser General Public License for more details.
129+ *
130+ * You should have received a copy of the GNU Lesser General Public License
131+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
132+ */
133+
134+#ifndef CORE_GYROSCOPE_H
135+#define CORE_GYROSCOPE_H
136+
137+#include <QGyroscopeReading>
138+
139+#include <qsensorbackend.h>
140+
141+namespace core
142+{
143+class Gyroscope : public QSensorBackend
144+{
145+ Q_OBJECT
146+public:
147+ inline static const char* id()
148+ {
149+ return "core.gyroscope";
150+ }
151+
152+ Gyroscope(QSensor *sensor);
153+ virtual ~Gyroscope() = default;
154+
155+ void start();
156+ void stop();
157+
158+public Q_SLOTS:
159+ void onGyroscopeReadingChanged(QSharedPointer<QGyroscopeReading> reading);
160+
161+private:
162+ QGyroscopeReading m_reading;
163+};
164+}
165+
166+#endif // CORE_GYROSCOPE_H
167
168=== added file 'plugins/sensors/core_magnetometer.cpp'
169--- plugins/sensors/core_magnetometer.cpp 1970-01-01 00:00:00 +0000
170+++ plugins/sensors/core_magnetometer.cpp 2016-03-22 17:02:47 +0000
171@@ -0,0 +1,70 @@
172+/*
173+ * Copyright (C) 2015 Canonical, Ltd.
174+ *
175+ * This program is free software; you can redistribute it and/or modify
176+ * it under the terms of the GNU Lesser General Public License as published by
177+ * the Free Software Foundation; version 3.
178+ *
179+ * This program is distributed in the hope that it will be useful,
180+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
181+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
182+ * GNU Lesser General Public License for more details.
183+ *
184+ * You should have received a copy of the GNU Lesser General Public License
185+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
186+ */
187+
188+#include "core_magnetometer.h"
189+#include "core_shared_magnetometer.h"
190+
191+#include <QDebug>
192+
193+core::Magnetometer::Magnetometer(QSensor *sensor)
194+ : QSensorBackend(sensor)
195+{
196+ // Register the reading instance with the parent
197+ setReading<QMagnetometerReading>(&m_reading);
198+
199+ const qreal minDelay = core::SharedMagnetometer::instance().getMinDelay();
200+ if (minDelay > -1)
201+ {
202+ // Min and max sensor sampling frequencies, in Hz
203+ addDataRate(minDelay, minDelay * 10);
204+ }
205+ addOutputRange(core::SharedMagnetometer::instance().getMinValue(),
206+ core::SharedMagnetometer::instance().getMaxValue(),
207+ core::SharedMagnetometer::instance().getResolution());
208+
209+ // Connect to the Magnetometer's readingChanged signal
210+ // This has to be a queued connection as the sensor callback
211+ // from the platform API can happen on an arbitrary thread.
212+ connect(
213+ &core::SharedMagnetometer::instance(),
214+ SIGNAL(MagnetometerReadingChanged(QSharedPointer<QMagnetometerReading>)),
215+ this,
216+ SLOT(onMagnetometerReadingChanged(QSharedPointer<QMagnetometerReading>)),
217+ Qt::QueuedConnection);
218+
219+ setDescription(QLatin1String("Magnetometer Sensor"));
220+}
221+
222+void core::Magnetometer::start()
223+{
224+ core::SharedMagnetometer::instance().start();
225+}
226+
227+void core::Magnetometer::stop()
228+{
229+ core::SharedMagnetometer::instance().stop();
230+}
231+
232+void core::Magnetometer::onMagnetometerReadingChanged(QSharedPointer<QMagnetometerReading> reading)
233+{
234+ // Capture the coordinates from the Magnetometer device
235+ m_reading.setX(reading->x());
236+ m_reading.setY(reading->y());
237+ m_reading.setZ(reading->z());
238+ m_reading.setTimestamp(reading->timestamp());
239+
240+ newReadingAvailable();
241+}
242
243=== added file 'plugins/sensors/core_magnetometer.h'
244--- plugins/sensors/core_magnetometer.h 1970-01-01 00:00:00 +0000
245+++ plugins/sensors/core_magnetometer.h 2016-03-22 17:02:47 +0000
246@@ -0,0 +1,49 @@
247+/*
248+ * Copyright (C) 2015 Canonical, Ltd.
249+ *
250+ * This program is free software; you can redistribute it and/or modify
251+ * it under the terms of the GNU Lesser General Public License as published by
252+ * the Free Software Foundation; version 3.
253+ *
254+ * This program is distributed in the hope that it will be useful,
255+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
256+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
257+ * GNU Lesser General Public License for more details.
258+ *
259+ * You should have received a copy of the GNU Lesser General Public License
260+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
261+ */
262+
263+#ifndef CORE_MAGNETOMETER_H
264+#define CORE_MAGNETOMETER_H
265+
266+#include <QMagnetometerReading>
267+
268+#include <qsensorbackend.h>
269+
270+namespace core
271+{
272+class Magnetometer : public QSensorBackend
273+{
274+ Q_OBJECT
275+public:
276+ inline static const char* id()
277+ {
278+ return "core.magnetometer";
279+ }
280+
281+ Magnetometer(QSensor *sensor);
282+ virtual ~Magnetometer() = default;
283+
284+ void start();
285+ void stop();
286+
287+public Q_SLOTS:
288+ void onMagnetometerReadingChanged(QSharedPointer<QMagnetometerReading> reading);
289+
290+private:
291+ QMagnetometerReading m_reading;
292+};
293+}
294+
295+#endif // CORE_MAGNETOMETER_H
296
297=== modified file 'plugins/sensors/core_sensor_plugins.cpp'
298--- plugins/sensors/core_sensor_plugins.cpp 2014-06-18 14:08:07 +0000
299+++ plugins/sensors/core_sensor_plugins.cpp 2016-03-22 17:02:47 +0000
300@@ -15,6 +15,8 @@
301 */
302
303 #include "core_accelerometer.h"
304+#include "core_gyroscope.h"
305+#include "core_magnetometer.h"
306 #include "core_orientation_sensor.h"
307 #include "core_sensor_plugins.h"
308
309@@ -35,6 +37,16 @@
310 QAccelerometer::type,
311 core::Accelerometer::id(),
312 this);
313+
314+ QSensorManager::registerBackend(
315+ QGyroscope::type,
316+ core::Gyroscope::id(),
317+ this);
318+
319+ QSensorManager::registerBackend(
320+ QMagnetometer::type,
321+ core::Magnetometer::id(),
322+ this);
323 }
324
325 // Instantiate all sensor backends here:
326@@ -49,5 +61,11 @@
327 if (sensor->identifier() == core::Accelerometer::id())
328 return new core::Accelerometer(sensor);
329
330+ if (sensor->identifier() == core::Gyroscope::id())
331+ return new core::Gyroscope(sensor);
332+
333+ if (sensor->identifier() == core::Magnetometer::id())
334+ return new core::Magnetometer(sensor);
335+
336 return NULL;
337 }
338
339=== modified file 'plugins/sensors/core_sensor_plugins.h'
340--- plugins/sensors/core_sensor_plugins.h 2014-01-07 10:35:49 +0000
341+++ plugins/sensors/core_sensor_plugins.h 2016-03-22 17:02:47 +0000
342@@ -20,10 +20,6 @@
343 #include <qsensorplugin.h>
344 #include <qsensorbackend.h>
345 #include <qsensormanager.h>
346-#include <QAccelerometer>
347-#include <QOrientationSensor>
348-
349-#include <QDebug>
350
351 namespace core
352 {
353
354=== added file 'plugins/sensors/core_shared_gyroscope.cpp'
355--- plugins/sensors/core_shared_gyroscope.cpp 1970-01-01 00:00:00 +0000
356+++ plugins/sensors/core_shared_gyroscope.cpp 2016-03-22 17:02:47 +0000
357@@ -0,0 +1,131 @@
358+/*
359+ * Copyright (C) 2015 Canonical, Ltd.
360+ *
361+ * This program is free software; you can redistribute it and/or modify
362+ * it under the terms of the GNU Lesser General Public License as published by
363+ * the Free Software Foundation; version 3.
364+ *
365+ * This program is distributed in the hope that it will be useful,
366+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
367+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
368+ * GNU Lesser General Public License for more details.
369+ *
370+ * You should have received a copy of the GNU Lesser General Public License
371+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
372+ */
373+
374+#include "core_shared_gyroscope.h"
375+
376+#include <QDebug>
377+#include <QMetaType>
378+#include <qmath.h>
379+
380+Q_DECLARE_METATYPE(QSharedPointer<QGyroscopeReading>)
381+
382+core::SharedGyroscope& core::SharedGyroscope::instance()
383+{
384+ static core::SharedGyroscope instance;
385+ return instance;
386+}
387+
388+core::SharedGyroscope::SharedGyroscope(QObject *parent)
389+ : QObject(parent),
390+ m_minDelay(-1),
391+ m_minValue(0.0),
392+ m_maxValue(0.0),
393+ m_resolution(0.0),
394+ m_available(false)
395+{
396+ qRegisterMetaType<QSharedPointer<QGyroscopeReading> >("QSharedPointer<QGyroscopeReading>");
397+ m_gyroscope = ua_sensors_gyroscope_new();
398+ if (m_gyroscope == NULL)
399+ return;
400+
401+ ua_sensors_gyroscope_set_reading_cb(
402+ m_gyroscope,
403+ core::SharedGyroscope::onGyroscopeReadingCb,
404+ static_cast<void *>(this));
405+
406+ // Get the minimum sensor reading delay
407+ m_minDelay = ua_sensors_gyroscope_get_min_delay(m_gyroscope);
408+
409+ float value;
410+ if (ua_sensors_gyroscope_get_min_value(m_gyroscope, &value) == U_STATUS_SUCCESS)
411+ m_minValue = value;
412+
413+ if (ua_sensors_gyroscope_get_max_value(m_gyroscope, &value) == U_STATUS_SUCCESS)
414+ m_maxValue = value;
415+
416+ if (ua_sensors_gyroscope_get_resolution(m_gyroscope, &value) == U_STATUS_SUCCESS)
417+ m_resolution = value;
418+
419+ m_available = true;
420+
421+}
422+
423+void core::SharedGyroscope::start()
424+{
425+ if (m_available)
426+ ua_sensors_gyroscope_enable(m_gyroscope);
427+}
428+
429+void core::SharedGyroscope::stop()
430+{
431+ if (m_available)
432+ ua_sensors_gyroscope_disable(m_gyroscope);
433+}
434+
435+qreal core::SharedGyroscope::getMinDelay() const
436+{
437+ return m_minDelay;
438+}
439+
440+qreal core::SharedGyroscope::getMinValue() const
441+{
442+ return m_minValue;
443+}
444+
445+qreal core::SharedGyroscope::getMaxValue() const
446+{
447+ return m_maxValue;
448+}
449+
450+qreal core::SharedGyroscope::getResolution() const
451+{
452+ return m_resolution;
453+}
454+
455+void core::SharedGyroscope::setDelay(quint32 delay_us)
456+{
457+ ua_sensors_gyroscope_set_event_rate(m_gyroscope, delay_us * 1000);
458+}
459+
460+void core::SharedGyroscope::onGyroscopeReadingCb(UASGyroscopeEvent *event, void *context)
461+{
462+ SharedGyroscope* ac = static_cast<SharedGyroscope*>(context);
463+ if (ac != NULL)
464+ ac->onGyroscopeReading(event);
465+}
466+
467+void core::SharedGyroscope::onGyroscopeReading(UASGyroscopeEvent *event)
468+{
469+ Q_ASSERT(event != NULL);
470+
471+ // TODO(tvoss): We should rely on an object pool to recycle Gyroscope reading
472+ // instances here. We could use a custom deleter for the shared pointer to put
473+ // instances that have been successfully delivered to slots back into the pool.
474+ QSharedPointer<QGyroscopeReading> reading(new QGyroscopeReading());
475+
476+ float value = -1.;
477+
478+ if (uas_gyroscope_event_get_rate_of_rotation_around_x(event, &value) == U_STATUS_SUCCESS)
479+ reading->setX(qRadiansToDegrees(value));
480+ if (uas_gyroscope_event_get_rate_of_rotation_around_y(event, &value) == U_STATUS_SUCCESS)
481+ reading->setY(qRadiansToDegrees(value));
482+ if (uas_gyroscope_event_get_rate_of_rotation_around_z(event, &value) == U_STATUS_SUCCESS)
483+ reading->setZ(qRadiansToDegrees(value));
484+
485+ reading->setTimestamp(uas_gyroscope_event_get_timestamp(event));
486+
487+ Q_EMIT gyroscopeReadingChanged(reading);
488+}
489
490=== added file 'plugins/sensors/core_shared_gyroscope.h'
491--- plugins/sensors/core_shared_gyroscope.h 1970-01-01 00:00:00 +0000
492+++ plugins/sensors/core_shared_gyroscope.h 2016-03-22 17:02:47 +0000
493@@ -0,0 +1,64 @@
494+/*
495+ * Copyright (C) 2015 Canonical, Ltd.
496+ *
497+ * This program is free software; you can redistribute it and/or modify
498+ * it under the terms of the GNU Lesser General Public License as published by
499+ * the Free Software Foundation; version 3.
500+ *
501+ * This program is distributed in the hope that it will be useful,
502+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
503+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
504+ * GNU Lesser General Public License for more details.
505+ *
506+ * You should have received a copy of the GNU Lesser General Public License
507+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
508+ */
509+
510+#ifndef CORE_SHARED_GYROSCOPE_H
511+#define CORE_SHARED_GYROSCOPE_H
512+
513+#include <ubuntu/application/sensors/gyroscope.h>
514+
515+#include <QGyroscopeReading>
516+#include <QObject>
517+#include <QSharedPointer>
518+
519+namespace core
520+{
521+class SharedGyroscope : public QObject
522+{
523+ Q_OBJECT
524+
525+public:
526+ static SharedGyroscope& instance();
527+
528+ void start();
529+ void stop();
530+
531+ qreal getMinDelay() const;
532+ qreal getMinValue() const;
533+ qreal getMaxValue() const;
534+ qreal getResolution() const;
535+ void setDelay(quint32 delay_us);
536+
537+Q_SIGNALS:
538+ void gyroscopeReadingChanged(QSharedPointer<QGyroscopeReading> reading);
539+
540+private:
541+ SharedGyroscope(QObject *parent = NULL);
542+
543+ UASensorsGyroscope *m_gyroscope;
544+
545+ qreal m_minDelay;
546+ qreal m_minValue;
547+ qreal m_maxValue;
548+ qreal m_resolution;
549+ bool m_available;
550+
551+ // Gets called by the underlying platform when there is a new Gyroscope reading
552+ static void onGyroscopeReadingCb(UASGyroscopeEvent *event, void *context);
553+ void onGyroscopeReading(UASGyroscopeEvent *event);
554+};
555+}
556+
557+#endif // CORE_SHARED_GYROSCOPE_H
558
559=== added file 'plugins/sensors/core_shared_magnetometer.cpp'
560--- plugins/sensors/core_shared_magnetometer.cpp 1970-01-01 00:00:00 +0000
561+++ plugins/sensors/core_shared_magnetometer.cpp 2016-03-22 17:02:47 +0000
562@@ -0,0 +1,130 @@
563+/*
564+ * Copyright (C) 2015 Canonical, Ltd.
565+ *
566+ * This program is free software; you can redistribute it and/or modify
567+ * it under the terms of the GNU Lesser General Public License as published by
568+ * the Free Software Foundation; version 3.
569+ *
570+ * This program is distributed in the hope that it will be useful,
571+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
572+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
573+ * GNU Lesser General Public License for more details.
574+ *
575+ * You should have received a copy of the GNU Lesser General Public License
576+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
577+ */
578+
579+#include "core_shared_magnetometer.h"
580+
581+#include <QDebug>
582+#include <QMetaType>
583+
584+Q_DECLARE_METATYPE(QSharedPointer<QMagnetometerReading>)
585+
586+core::SharedMagnetometer& core::SharedMagnetometer::instance()
587+{
588+ static core::SharedMagnetometer instance;
589+ return instance;
590+}
591+
592+core::SharedMagnetometer::SharedMagnetometer(QObject *parent)
593+ : QObject(parent),
594+ m_minDelay(-1),
595+ m_minValue(0.0),
596+ m_maxValue(0.0),
597+ m_resolution(0.0),
598+ m_available(false)
599+{
600+ qRegisterMetaType<QSharedPointer<QMagnetometerReading> >("QSharedPointer<QMagnetometerReading>");
601+ m_magnetometer = ua_sensors_magnetic_new();
602+ if (m_magnetometer == NULL)
603+ return;
604+
605+ ua_sensors_magnetic_set_reading_cb(
606+ m_magnetometer,
607+ core::SharedMagnetometer::onMagnetometerReadingCb,
608+ static_cast<void *>(this));
609+
610+ // Get the minimum sensor reading delay
611+ m_minDelay = ua_sensors_magnetic_get_min_delay(m_magnetometer);
612+
613+ float value;
614+ if (ua_sensors_magnetic_get_min_value(m_magnetometer, &value) == U_STATUS_SUCCESS)
615+ m_minValue = value;
616+
617+ if (ua_sensors_magnetic_get_max_value(m_magnetometer, &value) == U_STATUS_SUCCESS)
618+ m_maxValue = value;
619+
620+ if (ua_sensors_magnetic_get_resolution(m_magnetometer, &value) == U_STATUS_SUCCESS)
621+ m_resolution = value;
622+
623+ m_available = true;
624+
625+}
626+
627+void core::SharedMagnetometer::start()
628+{
629+ if (m_available)
630+ ua_sensors_magnetic_enable(m_magnetometer);
631+}
632+
633+void core::SharedMagnetometer::stop()
634+{
635+ if (m_available)
636+ ua_sensors_magnetic_disable(m_magnetometer);
637+}
638+
639+qreal core::SharedMagnetometer::getMinDelay() const
640+{
641+ return m_minDelay;
642+}
643+
644+qreal core::SharedMagnetometer::getMinValue() const
645+{
646+ return m_minValue;
647+}
648+
649+qreal core::SharedMagnetometer::getMaxValue() const
650+{
651+ return m_maxValue;
652+}
653+
654+qreal core::SharedMagnetometer::getResolution() const
655+{
656+ return m_resolution;
657+}
658+
659+void core::SharedMagnetometer::setDelay(quint32 delay_us)
660+{
661+ ua_sensors_magnetic_set_event_rate(m_magnetometer, delay_us * 1000);
662+}
663+
664+void core::SharedMagnetometer::onMagnetometerReadingCb(UASMagneticEvent *event, void *context)
665+{
666+ SharedMagnetometer* ac = static_cast<SharedMagnetometer*>(context);
667+ if (ac != NULL)
668+ ac->onMagnetometerReading(event);
669+}
670+
671+void core::SharedMagnetometer::onMagnetometerReading(UASMagneticEvent *event)
672+{
673+ Q_ASSERT(event != NULL);
674+
675+ // TODO(tvoss): We should rely on an object pool to recycle Magnetometer reading
676+ // instances here. We could use a custom deleter for the shared pointer to put
677+ // instances that have been successfully delivered to slots back into the pool.
678+ QSharedPointer<QMagnetometerReading> reading(new QMagnetometerReading());
679+
680+ float value = -1.;
681+
682+ if (uas_magnetic_event_get_magnetic_field_x(event, &value) == U_STATUS_SUCCESS)
683+ reading->setX(value);
684+ if (uas_magnetic_event_get_magnetic_field_y(event, &value) == U_STATUS_SUCCESS)
685+ reading->setY(value);
686+ if (uas_magnetic_event_get_magnetic_field_z(event, &value) == U_STATUS_SUCCESS)
687+ reading->setZ(value);
688+
689+ reading->setTimestamp(uas_magnetic_event_get_timestamp(event));
690+
691+ Q_EMIT MagnetometerReadingChanged(reading);
692+}
693
694=== added file 'plugins/sensors/core_shared_magnetometer.h'
695--- plugins/sensors/core_shared_magnetometer.h 1970-01-01 00:00:00 +0000
696+++ plugins/sensors/core_shared_magnetometer.h 2016-03-22 17:02:47 +0000
697@@ -0,0 +1,64 @@
698+/*
699+ * Copyright (C) 2015 Canonical, Ltd.
700+ *
701+ * This program is free software; you can redistribute it and/or modify
702+ * it under the terms of the GNU Lesser General Public License as published by
703+ * the Free Software Foundation; version 3.
704+ *
705+ * This program is distributed in the hope that it will be useful,
706+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
707+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
708+ * GNU Lesser General Public License for more details.
709+ *
710+ * You should have received a copy of the GNU Lesser General Public License
711+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
712+ */
713+
714+#ifndef CORE_SHARED_MAGNETOMETER_H
715+#define CORE_SHARED_MAGNETOMETER_H
716+
717+#include <ubuntu/application/sensors/magnetic.h>
718+
719+#include <QMagnetometerReading>
720+#include <QObject>
721+#include <QSharedPointer>
722+
723+namespace core
724+{
725+class SharedMagnetometer : public QObject
726+{
727+ Q_OBJECT
728+
729+public:
730+ static SharedMagnetometer& instance();
731+
732+ void start();
733+ void stop();
734+
735+ qreal getMinDelay() const;
736+ qreal getMinValue() const;
737+ qreal getMaxValue() const;
738+ qreal getResolution() const;
739+ void setDelay(quint32 delay_us);
740+
741+Q_SIGNALS:
742+ void MagnetometerReadingChanged(QSharedPointer<QMagnetometerReading> reading);
743+
744+private:
745+ SharedMagnetometer(QObject *parent = NULL);
746+
747+ UASensorsMagnetic *m_magnetometer;
748+
749+ qreal m_minDelay;
750+ qreal m_minValue;
751+ qreal m_maxValue;
752+ qreal m_resolution;
753+ bool m_available;
754+
755+ // Gets called by the underlying platform when there is a new Magnetometer reading
756+ static void onMagnetometerReadingCb(UASMagneticEvent *event, void *context);
757+ void onMagnetometerReading(UASMagneticEvent *event);
758+};
759+}
760+
761+#endif // CORE_SHARED_MAGNETOMETER_H

Subscribers

People subscribed via source and target branches