~ubuntu-branches/ubuntu/trusty/qtubuntu-sensors/trusty

« back to all changes in this revision

Viewing changes to plugins/sensors/core_orientation_sensor.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2014-01-08 17:17:24 UTC
  • mto: This revision was merged to the branch mainline in revision 6.
  • Revision ID: package-import@ubuntu.com-20140108171724-177zeelodrmtchnd
Tags: upstream-0.6+14.04.20140108.2
ImportĀ upstreamĀ versionĀ 0.6+14.04.20140108.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013 Canonical, Ltd.
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 */
 
16
 
 
17
#include "core_orientation_sensor.h"
 
18
 
 
19
#include "core_shared_accelerometer.h"
 
20
 
 
21
#include <QDebug>
 
22
 
 
23
const float core::OrientationSensor::m_accelDelta = 7.35;
 
24
 
 
25
core::OrientationSensor::OrientationSensor(QSensor *sensor)
 
26
    : QSensorBackend(sensor)
 
27
{
 
28
    // Register the reading instance with the parent
 
29
    setReading<QOrientationReading>(&m_reading);
 
30
 
 
31
    const qreal minDelay = core::SharedAccelerometer::instance().getMinDelay();
 
32
    if (minDelay > -1)
 
33
    {
 
34
        // Min and max sensor sampling frequencies, in Hz
 
35
        addDataRate(minDelay, minDelay * 10);
 
36
    }
 
37
    addOutputRange(core::SharedAccelerometer::instance().getMinValue(),
 
38
                   core::SharedAccelerometer::instance().getMaxValue(),
 
39
                   core::SharedAccelerometer::instance().getResolution());
 
40
 
 
41
    // Connect to the accelerometer's readingChanged signal
 
42
    connect(&core::SharedAccelerometer::instance(),
 
43
            SIGNAL(accelerometerReadingChanged(QSharedPointer<QAccelerometerReading>)), 
 
44
            this, 
 
45
            SLOT(onAccelerometerReadingChanged(QSharedPointer<QAccelerometerReading>)),
 
46
            Qt::QueuedConnection);
 
47
 
 
48
    setDescription(QLatin1String("Orientation Sensor"));
 
49
}
 
50
 
 
51
void core::OrientationSensor::start()
 
52
{
 
53
    core::SharedAccelerometer::instance().start();
 
54
}
 
55
 
 
56
void core::OrientationSensor::stop()
 
57
{
 
58
    core::SharedAccelerometer::instance().stop();
 
59
}
 
60
 
 
61
void core::OrientationSensor::onAccelerometerReadingChanged(QSharedPointer<QAccelerometerReading> reading)
 
62
{
 
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);
 
76
 
 
77
    if (m_reading.orientation() != m_readingCache.orientation())
 
78
    {
 
79
        // Emit readingChanged signal only if orientation actually changes
 
80
        newReadingAvailable();
 
81
    }
 
82
 
 
83
    m_readingCache.setOrientation(m_reading.orientation());
 
84
}