-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
226 lines (202 loc) · 5.48 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Cyber Robot Chrome controller</title>
<script type="text/javascript">
var gattServer = null;
var oldLog = console.log;
console.log = function (message) {
logText(message);
oldLog.apply(console, arguments);
};
window.addEventListener('error', function (error) {
console.error(error);
error.preventDefault();
});
window.addEventListener("keydown", function (event) {
if (event.defaultPrevented) {
return;
}
if (document.getElementById('message').value) {
return;
}
switch (event.key) {
case 'F1':
sendToRobot('781');
break;
case 'F2':
sendToRobot('782');
break;
case 'F3':
sendToRobot('783');
break;
case 'F4':
sendToRobot('784');
break;
case 'ArrowDown':
sendToRobot('12D1S-1');
break;
case "ArrowUp":
sendToRobot('12D0S-1');
break;
case 'ArrowLeft':
sendToRobot('12D2S-1');
break;
case 'ArrowRight':
sendToRobot('12D3S-1');
break;
case 'Spacebar':
case ' ':
sendToRobot('12DE');
break;
case 'Enter':
sendToRobot('99SP');
break;
case '1':
sendToRobot('561E');
break;
case '2':
sendToRobot('562E');
break;
case '3':
sendToRobot('563E');
break;
case '4':
sendToRobot('564E');
break;
case '5':
sendToRobot('565E');
break;
case '6':
sendToRobot('566E');
break;
case '7':
sendToRobot('567E');
break;
case '8':
sendToRobot('568E');
break;
case '9':
sendToRobot('569E');
break;
case '0':
sendToRobot('5610E');
default:
return;
}
event.preventDefault();
}, true);
function sendToRobot(message) {
let encoder = new TextEncoder('utf-8');
let serviceUuid = '0000fff3-0000-1000-8000-00805f9b34fb';
let characteristicUuid = '0000fff5-0000-1000-8000-00805f9b34fb';
let notificationUuid = '0000fff4-0000-1000-8000-00805f9b34fb';
if (gattServer === null) {
gattServer = 'loading';
navigator.bluetooth.requestDevice({
optionalServices: [serviceUuid],
acceptAllDevices: true
})
.then(device => device.gatt.connect())
.then(server => {
gattServer = server;
return server;
})
.then(server => server.getPrimaryService(serviceUuid))
.then(service => service.getCharacteristic(notificationUuid))
.then(characteristic => characteristic.startNotifications()
.then(_ => characteristic.addEventListener('characteristicvaluechanged', handleNotifications)))
.catch(e => {
console.error(e);
gattServer = null;
});
}
if (gattServer == 'loading') {
setTimeout(() => sendToRobot(message), 100);
return;
}
gattServer.getPrimaryService(serviceUuid)
.then(service => service.getCharacteristic(characteristicUuid))
.then(characteristic => {
var binaryCommand = encoder.encode(message);
return characteristic.writeValue(binaryCommand);
})
.then(_ => {
console.log(`'${message}' sent.`);
})
.catch(e => console.error(e));
}
function onButtonClick() {
var message = document.getElementById('message').value;
sendToRobot(message);
}
function logText(text) {
document.getElementById('log').value += `${text}\n`;
}
function handleNotifications(event) {
let value = event.target.value;
let a = [];
for (let i = 0; i < value.byteLength; i++) {
a.push('0x' + ('00' + value.getUint8(i).toString(16)).slice(-2));
}
console.log('> ' + a.join(' '));
}
</script>
<style>
* {
font-size: 20px;
font-family: Tahoma, sans-serif;
}
*:focus {
outline: none;
}
html,
body {
width: 100%;
height: 100%;
}
body {
background: radial-gradient(circle at 30%, #eee, #fff 30%, #ccc 75%, #fff 75%);
overflow: hidden;
}
div#container {
margin-left: -300px;
margin-top: -250px;
position: absolute;
left: 50%;
top: 50%;
position: absolute;
width: 600px;
height: 400px;
}
input {
color: white;
border-radius: 8px;
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
background: rgb(66, 184, 221);
padding: 10px 20px;
}
textarea#log {
display: block;
font: 10px monospace;
width: 100%;
height : 250px;
margin: 10px;
}
</style>
</head>
<body>
<div id="container">
<ul>
<li>Use keyboard arrow keys to move, space to stop</li>
<li>Use F1-F4 to control lights</li>
<li>Use 0-9 to control sounds</li>
<li>Use Enter for `99SP`</li>
</ul>
<input type="text" id="message" style="text-transform: uppercase;" />
<input type="button" onclick="javascript:onButtonClick()" value="Send to robot" />
<textarea id="log" readonly></textarea>
</div>
</body>
</html>