-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWLED_CHRISTMAS_TREE_MAPPING_TOOL.html
More file actions
657 lines (586 loc) · 20.8 KB
/
WLED_CHRISTMAS_TREE_MAPPING_TOOL.html
File metadata and controls
657 lines (586 loc) · 20.8 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
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=3.0">
<title>WLED Christmas Tree Configurator</title>
<style>
body {
font-family: sans-serif;
font-size: 18px;
line-height: 150%;
-webkit-text-size-adjust: 100%;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
a {
color: inherit;
}
.generator {
overflow: auto;
}
table td {
padding: 5px;
white-space: nowrap;
@media (min-width: 768px) {
padding: 5px 10px;
}
}
section {
padding: 20px;
margin: 10px 0;
background: #FCF3FE;
border-radius: 10px;
}
section:nth-child(2n) {
background: #FBF7EA;
}
section#section_info {
color: #fff;
background: #772AB9;
}
section#section_error {
color: #fff;
background: #E85049;
}
.grid {
display: grid;
gap: 10px;
@media (min-width: 768px) {
justify-content: start;
grid-auto-flow: column;
align-items: center;
}
}
.tree {
width: 120px;
}
.header,
.group {
margin: 10px 0;
}
.header h2,
.header h3 {
margin: 0;
}
.text {
max-width: 800px;
}
hr {
height: 1px;
margin: 15px 0;
border: none;
background: #252525;
}
input[type=text],
input[type=number],
textarea {
width: 100%;
height: 28px;
border: 0;
border-radius: 5px;
background: #fff;
font-size: 16px;
}
textarea {
width: 100%;
height: 400px;
}
button {
width: 100%;
height: 28px;
padding: 0 20px;
cursor: pointer;
color: #fff;
border: 0;
border-radius: 5px;
background: #772AB9;
font-size: 16px;
white-space: nowrap;
transition: background 0.2s;
}
button:hover,
button:focus {
background: #8D4AD3;
}
button:disabled {
background: #a06bcd;
pointer-events: none;
}
.submit {
margin-top: 20px;
}
</style>
<script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
const {useRef, useState, useEffect} = React;
function App() {
const connection = useRef(null);
const connectionTimer = useRef(null);
const mappingSection = useRef(null);
const [ip, setIp] = useState('wled-37fdb1.local');
const [started, setStarted] = useState(false);
const [connecting, setConnecting] = useState(false);
const [connected, setConnected] = useState(false);
const [error, setError] = useState(false);
const [imported, setImported] = useState(false);
const [count, setCount] = useState(0);
const [selectedLed, setSelectedLed] = useState(0);
const [selectedLedEnd, setSelectedLedEnd] = useState('');
const [width, setWidth] = useState('');
const [height, setHeight] = useState('');
const [rows, setRows] = useState([{offset: 0}, {offset: 0}, {offset: 0}]);
const [mapping, setMapping] = useState('');
async function start() {
setConnecting(true);
await importMapping();
await connect();
}
async function connect() {
setConnecting(true);
connection.current = new WebSocket(`ws://${ip}/ws`);
connection.current.addEventListener('open', () => {
setConnecting(false);
setConnected(true);
setError(false);
});
connection.current.addEventListener('message', (event) => {
const data = JSON.parse(event.data);
setCount(data?.info?.leds?.count);
setStarted(true);
});
connection.current.addEventListener('error', () => {
connection.current.close();
setError(true);
setConnecting(false);
setConnected(false);
});
connection.current.addEventListener('close', () => {
if (connectionTimer.current) return;
connectionTimer.current = setTimeout(async () => {
connectionTimer.current = null;
await connect();
}, 1000);
});
}
async function importMapping() {
try {
const res = await fetch(`http://${ip}/edit?edit=%2Fledmap.json`);
const data = await res.json();
if (data.width && data.height && data.height) {
setWidth(data.width);
setHeight(data.height);
const rows = Array.from({length: data.height}, (_, i) => {
const row = data.map.slice(i * data.width, i * data.width + data.width);
const offset = Math.round(((row.findIndex(v => v !== -1) + row.findLastIndex(v => v !== -1)) / 2) - (row.length - 1) / 2);
return {
left: row.find(v => v !== -1),
right: row.findLast(v => v !== -1),
offset: offset,
};
});
setRows(rows.reverse());
setImported(true);
} else {
setImported(false);
}
} catch {
setImported(false);
}
}
async function exportMapping() {
try {
const formData = new FormData();
const blob = new Blob([mapping], {type: 'text/json'});
formData.append('data', blob, '/ledmap.json');
await fetch(`http://${ip}/edit`, {
method: 'POST',
body: formData,
});
alert('Mapping uploaded!');
} catch {
alert('Failed saving ledmap.json');
}
}
async function configure2DMatrix() {
try {
await fetch(`http://${ip}/settings/2D`, {
method: 'POST',
body: `SOMP=1&PW=8&PH=8&MPH=1&MPV=1&PB=0&PR=0&PV=0&MPC=1&P0B=0&P0R=0&P0V=0&P0W=${width}&P0H=${height}&P0X=0&P0Y=0&data=`,
});
alert('2D matrix successfully configured!');
} catch {
alert('Failed configure 2D matrix in WLED');
}
}
async function reboot() {
try {
await fetch(`http://${ip}/reset`);
alert('Please wait ~10 seconds...');
} catch {
alert('Failed reboot WLED');
}
}
async function highlightLED(start, end) {
setSelectedLed(start);
setSelectedLedEnd(end);
connection.current.send(JSON.stringify({
on: true,
bri: 255,
ledmap: 9, // to avoid affect current ledmap
seg: {
i: [0, count, '000000', start, end ? end + 1 : start, 'FFFFFF'],
rev: false,
mi: false,
},
}));
}
async function highlightCenterLEDs() {
const centerLeds = rows.reduce((a, v) => {
return [...a, Math.round((parseInt(v.left) + parseInt(v.right)) / 2) + parseInt(v.offset)];
}, []);
connection.current.send(JSON.stringify({
on: true,
bri: 255,
ledmap: 10, // to avoid affect current ledmap
seg: {
i: [0, count, '000000', ...centerLeds.flatMap(x => [x, 'FFFFFF'])],
rev: false,
mi: false,
},
}));
}
async function testWLED() {
connection.current.send(JSON.stringify({
on: true,
bri: 255,
ledmap: 0,
seg: {
fx: 180,
pal: 48,
frz: false,
},
}));
}
function generateMatrix(ranges) {
const height = ranges.length;
let width = 0;
for (const [start, end, offset = 0] of ranges) {
const len = Math.abs(end - start) + 1;
const requiredWidth = len + Math.abs(offset) * 2;
width = Math.max(width, requiredWidth);
}
const map = Array.from({length: height}, () =>
Array(width).fill(-1),
);
for (let row = 0; row < height; row++) {
let [start, end, offset = 0] = ranges[row];
const step = start <= end ? 1 : -1;
const values = [];
for (let v = start; step > 0 ? v <= end : v >= end; v += step) {
values.push(v);
}
const len = values.length;
let startCol = Math.floor((width - len) / 2) + offset;
for (let i = 0; i < len; i++) {
map[row][startCol + i] = values[i];
}
}
return {width, height, map};
}
function generateMapping() {
const ranges = [...rows].map(r => [parseInt(r.left), parseInt(r.right), parseInt(r.offset)]).reverse();
const isValid = ranges.every(a => a.every((n) => typeof n === 'number' && !isNaN(n)));
if (!isValid) {
alert('Please fill in all the values in the table!');
return;
}
const matrix = generateMatrix(ranges);
const json = `{"width":${matrix.width},"height":${matrix.height},"map":[\n` +
matrix.map.map(r => r.toString()).join(',\n') +
'\n]}';
setMapping(json);
setWidth(matrix.width);
setHeight(matrix.height);
}
useEffect(() => {
if (mapping && mappingSection.current) mappingSection.current.scrollIntoView({behavior: 'smooth'});
}, [mapping]);
return (
<>
<section>
<div className="header">
<h3>Preparations</h3>
</div>
<div className="grid" style={{gap: '20px'}}>
<div className="col">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 70 90" className="tree">
<path fill="#6bb42e" d="M27.22 90V75.17H0l19.44-24.73H7.78l19.44-24.72H15.56L35 1l19.44 24.72H42.78l19.44 24.72H50.56L70 75.17H42.78V90z"/>
<mask id="a" width="70" height="89" x="0" y="1" maskUnits="userSpaceOnUse" style={{maskType: 'alpha'}}>
<path fill="#6bb42e" d="M27.22 90V75.17H0l19.44-24.73H7.78l19.44-24.72H15.56L35 1l19.44 24.72H42.78l19.44 24.72H50.56L70 75.17H42.78V90z"/>
</mask>
<g stroke="#ffff05" strokeDasharray="0.1 3" strokeLinecap="round" mask="url(#a)">
<path d="M.5-.5h63.14" transform="matrix(-0.999391 -0.0348995 0.02673 -0.999643 67.0532 69.9836)"/>
<path d="M.5-.5h63.14" transform="matrix(-0.999391 -0.0348995 0.02673 -0.999643 68.0537 58.2383)"/>
<path d="M.5-.5h63.14" transform="matrix(-0.999391 -0.0348995 0.02673 -0.999643 69.0532 46.4919)"/>
<path d="M.5-.5h63.14" transform="matrix(-0.999391 -0.0348995 0.02673 -0.999643 69.0527 34.7468)"/>
<path d="M.5-.5h63.14" transform="matrix(-0.999391 -0.0348995 0.02673 -0.999643 72.0532 23.0005)"/>
<path d="M.5-.5h63.14" transform="matrix(-0.999391 -0.0348995 0.02673 -0.999643 74.0532 11.2554)"/>
</g>
</svg>
</div>
<div className="col">
<div className="text">
<p>Welcome to WLED Christmas Tree Lights Mapping Tool!</p>
<p>Wrap your addressable LED strip around the tree from bottom to top so that you get even
horizontal rows. Configure the required number of LEDs in WLED in the regular 1D
mode. Make sure everything works correctly. Also make sure that the
files <b>ledmap.json</b> (<b>ledmapX.json</b>) and <b>2d-gaps.json</b> are not
present in the WLED File Editor.</p>
<p style={{opacity: 0.5}}>
<a href="https://github.com/Drafteed/wled-christmas-tree-mapping-tool" target="_blank">Open project on GitHub</a>
</p>
</div>
</div>
</div>
</section>
<section>
<div className="grid">
<div className="col">
WLED IP:
</div>
<div className="col">
<input type="text" value={ip} onChange={e => setIp(e.target.value)}/>
</div>
<div className="col">
<button onClick={start} disabled={connecting}>{connecting ? 'Connecting...' : 'Connect'}</button>
</div>
</div>
</section>
{
error && (
<section id="section_error">
😢 Error while connecting to WLED! Reconnecting...
</section>
)
}
{
connected && (
<section id="section_info">
👌 Connected. LED count: {count}.{imported && ' Mapping imported.'}
</section>
)
}
{
started && (
<section>
<div className="header">
<h3>Let's generate your matrix!</h3>
</div>
<div className="text">
<p>
🎄 Take a look at your beautiful tree! Calculate the number of rows (lines) vertically and add the
corresponding number of rows. Then, for each row, find the <b>leftmost</b> and <b>rightmost</b> LED
number.
</p>
<p>
🔎 The slider below will help you locate and identify the number of each LED.<br/>
✅ You can verify that the row is filled correctly by clicking the <b>Highlight</b> button.<br/>
⚠️ For the initial setup, you can leave the offsets at 0.<br/>
ℹ️ Once the rows are set, you can highlight the center LED in each row to adjust the offsets.
</p>
</div>
<hr/>
<div className="group">
{connected ? (
<div className="group">
Highlighted LED Number: {selectedLed}{selectedLedEnd && ` - ${selectedLedEnd}`}
</div>
) : (
<div className="group" style={{color: 'red'}}>
WLED disconnected. Reconnecting...
</div>
)}
<input
type="range"
min="0"
max={count - 1}
disabled={!connected}
value={selectedLed}
onChange={e => highlightLED(parseInt(e.target.value))}
style={{width: '100%'}}
/>
</div>
<hr/>
<div className="generator">
<table className="table">
<thead>
<tr>
<td></td>
<td>Left LED</td>
<td>Right LED</td>
<td>Offset</td>
<td></td>
</tr>
</thead>
<tbody>
{rows
.map((row, i) => (
<tr key={i}>
<td>Row {i + 1}</td>
<td>
<input
type="number"
min="0"
max={count - 1}
value={row.left}
onChange={(e) => {
const r = [...rows];
r[i].left = parseInt(e.target.value);
setRows(r);
}}
/>
</td>
<td>
<input
type="number"
min="0"
max={count - 1}
value={row.right}
onChange={(e) => {
const r = [...rows];
r[i].right = parseInt(e.target.value);
setRows(r);
}}
/>
</td>
<td>
<input
type="number"
min={-count - 1}
max={count - 1}
value={row.offset}
onChange={(e) => {
const r = [...rows];
r[i].offset = parseInt(e.target.value);
setRows(r);
}}
/>
</td>
<td>
<div className="grid" style={{display: 'flex'}}>
<div className="col">
<button
disabled={!connected}
onClick={() => {
const r = [...rows];
const left = parseInt(r[i].left);
const right = parseInt(r[i].right);
if (!Number.isInteger(left) || !Number.isInteger(right)) {
alert('Please fill in all the values in the row!');
return;
}
highlightLED(left < right ? left : right, right > left ? right : left);
}}
>
Highlight
</button>
</div>
<div className="col">
<button
onClick={() => {
setRows([...rows.slice(0, i), ...rows.slice(i + 1)]);
}}
>
Delete
</button>
</div>
</div>
</td>
</tr>
))
.reverse()}
</tbody>
</table>
</div>
<div className="submit">
<div className="grid">
<div className="col">
<button onClick={generateMapping}>Generate mapping</button>
</div>
<div className="col">
<button onClick={() => setRows([...rows, {offset: 0}])}>Add row</button>
</div>
<div className="col">
<button disabled={!connected} onClick={highlightCenterLEDs}>
Highlight center LEDs
</button>
</div>
</div>
</div>
</section>
)
}
{
mapping && (
<section ref={mappingSection}>
<div className="header">
<h3>Magic is coming!</h3>
</div>
<div className="text">
<p>
🎉 Your mapping is done!<br/>
Upload mapping to WLED, configure 2D matrix and reboot device.
</p>
</div>
<textarea value={mapping} readOnly/>
<div className="submit">
<div className="grid">
<div className="col">
<button disabled={!connected} onClick={exportMapping}>
Upload mapping to WLED
</button>
</div>
<div className="col">
<button disabled={!connected} onClick={configure2DMatrix}>
Configure 2D matrix in WLED
</button>
</div>
<div className="col">
<button disabled={!connected} onClick={reboot}>
Reboot WLED
</button>
</div>
<div className="col">
<button disabled={!connected} onClick={testWLED}>
Test run
</button>
</div>
</div>
</div>
<div className="text">
<p>
<b>Manual configuration</b>:<br/>
Upload mapping to WLED via the <a href={`http://${ip}/edit`} target="_blank">File
Editor</a> into <b>/ledmap.json</b> (create the file if it does not exist).<br/> Then
setup <a href={`http://${ip}/settings/2D`} target="_blank">WLED into 2D
mode</a> in <b>Setup > 2D Configuration > 2D Matrix > Panel Dimensions</b> and
enter your matrix dimensions (<b>{width}x{height}</b>).<br/>
Save and <a href={`http://${ip}/reset`} target="_blank">fully reboot WLED</a>.
</p>
</div>
</section>
)
}
</>
);
}
ReactDOM.createRoot(document.getElementById('root')).render(<App/>);
</script>
</body>
</html>