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 <QDebug> |
41 | |
42 | #include "qshake2recognizer.h" |
43 | #include <math.h> |
44 | |
45 | |
46 | QT_BEGIN_NAMESPACE |
47 | |
48 | QShake2SensorGestureRecognizer::QShake2SensorGestureRecognizer(QObject *parent) |
49 | : QSensorGestureRecognizer(parent) |
50 | , active(0) |
51 | , shakeDirection(QShake2SensorGestureRecognizer::ShakeUndefined) |
52 | , shaking(0) |
53 | , shakeCount(0) |
54 | , lapsedTime(0) |
55 | , lastTimestamp(0), |
56 | timerActive(0) |
57 | { |
58 | timerTimeout = 250; |
59 | } |
60 | |
61 | QShake2SensorGestureRecognizer::~QShake2SensorGestureRecognizer() |
62 | { |
63 | } |
64 | |
65 | void QShake2SensorGestureRecognizer::create() |
66 | { |
67 | } |
68 | |
69 | bool QShake2SensorGestureRecognizer::start() |
70 | { |
71 | if (QtSensorGestureSensorHandler::instance()->startSensor(sensor: QtSensorGestureSensorHandler::Accel)) { |
72 | active = true; |
73 | connect(sender: QtSensorGestureSensorHandler::instance(),SIGNAL(accelReadingChanged(QAccelerometerReading*)), |
74 | receiver: this,SLOT(accelChanged(QAccelerometerReading*))); |
75 | } else { |
76 | active = false; |
77 | } |
78 | prevData.x = 0; |
79 | prevData.y = 0; |
80 | prevData.z = 0; |
81 | shakeCount = 0; |
82 | shaking = false; |
83 | shakeDirection = QShake2SensorGestureRecognizer::ShakeUndefined; |
84 | |
85 | return active; |
86 | } |
87 | |
88 | bool QShake2SensorGestureRecognizer::stop() |
89 | { |
90 | QtSensorGestureSensorHandler::instance()->stopSensor(sensor: QtSensorGestureSensorHandler::Accel); |
91 | disconnect(sender: QtSensorGestureSensorHandler::instance(),SIGNAL(accelReadingChanged(QAccelerometerReading*)), |
92 | receiver: this,SLOT(accelChanged(QAccelerometerReading*))); |
93 | active = false; |
94 | return active; |
95 | } |
96 | |
97 | bool QShake2SensorGestureRecognizer::isActive() |
98 | { |
99 | return active; |
100 | } |
101 | |
102 | QString QShake2SensorGestureRecognizer::id() const |
103 | { |
104 | return QString("QtSensors.shake2" ); |
105 | } |
106 | |
107 | #define NUMBER_SHAKES 3 |
108 | #define THRESHOLD 25 |
109 | |
110 | void QShake2SensorGestureRecognizer::accelChanged(QAccelerometerReading *reading) |
111 | { |
112 | const qreal x = reading->x(); |
113 | const qreal y = reading->y(); |
114 | const qreal z = reading->z(); |
115 | |
116 | const quint64 timestamp = reading->timestamp(); |
117 | |
118 | currentData.x = x; |
119 | currentData.y = y; |
120 | currentData.z = z; |
121 | |
122 | if (qAbs(t: prevData.x - currentData.x) < 1 |
123 | && qAbs(t: prevData.y - currentData.y) < 1 |
124 | && qAbs(t: prevData.z - currentData.z) < 1) { |
125 | |
126 | prevData.x = currentData.x; |
127 | prevData.y = currentData.y; |
128 | prevData.z = currentData.z; |
129 | return; |
130 | } |
131 | |
132 | bool wasShake; |
133 | wasShake = checkForShake(prevSensorData: prevData, currentSensorData: currentData, THRESHOLD); |
134 | |
135 | if (!shaking && wasShake && |
136 | shakeCount == NUMBER_SHAKES) { |
137 | shaking = true; |
138 | shakeCount = 0; |
139 | lapsedTime = 0; |
140 | timerActive = false; |
141 | switch (shakeDirection) { |
142 | case QShake2SensorGestureRecognizer::ShakeLeft: |
143 | Q_EMIT shakeLeft(); |
144 | Q_EMIT detected("shakeLeft" ); |
145 | break; |
146 | case QShake2SensorGestureRecognizer::ShakeRight: |
147 | Q_EMIT shakeRight(); |
148 | Q_EMIT detected("shakeRight" ); |
149 | break; |
150 | case QShake2SensorGestureRecognizer::ShakeUp: |
151 | Q_EMIT shakeUp(); |
152 | Q_EMIT detected("shakeUp" ); |
153 | break; |
154 | case QShake2SensorGestureRecognizer::ShakeDown: |
155 | Q_EMIT shakeDown(); |
156 | Q_EMIT detected("shakeDown" ); |
157 | break; |
158 | default: |
159 | break; |
160 | }; |
161 | |
162 | } else if (wasShake) { |
163 | |
164 | if (shakeCount == 0 && shakeDirection == QShake2SensorGestureRecognizer::ShakeUndefined) { |
165 | |
166 | const int xdiff = prevData.x - currentData.x; |
167 | const int ydiff = prevData.x - currentData.y; |
168 | |
169 | const int max = qMax(a: qAbs(t: ydiff), b: qAbs(t: xdiff)); |
170 | if (max == qAbs(t: xdiff)) { |
171 | if (isNegative(num: xdiff)) |
172 | shakeDirection = QShake2SensorGestureRecognizer::ShakeLeft; |
173 | else |
174 | shakeDirection = QShake2SensorGestureRecognizer::ShakeRight; |
175 | |
176 | } else if (max == qAbs(t: ydiff)) { |
177 | if (isNegative(num: ydiff)) |
178 | shakeDirection = QShake2SensorGestureRecognizer::ShakeDown; |
179 | else |
180 | shakeDirection = QShake2SensorGestureRecognizer::ShakeUp; |
181 | } |
182 | } |
183 | shakeCount++; |
184 | if (shakeCount == NUMBER_SHAKES) { |
185 | timerActive = true; |
186 | } |
187 | } |
188 | |
189 | if (timerActive && lastTimestamp > 0) |
190 | lapsedTime += (timestamp - lastTimestamp )/1000; |
191 | |
192 | if (timerActive && lapsedTime >= timerTimeout) { |
193 | timeout(); |
194 | } |
195 | prevData.x = currentData.x; |
196 | prevData.y = currentData.y; |
197 | prevData.z = currentData.z; |
198 | lastTimestamp = timestamp; |
199 | } |
200 | |
201 | void QShake2SensorGestureRecognizer::timeout() |
202 | { |
203 | shakeCount = 0; |
204 | shaking = false; |
205 | shakeDirection = QShake2SensorGestureRecognizer::ShakeUndefined; |
206 | timerActive = false; |
207 | lapsedTime = 0; |
208 | lastTimestamp = 0; |
209 | } |
210 | |
211 | bool QShake2SensorGestureRecognizer::checkForShake(ShakeData prevSensorData, ShakeData currentSensorData, qreal threshold) |
212 | { |
213 | const double deltaX = qAbs(t: prevSensorData.x - currentSensorData.x); |
214 | const double deltaY = qAbs(t: prevSensorData.y - currentSensorData.y); |
215 | const double deltaZ = qAbs(t: prevSensorData.z - currentSensorData.z); |
216 | |
217 | return (deltaX > threshold |
218 | || deltaY > threshold |
219 | || deltaZ > threshold); |
220 | } |
221 | |
222 | bool QShake2SensorGestureRecognizer::isNegative(qreal num) |
223 | { |
224 | if (num < 0) |
225 | return true; |
226 | return false; |
227 | } |
228 | |
229 | |
230 | |
231 | QT_END_NAMESPACE |
232 | |