-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
executable file
·73 lines (67 loc) · 2.4 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>Fidgetly Ctrl Pitch/Roll/Yaw Visualiser</title>
<style>
body{
font-family: Helvetica;
text-align:center;
}
ol{
width:400px;
text-align:left;
margin:0 auto;
}
</style>
</head>
<body>
<h1>Fidgetly Ctrl Pitch/Roll/Yaw Visualiser</h1>
Click here: <button id="connect">Connect</button>
<canvas data-processing-sources="processing.pde"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/processing.js/1.6.2/processing.min.js"></script>
<script>
//BLE
let characteristic = 0xFEED;
let byteLength = 20;
//IMU global vars for processing
let yaw = 0.0;
let pitch = 0.0;
let roll = 0.0;
window.onload = () => {
document.getElementById('connect').onclick = connectBLE;
}
function connectBLE(){
//BLE setup. Connect and get service/characteristic notifications
console.log('Calling requestDevice.');
navigator.bluetooth.requestDevice()
.then(device => device.gatt.connect())
.then(server => server.getPrimaryService('service))
.then(service => service.getCharacteristic(characteristic))
.then(characteristic => {
return characteristic.startNotifications()
.then(_ => {
characteristic.addEventListener('characteristicvaluechanged',
handleCharacteristicValueChanged);
});
})
.then(_ => {
console.log('Notifications have been started.');
})
.catch(error => { console.log(error); });
function handleCharacteristicValueChanged(event){
var value = event.target.value;
var str = "";
//concat and split string for roll, pitch, yaw (e.g. "-0.58,2.20,328.76")
for(var i=0; i<byteLength; i++){
str = str + String.fromCharCode(value.getUint8(i));
}
var imu = str.split(',');
//update globals
roll = parseFloat(imu[0]);
pitch = parseFloat(imu[1]);
yaw = parseFloat(imu[2]);
}
}
</script>
</body>
</html>