1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "genericorientationsensor.h"
5#include <QDebug>
6
7char const * const genericorientationsensor::id("generic.orientation");
8
9genericorientationsensor::genericorientationsensor(QSensor *sensor)
10 : QSensorBackend(sensor)
11{
12 accelerometer = new QAccelerometer(this);
13 accelerometer->addFilter(filter: this);
14 accelerometer->connectToBackend();
15
16 setReading<QOrientationReading>(&m_reading);
17 setDataRates(accelerometer);
18}
19
20void genericorientationsensor::start()
21{
22 accelerometer->setDataRate(sensor()->dataRate());
23 accelerometer->setAlwaysOn(sensor()->isAlwaysOn());
24 accelerometer->start();
25 if (!accelerometer->isActive())
26 sensorStopped();
27 if (accelerometer->isBusy())
28 sensorBusy();
29}
30
31void genericorientationsensor::stop()
32{
33 accelerometer->stop();
34}
35
36bool genericorientationsensor::filter(QAccelerometerReading *reading)
37{
38 QOrientationReading::Orientation o = m_reading.orientation();
39
40 if (reading->y() > 7.35)
41 o = QOrientationReading::TopUp;
42 else if (reading->y() < -7.35)
43 o = QOrientationReading::TopDown;
44 else if (reading->x() > 7.35)
45 o = QOrientationReading::RightUp;
46 else if (reading->x() < -7.35)
47 o = QOrientationReading::LeftUp;
48 else if (reading->z() > 7.35)
49 o = QOrientationReading::FaceUp;
50 else if (reading->z() < -7.35)
51 o = QOrientationReading::FaceDown;
52
53 if (o != m_reading.orientation() || m_reading.timestamp() == 0) {
54 m_reading.setTimestamp(reading->timestamp());
55 m_reading.setOrientation(o);
56 newReadingAvailable();
57 }
58
59 return false;
60}
61
62

source code of qtsensors/src/plugins/sensors/generic/genericorientationsensor.cpp