summaryrefslogtreecommitdiffstats
path: root/src/plugins/sensors/android/androidgyroscope.cpp
blob: acc4798a03906ec954572aff92ffcfbce0acd164 (plain)
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
// Copyright (C) 2019 BogDan Vatra <bogdan@kde.org>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "androidgyroscope.h"
#include <QtCore/qmath.h>

AndroidGyroscope::AndroidGyroscope(int type, QSensor *sensor, QObject *parent)
    : SensorEventQueue<QGyroscopeReading>(type, sensor, parent)
{}

void AndroidGyroscope::dataReceived(const ASensorEvent &event)
{
    // check https://developer.android.com/reference/android/hardware/SensorEvent.html#sensor.type_gyroscope:
    const auto &vec = event.vector;
    qreal x = qRadiansToDegrees(qreal(vec.x));
    qreal y = qRadiansToDegrees(qreal(vec.y));
    qreal z = qRadiansToDegrees(qreal(vec.z));
    if (sensor()->skipDuplicates() && qFuzzyCompare(m_reader.x(), x) &&
            qFuzzyCompare(m_reader.y(), y) &&
            qFuzzyCompare(m_reader.z(), z)) {
        return;
    }
    m_reader.setTimestamp(uint64_t(event.timestamp / 1000));
    m_reader.setX(x);
    m_reader.setY(y);
    m_reader.setZ(z);
    newReadingAvailable();
}