blob: f343902656a1eb642ea799b960ef50c762cbe1f4 [file] [log] [blame]
/**
* This class provides access to device accelerometer data.
* @constructor
*/
var utils = require("cordova/utils"),
exec = require("cordova/exec"),
Acceleration = require('cordova/plugin/Acceleration');
// Is the accel sensor running?
var running = false;
// Keeps reference to watchAcceleration calls.
var timers = {};
// Array of listeners; used to keep track of when we should call start and stop.
var listeners = [];
// Last returned acceleration object from native
var accel = null;
// Tells native to start.
function start() {
exec(function(a) {
var tempListeners = listeners.slice(0);
accel = new Acceleration(a.x, a.y, a.z, a.timestamp);
for (var i = 0, l = tempListeners.length; i < l; i++) {
tempListeners[i].win(accel);
}
}, function(e) {
var tempListeners = listeners.slice(0);
for (var i = 0, l = tempListeners.length; i < l; i++) {
tempListeners[i].fail(e);
}
}, "Accelerometer", "start", []);
running = true;
}
// Tells native to stop.
function stop() {
exec(null, null, "Accelerometer", "stop", []);
running = false;
}
// Adds a callback pair to the listeners array
function createCallbackPair(win, fail) {
return {win:win, fail:fail};
}
// Removes a win/fail listener pair from the listeners array
function removeListeners(l) {
var idx = listeners.indexOf(l);
if (idx > -1) {
listeners.splice(idx, 1);
if (listeners.length === 0) {
stop();
}
}
}
var accelerometer = {
/**
* Asynchronously aquires the current acceleration.
*
* @param {Function} successCallback The function to call when the acceleration data is available
* @param {Function} errorCallback The function to call when there is an error getting the acceleration data. (OPTIONAL)
* @param {AccelerationOptions} options The options for getting the accelerometer data such as timeout. (OPTIONAL)
*/
getCurrentAcceleration: function(successCallback, errorCallback, options) {
// successCallback required
if (typeof successCallback !== "function") {
throw "getCurrentAcceleration must be called with at least a success callback function as first parameter.";
}
var p;
var win = function(a) {
successCallback(a);
removeListeners(p);
};
var fail = function(e) {
errorCallback(e);
removeListeners(p);
};
p = createCallbackPair(win, fail);
listeners.push(p);
if (!running) {
start();
}
},
/**
* Asynchronously aquires the acceleration 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 acceleration data. (OPTIONAL)
* @param {AccelerationOptions} options The options for getting the accelerometer data such as timeout. (OPTIONAL)
* @return String The watch id that must be passed to #clearWatch to stop watching.
*/
watchAcceleration: function(successCallback, errorCallback, options) {
// Default interval (10 sec)
var frequency = (options && options.frequency && typeof options.frequency == 'number') ? options.frequency : 10000;
// successCallback required
if (typeof successCallback !== "function") {
throw "watchAcceleration must be called with at least a success callback function as first parameter.";
}
// Keep reference to watch id, and report accel readings as often as defined in frequency
var id = utils.createUUID();
var p = createCallbackPair(function(){}, function(e) {
errorCallback(e);
removeListeners(p);
});
listeners.push(p);
timers[id] = {
timer:window.setInterval(function() {
if (accel) {
successCallback(accel);
}
}, frequency),
listeners:p
};
if (running) {
// If we're already running then immediately invoke the success callback
successCallback(accel);
} else {
start();
}
return id;
},
/**
* Clears the specified accelerometer watch.
*
* @param {String} id The id of the watch returned from #watchAcceleration.
*/
clearWatch: function(id) {
// Stop javascript timer & remove from timer list
if (id && timers[id]) {
window.clearInterval(timers[id].timer);
removeListeners(timers[id].listeners);
delete timers[id];
}
}
};
module.exports = accelerometer;