-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex_grabber.html
213 lines (193 loc) · 8.17 KB
/
index_grabber.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
<html>
<head>
<title>Candy Grabber</title>
<meta property="og:description" content="Control the Candy Grabber with Web Bluetooth" />
<meta property="og:image" content="images/nordic_icon_144.png"/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body style="padding: 0 !important; margin: 0 !important">
<div style="background-color:#009cde; padding-top:10; padding-bottom:10 ">
<h1 style="color:#fff; display:flex; justify-content:center">CANDY GRABBER</h1>
</div>
<div id="button" style="display:flex; justify-content:center; align-items:center; margin-top:35">
<input style="width:50%" type="image" id="connect" src="images/connect.png"/>
</div>
<div id="arrows" style="display:none; margin-top:50">
<div style="display:flex; justify-content:center; align-items:center">
<table>
<tr>
<td></td>
<td><input type="image" id="forward" src="images/arrow_forward.png"/></td>
<td></td>
<td style="width:25%"></td>
<td><input type="image" id="up" src="images/arrow_up.png"/></td>
</tr>
<tr>
<td><input type="image" id="left" src="images/arrow_left.png"/></td>
<td></td>
<td><input type="image" id="right" src="images/arrow_right.png"/></td>
<td style="width:25%"></td>
<td></td>
</tr>
<tr>
<td></td>
<td><input type="image" id="backward" src="images/arrow_backward.png"/></td>
<td></td>
<td style="width:25%"></td>
<td><input type="image" id="down" src="images/arrow_down.png"/></td>
</tr>
</table>
</div>
<div style="padding-top:40">
<p align="center">Time remaining: <span id="timeout"></span></p>
</div>
</div>
<pre id="log"></pre>
<script>
var isConnecting = false;
var isConnected = false;
var sInterval;
var sTimeout;
var sServer;
var sCharacteristic;
document.querySelector('#connect').addEventListener('click', connect);
document.querySelector('#forward').addEventListener('touchstart', function(e) { move(e, "forward"); });
document.querySelector('#backward').addEventListener('touchstart', function(e) { move(e, "backward"); });
document.querySelector('#left').addEventListener('touchstart', function(e) { move(e, "left"); });
document.querySelector('#right').addEventListener('touchstart', function(e) { move(e, "right"); });
document.querySelector('#up').addEventListener('touchstart', function(e) { move(e, "up"); });
document.querySelector('#down').addEventListener('touchstart', function(e) { move(e, "down"); });
document.querySelector('#forward').addEventListener('touchend', function(e) { stop(e); });
document.querySelector('#backward').addEventListener('touchend', function(e) { stop(e); });
document.querySelector('#left').addEventListener('touchend', function(e) { stop(e); });
document.querySelector('#right').addEventListener('touchend', function(e) { stop(e); });
document.querySelector('#up').addEventListener('touchend', function(e) { stop(e); });
document.querySelector('#down').addEventListener('touchend', function(e) { stop(e); });
function log(text) {
document.querySelector('#log').textContent += text + '\n';
}
function setConnecting(connecting) {
isConnecting = connecting;
if (connecting) {
document.querySelector('#connect').src = "images/connecting.png";
} else {
document.querySelector('#connect').src = "images/connect.png";
}
}
function setConnected(connected) {
isConnected = connected;
if (connected) {
document.querySelector('#button').style.display = "none";
document.querySelector('#arrows').style.display = "block";
sTimeout = 50;
document.querySelector('#timeout').textContent = sTimeout;
sInterval = setInterval(function() {
sTimeout -= 1;
if (sTimeout <= 0) {
setConnected(false);
clearInterval(sInterval);
} else {
document.querySelector('#timeout').textContent = sTimeout;
}
}, 1000);
} else {
document.querySelector('#button').style.display = "flex";
document.querySelector('#arrows').style.display = "none";
sServer.disconnect();
}
}
function connect() {
'use strict';
if (!navigator.bluetooth) {
log('Web Bluetooth API is not available.\n' +
'Please make sure the Web Bluetooth flag is enabled.');
return;
}
if (isConnecting) {
log('Connecting. Please wait.');
return;
}
log('Requesting Bluetooth Device...');
setConnecting(true);
navigator.bluetooth.requestDevice({
filters: [{
services:['b6c31337-6c07-453e-961a-d8a8a41bf368']
}]
})
.then(device => {
connect2(device, 0);
})
.catch(error => {
setConnecting(false);
log(error);
});
}
function connect2(device, retryCount) {
'use strict';
device.gatt.connect()
.then(server => {
log('Got server');
sServer = server;
return server.getPrimaryService('b6c31337-6c07-453e-961a-d8a8a41bf368');
})
.then(service => {
log('Got service');
return service.getCharacteristic('b6c31338-6c07-453e-961a-d8a8a41bf368');
})
.then(characteristic => {
log('Got characteristic');
sCharacteristic = characteristic;
setConnecting(false);
setConnected(true);
})
.catch(error => {
log(error);
if (retryCount < 1) {
log('Retrying...');
connect2(device, retryCount + 1);
} else {
setConnecting(false);
}
});
}
function move(event, direction) {
log("move(" + event + ", " + direction + ")");
try {
switch (direction) {
case "forward":
sCharacteristic.writeValue(new Uint8Array([1, 0, 0, 0, 0, 0]));
break;
case "backward":
sCharacteristic.writeValue(new Uint8Array([0, 1, 0, 0, 0, 0]));
break;
case "left":
sCharacteristic.writeValue(new Uint8Array([0, 0, 1, 0, 0, 0]));
break;
case "right":
sCharacteristic.writeValue(new Uint8Array([0, 0, 0, 1, 0, 0]));
break;
case "up":
sCharacteristic.writeValue(new Uint8Array([0, 0, 0, 0, 1, 0]));
break;
case "down":
sCharacteristic.writeValue(new Uint8Array([0, 0, 0, 0, 0, 1]));
break;
}
event.preventDefault();
} catch (error) {
setConnected(false);
log(error);
}
}
function stop(event) {
log("stop(" + event + ")");
try {
sCharacteristic.writeValue(new Uint8Array([0, 0, 0, 0, 0, 0]));
event.preventDefault();
} catch (error) {
log(error);
}
}
</script>
</body>
</html>