Repository navigation
Expand file tree
/
Copy pathShake.cpp
More file actions
114 lines (99 loc) · 3.48 KB
/
Copy pathShake.cpp
File metadata and controls
114 lines (99 loc) · 3.48 KB
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
#include "Shake.hpp"
#include <bb/cascades/application.h>
#include <QtCore/QDebug>
#include <QtSensors/QAccelerometer>
#include <math.h>
namespace {
const double SHAKE_SENSITIVITY = 1.7;
const double SHAKE_FILTER = 0.8;
const qint64 SHAKE_TIMEOUT_MS = 1500; // milliseconds
}
Shake::Shake(QObject *parent)
: QObject(parent),
m_accel(0.0),
m_accelLast(0.0),
m_accelCurrent(0.0)
{
m_lastShake = QDateTime::currentDateTime().addSecs(3);
if(!m_sensor.isConnectedToBackend()) {
if (!m_sensor.connectToBackend()) {
qDebug() << "Cannot connect to shake sensor backend!";
} else {
bool rc = QObject::connect(&m_sensor,
SIGNAL(readingChanged()),
this,
SLOT(onReadingChanged()));
Q_ASSERT(rc);
rc = QObject::connect(bb::cascades::Application::instance(),
SIGNAL(fullscreen()),
this,
SLOT(enableSensor()));
Q_ASSERT(rc);
rc = QObject::connect(bb::cascades::Application::instance(),
SIGNAL(thumbnail()),
this,
SLOT(disableSensor()));
Q_ASSERT(rc);
rc = QObject::connect(bb::cascades::Application::instance(),
SIGNAL(invisible()),
this,
SLOT(disableSensor()));
Q_ASSERT(rc);
rc = QObject::connect(bb::cascades::Application::instance(),
SIGNAL(asleep()),
this,
SLOT(disableSensor()));
Q_ASSERT(rc);
Q_UNUSED(rc);
}
}
}
void Shake::onReadingChanged() {
QtMobility::QAccelerometerReading *reading = m_sensor.reading();
const double x = reading->x();
const double y = reading->y();
const double z = reading->z();
m_accelLast = m_accelCurrent;
m_accelCurrent = sqrt(x * x + y * y + z * z);
m_accel = m_accel * SHAKE_FILTER +
(m_accelCurrent - m_accelLast) * (1 - SHAKE_FILTER);
if(m_accel > SHAKE_SENSITIVITY) {
QDateTime now = QDateTime::currentDateTime();
if(m_lastShake.msecsTo(now) > SHAKE_TIMEOUT_MS) {
qDebug() << "Shake detected:" << m_accel;
emit shakeDetected();
m_lastShake = now;
}
}
}
bool Shake::active() const {
return m_sensor.isActive();
}
void Shake::setActive(bool value) {
if(active() != value) {
if (value) {
m_accel = 0.0;
m_accelLast = 0.0;
m_accelCurrent = 0.0;
m_lastShake = QDateTime::currentDateTime().addSecs(3);
if(m_sensor.isConnectedToBackend()) {
m_sensor.setAxesOrientationMode(QtMobility::QAccelerometer::FixedOrientation);
m_sensor.setAccelerationMode(QtMobility::QAccelerometer::User);
m_sensor.setSkipDuplicates(true);
m_sensor.setAlwaysOn(true);
m_sensor.start();
}
} else {
if(m_sensor.isConnectedToBackend()) {
m_sensor.stop();
}
}
emit activeChanged();
}
}
void Shake::enableSensor() {
setActive(true);
}
void Shake::disableSensor() {
setActive(false);
}