cordova-android/framework/assets/js/accelerometer.js

103 lines
2.8 KiB
JavaScript
Raw Normal View History

function Acceleration(x, y, z)
{
this.x = x;
this.y = y;
this.z = z;
this.timestamp = new Date().getTime();
2010-04-29 06:46:15 +08:00
this.win = null;
this.fail = null;
}
2010-04-29 06:46:15 +08:00
var accelListeners = [];
/**
* This class provides access to device accelerometer data.
* @constructor
*/
function Accelerometer() {
/**
* The last known acceleration.
*/
this.lastAcceleration = null;
}
/**
* 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.
* @param {AccelerationOptions} options The options for getting the accelerometer data
* such as timeout.
*/
Accelerometer.prototype.getCurrentAcceleration = function(successCallback, errorCallback, options) {
// If the acceleration is available then call success
// If the acceleration is not available then call error
// Created for iPhone, Iphone passes back _accel obj litteral
if (typeof successCallback == "function") {
2010-04-29 06:46:15 +08:00
if(this.lastAcceleration)
successCallback(accel);
else
{
watchAcceleration(this.gotCurrentAcceleration, this.fail);
}
}
}
2010-04-29 06:46:15 +08:00
Accelerometer.prototype.gotAccel = function(key, x, y, z)
{
console.log('we won');
var a = new Acceleration(x,y,z);
2010-04-29 06:46:15 +08:00
a.x = x;
a.y = y;
a.x = z;
a.win = accelListeners[key].win;
a.fail = accelListeners[key].fail;
2010-04-29 06:46:15 +08:00
this.timestamp = new Date().getTime();
this.lastAcceleration = a;
accelListeners[key] = a;
2010-04-29 06:46:15 +08:00
if (typeof a.win == "function") {
a.win(a);
}
}
/**
* 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.
* @param {AccelerationOptions} options The options for getting the accelerometer data
* such as timeout.
*/
Accelerometer.prototype.watchAcceleration = function(successCallback, errorCallback, options) {
// TODO: add the interval id to a list so we can clear all watches
2010-06-05 04:25:28 +08:00
var frequency = (options != undefined)? options.frequency : 10000;
2010-04-29 06:46:15 +08:00
var accel = Acceleration(0,0,0);
accel.win = successCallback;
accel.fail = errorCallback;
accel.opts = options;
var key = accelListeners.push( accel ) - 1;
Accel.start(frequency, key);
}
/**
* Clears the specified accelerometer watch.
* @param {String} watchId The ID of the watch returned from #watchAcceleration.
*/
Accelerometer.prototype.clearWatch = function(watchId) {
2010-04-29 06:46:15 +08:00
Accel.stop(watchId);
}
Accelerometer.prototype.epicFail = function(watchId, message) {
accelWatcher[key].fail();
}
PhoneGap.addConstructor(function() {
if (typeof navigator.accelerometer == "undefined") navigator.accelerometer = new Accelerometer();
});