|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "genericrotationsensor.h" |
|
43 #include <QDebug> |
|
44 #include <qmath.h> |
|
45 |
|
46 #define RADIANS_TO_DEGREES 57.2957795 |
|
47 |
|
48 char const * const genericrotationsensor::id("generic.rotation"); |
|
49 |
|
50 genericrotationsensor::genericrotationsensor(QSensor *sensor) |
|
51 : QSensorBackend(sensor) |
|
52 { |
|
53 accelerometer = new QAccelerometer(this); |
|
54 accelerometer->addFilter(this); |
|
55 accelerometer->connectToBackend(); |
|
56 |
|
57 setReading<QRotationReading>(&m_reading); |
|
58 setDataRates(accelerometer); |
|
59 |
|
60 sensor->setProperty("hasZ", false); |
|
61 } |
|
62 |
|
63 void genericrotationsensor::start() |
|
64 { |
|
65 accelerometer->setDataRate(sensor()->dataRate()); |
|
66 accelerometer->start(); |
|
67 if (!accelerometer->isActive()) |
|
68 sensorStopped(); |
|
69 if (accelerometer->isBusy()) |
|
70 sensorBusy(); |
|
71 } |
|
72 |
|
73 void genericrotationsensor::stop() |
|
74 { |
|
75 accelerometer->stop(); |
|
76 } |
|
77 |
|
78 bool genericrotationsensor::filter(QSensorReading *reading) |
|
79 { |
|
80 QAccelerometerReading *ar = qobject_cast<QAccelerometerReading*>(reading); |
|
81 qreal pitch = 0; |
|
82 qreal roll = 0; |
|
83 |
|
84 qreal x = ar->x(); |
|
85 qreal y = ar->y(); |
|
86 qreal z = ar->z(); |
|
87 |
|
88 // Note that the formula used come from this document: |
|
89 // http://www.freescale.com/files/sensors/doc/app_note/AN3461.pdf |
|
90 pitch = qAtan(y / sqrt(x*x + z*z)) * RADIANS_TO_DEGREES; |
|
91 roll = qAtan(x / sqrt(y*y + z*z)) * RADIANS_TO_DEGREES; |
|
92 // Roll is a left-handed rotation but we need right-handed rotation |
|
93 roll = -roll; |
|
94 |
|
95 // We need to fix up roll to the (-180,180] range required. |
|
96 // Check for negative theta values and apply an offset as required. |
|
97 // Note that theta is defined as the angle of the Z axis relative |
|
98 // to gravity (see referenced document). It's negative when the |
|
99 // face of the device points downward. |
|
100 qreal theta = qAtan(sqrt(x*x + y*y) / z) * RADIANS_TO_DEGREES; |
|
101 if (theta < 0) { |
|
102 if (roll > 0) |
|
103 roll = 180 - roll; |
|
104 else |
|
105 roll = -180 - roll; |
|
106 } |
|
107 |
|
108 m_reading.setTimestamp(ar->timestamp()); |
|
109 m_reading.setX(pitch); |
|
110 m_reading.setY(roll); |
|
111 m_reading.setZ(0); |
|
112 newReadingAvailable(); |
|
113 return false; |
|
114 } |
|
115 |