You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by he...@apache.org on 2012/03/23 23:21:49 UTC
[10/10] webos commit: Added initial compass support
Added initial compass support
Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-webos/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-webos/commit/7eeda79d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-webos/tree/7eeda79d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-webos/diff/7eeda79d
Branch: refs/heads/master
Commit: 7eeda79d34df3db0c469091d9e41b2d50b5d38bb
Parents: e1ca3d8
Author: Markus Leutwyler <ml...@yahoo.com>
Authored: Wed Mar 7 14:07:15 2012 +0100
Committer: Markus Leutwyler <ml...@yahoo.com>
Committed: Wed Mar 7 14:07:15 2012 +0100
----------------------------------------------------------------------
Makefile | 1 +
js/compass.js | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 103 insertions(+), 0 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-cordova-webos/blob/7eeda79d/Makefile
----------------------------------------------------------------------
diff --git a/Makefile b/Makefile
index 92162ba..77d3b17 100755
--- a/Makefile
+++ b/Makefile
@@ -56,6 +56,7 @@ lib/phonegap.js: js/phonegap-core.js js/acceleration.js js/accelerometer.js js/a
$(CAT) js/audio.js >> $@
$(CAT) js/camera.js >> $@
$(CAT) js/contacts.js >> $@
+ $(CAT) js/compass.js >> $@
$(CAT) js/debugconsole.js >> $@
$(CAT) js/device.js >> $@
$(CAT) js/file.js >> $@
http://git-wip-us.apache.org/repos/asf/incubator-cordova-webos/blob/7eeda79d/js/compass.js
----------------------------------------------------------------------
diff --git a/js/compass.js b/js/compass.js
new file mode 100644
index 0000000..824d0df
--- /dev/null
+++ b/js/compass.js
@@ -0,0 +1,102 @@
+/*
+ * This class provides access to device compass data.
+ * @constructor
+ */
+function Compass() {
+ /*
+ * The last known heading.
+ */
+ this.lastHeading = null;
+};
+
+/*
+ * Asynchronously aquires the current compass heading.
+ * @param {Function} successCallback The function to call when the compass
+ * data is available
+ * @param {Function} errorCallback The function to call when there is an error
+ * getting the compass data.
+ * @param {CompassOptions} options The options for getting the compass data
+ * such as timeout.
+ */
+
+Compass.prototype.getCurrentHeading = function(successCallback, errorCallback, options) {
+
+ var referenceTime = 0;
+ if (this.lastHeading)
+ referenceTime = this.lastHeading.timestamp;
+ else
+ this.start();
+
+ var timeout = 20000;
+ var interval = 500;
+ if (typeof(options) == 'object' && options.interval)
+ interval = options.interval;
+
+ if (typeof(successCallback) != 'function')
+ successCallback = function() {};
+ if (typeof(errorCallback) != 'function')
+ errorCallback = function() {};
+
+ var dis = this;
+ var delay = 0;
+ var timer = setInterval(function() {
+ delay += interval;
+
+ //if we have a new compass heading, call success and cancel the timer
+ if (typeof(dis.lastHeading) == 'object' && dis.lastHeading != null && dis.lastHeading.timestamp > referenceTime) {
+ successCallback(dis.lastHeading.magHeading);
+ clearInterval(timer);
+ } else if (delay >= timeout) { //else if timeout has occured then call error and cancel the timer
+ errorCallback();
+ clearInterval(timer);
+ }
+ //else the interval gets called again
+ }, interval);
+};
+
+
+/*
+ * Asynchronously aquires the compass heading repeatedly at a given interval.
+ * @param {Function} successCallback The function to call each time the acceleration
+ * data is available
+ * @param {Function} errorCallback The function to call when there is an error
+ * getting the compass data.
+ * @param {CompassOptions} options The options for getting the compass data
+ * such as timeout.
+ */
+
+Compass.prototype.watchHeading = function(successCallback, errorCallback, options) {
+ this.getCurrentHeading(successCallback, errorCallback, options);
+ // TODO: add the interval id to a list so we can clear all watches
+ var frequency = (options != undefined)? options.frequency : 10000;
+ var that = this;
+ return setInterval(function() {
+ that.getCurrentHeading(successCallback, errorCallback, options);
+ }, frequency);
+};
+
+/*
+ * Clears the specified heading watch.
+ * @param {String} watchId The ID of the watch returned from #watchHeading.
+ */
+Compass.prototype.clearWatch = function(watchId) {
+ clearInterval(watchId);
+};
+
+/*
+ * Starts the native compass listener.
+ */
+
+Compass.prototype.start = function() {
+ var that = this;
+ //Mojo.Event.listen(document, "acceleration", function(event) {
+ document.addEventListener("compass", function(event) {
+ var heading={};
+ heading.trueHeading=event.trueHeading;
+ heading.magHeading=event.magHeading;
+ heading.timestamp = new Date().getTime();
+ that.lastHeading = heading;
+ });
+};
+
+if (typeof navigator.compass == "undefined") navigator.compass = new Compass();