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 "qtsensorgesturesensorhandler.h"
43
44QtSensorGestureSensorHandler::QtSensorGestureSensorHandler(QObject *parent) :
45 QObject(parent),
46 accel(0), orientation(0), proximity(0), irProx(0),tapSensor(0)
47{
48}
49
50QtSensorGestureSensorHandler* QtSensorGestureSensorHandler::instance()
51{
52 static QtSensorGestureSensorHandler *instance = 0;
53 if (!instance) {
54 instance = new QtSensorGestureSensorHandler;
55 }
56 return instance;
57}
58
59void QtSensorGestureSensorHandler::accelChanged()
60{
61 Q_EMIT accelReadingChanged(reading: accel->reading());
62}
63
64void QtSensorGestureSensorHandler::orientationChanged()
65{
66 Q_EMIT orientationReadingChanged(reading: orientation->reading());
67}
68
69void QtSensorGestureSensorHandler::proximityChanged()
70{
71 Q_EMIT proximityReadingChanged(reading: proximity->reading());
72}
73
74void QtSensorGestureSensorHandler::irProximityChanged()
75{
76 Q_EMIT irProximityReadingChanged(reading: irProx->reading());
77}
78
79void QtSensorGestureSensorHandler::doubletap()
80{
81 Q_EMIT dTabReadingChanged(reading: tapSensor->reading());
82}
83
84bool QtSensorGestureSensorHandler::startSensor(SensorGestureSensors sensor)
85{
86 bool ok = true;
87 switch (sensor) {
88 case Accel:
89 //accel
90 if (accel == 0x0) {
91 accel = new QAccelerometer(this);
92 ok = accel->connectToBackend();
93 accel->setDataRate(100);
94 qoutputrangelist outputranges = accel->outputRanges();
95
96 if (outputranges.count() > 0)
97 accelRange = (int)(outputranges.at(i: 0).maximum);//39
98 else
99 accelRange = 39; //this should never happen
100 connect(sender: accel,SIGNAL(readingChanged()),receiver: this,SLOT(accelChanged()));
101 }
102 if (ok && !accel->isActive())
103 accel->start();
104 break;
105 case Orientation:
106 //orientation
107 if (orientation == 0x0) {
108 orientation = new QOrientationSensor(this);
109 ok = orientation->connectToBackend();
110 orientation->setDataRate(50);
111 connect(sender: orientation,SIGNAL(readingChanged()),receiver: this,SLOT(orientationChanged()));
112 }
113 if (ok && !orientation->isActive())
114 orientation->start();
115 break;
116 case Proximity:
117 //proximity
118 if (proximity == 0x0) {
119 proximity = new QProximitySensor(this);
120 ok = proximity->connectToBackend();
121 connect(sender: proximity,SIGNAL(readingChanged()),receiver: this,SLOT(proximityChanged()));
122 }
123 if (ok && !proximity->isActive())
124 proximity->start();
125 break;
126 case IrProximity:
127 //irproximity
128 if (irProx == 0x0) {
129 irProx = new QIRProximitySensor(this);
130 irProx->setDataRate(50);
131 ok = irProx->connectToBackend();
132 connect(sender: irProx,SIGNAL(readingChanged()),receiver: this,SLOT(irProximityChanged()));
133 }
134 if (ok && !irProx->isActive())
135 irProx->start();
136 break;
137 case Tap:
138 //dtap
139 if (tapSensor == 0x0) {
140 tapSensor = new QTapSensor(this);
141 ok = tapSensor->connectToBackend();
142 connect(sender: tapSensor,SIGNAL(readingChanged()),receiver: this,SLOT(doubletap()));
143 }
144 if (ok && !tapSensor->isActive())
145 tapSensor->start();
146 break;
147 };
148 int val = usedSensorsMap.value(akey: sensor);
149 usedSensorsMap.insert(akey: sensor,avalue: ++val);
150
151 return ok;
152}
153
154void QtSensorGestureSensorHandler::stopSensor(SensorGestureSensors sensor)
155{
156 // qDebug() << __FUNCTION__ << sensor;
157 if (usedSensorsMap.value(akey: sensor) == 0)
158 return;
159 int val = usedSensorsMap.value(akey: sensor);
160 usedSensorsMap.insert(akey: sensor,avalue: --val);
161 switch (sensor) {
162 case Accel:
163 //accel
164 if (usedSensorsMap.value(akey: sensor) == 0) {
165 accel->stop();
166 }
167 break;
168 case Orientation:
169 if (usedSensorsMap.value(akey: sensor) == 0) {
170 orientation->stop();
171 }
172 //orientation
173 break;
174 case Proximity:
175 if (usedSensorsMap.value(akey: sensor) == 0) {
176 proximity->stop();
177 }
178 //proximity
179 break;
180 case IrProximity:
181 if (usedSensorsMap.value(akey: sensor) == 0) {
182 irProx->stop();
183 }
184 //irproximity
185 break;
186 case Tap:
187 if (usedSensorsMap.value(akey: sensor) == 0) {
188 tapSensor->stop();
189 }
190 //dtap
191 break;
192 };
193}
194

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