summaryrefslogtreecommitdiffstats
path: root/src/plugins/sensorgestures/qtsensors/qhoversensorgesturerecognizer.cpp
blob: bf26d219386000db9b0e8a8f2a2ad50e09541516 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only


#include "qhoversensorgesturerecognizer.h"
#include <math.h>

#define TIMER2_TIMEOUT 5000

QT_BEGIN_NAMESPACE

QHoverSensorGestureRecognizer::QHoverSensorGestureRecognizer(QObject *parent) :
    QSensorGestureRecognizer(parent),
    orientationReading(0),reflectance(0),
    hoverOk(0), detecting(0), active(0), initialReflectance(0), useHack(0),
    lastTimestamp(0), timer2Active(0), lapsedTime2(0)
{
}

QHoverSensorGestureRecognizer::~QHoverSensorGestureRecognizer()
{
}

void QHoverSensorGestureRecognizer::create()
{

}

QString QHoverSensorGestureRecognizer::id() const
{
    return QString("QtSensors.hover");
}

bool QHoverSensorGestureRecognizer::start()
{
    if (QtSensorGestureSensorHandler::instance()->startSensor(QtSensorGestureSensorHandler::IrProximity)) {
        if (QtSensorGestureSensorHandler::instance()->startSensor(QtSensorGestureSensorHandler::Orientation)) {
            active = true;
            connect(QtSensorGestureSensorHandler::instance(),SIGNAL(irProximityReadingChanged(QIRProximityReading*)),
                    this,SLOT(irProximityReadingChanged(QIRProximityReading*)));
            connect(QtSensorGestureSensorHandler::instance(),SIGNAL(orientationReadingChanged(QOrientationReading*)),
                    this,SLOT(orientationReadingChanged(QOrientationReading*)));
        } else {
            QtSensorGestureSensorHandler::instance()->stopSensor(QtSensorGestureSensorHandler::IrProximity);
            active = false;
        }
    } else {
        active = false;
    }

    detecting = false;
    detectedHigh = 0;
    initialReflectance = 0;
    useHack = false;
    timer2Active = false;
    lapsedTime2 = 0;
    return active;
}

bool QHoverSensorGestureRecognizer::stop()
{
    QtSensorGestureSensorHandler::instance()->stopSensor(QtSensorGestureSensorHandler::IrProximity);
    QtSensorGestureSensorHandler::instance()->stopSensor(QtSensorGestureSensorHandler::Orientation);
    disconnect(QtSensorGestureSensorHandler::instance(),SIGNAL(irProximityReadingChanged(QIRProximityReading*)),
            this,SLOT(irProximityReadingChanged(QIRProximityReading*)));
    disconnect(QtSensorGestureSensorHandler::instance(),SIGNAL(orientationReadingChanged(QOrientationReading*)),
            this,SLOT(orientationReadingChanged(QOrientationReading*)));
    active = false;
    timer2Active = false;
    initialReflectance = 0;
    return active;
}

bool QHoverSensorGestureRecognizer::isActive()
{
    return active;
}


void QHoverSensorGestureRecognizer::orientationReadingChanged(QOrientationReading *reading)
{
    orientationReading = reading;
}

void QHoverSensorGestureRecognizer::irProximityReadingChanged(QIRProximityReading *reading)
{
    reflectance = reading->reflectance();
    if (reflectance == 0)
        return;

    if (initialReflectance == 0) {
        initialReflectance = reflectance;
    }

    if (initialReflectance > .2) {
        useHack = true;
        initialReflectance -= .1;
    }
    if (useHack)
        reflectance -= .1;

    if (detecting && !hoverOk) {
        detectedHigh = qMax(detectedHigh, reflectance);
    }

    if (reflectance > 0.4) {
        // if close stop detecting
        hoverOk = false;
        detecting = false;
        detectedHigh = 0;
    }

    qreal detectedPercent =  100 - (detectedHigh / reflectance * 100);

    qint16 percentCheck;
    if (useHack)
        percentCheck = -60;
    else
        percentCheck = -101;

    quint64 timestamp = reading->timestamp();

    if (!detecting
            && checkForHovering()) {
        detecting = true;
        detecting = true;
        timer2Active = true;
        detectedHigh = reflectance;
    } else if (detecting
                && detectedPercent < percentCheck
               && !checkForHovering()) {
        // went light again after 1 seconds
        Q_EMIT hover();
        Q_EMIT detected("hover");
        hoverOk = false;
        detecting = false;
        detectedHigh = 0;
        timer2Active = false;;
    }
    if (detecting && reflectance <  0.2) {
        timeout();
    }
    if (timer2Active && lastTimestamp > 0)
        lapsedTime2 += (timestamp - lastTimestamp )/1000;

    if (timer2Active && lapsedTime2 >= TIMER2_TIMEOUT) {
        timeout2();
    }

    lastTimestamp = reading->timestamp();
}

bool QHoverSensorGestureRecognizer::checkForHovering()
{
    if (orientationReading == 0) {
        return false;
    }
    if (orientationReading->orientation() != QOrientationReading::FaceUp)
        return false;
    if ( (reflectance > 0.2 && reflectance < 0.4)
         && (initialReflectance - reflectance) < -0.1)
        return true;

    return false;
}


void QHoverSensorGestureRecognizer::timeout()
{
    if (checkForHovering()) {
        hoverOk = true;
        timer2Active = true;
    } else {
        detecting = false;
        detectedHigh = 0;
    }
}

void QHoverSensorGestureRecognizer::timeout2()
{
    detecting = false;
    hoverOk = false;
    detectedHigh = 0;
}

QT_END_NAMESPACE