2
* Copyright (C) 2013 Canonical, Ltd.
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser General Public License as published by
6
* the Free Software Foundation; version 3.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
#include "core_orientation_sensor.h"
19
#include "core_shared_accelerometer.h"
23
const float core::OrientationSensor::m_accelDelta = 7.35;
25
core::OrientationSensor::OrientationSensor(QSensor *sensor)
26
: QSensorBackend(sensor)
28
// Register the reading instance with the parent
29
setReading<QOrientationReading>(&m_reading);
31
const qreal minDelay = core::SharedAccelerometer::instance().getMinDelay();
34
// Min and max sensor sampling frequencies, in Hz
35
addDataRate(minDelay, minDelay * 10);
37
addOutputRange(core::SharedAccelerometer::instance().getMinValue(),
38
core::SharedAccelerometer::instance().getMaxValue(),
39
core::SharedAccelerometer::instance().getResolution());
41
// Connect to the accelerometer's readingChanged signal
42
connect(&core::SharedAccelerometer::instance(),
43
SIGNAL(accelerometerReadingChanged(QSharedPointer<QAccelerometerReading>)),
45
SLOT(onAccelerometerReadingChanged(QSharedPointer<QAccelerometerReading>)),
46
Qt::QueuedConnection);
48
setDescription(QLatin1String("Orientation Sensor"));
51
void core::OrientationSensor::start()
53
core::SharedAccelerometer::instance().start();
56
void core::OrientationSensor::stop()
58
core::SharedAccelerometer::instance().stop();
61
void core::OrientationSensor::onAccelerometerReadingChanged(QSharedPointer<QAccelerometerReading> reading)
63
// Interpret the accelerometer data into a meaningful orientation
64
if (reading->y() > m_accelDelta)
65
m_reading.setOrientation(QOrientationReading::TopUp);
66
else if (reading->y() < -m_accelDelta)
67
m_reading.setOrientation(QOrientationReading::TopDown);
68
else if (reading->x() > m_accelDelta)
69
m_reading.setOrientation(QOrientationReading::RightUp);
70
else if (reading->x() < -m_accelDelta)
71
m_reading.setOrientation(QOrientationReading::LeftUp);
72
else if (reading->z() > m_accelDelta)
73
m_reading.setOrientation(QOrientationReading::FaceUp);
74
else if (reading->z() < -m_accelDelta)
75
m_reading.setOrientation(QOrientationReading::FaceDown);
77
if (m_reading.orientation() != m_readingCache.orientation())
79
// Emit readingChanged signal only if orientation actually changes
80
newReadingAvailable();
83
m_readingCache.setOrientation(m_reading.orientation());