Change compass listener and optimize accelerometer listener.

This commit is contained in:
Bryce Curtis
2010-08-20 10:59:45 -05:00
parent e5bbbbe35c
commit 27c4de6aa0
7 changed files with 342 additions and 295 deletions
+144 -65
View File
@@ -6,90 +6,169 @@ function Compass() {
/**
* The last known Compass position.
*/
this.lastHeading = null;
this.lastError = null;
this.callbacks = {
onHeadingChanged: [],
onError: []
};
this.lastHeading = null;
/**
* List of compass watch timers
*/
this.timers = {};
};
Compass.STOPPED = 0;
Compass.STARTING = 1;
Compass.RUNNING = 2;
Compass.ERROR_FAILED_TO_START = 3;
Compass.ERROR_MSG = ["Not running", "Starting", "", "Failed to start"];
/**
* Asynchronously aquires the current heading.
* @param {Function} successCallback The function to call when the heading
* data is available
* @param {Function} errorCallback The function to call when there is an error
* getting the heading data.
* @param {PositionOptions} options The options for getting the heading data
* such as timeout.
*
* @param {Function} successCallback The function to call when the heading data is available
* @param {Function} errorCallback The function to call when there is an error getting the heading data.
* @param {PositionOptions} options The options for getting the heading data such as timeout.
*/
Compass.prototype.getCurrentHeading = function(successCallback, errorCallback, options) {
if (this.lastHeading == null) {
CompassHook.start();
}
else
if (typeof successCallback == "function") {
successCallback(this.lastHeading);
}
};
// successCallback required
if (typeof successCallback != "function") {
console.log("Compass Error: successCallback is not a function");
return;
}
// errorCallback optional
if (errorCallback && (typeof errorCallback != "function")) {
console.log("Compass Error: errorCallback is not a function");
return;
}
// Get current compass status
var status = CompassHook.getStatus();
// If running, then call successCallback
if (status == Compass.RUNNING) {
try {
var heading = CompassHook.getHeading();
successCallback(heading);
} catch (e) {
console.log("Compass Error in successCallback: " + e);
}
}
// If not running, then start it
else {
CompassHook.start();
// Wait until started
var timer = setInterval(function() {
var status = CompassHook.getStatus();
if (status != Compass.STARTING) {
clearInterval(timer);
// If compass is running
if (status == Compass.RUNNING) {
try {
var heading = CompassHook.getHeading();
successCallback(heading);
} catch (e) {
console.log("Compass Error in successCallback: " + e);
}
}
// If compass error
else {
console.log("Compass Error: "+ Compass.ERROR_MSG[status]);
try {
if (errorCallback) {
errorCallback(status);
}
} catch (e) {
console.log("Compass Error in errorCallback: " + e);
}
}
}
}, 10);
}
}
/**
* Asynchronously aquires the heading repeatedly at a given interval.
* @param {Function} successCallback The function to call each time the heading
* data is available
* @param {Function} errorCallback The function to call when there is an error
* getting the heading data.
* @param {HeadingOptions} options The options for getting the heading data
* such as timeout and the frequency of the watch.
*
* @param {Function} successCallback The function to call each time the heading data is available
* @param {Function} errorCallback The function to call when there is an error getting the heading data.
* @param {HeadingOptions} options The options for getting the heading data such as timeout and the frequency of the watch.
* @return String The watch id that must be passed to #clearWatch to stop watching.
*/
Compass.prototype.watchHeading= function(successCallback, errorCallback, options) {
// Invoke the appropriate callback with a new Position object every time the implementation
// determines that the position of the hosting device has changed.
this.getCurrentHeading(successCallback, errorCallback, options);
var frequency = 100;
if (typeof(options) == 'object' && options.frequency)
frequency = options.frequency;
var self = this;
return setInterval(function() {
self.getCurrentHeading(successCallback, errorCallback, options);
}, frequency);
};
// Default interval (100 msec)
var frequency = (options != undefined) ? options.frequency : 100;
// successCallback required
if (typeof successCallback != "function") {
console.log("Compass Error: successCallback is not a function");
return;
}
// errorCallback optional
if (errorCallback && (typeof errorCallback != "function")) {
console.log("Compass Error: errorCallback is not a function");
return;
}
// Make sure compass timeout > frequency + 10 sec
var timeout = CompassHook.getTimeout();
if (timeout < (frequency + 10000)) {
CompassHook.setTimeout(frequency + 10000); // set to frequency + 10 sec
}
var id = PhoneGap.createUUID();
CompassHook.start();
// Start watch timer
navigator.compass.timers[id] = setInterval(function() {
var status = CompassHook.getStatus();
// If compass is running
if (status == Compass.RUNNING) {
try {
var heading = CompassHook.getHeading();
successCallback(heading);
} catch (e) {
console.log("Compass Error in successCallback: " + e);
}
}
// If compass had error
else if (status != Compass.STARTING) {
console.log("Compass Error: "+ Compass.ERROR_MSG[status]);
try {
navigator.compass.clearWatch(id);
if (errorCallback) {
errorCallback(status);
}
} catch (e) {
console.log("Compass Error in errorCallback: " + e);
}
}
}, (frequency ? frequency : 1));
return id;
}
/**
* Clears the specified heading watch.
* @param {String} watchId The ID of the watch returned from #watchHeading.
*
* @param {String} id The ID of the watch returned from #watchHeading.
*/
Compass.prototype.clearWatch = function(watchId) {
clearInterval(watchId);
};
Compass.prototype.clearWatch = function(id) {
/**
* Called by the geolocation framework when the current heading is found.
* @param {HeadingOptions} position The current heading.
*/
Compass.prototype.setHeading = function(heading) {
this.lastHeading = heading;
for (var i = 0; i < this.callbacks.onHeadingChanged.length; i++) {
var f = this.callbacks.onHeadingChanged.shift();
f(heading);
// Stop javascript timer & remove from timer list
if (id && navigator.compass.timers[id]) {
clearInterval(navigator.compass.timers[id]);
delete navigator.compass.timers[id];
}
};
/**
* Called by the geolocation framework when an error occurs while looking up the current position.
* @param {String} message The text of the error message.
*/
Compass.prototype.setError = function(message) {
this.lastError = message;
for (var i = 0; i < this.callbacks.onError.length; i++) {
var f = this.callbacks.onError.shift();
f(message);
}
};
}
PhoneGap.addConstructor(function() {
if (typeof navigator.compass == "undefined") navigator.compass = new Compass();