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
41#include "qhoversensorgesturerecognizer.h"
42#include <math.h>
43
44#define TIMER2_TIMEOUT 5000
45
46QT_BEGIN_NAMESPACE
47
48QHoverSensorGestureRecognizer::QHoverSensorGestureRecognizer(QObject *parent) :
49 QSensorGestureRecognizer(parent),
50 orientationReading(0),reflectance(0),
51 hoverOk(0), detecting(0), active(0), initialReflectance(0), useHack(0),
52 lastTimestamp(0), timer2Active(0), lapsedTime2(0)
53{
54}
55
56QHoverSensorGestureRecognizer::~QHoverSensorGestureRecognizer()
57{
58}
59
60void QHoverSensorGestureRecognizer::create()
61{
62
63}
64
65QString QHoverSensorGestureRecognizer::id() const
66{
67 return QString("QtSensors.hover");
68}
69
70bool QHoverSensorGestureRecognizer::start()
71{
72 if (QtSensorGestureSensorHandler::instance()->startSensor(sensor: QtSensorGestureSensorHandler::IrProximity)) {
73 if (QtSensorGestureSensorHandler::instance()->startSensor(sensor: QtSensorGestureSensorHandler::Orientation)) {
74 active = true;
75 connect(sender: QtSensorGestureSensorHandler::instance(),SIGNAL(irProximityReadingChanged(QIRProximityReading*)),
76 receiver: this,SLOT(irProximityReadingChanged(QIRProximityReading*)));
77 connect(sender: QtSensorGestureSensorHandler::instance(),SIGNAL(orientationReadingChanged(QOrientationReading*)),
78 receiver: this,SLOT(orientationReadingChanged(QOrientationReading*)));
79 } else {
80 QtSensorGestureSensorHandler::instance()->stopSensor(sensor: QtSensorGestureSensorHandler::IrProximity);
81 active = false;
82 }
83 } else {
84 active = false;
85 }
86
87 detecting = false;
88 detectedHigh = 0;
89 initialReflectance = 0;
90 useHack = false;
91 timer2Active = false;
92 lapsedTime2 = 0;
93 return active;
94}
95
96bool QHoverSensorGestureRecognizer::stop()
97{
98 QtSensorGestureSensorHandler::instance()->stopSensor(sensor: QtSensorGestureSensorHandler::IrProximity);
99 QtSensorGestureSensorHandler::instance()->stopSensor(sensor: QtSensorGestureSensorHandler::Orientation);
100 disconnect(sender: QtSensorGestureSensorHandler::instance(),SIGNAL(irProximityReadingChanged(QIRProximityReading*)),
101 receiver: this,SLOT(irProximityReadingChanged(QIRProximityReading*)));
102 disconnect(sender: QtSensorGestureSensorHandler::instance(),SIGNAL(orientationReadingChanged(QOrientationReading*)),
103 receiver: this,SLOT(orientationReadingChanged(QOrientationReading*)));
104 active = false;
105 timer2Active = false;
106 initialReflectance = 0;
107 return active;
108}
109
110bool QHoverSensorGestureRecognizer::isActive()
111{
112 return active;
113}
114
115
116void QHoverSensorGestureRecognizer::orientationReadingChanged(QOrientationReading *reading)
117{
118 orientationReading = reading;
119}
120
121void QHoverSensorGestureRecognizer::irProximityReadingChanged(QIRProximityReading *reading)
122{
123 reflectance = reading->reflectance();
124 if (reflectance == 0)
125 return;
126
127 if (initialReflectance == 0) {
128 initialReflectance = reflectance;
129 }
130
131 if (initialReflectance > .2) {
132 useHack = true;
133 initialReflectance -= .1;
134 }
135 if (useHack)
136 reflectance -= .1;
137
138 if (detecting && !hoverOk) {
139 detectedHigh = qMax(a: detectedHigh, b: reflectance);
140 }
141
142 if (reflectance > 0.4) {
143 // if close stop detecting
144 hoverOk = false;
145 detecting = false;
146 detectedHigh = 0;
147 }
148
149 qreal detectedPercent = 100 - (detectedHigh / reflectance * 100);
150
151 qint16 percentCheck;
152 if (useHack)
153 percentCheck = -60;
154 else
155 percentCheck = -101;
156
157 quint64 timestamp = reading->timestamp();
158
159 if (!detecting
160 && checkForHovering()) {
161 detecting = true;
162 detecting = true;
163 timer2Active = true;
164 detectedHigh = reflectance;
165 } else if (detecting
166 && detectedPercent < percentCheck
167 && !checkForHovering()) {
168 // went light again after 1 seconds
169 Q_EMIT hover();
170 Q_EMIT detected("hover");
171 hoverOk = false;
172 detecting = false;
173 detectedHigh = 0;
174 timer2Active = false;;
175 }
176 if (detecting && reflectance < 0.2) {
177 timeout();
178 }
179 if (timer2Active && lastTimestamp > 0)
180 lapsedTime2 += (timestamp - lastTimestamp )/1000;
181
182 if (timer2Active && lapsedTime2 >= TIMER2_TIMEOUT) {
183 timeout2();
184 }
185
186 lastTimestamp = reading->timestamp();
187}
188
189bool QHoverSensorGestureRecognizer::checkForHovering()
190{
191 if (orientationReading == 0) {
192 return false;
193 }
194 if (orientationReading->orientation() != QOrientationReading::FaceUp)
195 return false;
196 if ( (reflectance > 0.2 && reflectance < 0.4)
197 && (initialReflectance - reflectance) < -0.1)
198 return true;
199
200 return false;
201}
202
203
204void QHoverSensorGestureRecognizer::timeout()
205{
206 if (checkForHovering()) {
207 hoverOk = true;
208 timer2Active = true;
209 } else {
210 detecting = false;
211 detectedHigh = 0;
212 }
213}
214
215void QHoverSensorGestureRecognizer::timeout2()
216{
217 detecting = false;
218 hoverOk = false;
219 detectedHigh = 0;
220}
221
222QT_END_NAMESPACE
223

source code of qtsensors/src/plugins/sensorgestures/qtsensors/qhoversensorgesturerecognizer.cpp