File indexing completed on 2024-12-08 06:30:26
0001 /* 0002 SPDX-FileCopyrightText: 2017 Artem Fedoskin <afedoskin3@gmail.com> 0003 SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 #include "deviceorientation.h" 0006 0007 #if defined(Q_OS_ANDROID) 0008 #include <QAndroidJniObject> 0009 #include <QtAndroidExtras> 0010 #include <QtAndroid> 0011 #define ALPHA_LOW_PASS 0.1 0012 #endif 0013 0014 DeviceOrientation::DeviceOrientation(QObject *parent) : QObject(parent) 0015 { 0016 } 0017 0018 void DeviceOrientation::stopSensors() 0019 { 0020 #if defined(Q_OS_ANDROID) 0021 QAndroidJniObject activity = QtAndroid::androidActivity(); 0022 activity.callMethod<void>("stopSensors"); 0023 #endif 0024 } 0025 0026 void DeviceOrientation::startSensors() 0027 { 0028 #if defined(Q_OS_ANDROID) 0029 QAndroidJniObject activity = QtAndroid::androidActivity(); 0030 activity.callMethod<void>("startSensors"); 0031 #endif 0032 } 0033 0034 void DeviceOrientation::getOrientation() 0035 { 0036 #if defined(Q_OS_ANDROID) 0037 QAndroidJniObject activity = QtAndroid::androidActivity(); 0038 m_Azimuth = m_Azimuth + ALPHA_LOW_PASS * (activity.callMethod<float>("getAzimuth") - m_Azimuth); 0039 m_Altitude = m_Altitude + ALPHA_LOW_PASS * (activity.callMethod<float>("getPitch") - m_Altitude); 0040 0041 float newRoll = activity.callMethod<float>("getRoll"); 0042 m_Roll = abs(newRoll - m_Roll) > 10 ? newRoll : m_Roll + ALPHA_LOW_PASS * (newRoll - m_Roll); 0043 #endif 0044 }