-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest2.html
More file actions
113 lines (111 loc) · 2.61 KB
/
Copy pathtest2.html
File metadata and controls
113 lines (111 loc) · 2.61 KB
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
<html>
<head>
<title>Bangle.js Accelerometer streaming</title>
</head>
<body>
<script src="https://www.puck-js.com/puck.js"></script>
<button id="btnConnect">Connect</button>
<p>X: <span class="bar"><span id="barX"></span></span></p>
<p>Y: <span class="bar"><span id="barY"></span></span></p>
<p>Z: <span class="bar"><span id="barZ"></span></span></p>
<script>
// Code to upload to Bangle.js
var BANGLE_CODE = `
Bangle.on('accel',function(a) {
var d = [
"A",
Math.round(a.x*100),
Math.round(a.y*100),
Math.round(a.z*100)
];
Bluetooth.println(d.join(","));
})
`;
// When we click the connect button...
var connection;
document.getElementById("btnConnect").addEventListener("click", function() {
// disconnect if connected already
if (connection) {
connection.close();
connection = undefined;
}
// Connect
Puck.connect(function(c) {
if (!c) {
alert("Couldn't connect!");
return;
}
connection = c;
// Handle the data we get back, and call 'onLine'
// whenever we get a line
var buf = "";
connection.on("data", function(d) {
buf += d;
var l = buf.split("\n");
buf = l.pop();
l.forEach(onLine);
});
// First, reset the Bangle
connection.write("reset();\n", function() {
// Wait for it to reset itself
setTimeout(function() {
// Now upload our code to it
connection.write("\x03\x10if(1){"+BANGLE_CODE+"}\n",
function() { console.log("Ready..."); });
}, 1500);
});
});
});
// When we get a line of data, check it and if it's
// from the accelerometer, update it
function onLine(line) {
console.log("RECEIVED:"+line);
var d = line.split(",");
if (d.length==4 && d[0]=="A") {
// we have an accelerometer reading
var accel = {
x : parseInt(d[1]),
y : parseInt(d[2]),
z : parseInt(d[3]),
};
// Update bar positions
setBarPos("barX", accel.x);
setBarPos("barY", accel.y);
setBarPos("barZ", accel.z);
}
}
// Set the position of each bar
function setBarPos(id,d) {
var s = document.getElementById(id).style;
if (d>150) d=150;
if (d<-150) d=-150;
if (d>=0) {
s.left="150px";
s.width=d+"px";
} else { // less than 0
s.left=(150+d)+"px";
s.width=(-d)+"px";
}
}
</script>
<style>
/* Styles just to make the bars for X Y and Z look neat */
.bar {
width : 500px;
height: 24px;
background-color : #D0D0D0;
position:relative;
display: inline-block;
}
.bar span {
width : 1px;
height: 20px;
background-color : red;
position:absolute;
display: inline-block;
left: 150px;
top: 2px;
}
</style>
</body>
</html>