Updated Branches: refs/heads/dev e701ebc1f -> f8116c650
[ubuntu] add missing files Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/commit/d02710c7 Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/tree/d02710c7 Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/diff/d02710c7 Branch: refs/heads/dev Commit: d02710c79f9501573d6f8ace980554e7410f31ae Parents: 0cad0fe Author: Maxim Ermilov <[email protected]> Authored: Mon Jan 20 18:03:45 2014 +0400 Committer: Maxim Ermilov <[email protected]> Committed: Mon Jan 20 18:03:45 2014 +0400 ---------------------------------------------------------------------- src/ubuntu/compass.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++++ src/ubuntu/compass.h | 58 +++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/blob/d02710c7/src/ubuntu/compass.cpp ---------------------------------------------------------------------- diff --git a/src/ubuntu/compass.cpp b/src/ubuntu/compass.cpp new file mode 100644 index 0000000..a2c3bee --- /dev/null +++ b/src/ubuntu/compass.cpp @@ -0,0 +1,75 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "compass.h" + +DeviceOrientation::DeviceOrientation(Cordova *cordova): CPlugin(cordova), _validData(false) { + _compass.connectToBackend(); + connect(&_compass, SIGNAL(readingChanged()), SLOT(updateSensor())); + connect(&_compass, SIGNAL(sensorError(int)), SLOT(sensorError(int))); +} + +void DeviceOrientation::getHeading(int scId, int ecId, QVariantMap options) { + Q_UNUSED(options); + if (_compass.isConnectedToBackend() || !_compass.start()) { + this->callback(ecId, "CompassError.COMPASS_NOT_SUPPORTED"); + return; + } + + _successCallbacks << scId; + _errorCallbacks << ecId; + + if (_validData) { + reportResult(); + return; + } +} + +void DeviceOrientation::sensorError(int error) { + Q_UNUSED(error); + + for (int ecId: _errorCallbacks) { + this->callback(ecId, "CompassError.COMPASS_INTERNAL_ERR"); + } + + _errorCallbacks.clear(); + _successCallbacks.clear(); + _validData = false; +} + +void DeviceOrientation::reportResult() { + QVariantMap obj; + + obj.insert("magneticHeading", _azymuth); + obj.insert("trueHeading", _azymuth); + obj.insert("headingAccuracy", _accuracy); + obj.insert("timestamp", _timestamp); + + for (int scId: _successCallbacks) { + this->cb(scId, obj); + } + + _errorCallbacks.clear(); + _successCallbacks.clear(); +} + +void DeviceOrientation::updateSensor(){ + QCompassReading *heading = _compass.reading(); + _azymuth = heading->azimuth(); + _accuracy = heading->calibrationLevel(); + _timestamp = QDateTime::currentDateTime().toMSecsSinceEpoch(); + + _validData = true; + reportResult(); +} http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/blob/d02710c7/src/ubuntu/compass.h ---------------------------------------------------------------------- diff --git a/src/ubuntu/compass.h b/src/ubuntu/compass.h new file mode 100644 index 0000000..a1f421e --- /dev/null +++ b/src/ubuntu/compass.h @@ -0,0 +1,58 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef COMPASS_H_HKFSAHKDFAS +#define COMPASS_H_HKFSAHKDFAS + +#include <cplugin.h> +#include <QCompass> +#include <QtCore> + +class DeviceOrientation: public CPlugin { + Q_OBJECT +public: + explicit DeviceOrientation(Cordova *cordova); + + virtual const QString fullName() override { + return DeviceOrientation::fullID(); + } + + virtual const QString shortName() override { + return "Compass"; + } + + static const QString fullID() { + return "Compass"; + } + +public slots: + void getHeading(int scId, int ecId, QVariantMap options); + +protected slots: + void updateSensor(); + void sensorError(int error); + +private: + void reportResult(); + QCompass _compass; + QList<int> _successCallbacks; + QList<int> _errorCallbacks; + + double _azymuth; + double _accuracy; + qtimestamp _timestamp; + bool _validData; +}; + +#endif
