-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.html
604 lines (546 loc) · 18.3 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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<title>ESP32 FastLED BLE</title>
<!-- Bootstrap CSS -->
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-zCbKRCUGaJDkqS1kPbPd7TveP5iyJE0EjAuZQTgFLD2ylzuqKfdKlfG/eSrtxUkn"
crossorigin="anonymous"
/>
<style>
body {
padding-top: 5rem;
}
</style>
</head>
<body>
<nav class="navbar navbar-expand-sm navbar-light bg-light fixed-top">
<a class="navbar-brand" href="#">ESP32 FastLED BLE</a>
<button
class="navbar-toggler"
type="button"
data-toggle="collapse"
data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent"></div>
</nav>
<main role="main" class="container-fluid">
<form>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Status</label>
<div class="col-sm-8">
<input
id="bluetooth"
class="form-control mr-sm-2"
readonly
value=""
/>
</div>
<div class="col-sm-2">
<button id="connectButton" class="btn btn-primary" type="button">
Connect
</button>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Brightness</label>
<div class="col-sm-8">
<div class="btn-group" role="group" aria-label="Power">
<button id="offButton" type="button" class="btn btn-primary">
Off
</button>
<button id="onButton" type="button" class="btn btn-secondary">
On
</button>
</div>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Brightness</label>
<div class="col-sm-8">
<input
id="brightnessRange"
class="form-control"
type="range"
step="1"
min="0"
max="255"
value="0"
/>
</div>
<div class="col-sm-2">
<input
id="brightnessInput"
class="form-control"
type="number"
step="1"
min="0"
max="255"
value="0"
/>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Pattern</label>
<div class="col-sm-8">
<input
id="patternRange"
class="form-control"
type="range"
step="1"
min="0"
max="255"
value="0"
/>
</div>
<div class="col-sm-2">
<input
id="patternInput"
class="form-control"
type="number"
step="1"
min="0"
max="255"
value="0"
/>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Palette</label>
<div class="col-sm-8">
<input
id="paletteRange"
class="form-control"
type="range"
step="1"
min="0"
max="255"
value="0"
/>
</div>
<div class="col-sm-2">
<input
id="paletteInput"
class="form-control"
type="number"
step="1"
min="0"
max="255"
value="0"
/>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Solid Color</label>
<div class="col-sm-10">
<div id="color-picker-container"></div>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label"></label>
<div class="col-sm-10">
<input type="color" id="solidColorInput" value="#000000" />
</div>
</div>
</form>
</main>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js"
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
crossorigin="anonymous"
></script>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-fQybjgWLrvvRgtW6bFlB7jaZrFsaBXjsOMm/tB9LTS58ONXgqbR9W8oWht/amnpF"
crossorigin="anonymous"
></script>
<script src="https://cdn.jsdelivr.net/npm/@jaames/iro/dist/iro.min.js"></script>
</body>
<script type="text/javascript">
const sleep = (milliseconds) => {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
};
var connected = false;
var maxRecords = 64;
var settings = {
power: {
uuid: "c7df1272-99f7-4059-8a35-ca03831f2897",
properties: ["BLENotify", "BLERead", "BLEWrite"],
structure: ["Uint8"],
data: { V: [] },
writeBusy: false, // we need to track this to avoid 'GATT operation in progress' errors
writeValue: null,
dataUpdated: (self) => {
if (self.data.V[0]) {
onButton.className = "btn btn-primary";
offButton.className = "btn btn-secondary";
} else {
offButton.className = "btn btn-primary";
onButton.className = "btn btn-secondary";
}
},
},
brightness: {
uuid: "eb817a17-301d-49cd-8048-b4840ec6d40c",
properties: ["BLENotify", "BLERead", "BLEWrite"],
structure: ["Uint8"],
data: { V: [] },
writeBusy: false, // we need to track this to avoid 'GATT operation in progress' errors
writeValue: null,
dataUpdated: (self) => {
brightnessRange.value = self.data.V[0];
brightnessInput.value = self.data.V[0];
},
},
pattern: {
uuid: "027db9ce-6d4a-48bb-876e-d4044aea8539",
properties: ["BLENotify", "BLERead", "BLEWrite"],
structure: ["Uint8"],
data: { V: [] },
writeBusy: false, // we need to track this to avoid 'GATT operation in progress' errors
writeValue: null,
dataUpdated: (self) => {
patternRange.value = self.data.V[0];
patternInput.value = self.data.V[0];
},
},
patternCount: {
uuid: "5c888a91-2033-45cb-8932-92f7a6538869",
properties: ["BLERead"],
structure: ["Uint8"],
data: { V: [] },
writeBusy: false, // we need to track this to avoid 'GATT operation in progress' errors
writeValue: null,
dataUpdated: (self) => {
patternRange.max = self.data.V[0] - 1;
patternInput.max = self.data.V[0] - 1;
},
},
palette: {
uuid: "a2f47ab8-a385-456e-bd29-60f29a6fffed",
properties: ["BLENotify", "BLERead", "BLEWrite"],
structure: ["Uint8"],
data: { V: [] },
writeBusy: false, // we need to track this to avoid 'GATT operation in progress' errors
writeValue: null,
dataUpdated: (self) => {
paletteRange.value = self.data.V[0];
paletteInput.value = self.data.V[0];
},
},
paletteCount: {
uuid: "692cc883-c9d1-458d-bf8d-c3c760dd7808",
properties: ["BLERead"],
structure: ["Uint8"],
data: { V: [] },
dataUpdated: (self) => {
paletteRange.max = self.data.V[0] - 1;
paletteInput.max = self.data.V[0] - 1;
},
},
solidColor: {
uuid: "5d35f786-8104-4646-b6cd-ef92ea953069",
properties: ["BLENotify", "BLERead", "BLEWrite"],
structure: ["Uint8", "Uint8", "Uint8"],
data: { R: [], G: [], B: [] },
writeBusy: false, // we need to track this to avoid 'GATT operation in progress' errors
writeValue: null,
dataUpdated: (self) => {
if (
self.data.R &&
self.data.R.length &&
self.data.G &&
self.data.G.length &&
self.data.B &&
self.data.B.length
) {
self.colorPicker.color.rgbString = `rgb(${self.data.R[0]}, ${self.data.G[0]}, ${self.data.B[0]})`;
}
},
},
};
const settingKeys = Object.keys(settings);
const SERVICE_UUID = "8bc01404-b072-413b-881e-6ca27b3f630d";
var bytesReceived = 0;
var bytesPrevious = 0;
function findSetting(uuid) {
for (const key of settingKeys) {
var setting = settings[key];
if (setting.uuid === uuid) return setting;
}
}
function getSettingKey(uuid) {
for (const key of settingKeys) {
var setting = settings[key];
if (setting.uuid === uuid) return key;
}
}
let color = {
rgb: {
r: 0,
g: 0,
b: 0,
},
hexString: "#000000",
};
// UI elements
const connectButton = document.getElementById("connectButton");
const bleStatus = document.getElementById("bluetooth");
const brightnessRange = document.getElementById("brightnessRange");
const brightnessInput = document.getElementById("brightnessInput");
brightnessRange.oninput = () => {
brightnessInput.value = brightnessRange.value;
updateBrightness(brightnessRange.value);
};
brightnessInput.oninput = () => {
brightnessRange.value = brightnessInput.value;
updateBrightness(brightnessRange.value);
};
const offButton = document.getElementById("offButton");
const onButton = document.getElementById("onButton");
offButton.onclick = () => {
offButton.className = "btn btn-primary";
onButton.className = "btn btn-secondary";
updatePower(false);
};
onButton.onclick = () => {
onButton.className = "btn btn-primary";
offButton.className = "btn btn-secondary";
updatePower(true);
};
const patternRange = document.getElementById("patternRange");
const patternInput = document.getElementById("patternInput");
patternRange.oninput = () => {
patternInput.value = patternRange.value;
updatePattern(patternRange.value);
};
patternInput.oninput = () => {
patternRange.value = patternInput.value;
updatePattern(patternRange.value);
};
const paletteRange = document.getElementById("paletteRange");
const paletteInput = document.getElementById("paletteInput");
paletteRange.oninput = () => {
paletteInput.value = paletteRange.value;
updatePalette(paletteRange.value);
};
paletteInput.oninput = () => {
paletteRange.value = paletteInput.value;
updatePalette(paletteRange.value);
};
const solidColorInput = document.getElementById("solidColorInput");
solidColorInput.oninput = () => {
const colorPicker = settings.solidColor.colorPicker;
colorPicker.color.hexString = solidColorInput.value;
};
initColorPicker();
if ("bluetooth" in navigator) {
connectButton.addEventListener("click", function (event) {
event.preventDefault();
connect();
});
// else the browser doesn't support bluetooth
} else {
msg("Browser doesn't support bluetooth");
connectButton.className = "btn btn-danger";
alert(
"Error: This browser doesn't support Web Bluetooth. Try using Chrome."
);
}
function msg(m) {
bleStatus.value = m;
}
async function connect() {
connectButton.className = "btn btn-secondary";
connectButton.disabled = true;
msg("Requesting device...");
try {
const device = await navigator.bluetooth.requestDevice({
filters: [
{
services: [SERVICE_UUID], // SERVICE_UUID
},
],
});
msg("Connecting to device ...");
device.addEventListener("gattserverdisconnected", onDisconnected);
const server = await device.gatt.connect();
msg("Getting primary service ...");
const service = await server.getPrimaryService(SERVICE_UUID);
// Set up the settings
for (const key of settingKeys) {
try {
// console.log(key);
const setting = settings[key];
msg("Getting characteristic " + key + "...");
setting.characteristic = await service.getCharacteristic(
setting.uuid
);
// console.log(setting.characteristic);
// await sleep(1000);
// Set up notification
if (setting.properties.includes("BLENotify")) {
setting.characteristic.addEventListener(
"characteristicvaluechanged",
(event) => {
handleIncoming(setting, event.target.value);
}
);
await setting.characteristic.startNotifications();
}
if (setting.properties.includes("BLERead")) {
// get the current value
setting.characteristic.readValue().then((data) => {
handleIncoming(setting, data);
});
}
setting.rendered = false;
} catch (error) {
console.log(`error loading characteristic ${key}`);
console.error(error);
}
}
connectButton.className = "btn btn-success";
msg(`Connected to BLE device "${device.name}"`);
connected = true;
connectButton.disabled = false;
} catch (error) {
console.error(error);
msg("Cancelled");
connectButton.className = "btn btn-primary";
connectButton.disabled = false;
}
}
function handleIncoming(setting, dataReceived) {
const key = getSettingKey(setting.uuid);
// console.log(key);
// console.log(setting);
const columns = Object.keys(setting.data); // column headings for this setting
const typeMap = {
Uint8: { fn: DataView.prototype.getUint8, bytes: 1 },
Uint16: { fn: DataView.prototype.getUint16, bytes: 2 },
Float32: { fn: DataView.prototype.getFloat32, bytes: 4 },
};
var packetPointer = 0,
i = 0;
// Read each setting value in the BLE packet and push into the data array
setting.structure.forEach(function (dataType) {
try {
// Lookup function to extract data for given characteristic property type
var dataViewFn = typeMap[dataType].fn.bind(dataReceived);
// Read characteristic ouput value - true => Little Endian
var unpackedValue = dataViewFn(packetPointer, true);
// Push characteristic reading onto data array
setting.data[columns[i]].push(unpackedValue);
// Keep array at buffer size
if (setting.data[columns[i]].length > maxRecords) {
setting.data[columns[i]].shift();
}
// move pointer forward in data packet to next value
packetPointer += typeMap[dataType].bytes;
bytesReceived += typeMap[dataType].bytes;
} catch (error) {
console.error(error);
}
i++;
});
setting.rendered = false; // flag - vizualization needs to be updated
// console.log(characteristic);
if (setting.dataUpdated) setting.dataUpdated(setting);
}
function onDisconnected(event) {
connected = false;
let device = event.target;
connectButton.className = "btn btn-danger";
// clear read polling
for (const key of settingKeys) {
const setting = settings[key];
if (typeof setting.polling !== "undefined") {
clearInterval(setting.polling);
}
}
msg("Device " + device.name + " is disconnected.");
}
function BLEwriteTo(key) {
if (!connected) return;
const setting = settings[key];
if (setting.writeBusy) return; // dropping writes when one is in progress instead of queuing as LED is non-critical / realtime
setting.writeBusy = true; // Ensure no write happens when GATT operation in progress
setting.characteristic
.writeValue(setting.writeValue)
.then((_) => {
setting.writeBusy = false;
})
.catch((error) => {
console.log(error);
});
}
function initColorPicker() {
settings.solidColor.colorPicker = new iro.ColorPicker(
"#color-picker-container",
{
width: 150,
color: `rgb(${color.rgb.r}, ${color.rgb.g}, ${color.rgb.b})`,
// borderWidth: 1,
// borderColor: "#fff",
// sliderHeight: 6,
// sliderMargin: 6
}
);
solidColorInput.value = color.hexString;
// RGB Color Picker
settings.solidColor.colorPicker.on("color:change", updateColor);
function updateColor(color) {
if (connected) {
var rgb_values = Uint8Array.of(color.rgb.r, color.rgb.g, color.rgb.b);
// console.log(rgb_values);
settings.solidColor.writeValue = rgb_values;
BLEwriteTo("solidColor");
}
solidColorInput.value = color.hexString;
}
}
function updatePower(state) {
if (connected) {
const value = state ? 1 : 0;
// console.log(value);
settings.power.writeValue = Uint8Array.of(value);
BLEwriteTo("power");
}
}
function updateBrightness(value) {
if (connected) {
// console.log(value);
settings.brightness.writeValue = Uint8Array.of(value);
BLEwriteTo("brightness");
}
}
function updatePattern(value) {
if (connected) {
// console.log(value);
settings.pattern.writeValue = Uint8Array.of(value);
BLEwriteTo("pattern");
}
}
function updatePalette(value) {
if (connected) {
// console.log(value);
settings.palette.writeValue = Uint8Array.of(value);
BLEwriteTo("palette");
}
}
</script>
</html>