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 "qaccelerometer.h" |
41 | #include "qaccelerometer_p.h" |
42 | |
43 | QT_BEGIN_NAMESPACE |
44 | |
45 | IMPLEMENT_READING(QAccelerometerReading) |
46 | |
47 | /*! |
48 | \class QAccelerometerReading |
49 | \ingroup sensors_reading |
50 | \inmodule QtSensors |
51 | \since 5.1 |
52 | |
53 | \brief The QAccelerometerReading class reports on linear acceleration |
54 | along the X, Y and Z axes. |
55 | |
56 | \section2 QAccelerometerReading Units |
57 | The scale of the values is meters per second squared. |
58 | The axes are arranged as follows. |
59 | |
60 | \image sensors-coordinates2.jpg |
61 | |
62 | A monoblock device sitting at rest, face up on a desk will experience |
63 | a force of approximately 9.8 on the Z axis (ie. towards the roof). |
64 | This is the proper acceleration the device experiences relative to |
65 | freefall. |
66 | */ |
67 | |
68 | /*! |
69 | \property QAccelerometerReading::x |
70 | \brief the acceleration on the X axis. |
71 | |
72 | The scale of the values is meters per second squared. |
73 | \sa {QAccelerometerReading Units} |
74 | */ |
75 | |
76 | qreal QAccelerometerReading::x() const |
77 | { |
78 | return d->x; |
79 | } |
80 | |
81 | /*! |
82 | Sets the acceleration on the X axis to \a x. |
83 | */ |
84 | void QAccelerometerReading::setX(qreal x) |
85 | { |
86 | d->x = x; |
87 | } |
88 | |
89 | /*! |
90 | \property QAccelerometerReading::y |
91 | \brief the acceleration on the Y axis. |
92 | |
93 | The scale of the values is meters per second squared. |
94 | \sa {QAccelerometerReading Units} |
95 | */ |
96 | |
97 | qreal QAccelerometerReading::y() const |
98 | { |
99 | return d->y; |
100 | } |
101 | |
102 | /*! |
103 | Sets the acceleration on the Y axis to \a y. |
104 | */ |
105 | void QAccelerometerReading::setY(qreal y) |
106 | { |
107 | d->y = y; |
108 | } |
109 | |
110 | /*! |
111 | \property QAccelerometerReading::z |
112 | \brief the acceleration on the Z axis. |
113 | |
114 | The scale of the values is meters per second squared. |
115 | \sa {QAccelerometerReading Units} |
116 | */ |
117 | |
118 | qreal QAccelerometerReading::z() const |
119 | { |
120 | return d->z; |
121 | } |
122 | |
123 | /*! |
124 | Sets the acceleration on the Z axis to \a z. |
125 | */ |
126 | void QAccelerometerReading::setZ(qreal z) |
127 | { |
128 | d->z = z; |
129 | } |
130 | |
131 | // ===================================================================== |
132 | |
133 | /*! |
134 | \class QAccelerometerFilter |
135 | \ingroup sensors_filter |
136 | \inmodule QtSensors |
137 | \since 5.1 |
138 | |
139 | \brief The QAccelerometerFilter class is a convenience wrapper around QSensorFilter. |
140 | |
141 | The only difference is that the filter() method features a pointer to QAccelerometerReading |
142 | instead of QSensorReading. |
143 | */ |
144 | |
145 | /*! |
146 | \fn QAccelerometerFilter::filter(QAccelerometerReading *reading) |
147 | |
148 | Called when \a reading changes. Returns false to prevent the reading from propagating. |
149 | |
150 | \sa QSensorFilter::filter() |
151 | */ |
152 | |
153 | bool QAccelerometerFilter::filter(QSensorReading *reading) |
154 | { |
155 | return filter(reading: static_cast<QAccelerometerReading*>(reading)); |
156 | } |
157 | |
158 | char const * const QAccelerometer::type("QAccelerometer" ); |
159 | |
160 | /*! |
161 | \enum QAccelerometer::AccelerationMode |
162 | |
163 | \brief This enum represents the acceleration mode of an acceleration sensor. |
164 | |
165 | The acceleration mode controls how the sensor reports acceleration. QAccelerometer::Combined |
166 | is the only mode in which the values can be directly physically measured, the others are an |
167 | approximation. |
168 | |
169 | \value Combined Both the acceleration caused by gravity and the acceleration caused by the |
170 | user moving the device is reported combined. |
171 | \value Gravity Only the acceleration caused by gravity is reported. Movements of the device |
172 | caused by the user have no effect other than changing the direction when the |
173 | device is rotated. |
174 | \value User Only the acceleration caused by the user moving the device is reported, the |
175 | effect of gravity is canceled out. A device at rest therefore should report |
176 | values of, or close to, zero. |
177 | In other APIs, this mode might be known as \e {linear acceleration}. |
178 | |
179 | \sa QAccelerometer::accelerationMode |
180 | \since 5.1 |
181 | */ |
182 | |
183 | /*! |
184 | \class QAccelerometer |
185 | \ingroup sensors_type |
186 | \inmodule QtSensors |
187 | \since 5.1 |
188 | |
189 | \brief The QAccelerometer class is a convenience wrapper around QSensor. |
190 | |
191 | The only behavioural difference is that this class sets the type properly. |
192 | |
193 | It also supports changing the acceleration mode, which controls whether the force of gravity |
194 | is included in the accelerometer values or not. |
195 | |
196 | Furthermore, this class features a reading() function that returns a QAccelerometerReading |
197 | instead of a QSensorReading. |
198 | |
199 | For details about how the sensor works, see \l QAccelerometerReading. |
200 | |
201 | \sa QAccelerometerReading |
202 | */ |
203 | |
204 | /*! |
205 | Construct the sensor as a child of \a parent. |
206 | */ |
207 | QAccelerometer::QAccelerometer(QObject *parent) |
208 | : QSensor(QAccelerometer::type, *new QAccelerometerPrivate, parent) |
209 | { |
210 | } |
211 | |
212 | /*! |
213 | Destroy the sensor. Stops the sensor if it has not already been stopped. |
214 | */ |
215 | QAccelerometer::~QAccelerometer() |
216 | { |
217 | } |
218 | |
219 | /*! |
220 | \property QAccelerometer::accelerationMode |
221 | \brief The acceleration mode controls how acceleration values are reported. |
222 | \since 5.1 |
223 | |
224 | The acceleration mode controls how the acceleration sensor reports its values. |
225 | The default mode is QAccelerometer::Combined, which means the acceleration caused |
226 | by gravity is included in the reported values. |
227 | |
228 | Acceleration caused by gravity and acceleration caused by the user moving the device |
229 | are physically impossible to distinguish because of general relativity. Most devices use |
230 | sensor fusion to figure out which parts of the acceleration is caused by gravity, for example |
231 | by using a rotation sensor to calculate the gravity direction and assuming a fixed magnitude |
232 | for gravity. Therefore the result is only an approximation and may be inaccurate. |
233 | The QAccelerometer::Combined mode is the most accurate one, as it does not involve approximating |
234 | the gravity. |
235 | |
236 | Not all backends and devices might support setting the acceleration mode. For those cases, the |
237 | default mode QAccelerometer::Combined is used, changing it has no effect. |
238 | */ |
239 | QAccelerometer::AccelerationMode QAccelerometer::accelerationMode() const |
240 | { |
241 | Q_D(const QAccelerometer); |
242 | return d->accelerationMode; |
243 | } |
244 | |
245 | /*! |
246 | Sets the acceleration mode to \a accelerationMode. |
247 | \since 5.1 |
248 | */ |
249 | void QAccelerometer::setAccelerationMode(QAccelerometer::AccelerationMode accelerationMode) |
250 | { |
251 | Q_D(QAccelerometer); |
252 | if (d->accelerationMode != accelerationMode) { |
253 | d->accelerationMode = accelerationMode; |
254 | emit accelerationModeChanged(accelerationMode: d->accelerationMode); |
255 | } |
256 | } |
257 | |
258 | /*! |
259 | \fn QAccelerometer::reading() const |
260 | |
261 | Returns the reading class for this sensor. |
262 | |
263 | \sa QSensor::reading() |
264 | */ |
265 | |
266 | QAccelerometerReading *QAccelerometer::reading() const |
267 | { |
268 | return static_cast<QAccelerometerReading*>(QSensor::reading()); |
269 | } |
270 | |
271 | /*! |
272 | \fn QAccelerometer::accelerationModeChanged(AccelerationMode accelerationMode) |
273 | |
274 | Emitted when the \a accelerationMode was changed. |
275 | |
276 | \since 5.1 |
277 | */ |
278 | |
279 | QT_END_NAMESPACE |
280 | |
281 | #include "moc_qaccelerometer.cpp" |
282 | |