-
Notifications
You must be signed in to change notification settings - Fork 5
/
joystick.html
536 lines (408 loc) · 16.9 KB
/
joystick.html
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.js"></script>
<script src="js/setBit.js"></script>
<script src="js/pep.js"></script>
<title>Web Bluetooth</title>
<style>
@import url(https://fonts.googleapis.com/css?family=Open+Sans|Roboto:400,500,700,300);
body {
font-family: "Roboto";
margin: 0;
padding: 0;
}
div {
margin: 0px;
}
h1, h3 {
text-align: center;
}
h1 {
font-size: 20px;
}
ul {
margin-top: 10px;
margin-bottom: 30px;
font-size: 14px;
}
label {
display: inline-block;
width: 120px;
font-weight: 500;
}
input[type="text"] {
width: 225px;
padding: 3px;
font-size: 12px;
}
button {
display: block;
padding: 10px;
margin: 0 auto;
border: 1px solid white;
font-weight: 700;
box-shadow: 0px 0px 5px 2px rgba(0, 0, 0, 0.1);
background-color: #DDD;
}
#main {
width: 100%;
max-width: 800px;
height: 100%;
border: px solid #DDD;
padding: 20px;
font-size: 14px;
margin: 0;
}
#config {
display: inline-block;
width: 300px;
vertical-align: top;
}
.slider-container {
display: inline-block;
float: left;
margin: 0 auto;
width: 100px;
text-align: center;
}
#ledon, #ledoff {
color: white;
margin-top: 20px;
margin-bottom: 20px;
}
#ledon {
background-color: #006600;
}
#ledoff {
background-color: #cc0000;
}
input[type=range] {
width: 20px;
height: 150px;
margin-top: 0px;
-webkit-appearance: slider-vertical;
}
#slider-value {
text-align: center;
font-size: 20px;
font-weight: 700;
}
#orientation-message {
display: none;
}
#fullscreen {
display: block;
cursor: pointer;
position: fixed;
top: 0px;
right: 0px;
padding: 2px;
background-color: #222;
color: white;
}
.demo {
background: #f3eee6;
width: 200px;
height: 200px;
display: inline-block;
border-radius: 100px;
margin: 0;
}
.ball {
width: 70px;
height: 70px;
background: red;
position: absolute;
top: 65px;
left: 65px;
border-radius: 70px;
}
.ball.outline {
background: #eee;
}
.ball.ease {
-moz-transition: all 0.2s ease;
-o-transition: all 0.2s ease;
-webkit-transition: all 0.2s ease;
transition: all 0.2s ease;
}
#pos-x, #pos-y {
margin-left: 20px;
}
@media screen and (orientation:portrait)
{
#orientation-message {
display: block;
position: fixed;
width: 100%;
height: 100%;
font-size: 25px;
background-color: rgba(0, 0, 0, 0.9);
color: white;
margin: 0;
padding: 0;
margin-top: -20px;
vertical-align: middle;
text-align: center;
font-weight: 700;
padding-top: 200px;
z-index: 1000;
}
}
</style>
<script>
// If we're running on a real web server (as opposed to localhost, which is whitelisted),
// then change the protocol to HTTPS.
// See https://goo.gl/lq4gCo for an explanation as to why this is needed for some features.
(function() {
var isLocalhost = !!(window.location.hostname === 'localhost' ||
// [::1] is the IPv6 localhost address.
window.location.hostname === '[::1]' ||
// 127.0.0.1/8 is considered localhost for IPv4.
window.location.hostname.match(/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/));
if (window.location.protocol === 'http:' && !isLocalhost) {
// Redirect to https: if we're currently using http: and we're not on localhost.
window.location.protocol = 'https:';
}
})();
window.addEventListener('error', function(error) {
if (ChromeSamples && ChromeSamples.setStatus) {
ChromeSamples.setStatus(error.message + ' (Nettleseren din støtter ikke Web Bluetooth.)');
error.preventDefault();
}
});
</script>
</head>
<body>
<div id="orientation-message">
<div>Vennligst snu enheten 90°</div>
</div>
<div id="main">
<div id="fullscreen" onclick="">Klikk for fullskjerm</div>
<div class='demo constrain-to-parent'>
<div class='ball outline'></div>
<div class='ball moveable'></div>
</div>
<div id="config">
<h1>Styring av LED med joystick</h1>
<form>
<!--<div><label for="service">Service: </label><input id="service" type="text" list="services" autofocus placeholder="Bluetooth Service" value="00001523-1212-efde-1523-785feabcd123"></div>
<div><label for="service">Characteristic: </label><input id="characteristic" type="text" list="characteristics" placeholder="Bluetooth Characteristic" value="00001525-1212-efde-1523-785feabcd123"></div>-->
<div><button id="connect" >Koble til enhet</button></div>
</form>
<h3>Logg</h3>
<div id="output" class="output">
<div id="content"></div>
<div id="status"></div>
<pre id="log"></pre>
</div>
<p id="pos-x"></p>
<p id="pos-y"></p>
</div>
<script>
var ChromeSamples = {
log: function() {
var line = Array.prototype.slice.call(arguments).map(function(argument) {
return typeof argument === 'string' ? argument : JSON.stringify(argument);
}).join(' ');
document.querySelector('#log').textContent += line + '\n';
},
clearLog: function() {
document.querySelector('#log').textContent = '';
},
setStatus: function(status) {
document.querySelector('#status').textContent = status;
},
setContent: function(newContent) {
var content = document.querySelector('#content');
while(content.hasChildNodes()) {
content.removeChild(content.lastChild);
}
content.appendChild(newContent);
}
};
</script>
<!-- // END OF MAIN -->
</div>
<script>
/****** START WEB BLUETOOTH-KODE *****/
// Setter en variabel for å lagre karakteristikken som skal brukes videre
// i funksjoner utenfor selve tilkoblingen til enheten.
// Variabelen settes utenfor alle funksjoner slik at den skal være global,
// og tilgjengelig i alle funksjoner.
var myCharacteristic;
var status = 0;
// Funksjonen 'onFormSubmit' trigges når 'Koble til enhet'-knappen trykkes
// Se egen funksjon for dette rett før </body>-taggen i denne filen
function onFormSubmit() {
'use strict';
// Henter service-UUID som er oppgitt i tekstboksen for dette
//let serviceUuid = document.getElementById('service').value;
let serviceUuid = '00001523-1212-efde-1523-785feabcd123';
if (serviceUuid.startsWith('0x')) {
serviceUuid = parseInt(serviceUuid, 16);
}
// Henter karakteristikk-UUID som er oppgitt i tekstboksen for dette
//let characteristicUuid = document.getElementById('characteristic').value;
let characteristicUuid = '00001525-1212-efde-1523-785feabcd123';
if (characteristicUuid.startsWith('0x')) {
characteristicUuid = parseInt(characteristicUuid, 16);
}
// Søker etter enheter som matcher valgt service-UUID
log('Søker etter enheter med valgt \nservice og characteristic...');
navigator.bluetooth.requestDevice({filters: [{services: [serviceUuid]}]})
.then(device => device.connectGATT())
.then(server => server.getPrimaryService(serviceUuid))
// Enhetene er nå koblet sammen, og nettleseren leter etter karakteristikk som matcher den ønskede UUID
.then(service => service.getCharacteristic(characteristicUuid))
.then(characteristic => {
// Lagrer karakteristikken som matcher i variabel slik at den kan brukes i andre funksjoner.
myCharacteristic = characteristic;
log('Tilkoblet, LED kan nå styres.');
})
// Dersom feil skjer i løpet av oppkoblingen, skrives feilen til loggen
.catch(error => {
log('Argh! ' + error);
});
}
var charVal = new Uint8Array(20);
//******** Funksjon for å styre LEDene ******/
// Bruker jQuery for å registrere når HTML-elementet med ID 'slider' endres.
// Når endring skjer, kjøres funksjonen.
$('.ball').on("mousemove touchmove", function() {
// Lagrer nå-verdien fra slideren i variabelen 'sliderVal' (med jQuery-selector)
var offset = $ball.position();
var offsetY = initialOffset.top - offset.top;
var offsetX = -1*(initialOffset.left - offset.left)
// Sjekker at tilkobling ble opprettet tidligere
if(myCharacteristic) {
// Sjekker i hvilket intervall slider-verdien er, og sender korresponderende verdi
// via BLE til firmware.
// Setter 1. bit lik 1 for å tenne 1. LED osv.
if(offsetX < 0 && offsetY < 0)
setBit(1, 0, 1); // 0001
else
setBit(1, 0, 0);
if(offsetX < 0 && offsetY > 0)
setBit(1, 1, 1); // 0010
else
setBit(1, 1, 0);
if(offsetX > 0 && offsetY > 0)
setBit(1, 2, 1); // 0100
else
setBit(1, 2, 0);
if(offsetX > 0 && offsetY < 0)
setBit(1, 3, 1); // 1000
else
setBit(1, 3, 0)
console.log(charVal);
if((offsetX == 0) && (offsetY == 0))
setBit(1, 'b', 0);
//myCharacteristic.writeValue(charVal);
console.log(charVal);
/*
return chosenHeartRateService.getCharacteristic('heart_rate_control_point'))
.then(controlPoint => {
let resetEnergyExpended = new Uint8Array([1]);
return controlPoint.writeValue(resetEnergyExpended);
});*/
return myCharacteristic.writeValue(charVal);
}
// Skriver veriden fra slideren til HTML-elementet med ID 'slider-value'
//$('#slider-value').html(sliderVal + '%');
});
function resetCharByte(byte) {
charVal[1] = 0;
if(myCharacteristic) {
myCharacteristic.writeValue(charVal);
console.log(charVal);
$('#pos-x').html('x: 0px');
$('#pos-y').html('y: 0px -> reset');
}
};
Object.defineProperty(window, "status", { set: function(v) {
} } );
</script>
<script>
// Funksjon for å vise siden i fullskjerm
function toggleFullscreen(elem) {
elem = elem || document.documentElement;
if (!document.fullscreenElement && !document.mozFullScreenElement &&
!document.webkitFullscreenElement && !document.msFullscreenElement) {
if (elem.requestFullscreen) {
elem.requestFullscreen();
} else if (elem.msRequestFullscreen) {
elem.msRequestFullscreen();
} else if (elem.mozRequestFullScreen) {
elem.mozRequestFullScreen();
} else if (elem.webkitRequestFullscreen) {
elem.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
}
$('#fullscreen').html("Avslutt fullskjerm");
} else {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
}
$('#fullscreen').html("Klikk for fullskjerm");
}
}
document.getElementById('fullscreen').addEventListener('click', function() {
toggleFullscreen();
});
</script>
<script>
document.querySelector('form').addEventListener('submit', function(event) {
event.stopPropagation();
event.preventDefault();
status = 1;
if (!navigator.bluetooth) {
ChromeSamples.setStatus('Web Bluetooth API er ikke tilgjengelig.\n' +
'Husk å aktivere Web Bluetooth-flagget: chrome://flags');
} else {
ChromeSamples.clearLog();
onFormSubmit();
}
});
log = ChromeSamples.log;
</script>
<script>
// Funksjon for at joysticken skal gå tilbake til nullpunktet når den slippes
var REVERT_THRESHOLD = 1000;
var $ball = $('.ball.moveable');
var initialOffset = $ball.offset();
$('.ball').on("change mousemove touchmove", function() {
var offset = $ball.position();
var offsetY = initialOffset.top - offset.top;
var offsetX = -1*(initialOffset.left - offset.left)
$('#pos-x').html('x: ' + offsetX + 'px');
$('#pos-y').html('y: ' + offsetY + 'px');
});
$ball.pep({
useCSSTranslation: false,
shouldEase: false,
constrainTo: 'parent',
initiate: function(){
this.$el.removeClass('ease')
},
stop: function(){
var offset = this.$el.position();
var diffTop = initialOffset.top - offset.top;
var diffLeft = -1*(initialOffset.left - offset.left);
if ( diffTop < REVERT_THRESHOLD && diffLeft < REVERT_THRESHOLD ){
this.$el.css( initialOffset ).addClass('ease') ;
setTimeout(resetCharByte(1), 2000);
}
}
})
</script>
</body>
</html>