1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtSensors module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qmagnetometer.h"
41#include "qmagnetometer_p.h"
42
43QT_BEGIN_NAMESPACE
44
45IMPLEMENT_READING(QMagnetometerReading)
46
47/*!
48 \class QMagnetometerReading
49 \ingroup sensors_reading
50 \inmodule QtSensors
51 \since 5.1
52
53 \brief The QMagnetometerReading class represents one reading from the
54 magnetometer.
55
56 \section2 QMagnetometerReading Units
57 The magnetometer returns magnetic flux density values along 3 axes.
58 The scale of the values is teslas. The axes are arranged as follows.
59
60 \image sensors-coordinates2.jpg
61
62 The magnetometer can report on either raw magnetic flux values or geomagnetic flux values.
63 By default it returns raw magnetic flux values. The QMagnetometer::returnGeoValues property
64 must be set to return geomagnetic flux values.
65
66 The primary difference between raw and geomagnetic values is that extra processing
67 is done to eliminate local magnetic interference from the geomagnetic values so they
68 represent only the effect of the Earth's magnetic field. This process is not perfect
69 and the accuracy of each reading may change.
70
71 The image below shows the difference between geomagnetic (on the left) and raw (on the right)
72 readings for a phone that is being subjected to magnetic interference.
73
74 \image sensors-geo-vs-raw-magnetism.jpg
75
76 The accuracy of each reading is measured as a number from 0 to 1.
77 A value of 1 is the highest level that the device can support and 0 is
78 the worst.
79
80 \section2 Calibration
81 If the device is reporting low accuracy, then calibration might be needed before acceptable measurements
82 can be provided.
83 Basic calibration can usually be done by either rotating your device in a figure of eight, or by
84 rotating the device along each of its three axes. For more information, check your device's
85 documentation on how to calibrate the magnetic sensor.
86*/
87
88/*!
89 \property QMagnetometerReading::x
90 \brief the raw magnetic flux density on the X axis.
91
92 Measured as teslas.
93 \sa {QMagnetometerReading Units}
94*/
95
96qreal QMagnetometerReading::x() const
97{
98 return d->x;
99}
100
101/*!
102 Sets the raw magnetic flux density on the X axis to \a x.
103*/
104void QMagnetometerReading::setX(qreal x)
105{
106 d->x = x;
107}
108
109/*!
110 \property QMagnetometerReading::y
111 \brief the raw magnetic flux density on the Y axis.
112
113 Measured as teslas.
114 \sa {QMagnetometerReading Units}
115*/
116
117qreal QMagnetometerReading::y() const
118{
119 return d->y;
120}
121
122/*!
123 Sets the raw magnetic flux density on the Y axis to \a y.
124*/
125void QMagnetometerReading::setY(qreal y)
126{
127 d->y = y;
128}
129
130/*!
131 \property QMagnetometerReading::z
132 \brief the raw magnetic flux density on the Z axis.
133
134 Measured as teslas.
135 \sa {QMagnetometerReading Units}
136*/
137
138qreal QMagnetometerReading::z() const
139{
140 return d->z;
141}
142
143/*!
144 Sets the raw magnetic flux density on the Z axis to \a z.
145*/
146void QMagnetometerReading::setZ(qreal z)
147{
148 d->z = z;
149}
150
151/*!
152 \property QMagnetometerReading::calibrationLevel
153 \brief the accuracy of the reading.
154
155 Measured as a value from 0 to 1 with higher values being better.
156
157 Note that this only changes when measuring geomagnetic flux density.
158 Raw magnetic flux readings will always have a value of 1.
159 \sa {QMagnetometerReading Units}, {Calibration}
160*/
161
162qreal QMagnetometerReading::calibrationLevel() const
163{
164 return d->calibrationLevel;
165}
166
167/*!
168 Sets the accuracy of the reading to \a calibrationLevel.
169*/
170void QMagnetometerReading::setCalibrationLevel(qreal calibrationLevel)
171{
172 d->calibrationLevel = calibrationLevel;
173}
174
175// =====================================================================
176
177/*!
178 \class QMagnetometerFilter
179 \ingroup sensors_filter
180 \inmodule QtSensors
181 \since 5.1
182
183 \brief The QMagnetometerFilter class is a convenience wrapper around QSensorFilter.
184
185 The only difference is that the filter() method features a pointer to QMagnetometerReading
186 instead of QSensorReading.
187*/
188
189/*!
190 \fn QMagnetometerFilter::filter(QMagnetometerReading *reading)
191
192 Called when \a reading changes. Returns false to prevent the reading from propagating.
193
194 \sa QSensorFilter::filter()
195*/
196
197bool QMagnetometerFilter::filter(QSensorReading *reading)
198{
199 return filter(reading: static_cast<QMagnetometerReading*>(reading));
200}
201
202char const * const QMagnetometer::type("QMagnetometer");
203
204/*!
205 \class QMagnetometer
206 \ingroup sensors_type
207 \inmodule QtSensors
208 \since 5.1
209
210 \brief The QMagnetometer class is a convenience wrapper around QSensor.
211
212 The only behavioural difference is that this class sets the type properly.
213
214 This class also features a reading() function that returns a QMagnetometerReading instead of a QSensorReading.
215
216 For details about how the sensor works, see \l QMagnetometerReading.
217
218 \sa QMagnetometerReading
219*/
220
221/*!
222 Construct the sensor as a child of \a parent.
223*/
224QMagnetometer::QMagnetometer(QObject *parent)
225 : QSensor(QMagnetometer::type, *new QMagnetometerPrivate, parent)
226{
227}
228
229/*!
230 Destroy the sensor. Stops the sensor if it has not already been stopped.
231*/
232QMagnetometer::~QMagnetometer()
233{
234}
235
236/*!
237 \fn QMagnetometer::reading() const
238
239 Returns the reading class for this sensor.
240
241 \sa QSensor::reading()
242*/
243
244QMagnetometerReading *QMagnetometer::reading() const
245{
246 return static_cast<QMagnetometerReading*>(QSensor::reading());
247}
248
249/*!
250 \property QMagnetometer::returnGeoValues
251 \brief a value indicating if geomagnetic values should be returned.
252
253 Set to true to return geomagnetic flux density.
254 Set to false (the default) to return raw magnetic flux density.
255
256 The property must be set before calling start().
257*/
258
259bool QMagnetometer::returnGeoValues() const
260{
261 Q_D(const QMagnetometer);
262 return d->returnGeoValues;
263}
264
265void QMagnetometer::setReturnGeoValues(bool returnGeoValues)
266{
267 Q_D(QMagnetometer);
268 if (d->returnGeoValues != returnGeoValues) {
269 d->returnGeoValues = returnGeoValues;
270 emit returnGeoValuesChanged(returnGeoValues);
271 }
272}
273
274QT_END_NAMESPACE
275
276#include "moc_qmagnetometer.cpp"
277

source code of qtsensors/src/sensors/qmagnetometer.cpp