-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgyro.js
483 lines (420 loc) · 15.2 KB
/
gyro.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/* ===================================================
* gyro.js v1.0.0
* http://github.com/joeymarburger/gyro
* ===================================================
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ========================================================== */
$(function(){
// Global variables
var $gyro,
gyroInterval,
current = 0, // Controls the loop against the threshhold to tune data output
threshhold = 20, // How often data is displayed, not for data sending
gyrox = 0,
gyroy = 0,
gyroz = 0,
gyrovx = 0,
gyrovy = 0,
gyrovz = 0,
gyroax = 0,
gyroay = 0,
gyroaz = 0,
gyroai = 0,
gyroarAlpha = 0,
gyroarBeta = 0,
gyroarGamma = 0,
gyrodelay = 100,
gyrovMultiplier = 0.01,
gyroalpha = 0,
gyrobeta = 0,
gyrogamma = 0,
gyroenabletilt = false,
gyroplane,
gyrosimdegree,
rotateSim,
makeGyroColor,
makeAcceleratedColor,
gyrod2h,
gyroh2d,
showsimulator = false,
shaketoggle = true,
gyroactive = false,
simactive = false,
trackdata = true, // Change this to true to enable data capturing
datasamplerate = 5000, // Change this to enable data capturing, in milliseconds
showdatamessage = true, // If you want the alert bar to display at the top when data is sent
sendGyroData, // The function to handle data capturing via ajax or whatever
dataurl = '',
gyroMotionHandler,
gyroOrientationHandler,
toggleGyro,
toggleSim,
initializeGyro,
initializePlatform,
resetData,
trackOrientation,
deviceorientation,
pingNetwork, // Function to control connectivity detection
connection = true; // Detect connectivity for data sending/receiving
// Setup
if(shaketoggle){
$('input#shake-toggle').prop('checked', true); }
if(trackdata){
$('input#data-toggle').prop('checked', true); }
if(showdatamessage){
$('input#show-data-toggle').prop('checked', true); }
if(showsimulator){
toggleSim();
$('input#sim-toggle').prop('checked', true); }
$('input#data-toggle').click(function(){
if(this.checked){
trackdata = true;
datasamplerate = 5000;
} else {
trackdata = false;
datasamplerate = 0;
}
});
$('input#show-data-toggle').click(function(){
if(this.checked){
showdatamessage = true;
} else {
showdatamessage = false;
}
});
$('input#sim-toggle').click(function(){
toggleSim();
});
var onlabel = '<span class="label label-success nocorners">On <i class="icon-ok-circle"></i></span>';
var offlabel = '<span class="label label-important nocorners">Off <i class="icon-off"></i></span>';
pingNetwork = function(){
var c = navigator.onLine;
if(c){
connection = true;
} else {
connection = false;
}
};
// Ping for connection every 30 secs
setInterval(function(){
pingNetwork();
},30000);
// Orientation
trackOrientation = function(){
if(window.orientation == 90 || window.orientation == -90){
deviceorientation = 'Landscape';
} else {
deviceorientation = 'Portrait';
}
document.getElementById("orientationlabel").innerHTML = deviceorientation;
};
// Clear all data when toggled off
resetData = function(){
gyroactive = false;
simactive = false;
document.getElementById("simphone-degree").innerHTML = "--°";
rotateSim(0,0,0);
document.getElementById("accelcolor").innerHTML = '<span class="head">Color: </span>';
document.getElementById("accelcolor").style.background = '#cccccc';
document.getElementById("accel").style.background = '#f5f5f5';
document.getElementById("accel").style.color = "#000000";
document.getElementById("gyrocolor").innerHTML = '<span class="head">Color: </span>';
document.getElementById("gyrocolor").style.background = '#cccccc';
document.getElementById("gyro").style.background = '#f5f5f5';
document.getElementById("gyro").style.color = "#000000";
document.getElementById("xlabel").innerHTML = "x: ";
document.getElementById("ylabel").innerHTML = "y: ";
document.getElementById("zlabel").innerHTML = "z: ";
document.getElementById("ilabel").innerHTML = "i: ";
document.getElementById("arAlphaLabel").innerHTML = "arA: ";
document.getElementById("arBetaLabel").innerHTML = "arB: ";
document.getElementById("arGammaLabel").innerHTML = "arG: ";
document.getElementById("alphalabel").innerHTML = "Alpha (α): ";
document.getElementById("betalabel").innerHTML = "Beta (β): ";
document.getElementById("gammalabel").innerHTML = "Gamma (γ): ";
document.getElementById("orientationlabel").innerHTML = "";
initializePlatform();
};
// Toggle Gyro on/off
toggleGyro = function(){
if(gyroactive){
gyroactive = false;
document.getElementById("gyrosupport").innerHTML = offlabel;
document.getElementById("togglegyro").innerHTML = '<i class="icon-off icon-2x"></i>';
} else {
gyroactive = true;
document.getElementById("gyrosupport").innerHTML = onlabel;
document.getElementById("togglegyro").innerHTML = '<i class="icon-remove-circle icon-2x"></i>';
trackOrientation();
}
initializePlatform();
};
$('#togglegyro').click(function(e){
e.preventDefault();
$(this).toggleClass('btn-success');
$(this).toggleClass('btn-danger');
toggleGyro();
return false;
});
toggleSim = function(){
$('#compass').toggleClass('no-sim');
$('#simphone').toggle();
if(simactive){
simactive = false;
//document.getElementById("togglesim").innerHTML = '<i class="icon-mobile-phone icon-2x light"></i>';
$('input#sim-toggle').prop('checked', false);
} else {
simactive = true;
//document.getElementById("togglesim").innerHTML = '<i class="icon-mobile-phone icon-2x"></i>';
}
$('#simphone-degree-container').toggle();
};
$('#togglesim').click(function(e){
e.preventDefault();
toggleSim();
return false;
});
$('#toggleoptions').click(function(e){
e.preventDefault();
$('#options-bar').toggle();
return false;
});
$('#resetdata').click(function(e){
e.preventDefault();
resetData();
return false;
});
sendGyroData = function(mEvent){
// Put your database or storage
// stuff here for tracking
if(mEvent){
// Connection?
if(connection && dataurl){
// Ajax out data
//var send_data = $.serialize(mEvent);
$.ajax({
type: "POST",
url: dataurl,
data: JSON.stringify( mEvent ),
success: function(data){
if(showdatamessage){
$('#data-sent-log').html('<div class="alert alert-success"><span class="bold up"><strong>Data sent! <i class="icon-cloud"></i></span> '+data.length+'</div>');
$('#header').parent('div').toggleClass('alert-offset');
setTimeout(function(){
$('#data-sent-log').html('');
$('#header').parent('div').toggleClass('alert-offset');
},5000);
}
},
complete: function() {
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
if(textStatus == 'timeout') {
if(showdatamessage){
$('#data-sent-log').html('<div class="alert alert-danger"><span class="bold up"><strong>Connection unavailable! <i class="icon-frown"></i></span> '+data.length+'</div>');
$('#header').parent('div').toggleClass('alert-offset');
setTimeout(function(){
$('#data-sent-log').html('');
$('#header').parent('div').toggleClass('alert-offset');
},5000);
}
}
}
});
}
}
};
rotateSim = function(degree,skew,tilt){
$('#simphone-degree').html(degree+'°');
var $elie = $("#simphone");
$elie.css({ WebkitTransform: 'rotate(' + degree + 'deg)'});
$elie.css({ '-moz-transform': 'rotate(' + degree + 'deg)'});
//$elie.css({ WebkitTransform: 'scaleY(' + skew + ')'});
//$elie.css({ '-moz-transform': 'scaleY(' + skew + ')'});
//$elie.css({ WebkitTransform: 'skewX(' + tilt + 'deg)'});
//$elie.css({ '-moz-transform': 'skewX(' + tilt + 'deg)'});
};
gyrod2h = function(d) { return d.toString(16); };
gyroh2d = function(h) { return parseInt(h,16); };
makeGyroColor = function(a, b, c) {
red = Math.abs(a) % 255;
green = Math.abs(b) % 255;
blue = Math.abs(c) % 255;
return "#" + gyrod2h(red) + gyrod2h(green) + gyrod2h(blue);
};
makeAcceleratedColor = function(a, b, c) {
red = Math.round(Math.abs(a + gyroaz) % 255);
green = Math.round(Math.abs(b + gyroay) % 255);
blue = Math.round(Math.abs(c + gyroaz) % 255);
return "#" + gyrod2h(red) + gyrod2h(green) + gyrod2h(blue);
};
// Collect where data is going
initializeGyro = function (gyro) {
$gyro = $(gyro);
// Data output
$dataoutput = $gyro.find('#dataoutput');
/*gyroInterval = setInterval(function() {
document.getElementById("gyrosupport").innerHTML = '<span class="label label-success">On</span> - Send data...';
}, delay);*/
};
$('#simphone').click(function(){
// Reset sim
rotateSim(0,0,0);
return false;
});
// Handle device orientation rotation
gyroOrientationHandler = function(rotation){
gyroalpha = Math.round(rotation.alpha);
gyrobeta = Math.round(rotation.beta);
gyrogamma = Math.round(rotation.gamma);
// Rotate phone
if(simactive){
$('#simphone-degree').html(-gyroalpha);
rotateSim(-gyroalpha,-gyrogamma,-gyrobeta);
}
document.getElementById("alphalabel").innerHTML = "Alpha (α): " + gyroalpha;
document.getElementById("betalabel").innerHTML = "Beta (β): " + gyrobeta;
document.getElementById("gammalabel").innerHTML = "Gamma (γ): " + gyrogamma;
document.getElementById("gyrocolor").innerHTML = '<span class="head">Color: ' + makeGyroColor(gyroalpha, gyrobeta, gyrogamma) + '</span>';
document.getElementById("gyrocolor").style.background = makeGyroColor(gyroalpha, gyrobeta, gyrogamma);
document.getElementById("gyro").style.background = makeGyroColor(gyroalpha, gyrobeta, gyrogamma);
document.getElementById("gyrocolor").style.color = "#FFFFFF";
document.getElementById("gyro").style.color = "#FFFFFF";
document.getElementById("gyrocolor").style.fontWeight = "bold";
};
// Handle device motion
gyroMotionHandler = function(motion){
current++;
//if (current%datasamplerate != 0) {return;}
gyrox = Math.round(Math.abs(motion.acceleration.x * 1));
gyroy = Math.round(Math.abs(motion.acceleration.y * 1));
gyroz = Math.round(Math.abs(motion.acceleration.z * 1));
gyroax = Math.round(Math.abs(motion.accelerationIncludingGravity.x * 1));
gyroay = Math.round(Math.abs(motion.accelerationIncludingGravity.y * 1));
gyroaz = Math.round(Math.abs(motion.accelerationIncludingGravity.z * 1));
gyroai = Math.round(motion.interval * 100) / 100;
rR = motion.rotationRate;
if (rR != null) {
gyroarAlpha = Math.round(rR.alpha);
gyroarBeta = Math.round(rR.beta);
gyroarGamma = Math.round(rR.gamma);
}
// Detect Shake
if(shaketoggle){
var sensitivity = 80;
var x2 = 0, y2 = 0, z2 = 0;
setInterval(function () {
var change = Math.abs(gyroax-x2+gyroay-y2+gyroaz-z2);
if (change > sensitivity) {
// Toggle Gyro Off
toggleGyro();
resetData();
// Show message
$('#shake-alert').fadeIn();
setTimeout(function(){
$('#shake-alert').fadeOut();
},3000);
}
// Update new position
x2 = gyroax;
y2 = gyroay;
z2 = gyroaz;
}, 150);
}
var motionEvent = {
"interval": motion.interval,
"acceleration": {
"x": gyrox,
"y": gyroy,
"z": gyroz
}, "accelerationIncludingGravity": {
"x": gyroax,
"y": gyroay,
"z": gyroaz
}, "rotationRate": {
"alpha": gyroarAlpha,
"beta": gyroarBeta,
"gamma": gyroarGamma
}, "colors": {
"accColor": makeGyroColor(gyroax, gyroay, gyroaz),
"gyroColor": makeGyroColor(gyroalpha, gyrobeta, gyrogamma)
}
};
gyroalpha = Math.round(motion.alpha);
gyrobeta = Math.round(motion.beta);
gyrogamma = Math.round(motion.gamma);
if(current==(threshhold*2)){
if(trackdata && datasamplerate > 0){
sendGyroData(motionEvent);
}
current = 0;
}
// Output Data to Page
document.getElementById("xlabel").innerHTML = "x: " + gyroax;
document.getElementById("ylabel").innerHTML = "y: " + gyroay;
document.getElementById("zlabel").innerHTML = "z: " + gyroaz;
document.getElementById("ilabel").innerHTML = "i: " + gyroai;
document.getElementById("arAlphaLabel").innerHTML = "arA: " + gyroarAlpha;
document.getElementById("arBetaLabel").innerHTML = "arB: " + gyroarBeta;
document.getElementById("arGammaLabel").innerHTML = "arG: " + gyroarGamma;
document.getElementById("accelcolor").innerHTML = '<span class="head">Color: ' + makeGyroColor(gyroax, gyroay, gyroaz) + '</span>';
document.getElementById("accelcolor").style.background = makeGyroColor(gyroax, gyroay, gyroaz);
document.getElementById("accel").style.background = makeGyroColor(gyroax, gyroay, gyroaz);
document.getElementById("accelcolor").style.color = "#FFFFFF";
document.getElementById("accel").style.color = "#FFFFFF";
document.getElementById("accelcolor").style.fontWeight = "bold";
// Gamma nav example
/*if(gyrogamma >= 25 && gyrogamma <= 30 && (gyroarGamma >= 15 || gyroarGamma <= -15)){
console.log('Forward! '+gyrogamma+'γ, '+gyroarGamma+'arγ');
}
if(gyrogamma <= -25 && gyrogamma >= -30 && (gyroarGamma >= 15 || gyroarGamma <= -15)){
//window.history.back();
console.log('Back! '+gyrogamma+'γ, '+gyroarGamma+'arγ');
}*/
};
// Platform setup
initializePlatform = function(){
if(gyroactive){
document.getElementById("gyrosupport").innerHTML = onlabel;
// Rotation orientation listener
window.addEventListener('deviceorientation', gyroOrientationHandler, false);
// Motion listener
window.addEventListener('devicemotion', gyroMotionHandler, false);
// Orientation detection
var supportsOrientationChange = "onorientationchange" in window,
orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";
window.addEventListener(orientationEvent, function () {
trackOrientation();
}, false);
// Init data against a holder
// for testing output
initializeGyro('#data');
} else {
document.getElementById("gyrosupport").innerHTML = offlabel;
window.DeviceMotionEvent = null;
window.removeEventListener('deviceorientation', gyroOrientationHandler, false);
window.removeEventListener('devicemotion', gyroMotionHandler, false);
resetData();
} // gyroactive?
}; // initializePlatform();
// Main init
// Check for device support
if (window.DeviceMotionEvent && isMobile) {
initializePlatform();
} else {
document.getElementById("gyrosupport").innerHTML = '<span class="label label-important">Error <i class="icon-warning-sign"></i></span>';
document.getElementById("data").innerHTML = '<div class="text-center alert alert-danger"><h3>Device or browser not supported.</h3></div>';
document.getElementById("toggle-buttons").style.visibility = 'hidden';
}
}); // Close main function