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