|
1 /** |
|
2 * This class provides access to device accelerometer data. |
|
3 * @constructor |
|
4 */ |
|
5 function Accelerometer() { |
|
6 /** |
|
7 * The last known acceleration. |
|
8 */ |
|
9 this.lastAcceleration = null; |
|
10 } |
|
11 |
|
12 /** |
|
13 * Asynchronously aquires the current acceleration. |
|
14 * @param {Function} successCallback The function to call when the acceleration |
|
15 * data is available |
|
16 * @param {Function} errorCallback The function to call when there is an error |
|
17 * getting the acceleration data. |
|
18 * @param {AccelerationOptions} options The options for getting the accelerometer data |
|
19 * such as timeout. |
|
20 */ |
|
21 |
|
22 Accelerometer.prototype.getCurrentAcceleration = function(successCallback, errorCallback, options) { |
|
23 // If the acceleration is available then call success |
|
24 // If the acceleration is not available then call error |
|
25 |
|
26 try { |
|
27 if (!this.serviceObj) |
|
28 this.serviceObj = this.getServiceObj(); |
|
29 |
|
30 if (this.serviceObj == null) |
|
31 throw { |
|
32 name: "DeviceErr", |
|
33 message: "Could not initialize service object" |
|
34 }; |
|
35 |
|
36 //get the sensor channel |
|
37 var SensorParams = { |
|
38 SearchCriterion: "AccelerometerAxis" |
|
39 }; |
|
40 var returnvalue = this.serviceObj.ISensor.FindSensorChannel(SensorParams); |
|
41 var error = returnvalue["ErrorCode"]; |
|
42 var errmsg = returnvalue["ErrorMessage"]; |
|
43 if (!(error == 0 || error == 1012)) { |
|
44 var ex = { |
|
45 name: "Unable to find Sensor Channel: " + error, |
|
46 message: errmsg |
|
47 }; |
|
48 throw ex; |
|
49 } |
|
50 var channelInfoMap = returnvalue["ReturnValue"][0]; |
|
51 var criteria = { |
|
52 ChannelInfoMap: channelInfoMap, |
|
53 ListeningType: "ChannelData" |
|
54 }; |
|
55 |
|
56 if (typeof(successCallback) != 'function') |
|
57 successCallback = function(){ |
|
58 }; |
|
59 if (typeof(errorCallback) != 'function') |
|
60 errorCallback = function(){ |
|
61 }; |
|
62 |
|
63 this.success_callback = successCallback; |
|
64 this.error_callback = errorCallback; |
|
65 //create a closure to persist this instance of Accelerometer into the RegisterForNofication callback |
|
66 var obj = this; |
|
67 |
|
68 // TODO: this call crashes WRT, but there is no other way to read the accel sensor |
|
69 // http://discussion.forum.nokia.com/forum/showthread.php?t=182151&highlight=memory+leak |
|
70 this.serviceObj.ISensor.RegisterForNotification(criteria, function(transId, eventCode, result){ |
|
71 try { |
|
72 var criteria = { |
|
73 TransactionID: transId |
|
74 }; |
|
75 obj.serviceObj.ISensor.Cancel(criteria); |
|
76 |
|
77 var accel = new Acceleration(result.ReturnValue.XAxisData, result.ReturnValue.YAxisData, result.ReturnValue.ZAxisData); |
|
78 Accelerometer.lastAcceleration = accel; |
|
79 |
|
80 obj.success_callback(accel); |
|
81 |
|
82 } |
|
83 catch (ex) { |
|
84 obj.serviceObj.ISensor.Cancel(criteria); |
|
85 obj.error_callback(ex); |
|
86 } |
|
87 |
|
88 }); |
|
89 } catch (ex) { |
|
90 errorCallback(ex); |
|
91 } |
|
92 |
|
93 }; |
|
94 |
|
95 |
|
96 /** |
|
97 * Asynchronously aquires the acceleration repeatedly at a given interval. |
|
98 * @param {Function} successCallback The function to call each time the acceleration |
|
99 * data is available |
|
100 * @param {Function} errorCallback The function to call when there is an error |
|
101 * getting the acceleration data. |
|
102 * @param {AccelerationOptions} options The options for getting the accelerometer data |
|
103 * such as timeout. |
|
104 */ |
|
105 |
|
106 Accelerometer.prototype.watchAcceleration = function(successCallback, errorCallback, options) { |
|
107 this.getCurrentAcceleration(successCallback, errorCallback, options); |
|
108 // TODO: add the interval id to a list so we can clear all watches |
|
109 var frequency = (options != undefined)? options.frequency : 10000; |
|
110 return setInterval(function() { |
|
111 navigator.accelerometer.getCurrentAcceleration(successCallback, errorCallback, options); |
|
112 }, frequency); |
|
113 }; |
|
114 |
|
115 /** |
|
116 * Clears the specified accelerometer watch. |
|
117 * @param {String} watchId The ID of the watch returned from #watchAcceleration. |
|
118 */ |
|
119 Accelerometer.prototype.clearWatch = function(watchId) { |
|
120 clearInterval(watchId); |
|
121 }; |
|
122 |
|
123 //gets the Acceleration Service Object from WRT |
|
124 Accelerometer.prototype.getServiceObj = function() { |
|
125 var so; |
|
126 |
|
127 try { |
|
128 so = device.getServiceObject("Service.Sensor", "ISensor"); |
|
129 } catch (ex) { |
|
130 throw { |
|
131 name: "DeviceError", |
|
132 message: "Could not initialize accel service object (" + ex.name + ": " + ex.message + ")" |
|
133 }; |
|
134 } |
|
135 return so; |
|
136 }; |
|
137 |
|
138 if (typeof navigator.accelerometer == "undefined") navigator.accelerometer = new Accelerometer(); |