Skip to content

Commit 353d6fa

Browse files
author
Doruk Eker
committed
Availibility check updated
_ready() function is called after events had been triggered 3 times
1 parent 1dca5f5 commit 353d6fa

File tree

1 file changed

+75
-37
lines changed

1 file changed

+75
-37
lines changed

js/gyronorm.js

Lines changed: 75 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* @author Doruk Eker <dorukeker@gmail.com>
55
* @copyright 2014 Doruk Eker <http://dorukeker.com>
6-
* @version 0.0.10
6+
* @version 1.0.10
77
* @license MIT License | http://opensource.org/licenses/MIT
88
*/
99

@@ -28,12 +28,21 @@
2828
var _gravityCoefficient = 0; // Coefficient to normalze gravity related values
2929
var _logger = null; // Function to callback on error. There is no default value. It can only be set by the user on gn.init()
3030
var _ready = null; // Function to callback after trying to add all listeners
31-
var _deviceOrientationAvailable = true; // Boolean flag if deviceorientation event is available on the device/browser
32-
var _accelerationAvailable = true; // Boolean flag if accleration of devicemotion event is available on the device/browser
33-
var _accelerationIncludingGravityAvailable = true; // Boolean flag if accleration incl. gravity of devicemotion event is available on the device/browser
34-
var _rotationRateAvailable = true; // Boolean flag if accleration incl. gravity of devicemotion event is available on the device/browser
35-
var _compassNeedsCalibrationAvailable = true; // Boolean flag if devicemotion event is available on the device/browser
36-
var _addedEventCounter = 0 // Counts the number of events that are tried to be added
31+
32+
var _deviceOrientationAvailable = false; // Boolean flag if deviceorientation event is available on the device/browser
33+
var _accelerationAvailable = false; // Boolean flag if accleration of devicemotion event is available on the device/browser
34+
var _accelerationIncludingGravityAvailable = false; // Boolean flag if accleration incl. gravity of devicemotion event is available on the device/browser
35+
var _rotationRateAvailable = false; // Boolean flag if accleration incl. gravity of devicemotion event is available on the device/browser
36+
var _compassNeedsCalibrationAvailable = false; // Boolean flag if devicemotion event is available on the device/browser
37+
38+
var _addedEventCounter = 0; // Counts the number of events that are tried to be added
39+
var _devicemotionCheckFlag = false; // Boolean to show if device motion event has been checked for availibility
40+
var _deviceorientationCheckFlag = false; // Boolean to show if device motion event has been checked for availibility
41+
var _devicemotionCallbackCount = 0; // Counts the number of time the devicemotion call back function has been called
42+
var _deviceorientationCallbackCount = 0; // Counts the number of time the deviceorientation call back function has been called
43+
var _eventInitialCallbackLimit = 2; // Number of times a callback function for an event will be called before it is assumed to return correct values
44+
var _readyGracePeriod = 5000; // The time in milliseconds, after which the ready function will be forced called.
45+
var _readyGracePeriodTimeout = null; // Timeout variable for the grace period
3746

3847
/* OPTIONS */
3948
var _frequency = 50; // Frequency for the return data in milliseconds
@@ -91,7 +100,6 @@
91100
calibrate();
92101
setupListeners();
93102
} catch(err){
94-
alert('error');
95103
log(err);
96104
}
97105
}
@@ -199,8 +207,8 @@
199207
* @return true if event is available false if not
200208
*
201209
*/
202-
GyroNorm.prototype.isAvailable = function(_valueType){
203-
switch(_valueType){
210+
GyroNorm.prototype.isAvailable = function(_eventType){
211+
switch(_eventType){
204212
case 'deviceorientation':
205213
return _deviceOrientationAvailable;
206214
break;
@@ -243,27 +251,25 @@
243251
*/
244252
function setupListeners(){
245253
if(window.ondeviceorientation === undefined){
246-
_deviceOrientationAvailable = false;
247254
onEventAddedHandler();
248255
} else {
249-
window.ondeviceorientation = onDeviceOrientationHandler;
256+
window.addEventListener('deviceorientation',onDeviceOrientationHandler);
250257
}
251258

252259
if(window.ondevicemotion === undefined){
253-
_accelerationAvailable = false;
254-
_accelerationIncludingGravityAvailable = false;
255-
_rotationRateAvailable = false;
256260
onEventAddedHandler();
257261
} else {
258-
window.ondevicemotion = onDeviceMotionHandler;
262+
window.addEventListener('devicemotion',onDeviceMotionHandler);
259263
}
260264

261265
if(window.oncompassneedscalibration === undefined){
262-
_compassNeedsCalibrationAvailable = false;
263266
onEventAddedHandler();
264267
} else {
265-
window.oncompassneedscalibration = onCompassNeedsCalibrationHandler;
266-
}
268+
window.addEventListener('compassneedscalibration',onCompassNeedsCalibrationHandler);
269+
}
270+
271+
_readyGracePeriodTimeout = setTimeout(onReadyGracePeriodComplete,_readyGracePeriod);
272+
267273
}
268274

269275
/*
@@ -289,9 +295,17 @@
289295
*
290296
*/
291297
function onDeviceOrientationHandler(event){
298+
if(_deviceorientationCallbackCount < _eventInitialCallbackLimit){
299+
_deviceorientationCallbackCount++;
300+
return;
301+
}
302+
292303
// Check if values are returned correctly
293-
if(!event.alpha && !event.beta && !event.gamma){
294-
_deviceOrientationAvailable = false;
304+
if(
305+
(!event.alpha || event.alpha === null) &&
306+
(!event.beta || event.beta === null) &&
307+
(!event.gamma || event.gamma === null)
308+
){
295309
window.removeEventListener('deviceorientation',onDeviceOrientationHandler);
296310
onEventAddedHandler();
297311
return;
@@ -308,9 +322,13 @@
308322
_values.do.alpha = event.alpha;
309323
_values.do.beta = event.beta;
310324
_values.do.gamma = event.gamma;
311-
_values.do.absolute = event.absolute;
325+
_values.do.absolute = event.absolute;
312326

313-
onEventAddedHandler();
327+
if(!_deviceorientationCheckFlag){
328+
_deviceOrientationAvailable = true;
329+
_deviceorientationCheckFlag = true;
330+
onEventAddedHandler();
331+
}
314332
}
315333

316334
/*
@@ -319,24 +337,33 @@
319337
*
320338
*/
321339
function onDeviceMotionHandler(event){
322-
323-
if(!event.acceleration || !event.acceleration.x || !event.acceleration.y || !event.acceleration.z){
324-
_accelerationAvailable = false;
340+
if(_devicemotionCallbackCount < _eventInitialCallbackLimit){
341+
_devicemotionCallbackCount++;
342+
return;
325343
}
326344

327-
if(!event.accelerationIncludingGravity || !event.accelerationIncludingGravity.x || !event.accelerationIncludingGravity.y || !event.accelerationIncludingGravity.z){
328-
_accelerationIncludingGravityAvailable = false;
329-
}
345+
if(!_devicemotionCheckFlag){
346+
if(event.acceleration && event.acceleration.x && event.acceleration.y && event.acceleration.z){
347+
_accelerationAvailable = true;
348+
}
330349

331-
if(!event.rotationRate || !event.rotationRate.alpha || !event.rotationRate.beta || !event.rotationRate.gamma){
332-
_rotationRateAvailable = false;
333-
}
350+
if(event.accelerationIncludingGravity && event.accelerationIncludingGravity.x && event.accelerationIncludingGravity.y && event.accelerationIncludingGravity.z){
351+
_accelerationIncludingGravityAvailable = true;
352+
}
334353

335-
onEventAddedHandler();
354+
if(event.rotationRate && event.rotationRate.alpha && event.rotationRate.beta && event.rotationRate.gamma){
355+
_rotationRateAvailable = true;
356+
}
336357

337-
if(!_accelerationAvailable && !_accelerationIncludingGravityAvailable && !_rotationRateAvailable){
338-
window.removeEventListener('devicemotion',onDeviceMotionHandler);
358+
if(!_accelerationAvailable && !_accelerationIncludingGravityAvailable && !_rotationRateAvailable){
359+
window.removeEventListener('devicemotion',onDeviceMotionHandler);
360+
}
361+
362+
onEventAddedHandler();
363+
364+
_devicemotionCheckFlag = true;
339365
}
366+
340367

341368
// Assign gravity coefficient. Assumes that the user is holding the phot up right facing the screen.
342369
// If you cannot make this assumption because of the usecase, disable the normalization via changing the option 'gravityNormalized' value to false
@@ -379,13 +406,24 @@
379406
* Calls the ready function when all 3 are tried
380407
*
381408
*/
382-
function onEventAddedHandler(location){
409+
function onEventAddedHandler(){
383410
_addedEventCounter++;
384411
if(_addedEventCounter === 3 && _ready !== null && typeof(_ready) === 'function'){
412+
window.clearTimeout(_readyGracePeriodTimeout);
385413
_ready();
386414
}
387415
}
388416

417+
/*
418+
*
419+
* Called when an if the grace period times out.
420+
*
421+
*/
422+
function onReadyGracePeriodComplete(){
423+
_addedEventCounter = 2;
424+
onEventAddedHandler();
425+
}
426+
389427
/*
390428
*
391429
* Utility function to round with digits after the decimal point
@@ -471,4 +509,4 @@
471509

472510

473511
return GyroNorm;
474-
}));
512+
}));

0 commit comments

Comments
 (0)