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
+25 -65
View File
@@ -16,48 +16,17 @@ function Accelerometer() {
*/
this.lastAcceleration = null;
/**
* List of accelerometer watch listeners
*/
this.accelListeners = {};
/**
* List of accelerometer watch timers
*/
this.accelTimers = {};
/**
* Next id to use
*/
this.listenerId = 0;
/**
* Timer that turns off accelerometer when it reaches maximum time.
* This timer is only used for getCurrentAcceleration.
*/
this.turnOffTimer = 0;
// Turn off accelerometer if not accessed for certain amount of time
setInterval(function() {
navigator.accelerometer.turnOffTimer += 10;
if (navigator.accelerometer.turnOffTimer > Accelerometer.MAX_TIMER) {
Accel.stop("timer");
navigator.accelerometer.turnOffTimer = 0;
}
}, 10000);
this.timers = {};
}
Accelerometer.STOPPED = 0;
Accelerometer.STARTING = 1;
Accelerometer.RUNNING = 2;
Accelerometer.ERROR_FAILED_TO_START = 3;
Accelerometer.ERROR_NOT_FOUND = 4;
Accelerometer.ERROR_MSG = ["Not running", "Starting", "", "Failed to start", "Listener not found"];
/**
* Time (in seconds) to turn off accelerometer if getCurrentAcceleration() hasn't been called.
*/
Accelerometer.MAX_TIMER = 30;
Accelerometer.ERROR_MSG = ["Not running", "Starting", "", "Failed to start"];
/**
* Asynchronously aquires the current acceleration.
@@ -96,9 +65,7 @@ Accelerometer.prototype.getCurrentAcceleration = function(successCallback, error
// If not running, then start it
else {
Accel.start("timer");
navigator.accelerometer.turnOffTimer = 0;
var obj = this;
Accel.start();
// Wait until started
var timer = setInterval(function() {
@@ -117,16 +84,16 @@ Accelerometer.prototype.getCurrentAcceleration = function(successCallback, error
}
// If accelerometer error
else {
console.log("Accelerometer Error: "+ Accelerometer.ERROR_MSG[status]);
try {
if (errorCallback) {
errorCallback(status);
else {
console.log("Accelerometer Error: "+ Accelerometer.ERROR_MSG[status]);
try {
if (errorCallback) {
errorCallback(status);
}
} catch (e) {
console.log("Accelerometer Error in errorCallback: " + e);
}
}
} catch (e) {
console.log("Accelerometer Error in errorCallback: " + e);
}
}
}
}, 10);
}
@@ -141,6 +108,7 @@ Accelerometer.prototype.getCurrentAcceleration = function(successCallback, error
* @return String The watch id that must be passed to #clearWatch to stop watching.
*/
Accelerometer.prototype.watchAcceleration = function(successCallback, errorCallback, options) {
// Default interval (10 sec)
var frequency = (options != undefined)? options.frequency : 10000;
@@ -156,12 +124,17 @@ Accelerometer.prototype.watchAcceleration = function(successCallback, errorCallb
return;
}
var id = ""+(navigator.accelerometer.listenerId++);
navigator.accelerometer.accelListeners[id] = id;
Accel.start(id);
// Make sure accelerometer timeout > frequency + 10 sec
var timeout = Accel.getTimeout();
if (timeout < (frequency + 10000)) {
Accel.setTimeout(frequency + 10000); // set to frequency + 10 sec
}
var id = PhoneGap.createUUID();
Accel.start();
// Start watch timer
navigator.accelerometer.accelTimers[id] = setInterval(function() {
navigator.accelerometer.timers[id] = setInterval(function() {
var status = Accel.getStatus();
// If accelerometer is running
@@ -199,22 +172,9 @@ Accelerometer.prototype.watchAcceleration = function(successCallback, errorCallb
Accelerometer.prototype.clearWatch = function(id) {
// Stop javascript timer & remove from timer list
if (id && navigator.accelerometer.accelTimers[id]) {
clearInterval(navigator.accelerometer.accelTimers[id]);
delete navigator.accelerometer.accelTimers[id];
}
// Remove from watch list
if (id && navigator.accelerometer.accelListeners[id]) {
delete navigator.accelerometer.accelListeners[id];
}
// Stop accelerometer for this watch listener
if (id) {
try {
Accel.stop(id);
} catch (e) {
}
if (id && navigator.accelerometer.timers[id]) {
clearInterval(navigator.accelerometer.timers[id]);
delete navigator.accelerometer.timers[id];
}
}
+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();
+27 -1
View File
@@ -185,4 +185,30 @@ PhoneGap.JSCallback = function() {
xmlhttp.open("GET", "http://127.0.0.1:"+CallbackServer.getPort()+"/" , true);
xmlhttp.send();
}
};
/**
* Create a UUID
*
* @return
*/
PhoneGap.createUUID = function() {
return PhoneGap.UUIDcreatePart(4) + '-' +
PhoneGap.UUIDcreatePart(2) + '-' +
PhoneGap.UUIDcreatePart(2) + '-' +
PhoneGap.UUIDcreatePart(2) + '-' +
PhoneGap.UUIDcreatePart(6);
};
PhoneGap.UUIDcreatePart = function(length) {
var uuidpart = "";
for (var i=0; i<length; i++) {
var uuidchar = parseInt((Math.random() * 256)).toString(16);
if (uuidchar.length == 1) {
uuidchar = "0" + uuidchar;
}
uuidpart += uuidchar;
}
return uuidpart;
};