qtubuntu-sensors-0.6+16.04.20151127/0000755000015300001610000000000012626127552017304 5ustar pbuserpbgroup00000000000000qtubuntu-sensors-0.6+16.04.20151127/data/0000755000015300001610000000000012626127552020215 5ustar pbuserpbgroup00000000000000qtubuntu-sensors-0.6+16.04.20151127/data/Sensors.conf0000644000015300001610000000013212626127204022506 0ustar pbuserpbgroup00000000000000[Default] QAccelerometer = core.accelerometer QOrientationSensor = core.orientationsensor qtubuntu-sensors-0.6+16.04.20151127/CMakeLists.txt0000644000015300001610000000202612626127204022036 0ustar pbuserpbgroup00000000000000cmake_minimum_required(VERSION 2.8) project(qtubuntu-sensors) # Find includes in corresponding build directories set(CMAKE_INCLUDE_CURRENT_DIR ON) # Instruct CMake to run moc automatically when needed. set(CMAKE_AUTOMOC ON) include(GNUInstallDirs) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -pedantic -Wextra -fPIC -fvisibility=hidden") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -fno-strict-aliasing -fvisibility=hidden -fvisibility-inlines-hidden -pedantic -Wextra") set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-undefined") option(QTUBUNTU_SENSORS_WERROR "Treat warnings as errors" ON) if (QTUBUNTU_SENSORS_WERROR) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror") endif(QTUBUNTU_SENSORS_WERROR) # enable tests with gtest include(CTest) set(GTEST_ROOT /usr/src/gtest) add_subdirectory(${GTEST_ROOT} ${CMAKE_CURRENT_BINARY_DIR}/gtest) find_package(Threads REQUIRED) add_definitions(-DQT_PLUGIN) add_subdirectory(plugins) add_subdirectory(tests) qtubuntu-sensors-0.6+16.04.20151127/COPYING0000644000015300001610000001674312626127204020344 0ustar pbuserpbgroup00000000000000 GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library. qtubuntu-sensors-0.6+16.04.20151127/tests/0000755000015300001610000000000012626127552020446 5ustar pbuserpbgroup00000000000000qtubuntu-sensors-0.6+16.04.20151127/tests/test_sensor_api.cpp0000644000015300001610000001076512626127204024356 0ustar pbuserpbgroup00000000000000/* * Copyright (C) 2014 Canonical Ltd * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License version 3 as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . * * Authored by: Martin Pitt * * This test suite uses the simulated sensor test backend from * libubuntu-application-api-test1. Please see * /usr/share/doc/libubuntu-application-api-test1/README.md for how to use it * and the format of the sensor data files. */ #include #include #include #include #include #include #include #include #include #include #include using namespace std; // structures for logging received events from signals struct AccelEvent { qreal x, y, z; chrono::time_point time; }; struct OrientationEvent { QOrientationReading::Orientation orientation; chrono::time_point time; }; /******************************************* * * Tests with real sensor backend * *******************************************/ class RealBackendTest : public testing::Test { virtual void SetUp() { } }; TESTP_F(RealBackendTest, CreateAccelerometer, { QAccelerometer sensor; // connect to the qtubuntu-sensors backend; default is dummy, and there // does not seem to be a way to use data/Sensors.conf sensor.setIdentifier("core.accelerometer"); // this can succeed for fail depending on whether the hardware we run this // on actually exists; but it should never crash if (sensor.start()) { EXPECT_EQ(sensor.error(), 0); EXPECT_EQ(sensor.isConnectedToBackend(), true); EXPECT_EQ(sensor.accelerationMode(), QAccelerometer::Combined); } else { EXPECT_NE(sensor.error(), 0); EXPECT_EQ(sensor.isConnectedToBackend(), false); } EXPECT_EQ(sensor.identifier(), "core.accelerometer"); }) TESTP_F(RealBackendTest, CreateOrientation, { QOrientationSensor sensor; // connect to the qtubuntu-sensors backend; default is dummy, and there // does not seem to be a way to use data/Sensors.conf sensor.setIdentifier("core.orientation"); // this can succeed for fail depending on whether the hardware we run this // on actually exists; but it should never crash if (sensor.start()) { EXPECT_EQ(sensor.error(), 0); EXPECT_EQ(sensor.isConnectedToBackend(), true); } else { EXPECT_NE(sensor.error(), 0); EXPECT_EQ(sensor.isConnectedToBackend(), false); } EXPECT_EQ(sensor.identifier(), "core.orientation"); }) /******************************************* * * Tests with default sensor backend * *******************************************/ class DefaultBackendTest : public testing::Test { virtual void SetUp() { } }; TESTP_F(DefaultBackendTest, CreateAccelerometer, { QAccelerometer sensor; // don't set any particular identifier here // this can succeed for fail depending on whether the hardware we run this // on actually exists; but it should never crash if (sensor.start()) { EXPECT_EQ(sensor.error(), 0); EXPECT_EQ(sensor.isConnectedToBackend(), true); } else { EXPECT_NE(sensor.error(), 0); EXPECT_EQ(sensor.isConnectedToBackend(), false); } cout << "default backend connected to " << sensor.identifier().constData() << endl; }) TESTP_F(DefaultBackendTest, CreateOrientation, { QOrientationSensor sensor; // don't set any particular identifier here // this can succeed for fail depending on whether the hardware we run this // on actually exists; but it should never crash if (sensor.start()) { EXPECT_EQ(sensor.error(), 0); EXPECT_EQ(sensor.isConnectedToBackend(), true); } else { EXPECT_NE(sensor.error(), 0); EXPECT_EQ(sensor.isConnectedToBackend(), false); } cout << "default backend connected to " << sensor.identifier().constData() << endl; }) qtubuntu-sensors-0.6+16.04.20151127/tests/CMakeLists.txt0000644000015300001610000000224012626127204023176 0ustar pbuserpbgroup00000000000000find_package(Qt5Core REQUIRED) find_package(Qt5Sensors REQUIRED) find_package(PkgConfig REQUIRED) pkg_check_modules(PROCESS_CPP process-cpp REQUIRED) #include_directories(${GTEST_ROOT}/src) include_directories(${PROCESS_CPP_INCLUDE_DIRS}) add_executable( test_sensor_api test_sensor_api.cpp ) target_link_libraries( test_sensor_api gtest gtest_main ${PROCESS_CPP_LIBRARIES} ) qt5_use_modules( test_sensor_api Core Sensors ) add_executable( test_sensor_api_mock test_sensor_api_mock.cpp ) target_link_libraries( test_sensor_api_mock gtest gtest_main ${PROCESS_CPP_LIBRARIES} ) qt5_use_modules( test_sensor_api_mock Core Sensors ) # we need to call the tests with pointing to the locally built plugins add_test( test_sensor_api_mock # add QT_DEBUG_PLUGINS=1 if plugin isn't loading env QT_PLUGIN_PATH=${CMAKE_BINARY_DIR}/plugins ${CMAKE_CURRENT_BINARY_DIR}/test_sensor_api_mock ) if(DEFINED ENV{UBUNTU_PLATFORM_API_BACKEND}) add_test( test_sensor_api # add QT_DEBUG_PLUGINS=1 if plugin isn't loading env QT_PLUGIN_PATH=${CMAKE_BINARY_DIR}/plugins ${CMAKE_CURRENT_BINARY_DIR}/test_sensor_api ) endif() qtubuntu-sensors-0.6+16.04.20151127/tests/test_sensor_api_mock.cpp0000644000015300001610000002133012626127204025355 0ustar pbuserpbgroup00000000000000/* * Copyright (C) 2014 Canonical Ltd * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License version 3 as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . * * Authored by: Martin Pitt * * This test suite uses the simulated sensor test backend from * libubuntu-application-api-test1. Please see * /usr/share/doc/libubuntu-application-api-test1/README.md for how to use it * and the format of the sensor data files. */ #include #include #include #include #include #include #include #include #include #include #include using namespace std; // structures for logging received events from signals struct AccelEvent { qreal x, y, z; chrono::time_point time; }; struct OrientationEvent { QOrientationReading::Orientation orientation; chrono::time_point time; }; /******************************************* * * Tests with simulated sensor backend * *******************************************/ class SimBackendTest : public testing::Test { public: SimBackendTest() { QCoreApplication::setLibraryPaths(QStringList("../plugins/")); accel_sensor = new QAccelerometer(); orientation_sensor = new QOrientationSensor(); } protected: virtual void SetUp() { bool openResult = data_file.open(); EXPECT_EQ(openResult, true); setenv("UBUNTU_PLATFORM_API_SENSOR_TEST", qPrintable(data_file.fileName()), 1); setenv("UBUNTU_PLATFORM_API_BACKEND", "test", 1); // ensure the queues are clear while (accel_events.size() > 0) accel_events.pop(); while (orientation_events.size() > 0) orientation_events.pop(); } virtual void TearDown() { data_file.remove(); } void set_data(const char* data) { data_file.write(data); data_file.flush(); } // run main loop for given number of ms void run_events(unsigned ms) { int argc = 0; char* argv[] = { NULL }; const unsigned step = 10; // ms per iteration unsigned count = ms / step; QCoreApplication app(argc, argv); while (count-- > 0) { QThread::msleep(step); app.processEvents(); } } // check the next event in accel_events for expected values and delay void check_accel_event(qreal x, qreal y, qreal z, unsigned ms) { auto e = accel_events.front(); accel_events.pop(); EXPECT_FLOAT_EQ(e.x, x); EXPECT_FLOAT_EQ(e.y, y); EXPECT_FLOAT_EQ(e.z, z); // allow -5 to +10 ms slack in event time auto delay = chrono::duration_cast(e.time - start_time).count(); EXPECT_GE(delay, ms - 10); EXPECT_LE(delay, ms + 20); } // check the next event in orientation_events for expected value and delay void check_orientation_event(QOrientationReading::Orientation value, unsigned ms) { auto e = orientation_events.front(); orientation_events.pop(); EXPECT_EQ(e.orientation, value); // allow -5 to +10 ms slack in event time auto delay = chrono::duration_cast(e.time - start_time).count(); EXPECT_GE(delay, ms - 10); EXPECT_LE(delay, ms + 40); } QTemporaryFile data_file; QAccelerometer *accel_sensor; QOrientationSensor *orientation_sensor; chrono::time_point start_time; queue accel_events; queue orientation_events; }; TESTP_F(SimBackendTest, CreateAccelerometer, { set_data("create accel 0.5 1000 0.1"); QAccelerometer sensor; // connect to the qtubuntu-sensors backend; default is dummy, and there // does not seem to be a way to use data/Sensors.conf sensor.setIdentifier("core.accelerometer"); EXPECT_EQ(sensor.start(), true); EXPECT_EQ(sensor.error(), 0); EXPECT_EQ(sensor.isConnectedToBackend(), true); EXPECT_EQ(sensor.identifier(), "core.accelerometer"); EXPECT_EQ(sensor.accelerationMode(), QAccelerometer::Combined); //EXPECT_EQ(sensor.outputRange(), 0); // FIXME: bug in code qoutputrange r = sensor.outputRanges()[0]; EXPECT_FLOAT_EQ(r.minimum, 0.5f); EXPECT_FLOAT_EQ(r.maximum, 1000.0f); // defined initial values auto reading = sensor.reading(); EXPECT_FLOAT_EQ(reading->x(), 0.0); EXPECT_FLOAT_EQ(reading->y(), 0.0); EXPECT_FLOAT_EQ(reading->z(), 0.0); }) TESTP_F(SimBackendTest, CreateOrientation, { // orientation sensor is based on acceleration set_data("create accel -500 500 1"); QOrientationSensor sensor; // connect to the qtubuntu-sensors backend; default is dummy, and there // does not seem to be a way to use data/Sensors.conf sensor.setIdentifier("core.orientation"); EXPECT_EQ(sensor.start(), true); EXPECT_EQ(sensor.error(), 0); EXPECT_EQ(sensor.isConnectedToBackend(), true); EXPECT_EQ(sensor.identifier(), "core.orientation"); // defined initial value EXPECT_EQ(sensor.reading()->orientation(), QOrientationReading::Undefined); }) TESTP_F(SimBackendTest, AccelerometerEvents, { set_data("create accel -500 500 0.1\n" "10 accel 0 -9.9 0\n" "200 accel 1.5 400 0.5\n" "300 accel -1 -9.8 -0.5\n"); // connect to the qtubuntu-sensors backend; default is dummy, and there // does not seem to be a way to use data/Sensors.conf accel_sensor->setIdentifier("core.accelerometer"); QObject::connect(accel_sensor, &QAccelerometer::readingChanged, [=]() { auto r = accel_sensor->reading(); accel_events.push({r->x(), r->y(), r->z(), chrono::system_clock::now()}); }); EXPECT_EQ(accel_sensor->start(), true); start_time = chrono::system_clock::now(); run_events(550); EXPECT_EQ(accel_events.size(), 3); check_accel_event(0.0, -9.9, 0.0, 10); check_accel_event(1.5, 400, 0.5, 210); check_accel_event(-1.0, -9.8, -0.5, 510); }) TESTP_F(SimBackendTest, AccelerometerReadings, { set_data("create accel -500 500 0.1\n" "10 accel 0 -9.9 0\n" "200 accel 1.5 400 0.5\n" "300 accel -1 -9.8 -0.5\n"); // connect to the qtubuntu-sensors backend; default is dummy, and there // does not seem to be a way to use data/Sensors.conf accel_sensor->setIdentifier("core.accelerometer"); EXPECT_EQ(accel_sensor->start(), true); // initial value auto reading = accel_sensor->reading(); EXPECT_FLOAT_EQ(reading->x(), 0.0); EXPECT_FLOAT_EQ(reading->y(), 0.0); EXPECT_FLOAT_EQ(reading->z(), 0.0); run_events(50); reading = accel_sensor->reading(); EXPECT_FLOAT_EQ(reading->x(), 0.0); EXPECT_FLOAT_EQ(reading->y(), -9.9); EXPECT_FLOAT_EQ(reading->z(), 0.0); run_events(200); reading = accel_sensor->reading(); EXPECT_FLOAT_EQ(reading->x(), 1.5); EXPECT_FLOAT_EQ(reading->y(), 400); EXPECT_FLOAT_EQ(reading->z(), 0.5); run_events(350); reading = accel_sensor->reading(); EXPECT_FLOAT_EQ(reading->x(), -1); EXPECT_FLOAT_EQ(reading->y(), -9.8); EXPECT_FLOAT_EQ(reading->z(), -0.5); }) TESTP_F(SimBackendTest, OrientationReading, { set_data("create accel -500 500 0.001\n" "70 accel 0.347 9.768 0.162\n" "70 accel -0.600 9.947 0.174\n" "70 accel -0.885 11.061 -0.177\n" "70 accel -2.121 10.397 -0.468\n" "70 accel -4.242 8.664 -0.023\n" "70 accel -5.882 7.633 0.169\n" "70 accel -7.031 5.822 -0.268\n" "70 accel -8.138 3.775 0.564\n" "70 accel -9.616 1.854 1.981\n" "70 accel -9.562 -0.195 1.138\n" "70 accel -10.373 -0.597 1.802\n" ); orientation_sensor->setIdentifier("core.orientation"); EXPECT_EQ(orientation_sensor->start(), true); // initial value EXPECT_EQ(orientation_sensor->reading()->orientation(), QOrientationReading::Undefined); // Right up after stream of svents run_events(1400); EXPECT_EQ(orientation_sensor->reading()->orientation(), QOrientationReading::LeftUp); }) qtubuntu-sensors-0.6+16.04.20151127/plugins/0000755000015300001610000000000012626127552020765 5ustar pbuserpbgroup00000000000000qtubuntu-sensors-0.6+16.04.20151127/plugins/feedback/0000755000015300001610000000000012626127552022511 5ustar pbuserpbgroup00000000000000qtubuntu-sensors-0.6+16.04.20151127/plugins/feedback/core_feedback.cpp0000644000015300001610000000716312626127214025753 0ustar pbuserpbgroup00000000000000/* * Copyright 2013 Canonical Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . * * Author: Christian Dywan */ #include "core_feedback.h" #include #include #include #include #include #include #include #include #include #include core::Feedback::Feedback() : QObject(), m_vibrator(NULL) { actuatorList << createFeedbackActuator(this, 42); if (qgetenv("UBUNTU_PLATFORM_API_BACKEND").isNull()) return; m_vibrator = ua_sensors_haptic_new(); ua_sensors_haptic_enable(m_vibrator); } core::Feedback::~Feedback() { if (m_vibrator) { ua_sensors_haptic_destroy(m_vibrator); } } QFeedbackInterface::PluginPriority core::Feedback::pluginPriority() { return PluginHighPriority; } QList core::Feedback::actuators() { return actuatorList; } void core::Feedback::setActuatorProperty(const QFeedbackActuator&, ActuatorProperty prop, const QVariant &) { switch (prop) { case Enabled: break; default: break; } } QVariant core::Feedback::actuatorProperty(const QFeedbackActuator &actuator, ActuatorProperty prop) { QVariant result; switch (prop) { case Name: result = QString::fromLocal8Bit("Ubuntu Vibrator"); break; case State: result = actuator.isValid() ? QFeedbackActuator::Ready : QFeedbackActuator::Unknown; break; case Enabled: result = true; break; } return result; } bool core::Feedback::isActuatorCapabilitySupported(const QFeedbackActuator &, QFeedbackActuator::Capability cap) { bool result = false; switch(cap) { case QFeedbackActuator::Envelope: case QFeedbackActuator::Period: result = true; break; } return result; } void core::Feedback::updateEffectProperty(const QFeedbackHapticsEffect *effect, EffectProperty) { if (effect->period() != -1) { /* Not currently supported */ reportError(effect, QFeedbackEffect::UnknownError); } } void core::Feedback::vibrateOnce(const QFeedbackEffect* effect) { int effectiveDuration = effect->duration(); switch (effectiveDuration) { case QFeedbackEffect::Infinite: case 0: effectiveDuration = 150; } if (m_vibrator) ua_sensors_haptic_vibrate_once(m_vibrator, effectiveDuration); } void core::Feedback::setEffectState(const QFeedbackHapticsEffect *effect, QFeedbackEffect::State state) { switch (state) { case QFeedbackEffect::Stopped: break; case QFeedbackEffect::Paused: break; case QFeedbackEffect::Running: vibrateOnce(effect); break; case QFeedbackEffect::Loading: break; } } QFeedbackEffect::State core::Feedback::effectState(const QFeedbackHapticsEffect *) { // We don't currently support on-going vibrations // This can be added when moving to a vibration service return QFeedbackEffect::Stopped; } qtubuntu-sensors-0.6+16.04.20151127/plugins/feedback/feedback.qrc0000644000015300001610000000014112626127204024732 0ustar pbuserpbgroup00000000000000 feedback.json qtubuntu-sensors-0.6+16.04.20151127/plugins/feedback/CMakeLists.txt0000644000015300001610000000164412626127204025250 0ustar pbuserpbgroup00000000000000find_package(PkgConfig) find_package(Qt5Core REQUIRED) find_package(Qt5Feedback REQUIRED) pkg_check_modules(UBUNTU_PLATFORM_API ubuntu-platform-api REQUIRED) qt5_add_resources(FEEDBACK_RESOURCES feedback.qrc) add_library( qtfeedback_ubuntu SHARED core_feedback.h core_feedback.cpp ${FEEDBACK_RESOURCES} ) # Ideally, we would read the plugin installation location from cmake # but this does not work currently. set(PLUGIN_INSTALL_LOCATION "${CMAKE_INSTALL_LIBDIR}/qt5/plugins/feedback") # get_target_property(PLUGIN_LOCATION Qt5::Feedback PLUGIN_LOCATION) message(STATUS "Installing Qt5 feedback plugin to: ${PLUGIN_INSTALL_LOCATION}") qt5_use_modules(qtfeedback_ubuntu Core Feedback) target_link_libraries( qtfeedback_ubuntu ${CMAKE_THREAD_LIBS_INIT} Qt5::Core Qt5::Feedback ${UBUNTU_PLATFORM_API_LIBRARIES} ) install( TARGETS qtfeedback_ubuntu LIBRARY DESTINATION ${PLUGIN_INSTALL_LOCATION}) qtubuntu-sensors-0.6+16.04.20151127/plugins/feedback/feedback.json0000644000015300001610000000006012626127204025116 0ustar pbuserpbgroup00000000000000{ "Interfaces": ["QFeedbackHapticsInterface"] } qtubuntu-sensors-0.6+16.04.20151127/plugins/feedback/core_feedback.h0000644000015300001610000000372712626127214025422 0ustar pbuserpbgroup00000000000000/* * Copyright 2013 Canonical Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . * * Author: Christian Dywan */ #ifndef CORE_FEEDBACK_H #define CORE_FEEDBACK_H #include #include #include #include namespace core { class Feedback : public QObject, public QFeedbackHapticsInterface { Q_OBJECT Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtFeedbackPlugin" FILE "feedback.json") Q_INTERFACES(QFeedbackHapticsInterface) public: Feedback(); virtual ~Feedback(); // From QFeedbackHapticsInterface virtual PluginPriority pluginPriority(); QList actuators(); QFeedbackEffect::State effectState(const QFeedbackHapticsEffect *); void setEffectState (const QFeedbackHapticsEffect* effect, QFeedbackEffect::State state); bool isActuatorCapabilitySupported(const QFeedbackActuator &, QFeedbackActuator::Capability); void setActuatorProperty(const QFeedbackActuator &, ActuatorProperty, const QVariant &); QVariant actuatorProperty(const QFeedbackActuator &, ActuatorProperty); void updateEffectProperty(const QFeedbackHapticsEffect* effect, EffectProperty property); private: QList actuatorList; void vibrateOnce(const QFeedbackEffect* effect); UASensorsHaptic* m_vibrator; }; } #endif // CORE_FEEDBACK_H qtubuntu-sensors-0.6+16.04.20151127/plugins/CMakeLists.txt0000644000015300001610000000011712626127204023516 0ustar pbuserpbgroup00000000000000add_subdirectory(feedback) add_subdirectory(position) add_subdirectory(sensors)qtubuntu-sensors-0.6+16.04.20151127/plugins/sensors/0000755000015300001610000000000012626127552022461 5ustar pbuserpbgroup00000000000000qtubuntu-sensors-0.6+16.04.20151127/plugins/sensors/core_sensor_plugins.cpp0000644000015300001610000000320412626127204027240 0ustar pbuserpbgroup00000000000000/* * Copyright (C) 2013 Canonical, Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ #include "core_accelerometer.h" #include "core_orientation_sensor.h" #include "core_sensor_plugins.h" #include #include #include #include #include void core::SensorPlugins::registerSensors() { QSensorManager::registerBackend( QOrientationSensor::type, core::OrientationSensor::id(), this); QSensorManager::registerBackend( QAccelerometer::type, core::Accelerometer::id(), this); } // Instantiate all sensor backends here: QSensorBackend* core::SensorPlugins::createBackend(QSensor *sensor) { if (qgetenv("UBUNTU_PLATFORM_API_BACKEND").isNull()) return NULL; if (sensor->identifier() == core::OrientationSensor::id()) return new core::OrientationSensor(sensor); if (sensor->identifier() == core::Accelerometer::id()) return new core::Accelerometer(sensor); return NULL; } qtubuntu-sensors-0.6+16.04.20151127/plugins/sensors/sensors.qrc0000644000015300001610000000014012626127204024651 0ustar pbuserpbgroup00000000000000 sensors.json qtubuntu-sensors-0.6+16.04.20151127/plugins/sensors/CMakeLists.txt0000644000015300001610000000215112626127204025212 0ustar pbuserpbgroup00000000000000find_package(PkgConfig) find_package(Qt5Core REQUIRED) find_package(Qt5Sensors REQUIRED) pkg_check_modules(UBUNTU_PLATFORM_API ubuntu-platform-api REQUIRED) qt5_add_resources(SENSORS_RESOURCES sensors.qrc) add_library( qtubuntu_sensors_plugins SHARED core_accelerometer.h core_orientation_sensor.h core_sensor_plugins.h core_shared_accelerometer.h core_accelerometer.cpp core_orientation_sensor.cpp core_sensor_plugins.cpp core_shared_accelerometer.cpp ${SENSORS_RESOURCES} ) # Ideally, we would read the plugin installation location from cmake # but this does not work currently. set(PLUGIN_INSTALL_LOCATION "${CMAKE_INSTALL_LIBDIR}/qt5/plugins/sensors") # get_target_property(PLUGIN_LOCATION Qt5::Sensors PLUGIN_LOCATION) message(STATUS "Installing Qt5 sensors plugin to: ${PLUGIN_INSTALL_LOCATION}") qt5_use_modules(qtubuntu_sensors_plugins Core Sensors) target_link_libraries( qtubuntu_sensors_plugins ${CMAKE_THREAD_LIBS_INIT} Qt5::Core Qt5::Sensors ${UBUNTU_PLATFORM_API_LIBRARIES}) install( TARGETS qtubuntu_sensors_plugins LIBRARY DESTINATION ${PLUGIN_INSTALL_LOCATION}) qtubuntu-sensors-0.6+16.04.20151127/plugins/sensors/core_orientation_sensor.h0000644000015300001610000000352412626127204027564 0ustar pbuserpbgroup00000000000000/* * Copyright (C) 2013 Canonical, Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ #ifndef CORE_ORIENTATION_SENSOR_H #define CORE_ORIENTATION_SENSOR_H #include #include #include #include namespace core { class OrientationSensor : public QSensorBackend { Q_OBJECT public: inline static const char* id() { return "core.orientation"; } OrientationSensor(QSensor *sensor); // From QSensorBackend void start(); void stop(); Q_SIGNALS: void readingChanged(); public Q_SLOTS: void onAccelerometerReadingChanged(QSharedPointer reading); private: // The distance from the center, right or left, that will trigger an // orientation change when the user rotates the target device. static const float m_accelDelta; static const float m_minAccel; static const float m_maxAccel; static const int m_maxTilt; static const int m_tiltTolerance[4][2]; int m_lastX; int m_lastY; int m_lastZ; unsigned long m_lastFilter; QOrientationReading m_reading; QOrientationReading m_readingCache; int m_lastOrientation; int m_lastRotation; QVector m_directionHistory; }; } #endif // CORE_ORIENTATION_SENSOR_H qtubuntu-sensors-0.6+16.04.20151127/plugins/sensors/core_accelerometer.cpp0000644000015300001610000000464312626127204027010 0ustar pbuserpbgroup00000000000000/* * Copyright (C) 2013 Canonical, Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ #include "core_accelerometer.h" #include "core_shared_accelerometer.h" #include core::Accelerometer::Accelerometer(QSensor *sensor) : QSensorBackend(sensor) { // Register the reading instance with the parent setReading(&m_reading); const qreal minDelay = core::SharedAccelerometer::instance().getMinDelay(); if (minDelay > -1) { // Min and max sensor sampling frequencies, in Hz addDataRate(minDelay, minDelay * 10); } addOutputRange(core::SharedAccelerometer::instance().getMinValue(), core::SharedAccelerometer::instance().getMaxValue(), core::SharedAccelerometer::instance().getResolution()); // Connect to the accelerometer's readingChanged signal // This has to be a queued connection as the sensor callback // from the platform API can happen on an arbitrary thread. connect( &core::SharedAccelerometer::instance(), SIGNAL(accelerometerReadingChanged(QSharedPointer)), this, SLOT(onAccelerometerReadingChanged(QSharedPointer)), Qt::QueuedConnection); setDescription(QLatin1String("Accelerometer Sensor")); } core::Accelerometer::~Accelerometer() { } void core::Accelerometer::start() { core::SharedAccelerometer::instance().start(); } void core::Accelerometer::stop() { core::SharedAccelerometer::instance().stop(); } void core::Accelerometer::onAccelerometerReadingChanged(QSharedPointer reading) { // Capture the coordinates from the accelerometer device m_reading.setX(reading->x()); m_reading.setY(reading->y()); m_reading.setZ(reading->z()); m_reading.setTimestamp(reading->timestamp()); newReadingAvailable(); } qtubuntu-sensors-0.6+16.04.20151127/plugins/sensors/core_accelerometer.h0000644000015300001610000000232112626127204026444 0ustar pbuserpbgroup00000000000000/* * Copyright (C) 2013 Canonical, Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ #ifndef CORE_ACCELEROMETER_H #define CORE_ACCELEROMETER_H #include #include namespace core { class Accelerometer : public QSensorBackend { Q_OBJECT public: inline static const char* id() { return "core.accelerometer"; } Accelerometer(QSensor *sensor); virtual ~Accelerometer(); void start(); void stop(); public Q_SLOTS: void onAccelerometerReadingChanged(QSharedPointer reading); private: QAccelerometerReading m_reading; }; } #endif // CORE_ACCELEROMETER_H qtubuntu-sensors-0.6+16.04.20151127/plugins/sensors/core_sensor_plugins.h0000644000015300001610000000252212626127204026707 0ustar pbuserpbgroup00000000000000/* * Copyright (C) 2013 Canonical, Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ #ifndef CORE_SENSOR_PLUGINS_H #define CORE_SENSOR_PLUGINS_H #include #include #include #include #include #include namespace core { class SensorPlugins : public QObject, public QSensorPluginInterface, public QSensorBackendFactory { Q_OBJECT Q_PLUGIN_METADATA(IID "com.qt-project.Qt.QSensorPluginInterface/1.0" FILE "sensors.json") Q_INTERFACES(QSensorPluginInterface) public: // From QSensorPluginInterface void registerSensors(); // From QSensorBackendFactory QSensorBackend *createBackend(QSensor *sensor); }; } #endif // CORE_SENSOR_PLUGINS_H qtubuntu-sensors-0.6+16.04.20151127/plugins/sensors/core_shared_accelerometer.cpp0000644000015300001610000000762012626127204030334 0ustar pbuserpbgroup00000000000000/* * Copyright (C) 2013 Canonical, Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ #include "core_shared_accelerometer.h" #include #include Q_DECLARE_METATYPE(QSharedPointer) core::SharedAccelerometer& core::SharedAccelerometer::instance() { static core::SharedAccelerometer instance; return instance; } core::SharedAccelerometer::SharedAccelerometer(QObject *parent) : QObject(parent), m_minDelay(-1), m_minValue(0.0), m_maxValue(0.0), m_resolution(0.0), m_available(false) { qRegisterMetaType >("QSharedPointer"); m_accelerometer = ua_sensors_accelerometer_new(); if (m_accelerometer == NULL) return; ua_sensors_accelerometer_set_reading_cb( m_accelerometer, core::SharedAccelerometer::onAccelerometerReadingCb, static_cast(this)); // Get the minimum sensor reading delay m_minDelay = ua_sensors_accelerometer_get_min_delay(m_accelerometer); float value; if (ua_sensors_accelerometer_get_min_value(m_accelerometer, &value) == U_STATUS_SUCCESS) m_minValue = value; if (ua_sensors_accelerometer_get_max_value(m_accelerometer, &value) == U_STATUS_SUCCESS) m_maxValue = value; if (ua_sensors_accelerometer_get_resolution(m_accelerometer, &value) == U_STATUS_SUCCESS) m_resolution = value; m_available = true; } void core::SharedAccelerometer::start() { if (m_available) ua_sensors_accelerometer_enable(m_accelerometer); } void core::SharedAccelerometer::stop() { if (m_available) ua_sensors_accelerometer_disable(m_accelerometer); } qreal core::SharedAccelerometer::getMinDelay() const { return m_minDelay; } qreal core::SharedAccelerometer::getMinValue() const { return m_minValue; } qreal core::SharedAccelerometer::getMaxValue() const { return m_maxValue; } qreal core::SharedAccelerometer::getResolution() const { return m_resolution; } void core::SharedAccelerometer::setDelay(quint32 delay_us) { ua_sensors_accelerometer_set_event_rate(m_accelerometer, delay_us * 1000); } void core::SharedAccelerometer::onAccelerometerReadingCb(UASAccelerometerEvent *event, void *context) { SharedAccelerometer* ac = static_cast(context); if (ac != NULL) ac->onAccelerometerReading(event); } void core::SharedAccelerometer::onAccelerometerReading(UASAccelerometerEvent *event) { Q_ASSERT(event != NULL); // TODO(tvoss): We should rely on an object pool to recycle accelerometer reading // instances here. We could use a custom deleter for the shared pointer to put // instances that have been successfully delivered to slots back into the pool. QSharedPointer reading(new QAccelerometerReading()); float value = -1.; if (uas_accelerometer_event_get_acceleration_x(event, &value) == U_STATUS_SUCCESS) reading->setX(value); if (uas_accelerometer_event_get_acceleration_y(event, &value) == U_STATUS_SUCCESS) reading->setY(value); if (uas_accelerometer_event_get_acceleration_z(event, &value) == U_STATUS_SUCCESS) reading->setZ(value); reading->setTimestamp(uas_accelerometer_event_get_timestamp(event)); Q_EMIT accelerometerReadingChanged(reading); } qtubuntu-sensors-0.6+16.04.20151127/plugins/sensors/core_orientation_sensor.cpp0000644000015300001610000001375012626127204030121 0ustar pbuserpbgroup00000000000000/* * Copyright (C) 2013 Canonical, Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ #include "core_orientation_sensor.h" #include "core_shared_accelerometer.h" #include #include const float core::OrientationSensor::m_accelDelta = 7.35; #define CLOCKWISE 1 #define COUNTER_CLOCKWISE 0 #define RAD_TO_DEG(x) (x * 57.2957) // Values taken from Android's orientation helper const float core::OrientationSensor::m_minAccel = 5.8; const float core::OrientationSensor::m_maxAccel = 13.8; const int core::OrientationSensor::m_maxTilt = 75; const int core::OrientationSensor::m_tiltTolerance[4][2] = { { -25, 70 }, { -25, 65 }, { -25, 60 }, { -25, 65 } }; core::OrientationSensor::OrientationSensor(QSensor *sensor) : QSensorBackend(sensor), m_lastX(0), m_lastY(0), m_lastZ(0) { // Register the reading instance with the parent setReading(&m_reading); const qreal minDelay = core::SharedAccelerometer::instance().getMinDelay(); if (minDelay > -1) { // Min and max sensor sampling frequencies, in Hz addDataRate(minDelay, minDelay * 10); } addOutputRange(core::SharedAccelerometer::instance().getMinValue(), core::SharedAccelerometer::instance().getMaxValue(), core::SharedAccelerometer::instance().getResolution()); // Connect to the accelerometer's readingChanged signal connect(&core::SharedAccelerometer::instance(), SIGNAL(accelerometerReadingChanged(QSharedPointer)), this, SLOT(onAccelerometerReadingChanged(QSharedPointer)), Qt::QueuedConnection); setDescription(QLatin1String("Orientation Sensor")); } void core::OrientationSensor::start() { core::SharedAccelerometer::instance().start(); core::SharedAccelerometer::instance().setDelay(66667); } void core::OrientationSensor::stop() { core::SharedAccelerometer::instance().stop(); } int nearestRotation = 0; void core::OrientationSensor::onAccelerometerReadingChanged(QSharedPointer reading) { float x = reading->x(); float y = reading->y(); float z = reading->z(); /* * Low pass filter to remove jitter. * Code inspired by Android's orientation helper */ unsigned long now = reading->timestamp(); unsigned long then = m_lastFilter; double timeDelta = (now-then)/1000000; if (timeDelta < 66) return; // If events come faster than 15Hz, drop float alpha = timeDelta / (200 + timeDelta); x = alpha * (x - m_lastX) + m_lastX; y = alpha * (y - m_lastY) + m_lastY; z = alpha * (z - m_lastZ) + m_lastZ; m_lastX = x; m_lastY = y; m_lastZ = z; m_lastFilter = now; int orientation = round(RAD_TO_DEG((atan2(-x,y)))); if (orientation < 0) orientation += 360; if (m_lastOrientation == orientation) // static hit, not considering return; m_lastOrientation = orientation; float magnitude = sqrt(x*x + y*y + z*z); /* * External acceleration and tilt decisions * Some code inspired by Android's orientation helper */ if (magnitude < m_minAccel || magnitude > m_maxAccel) return; int tiltAngle = round(RAD_TO_DEG(asin(z / magnitude))); if (tiltAngle > m_maxTilt) return; if (m_readingCache.orientation() == QOrientationReading::Undefined) { m_readingCache.setOrientation(QOrientationReading::TopUp); nearestRotation = 0; } else { nearestRotation = (orientation + 45) / 90; if (nearestRotation > 3) nearestRotation = 0; if (!(tiltAngle >= m_tiltTolerance[nearestRotation][0] && tiltAngle <= m_tiltTolerance[nearestRotation][1])) return; /* * Calculate rotation-hint offsets * Inspired by Android code */ if (m_lastRotation == nearestRotation || nearestRotation == (m_lastRotation + 1) % 4) { int lowerBound = (nearestRotation*90) - 45 + 45/2; if (nearestRotation == 0) { if (orientation >= 315 && orientation < lowerBound + 360) { return; } } else { if (orientation < lowerBound) { return; } } } if (m_lastRotation == nearestRotation || nearestRotation == (m_lastRotation + 3) % 4) { int upperBound = (nearestRotation*90) + 45 - 45/2; if (nearestRotation == 0) { if (orientation <= 45 && orientation > upperBound) { return; } } else { if (orientation > upperBound) { return; } } } } switch (nearestRotation) { case 0: m_reading.setOrientation(QOrientationReading::TopUp); break; case 1: m_reading.setOrientation(QOrientationReading::LeftUp); break; case 2: m_reading.setOrientation(QOrientationReading::TopDown); break; case 3: m_reading.setOrientation(QOrientationReading::RightUp); break; } if (m_reading.orientation() != m_readingCache.orientation()) { newReadingAvailable(); m_readingCache.setOrientation(m_reading.orientation()); } m_lastRotation = nearestRotation; } qtubuntu-sensors-0.6+16.04.20151127/plugins/sensors/sensors.json0000644000015300001610000000000312626127204025033 0ustar pbuserpbgroup00000000000000{} qtubuntu-sensors-0.6+16.04.20151127/plugins/sensors/core_shared_accelerometer.h0000644000015300001610000000337112626127204030000 0ustar pbuserpbgroup00000000000000/* * Copyright (C) 2013 Canonical, Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . */ #ifndef CORE_SHARED_ACCELEROMETER_H #define CORE_SHARED_ACCELEROMETER_H #include #include #include #include namespace core { class SharedAccelerometer : public QObject { Q_OBJECT public: static SharedAccelerometer& instance(); void start(); void stop(); qreal getMinDelay() const; qreal getMinValue() const; qreal getMaxValue() const; qreal getResolution() const; void setDelay(quint32 delay_us); Q_SIGNALS: void accelerometerReadingChanged(QSharedPointer reading); private: SharedAccelerometer(QObject *parent = NULL); UASensorsAccelerometer *m_accelerometer; qreal m_minDelay; qreal m_minValue; qreal m_maxValue; qreal m_resolution; bool m_available; // Gets called by the underlying platform when there is a new accelerometer reading static void onAccelerometerReadingCb(UASAccelerometerEvent *event, void *context); void onAccelerometerReading(UASAccelerometerEvent *event); }; } #endif // CORE_SHARED_ACCELEROMETER_H qtubuntu-sensors-0.6+16.04.20151127/plugins/position/0000755000015300001610000000000012626127552022631 5ustar pbuserpbgroup00000000000000qtubuntu-sensors-0.6+16.04.20151127/plugins/position/core_geo_position_info_source.h0000644000015300001610000000350312626127204031076 0ustar pbuserpbgroup00000000000000/* * Copyright 2013 Canonical Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . * * Author: Thomas Voß */ #ifndef CORE_GEO_POSITION_INFO_SOURCE_H #define CORE_GEO_POSITION_INFO_SOURCE_H #include namespace core { class GeoPositionInfoSource : public QGeoPositionInfoSource { Q_OBJECT public: GeoPositionInfoSource(QObject *parent = 0); ~GeoPositionInfoSource(); // From QGeoPositionInfoSource void setUpdateInterval(int msec); QGeoPositionInfo lastKnownPosition(bool fromSatellitePositioningMethodsOnly = false) const; PositioningMethods supportedPositioningMethods() const; void setPreferredPositioningMethods(PositioningMethods methods); int minimumUpdateInterval() const; Error error() const; // Updates request state enum State { stopped = 0, running, one_shot, suspended, }; public slots: void applicationStateChanged(); virtual void startUpdates(); virtual void stopUpdates(); virtual void requestUpdate(int timeout = 5000); void timeout(); private: bool m_applicationActive; int m_lastReqTimeout; int m_state; struct Private; QScopedPointer d; }; } #endif // CORE_GEO_POSITION_INFO_SOURCE_H qtubuntu-sensors-0.6+16.04.20151127/plugins/position/position.qrc0000644000015300001610000000013712626127204025177 0ustar pbuserpbgroup00000000000000 plugin.json qtubuntu-sensors-0.6+16.04.20151127/plugins/position/CMakeLists.txt0000644000015300001610000000216512626127204025367 0ustar pbuserpbgroup00000000000000find_package(PkgConfig) find_package(Qt5Core REQUIRED) find_package(Qt5Gui REQUIRED) find_package(Qt5Location REQUIRED) pkg_check_modules(UBUNTU_PLATFORM_API ubuntu-platform-api REQUIRED) qt5_add_resources(POSITION_RESOURCES position.qrc) add_library( qtposition_ubuntu SHARED core_geo_position_info_source.h core_geo_position_info_source_factory.h core_geo_position_info_source.cpp core_geo_position_info_source_factory.cpp ${POSITION_RESOURCES} ) # Ideally, we would read the plugin installation location from cmake # but this does not work currently. set(PLUGIN_INSTALL_LOCATION "${CMAKE_INSTALL_LIBDIR}/qt5/plugins/position") # get_target_property(PLUGIN_LOCATION Qt5::Location PLUGIN_LOCATION) message(STATUS "Installing Qt5 location plugin to: ${PLUGIN_INSTALL_LOCATION}") qt5_use_modules(qtposition_ubuntu Core Gui Location) qt5_use_modules(qtposition_ubuntu Gui Location) target_link_libraries( qtposition_ubuntu ${CMAKE_THREAD_LIBS_INIT} Qt5::Core Qt5::Gui Qt5::Location ${UBUNTU_PLATFORM_API_LIBRARIES} ) install( TARGETS qtposition_ubuntu LIBRARY DESTINATION ${PLUGIN_INSTALL_LOCATION}) qtubuntu-sensors-0.6+16.04.20151127/plugins/position/core_geo_position_info_source_factory.cpp0000644000015300001610000000244212626127204033161 0ustar pbuserpbgroup00000000000000/* * Copyright 2013 Canonical Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . * * Author: Thomas Voß */ #include "core_geo_position_info_source_factory.h" #include "core_geo_position_info_source.h" QGeoPositionInfoSource *core::GeoPositionInfoSourceFactory::positionInfoSource(QObject *parent) { core::GeoPositionInfoSource *src = new core::GeoPositionInfoSource(parent); return src; } QGeoSatelliteInfoSource *core::GeoPositionInfoSourceFactory::satelliteInfoSource(QObject *parent) { Q_UNUSED(parent); return 0; } #if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)) QGeoAreaMonitorSource *core::GeoPositionInfoSourceFactory::areaMonitor(QObject *parent) { Q_UNUSED(parent); return 0; } #endif qtubuntu-sensors-0.6+16.04.20151127/plugins/position/core_geo_position_info_source.cpp0000644000015300001610000003627712626127204031447 0ustar pbuserpbgroup00000000000000/* * Copyright 2013 Canonical Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . * * Author: Thomas Voß */ #include "core_geo_position_info_source.h" #include #include #include #include #include #include #include #include struct core::GeoPositionInfoSource::Private { // If an application requests an individual position update with a // timeout value of 0, we bump the timeout to the default value of // 10 seconds. static const unsigned int default_timeout_in_ms = 10 * 1000; // Our session flags for connecting to the backend service. static const unsigned int empty_creation_flags = 0; // Translates a backend service error to a QGeoPositionInfoSource::Error static QGeoPositionInfoSource::Error ua_location_error_to_qt(UALocationServiceError error); // Called for every new position update arriving from the backend service. static void processPositionUpdate( UALocationPositionUpdate* position, void* context); // Called for every new heading update arriving from the backend service. static void processHeadingUpdate( UALocationHeadingUpdate* heading, void* context); // Called for every new heading update arriving from the backend service. static void processVelocityUpdate( UALocationVelocityUpdate* velocity, void* context); // Processes the incoming position update and translates it to Qt world. void handlePositionUpdate(UALocationPositionUpdate* position); // Processes the incoming heading update and translates it to Qt world. void handleHeadingUpdate(UALocationHeadingUpdate* heading); // Processes the incoming velocity update and translates it to Qt world. void handleVelocityUpdate(UALocationVelocityUpdate* velocity); void createLocationServiceSession(); void destroyLocationServiceSession(); // Creates a new instance and attempts to connect to the background service. // Stores errors in the error member. Private(core::GeoPositionInfoSource* parent); ~Private(); core::GeoPositionInfoSource* parent; UALocationServiceSession* session; QMutex lastKnownPositionGuard; QGeoPositionInfo lastKnownPosition; QTimer timer; QGeoPositionInfoSource::Error error; }; core::GeoPositionInfoSource::GeoPositionInfoSource(QObject *parent) : QGeoPositionInfoSource(parent), m_applicationActive(true), m_lastReqTimeout(-1), m_state(State::stopped), d(new Private(this)) { d->timer.setSingleShot(true); QObject::connect(&d->timer, SIGNAL(timeout()), this, SLOT(timeout()), Qt::DirectConnection); // Whenever we receive an update, we stop the timeout timer immediately. QObject::connect(this, SIGNAL(positionUpdated(const QGeoPositionInfo&)), &d->timer, SLOT(stop())); QObject::connect(qApp, SIGNAL(applicationStateChanged(Qt::ApplicationState)), this, SLOT(applicationStateChanged())); qRegisterMetaType("Qt::ApplicationState"); } void core::GeoPositionInfoSource::applicationStateChanged() { Qt::ApplicationState state = qApp->applicationState(); if (state == Qt::ApplicationInactive) { if (m_applicationActive) { int state = m_state; stopUpdates(); m_applicationActive = false; if (state == State::one_shot) { // Save current time out if (d->timer.isActive()) { m_lastReqTimeout = d->timer.interval(); d->timer.stop(); } } else if (state == State::running) { // Stop continuous updates and suspend m_state = State::suspended; } } } else if (state == Qt::ApplicationActive) { if (!m_applicationActive) { m_applicationActive = true; // Only restart updates if active before suspending if (m_lastReqTimeout > -1) { requestUpdate(m_lastReqTimeout); m_lastReqTimeout = -1; } else if (m_state == State::suspended) { // Restart continuous updates startUpdates(); } } } } core::GeoPositionInfoSource::~GeoPositionInfoSource() { } void core::GeoPositionInfoSource::setUpdateInterval(int msec) { // We emit our current error state whenever a caller tries to interact // with the source although we are in error state. if (error() != QGeoPositionInfoSource::NoError) { Q_EMIT(QGeoPositionInfoSource::error(d->error)); return; } (void) msec; } void core::GeoPositionInfoSource::setPreferredPositioningMethods(PositioningMethods methods) { // We emit our current error state whenever a caller tries to interact // with the source although we are in error state. if (error() != QGeoPositionInfoSource::NoError) { Q_EMIT(QGeoPositionInfoSource::error(d->error)); return; } QGeoPositionInfoSource::setPreferredPositioningMethods(methods); } QGeoPositionInfo core::GeoPositionInfoSource::lastKnownPosition(bool fromSatellitePositioningMethodsOnly) const { // We emit our current error state whenever a caller tries to interact // with the source although we are in error state. if (error() != QGeoPositionInfoSource::NoError) return QGeoPositionInfo(); Q_UNUSED(fromSatellitePositioningMethodsOnly); QMutexLocker lock(&d->lastKnownPositionGuard); return QGeoPositionInfo(d->lastKnownPosition); } QGeoPositionInfoSource::PositioningMethods core::GeoPositionInfoSource::supportedPositioningMethods() const { // We emit our current error state whenever a caller tries to interact // with the source although we are in error state. if (error() != QGeoPositionInfoSource::NoError) { return QGeoPositionInfoSource::NoPositioningMethods; } return AllPositioningMethods; } void core::GeoPositionInfoSource::startUpdates() { if (d->session == nullptr) { d->createLocationServiceSession(); } // We emit our current error state whenever a caller tries to interact // with the source although we are in error state. if (error() != QGeoPositionInfoSource::NoError) { Q_EMIT(QGeoPositionInfoSource::error(d->error)); return; } ua_location_service_session_start_position_updates(d->session); ua_location_service_session_start_heading_updates(d->session); ua_location_service_session_start_velocity_updates(d->session); if (m_state != State::one_shot) m_state = State::running; } int core::GeoPositionInfoSource::minimumUpdateInterval() const { // We emit our current error state whenever a caller tries to interact // with the source although we are in error state. if (error() != QGeoPositionInfoSource::NoError) { return -1; } return 500; } void core::GeoPositionInfoSource::stopUpdates() { ua_location_service_session_stop_position_updates(d->session); ua_location_service_session_stop_heading_updates(d->session); ua_location_service_session_stop_velocity_updates(d->session); m_state = State::stopped; } void core::GeoPositionInfoSource::requestUpdate(int timeout) { // We emit our current error state whenever a caller tries to interact // with the source although we are in error state. if (error() != QGeoPositionInfoSource::NoError) { Q_EMIT(QGeoPositionInfoSource::error(d->error)); return; } if (d->timer.isActive()) { return; } // Bump the timeout if caller indicates "choose default value". if (timeout <= 0) timeout = Private::default_timeout_in_ms; startUpdates(); d->timer.start(timeout); } void core::GeoPositionInfoSource::timeout() { // Update timeout reached, clean up stopUpdates(); Q_EMIT updateTimeout(); } QGeoPositionInfoSource::Error core::GeoPositionInfoSource::error() const { return d->error; } QGeoPositionInfoSource::Error core::GeoPositionInfoSource::Private::ua_location_error_to_qt(UALocationServiceError error) { switch (error) { case UA_LOCATION_SERVICE_ERROR_NONE: return QGeoPositionInfoSource::NoError; case UA_LOCATION_SERVICE_ERROR_NO_CONNECTION: return QGeoPositionInfoSource::UnknownSourceError; case UA_LOCATION_SERVICE_ERROR_NO_ACCESS: return QGeoPositionInfoSource::AccessError; case UA_LOCATION_SERVICE_ERROR_GENERIC_ERROR: return QGeoPositionInfoSource::UnknownSourceError; } return QGeoPositionInfoSource::UnknownSourceError; } // Called for every new position update arriving from the backend service. void core::GeoPositionInfoSource::Private::processPositionUpdate(UALocationPositionUpdate* position, void* context) { if (!position) return; Private* thiz = static_cast(context); if (!thiz) return; thiz->handlePositionUpdate(position); } // Called for every new heading update arriving from the backend service. void core::GeoPositionInfoSource::Private::processHeadingUpdate(UALocationHeadingUpdate* heading, void* context) { if (!heading) return; Private* thiz = static_cast(context); if (!thiz) return; thiz->handleHeadingUpdate(heading); } // Called for every new heading update arriving from the backend service. void core::GeoPositionInfoSource::Private::processVelocityUpdate(UALocationVelocityUpdate* velocity, void* context) { if (!velocity) return; Private* thiz = static_cast(context); if (!thiz) return; thiz->handleVelocityUpdate(velocity); } // Processes the incoming position update and translates it to Qt world. void core::GeoPositionInfoSource::Private::handlePositionUpdate(UALocationPositionUpdate* position) { QGeoCoordinate coord( ua_location_position_update_get_latitude_in_degree(position), ua_location_position_update_get_longitude_in_degree(position), ua_location_position_update_has_altitude(position) ? ua_location_position_update_get_altitude_in_meter(position) : 0); QMutexLocker lock(&lastKnownPositionGuard); lastKnownPosition.setCoordinate(coord); if (ua_location_position_update_has_horizontal_accuracy(position)) { double accuracy = ua_location_position_update_get_horizontal_accuracy_in_meter(position); if (!std::isnan(accuracy)) lastKnownPosition.setAttribute(QGeoPositionInfo::HorizontalAccuracy, accuracy); } if (ua_location_position_update_has_vertical_accuracy(position)) { double accuracy = ua_location_position_update_get_vertical_accuracy_in_meter(position); if (!std::isnan(accuracy)) lastKnownPosition.setAttribute(QGeoPositionInfo::VerticalAccuracy, accuracy); } lastKnownPosition.setTimestamp( QDateTime::fromMSecsSinceEpoch( ua_location_position_update_get_timestamp(position)/1000)); QGeoPositionInfo info{lastKnownPosition}; QMetaObject::invokeMethod( parent, "positionUpdated", Qt::QueuedConnection, Q_ARG(QGeoPositionInfo, info)); if (timer.isActive()) timer.stop(); if (parent->m_state == State::one_shot) parent->stopUpdates(); } // Processes the incoming heading update and translates it to Qt world. void core::GeoPositionInfoSource::Private::handleHeadingUpdate(UALocationHeadingUpdate* heading) { QMutexLocker lock(&lastKnownPositionGuard); lastKnownPosition.setAttribute( QGeoPositionInfo::Direction, ua_location_heading_update_get_heading_in_degree(heading)); lastKnownPosition.setTimestamp( QDateTime::fromMSecsSinceEpoch( ua_location_heading_update_get_timestamp(heading)/1000)); QGeoPositionInfo info{lastKnownPosition}; QMetaObject::invokeMethod( parent, "positionUpdated", Qt::QueuedConnection, Q_ARG(QGeoPositionInfo, info)); } // Processes the incoming velocity update and translates it to Qt world. void core::GeoPositionInfoSource::Private::handleVelocityUpdate(UALocationVelocityUpdate* velocity) { QMutexLocker lock(&lastKnownPositionGuard); lastKnownPosition.setAttribute( QGeoPositionInfo::GroundSpeed, ua_location_velocity_update_get_velocity_in_meters_per_second(velocity)); lastKnownPosition.setTimestamp( QDateTime::fromMSecsSinceEpoch( ua_location_velocity_update_get_timestamp(velocity)/1000)); QGeoPositionInfo info{lastKnownPosition}; QMetaObject::invokeMethod( parent, "positionUpdated", Qt::QueuedConnection, Q_ARG(QGeoPositionInfo, info)); } void core::GeoPositionInfoSource::Private::createLocationServiceSession() { UALocationServiceError e; session = ua_location_service_try_create_session_for_high_accuracy(core::GeoPositionInfoSource::Private::empty_creation_flags, &e); error = core::GeoPositionInfoSource::Private::ua_location_error_to_qt(e); ua_location_service_session_set_position_updates_handler( session, core::GeoPositionInfoSource::Private::processPositionUpdate, this); ua_location_service_session_set_heading_updates_handler( session, core::GeoPositionInfoSource::Private::processHeadingUpdate, this); ua_location_service_session_set_velocity_updates_handler( session, core::GeoPositionInfoSource::Private::processVelocityUpdate, this); } void core::GeoPositionInfoSource::Private::destroyLocationServiceSession() { if (session != nullptr) { ua_location_service_session_set_position_updates_handler( session, nullptr, nullptr); ua_location_service_session_set_heading_updates_handler( session, nullptr, nullptr); ua_location_service_session_set_velocity_updates_handler( session, nullptr, nullptr); ua_location_service_session_unref(session); session = nullptr; } } // Creates a new instance and attempts to connect to the background service. // Stores errors in the error member. core::GeoPositionInfoSource::Private::Private(core::GeoPositionInfoSource* parent) : parent(parent), session(nullptr), error(QGeoPositionInfoSource::NoError) { qRegisterMetaType("QGeoPositionInfo"); } core::GeoPositionInfoSource::Private::~Private() { destroyLocationServiceSession(); } qtubuntu-sensors-0.6+16.04.20151127/plugins/position/core_geo_position_info_source_factory.h0000644000015300001610000000274312626127204032632 0ustar pbuserpbgroup00000000000000/* * Copyright 2013 Canonical Ltd. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . * * Author: Thomas Voß */ #ifndef CORE_GEO_POSITION_INFO_SOURCE_FACTORY_H #define CORE_GEO_POSITION_INFO_SOURCE_FACTORY_H #include #include "qgeopositioninfosource.h" #include "qgeopositioninfosourcefactory.h" namespace core { class GeoPositionInfoSourceFactory : public QObject, public QGeoPositionInfoSourceFactory { Q_OBJECT Q_PLUGIN_METADATA(IID "org.qt-project.qt.position.sourcefactory/5.0" FILE "plugin.json") Q_INTERFACES(QGeoPositionInfoSourceFactory) public: QGeoPositionInfoSource *positionInfoSource(QObject *parent); QGeoSatelliteInfoSource *satelliteInfoSource(QObject *parent); #if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)) QGeoAreaMonitorSource *areaMonitor(QObject *parent); #endif }; } #endif // CORE_GEO_POSITION_INFO_SOURCE_FACTORY_H qtubuntu-sensors-0.6+16.04.20151127/plugins/position/plugin.json0000644000015300001610000000017112626127204025013 0ustar pbuserpbgroup00000000000000{ "Keys": ["ubuntu"], "Provider": "ubuntu", "Position": true, "Satellite": false, "Priority": 2000 }