-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbots.html
More file actions
389 lines (343 loc) · 11.9 KB
/
bots.html
File metadata and controls
389 lines (343 loc) · 11.9 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
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
<!DOCTYPE html><html><head><meta name='viewport' content='width=device-width, initial-scale=1'><title>=^..^= Lego Gamepads</title>
<style>
.axes {
padding: 1em;
}
.buttons {
margin-left: 1em;
}
/*meter*/.axis {
min-width: 200px;
margin: 1em;
}
.button {
display: inline-block;
width: 1em;
text-align: center;
padding: 1em;
border-radius: 20px;
border: 1px solid black;
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAAXNSR0IArs4c6QAAAAxJREFUCNdjYPjPAAACAgEAqiqeJwAAAABJRU5ErkJggg==);
background-size: 0% 0%;
background-position: 50% 50%;
background-repeat: no-repeat;
}
.pressed {
border: 1px solid red;
}
.touched::after {
content: "touch";
display: block;
position: absolute;
margin-top: -0.2em;
margin-left: -0.5em;
font-size: 0.8em;
opacity: 0.7;
}
.hidden {
display: none;
}
.margins {
margin: 0.8em;
}
</style>
</head>
<body>
<h2 id="start">Press a button on your controller to start</h2>
<script>
var service_uuid = "c5f50001-8280-46da-89f4-6d8051e4aeef";
var char_uuid = "c5f50002-8280-46da-89f4-6d8051e4aeef";
var service_uuid_lwp = "00001623-1212-efde-1623-785feabcd123";
var char_uuid_lwp = "00001624-1212-efde-1623-785feabcd123";
var ledPort = 0x31;
function findHubAndConnect(gamepad)
{
navigator.bluetooth.requestDevice({ filters: [{ services: [service_uuid] }, { services: [service_uuid_lwp] }] })
.then(device => {
console.log(`Connecting To [${device.name}] ...`);
buttons[gamepad.index].innerText = `Connecting To [${device.name}] ...`;
device.addEventListener('gattserverdisconnected', () => {
console.log(`Hub disconnected.`);
buttons[gamepad.index].innerText = `Disconnected From [${device.name}], Trying To Reconnect...`;
hubs[gamepad.index] = null;
delayedConnect(100, gamepad, device);
});
clearTimeout(timers[gamepad.index]);
misc[gamepad.index].reconnect = 0;
return connect(gamepad, device);
})
.catch(error => {
})
}
function delayedConnect(delay, gamepad, device)
{
console.log(`Trying to reconnect...`);
clearTimeout(timers[gamepad.index]);
timers[gamepad.index] = setTimeout(connect, delay, gamepad, device);
}
function connect(gamepad, device)
{
return device.gatt.connect().then(server => {
console.log(`Connected server: [${server}].`);
// return server.getPrimaryService(service_uuid);
server.getPrimaryService(service_uuid).then(service => {
console.log(`Connected service: [${service}].`);
return service.getCharacteristic(char_uuid);
})
.then(ch => {
console.log(`Connected characteristic: [${ch}].`);
var decoder = new TextDecoder();
ch.addEventListener('characteristicvaluechanged',(event) => {
hubReady = true;
var b = event.target.value;
if (b.getUint8(0) == 1)
{
statuses[gamepad.index].innerText = decoder.decode(b.buffer.slice(1, -1));
}
});
ch.startNotifications();
ch.writeValueWithResponse(new Uint8Array([1]))
.then(r => {
console.log("Sending updates :3");
})
.catch(error => {
console.log('write value error: ' + error);
console.log("Sending updates anyway :3");
})
.then(r => {
hubs[gamepad.index] = (gp) => sendData(ch, gp);
buttons[gamepad.index].innerText = "Connected to " + ch.service.device.name;
});
})
server.getPrimaryService(service_uuid_lwp).then(service => {
console.log(`Connected service: [${service}].`);
return service.getCharacteristic(char_uuid_lwp);
})
.then(ch => {
console.log(`Connected characteristic: [${ch}].`);
ch.addEventListener('characteristicvaluechanged',(event) => {
hubReady = true;
var b = event.target.value;
if (b.getUint8(0) == 0x0F && b.getUint8(2) == 0x04 && b.getUint8(4) == 0x01 && b.getUint8(5) == 0x17)
ledPort = b.getUint8(3);
});
ch.startNotifications();
ch.writeValueWithoutResponse(new Uint8Array([0x06, 0x00, 0x61, 0x01, 0x00, 0x01]))
.then(r => {
console.log("Sending stuff");
})
.catch(error => {
console.log('write value error: ' + error);
})
.then(r => {
return ch.writeValueWithoutResponse(new Uint8Array([0x0a, 0x00, 0x41, ledPort, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00]));
})
.then(r => {
return ch.writeValueWithoutResponse(new Uint8Array([0x0a, 0x00, 0x81, ledPort, 0x11, 0x51, 0x01].concat(mode_colors[0])));
})
.then(r => {
var state = { mode:0 };
hubs[gamepad.index] = (gp) => sendDataLWP(ch, gp, state);
buttons[gamepad.index].innerText = "Connected to " + ch.service.device.name;
});
})
})
.catch(error => {
misc[gamepad.index].reconnect++;
if (misc[gamepad.index].reconnect > 10)
{
buttons[gamepad.index].innerText = "Disconnected... :("
misc[gamepad.index].reconnect = 0
}
else
{
delayedConnect(1000, gamepad, device);
}
})
}
function sendData(characteristic, gp) {
if (characteristic)
{
var btns = gp.buttons.map((v)=>Math.round(v.value * 127));
var axs = gp.axes.map((v)=>Math.round(v * 127))
var data = [0x06, btns.length, axs.length].concat(btns, axs);
hubReady = false;
characteristic.writeValueWithResponse(new Uint8Array(data))
.then(_ => {
//console.log('Sent: ' + data);
})
.catch(error => {
//console.log('send_data() error: ' + error);
});
} else {
//console.log('Ignored sending data because service is not connected.');
}
}
function clamp(v, min, max) {
return Math.min(Math.max(v, min), max);
}
mode_colors = [[0x00, 0xff, 0x00], [0xff, 0x00, 0xff]]
function sendDataLWP(characteristic, gp, state)
{
if (characteristic)
{
var speed = gp.buttons[7].value - gp.buttons[6].value;
var turn = gp.axes[2] + gp.axes[0];
if (gp.buttons[9].value > 0 && state.buttons[9].value <= 0)
{
state.mode += 1;
if (state.mode > 1)
state.mode = 0;
characteristic.writeValueWithoutResponse(new Uint8Array([0x0a, 0x00, 0x81, ledPort, 0x11, 0x51, 0x01].concat(mode_colors[state.mode])));
}
else
{
if (state.mode == 1)
speed -= gp.axes[1] + gp.axes[3];
var m1 = clamp(-speed - turn, -1, 1);
var m2 = clamp(-speed + turn, -1, 1);
characteristic.writeValueWithoutResponse(new Int8Array([0x08, 0x00, 0x81, 0x10, 0x00, 0x02, Math.round(m1 * 100), -Math.round(m2 * 100)]));
}
state.buttons = gp.buttons;
}
}
/*
* Gamepad API Test
* Written in 2013 by Ted Mielczarek <ted@mielczarek.org>
*
* To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
*
* You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
var haveEvents = 'GamepadEvent' in window;
var haveWebkitEvents = 'WebKitGamepadEvent' in window;
var controllers = {};
var hubs = {};
var buttons = {};
var statuses = {};
var timers = {};
var misc = {};
var hubReady = false;
var rAF = window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.requestAnimationFrame;
function connecthandler(e) {
addgamepad(e.gamepad);
}
function addgamepad(gamepad) {
controllers[gamepad.index] = gamepad; var d = document.createElement("div");
hubs[gamepad.index] = null;
d.setAttribute("id", "controller" + gamepad.index);
var t = document.createElement("h1");
t.appendChild(document.createTextNode("gamepad: " + gamepad.id));
d.appendChild(t);
var btn = document.createElement("button");
btn.setAttribute("id", "hub_btn_" + gamepad.index);
btn.innerText = "Connect To Hub";
btn.className = "margins";
btn.onpointerdown = (e) => findHubAndConnect(controllers[gamepad.index]);
buttons[gamepad.index] = btn;
d.appendChild(btn);
var status = document.createElement("span");
status.innerText = "Waiting for status..."
status.className = "margins";
d.appendChild(status);
statuses[gamepad.index] = status;
var b = document.createElement("div");
b.className = "buttons";
for (var i=0; i<gamepad.buttons.length; i++) {
var e = document.createElement("span");
e.className = "button";
e.innerHTML = i;
b.appendChild(e);
}
d.appendChild(b);
var a = document.createElement("div");
a.className = "axes";
for (i=0; i<gamepad.axes.length; i++) {
e = document.createElement("meter");
e.className = "axis";
e.setAttribute("min", "-1");
e.setAttribute("max", "1");
e.setAttribute("value", "0");
e.innerHTML = i;
a.appendChild(e);
}
d.appendChild(a);
document.getElementById("start").style.display = "none";
document.body.appendChild(d);
console.log("Gamepad added " + gamepad.index);
//rAF(updateStatus);
misc[gamepad.index] = {reconnect:0};
}
function disconnecthandler(e) {
removegamepad(e.gamepad);
clearTimeout(timers[e.gamepad.index]);
}
function removegamepad(gamepad) {
console.log("Gamepad removed");
var d = document.getElementById("controller" + gamepad.index);
document.body.removeChild(d);
delete controllers[gamepad.index];
}
function updateStatus() {
scangamepads();
for (j in controllers) {
var controller = controllers[j];
var d = document.getElementById("controller" + j);
var buttons = d.getElementsByClassName("button");
for (var i=0; i<controller.buttons.length; i++) {
var b = buttons[i];
var val = controller.buttons[i];
var pressed = val == 1.0;
var touched = false;
if (typeof(val) == "object") {
pressed = val.pressed;
if ('touched' in val) {
touched = val.touched;
}
val = val.value;
}
var pct = Math.round(val * 100) + "%";
b.style.backgroundSize = pct + " " + pct;
b.className = "button";
if (pressed) {
b.className += " pressed";
}
if (touched) {
b.className += " touched";
}
}
var axes = d.getElementsByClassName("axis");
for (var i=0; i<controller.axes.length; i++) {
var a = axes[i];
a.innerHTML = i + ": " + controller.axes[i].toFixed(4);
a.setAttribute("value", controller.axes[i]);
}
if (hubs[controller.index])
hubs[controller.index](controller);
}
//rAF(updateStatus);
setTimeout(updateStatus, 50);
}
function scangamepads() {
var gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);
for (var i = 0; i < gamepads.length; i++) {
if (gamepads[i] && (gamepads[i].index in controllers)) {
controllers[gamepads[i].index] = gamepads[i];
}
}
}
if (haveEvents) {
window.addEventListener("gamepadconnected", connecthandler);
window.addEventListener("gamepaddisconnected", disconnecthandler);
} else if (haveWebkitEvents) {
window.addEventListener("webkitgamepadconnected", connecthandler);
window.addEventListener("webkitgamepaddisconnected", disconnecthandler);
} else {
setInterval(scangamepads, 500);
}
updateStatus();
</script>
</body>
</html>