-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy path7-segment-animator6.html
711 lines (673 loc) · 27.9 KB
/
7-segment-animator6.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
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
<!--
7-Segment LED Display Animator Tool for 6-Digit Display
Author: Jason A. Cox - @jasonacox - https://github.com/jasonacox
https://jasonacox.github.io/TM1637TinyDisplay/examples/7-segment-animator6.html
Description:
This tool will allow the user to visually toggle on/off LEDs in a 6-digit
7-segment display to create a pattern. It will show the hexadecimal and binary
value for the pattern. The user can then SAVE the pattern as a frame in an
animation sequence which will show up in the "Animation Data" section below
the controls. The user can then PLAY back the animation on the display. Once
complete, the user can COPY the code to their clipboard and paste the data
into their sketch. Designed for the Arduino TM1637TinyDisplay library available
for download in the Arduino IDE or via GitHub:
https://github.com/jasonacox/TM1637TinyDisplay
Date: 2 January 2022
-->
<html>
<head>
<style>
div {
font:14px Arial, Serif;
}
#digitPane {
background-color: black;
width: 900px;
text-align: center;
border:1px solid #4e4e4e;
}
.off {
fill: #320000;
}
.on {
fill: red;
}
#readings {
display: inline-block;
width: 120px;
margin-left: 20px;
font:14px Arial, Serif;
text-align: center;
margin-top: 5px;
margin-bottom: 5px;
}
#valuePane {
width: 900px;
margin-left: 0px;
background-color: #d3d3d3;
border:1px solid #4e4e4e;
}
#codeOutput {
/* height:100px; */
width:888px;
margin-top: 5px;
margin-left: 5px;
margin-bottom: 5px;
border:1px solid #4e4e4e;
font:15px Arial, Serif;
/* overflow:auto; uncomment to turn on scrolling */
background-color: white;
}
#controls {
display: inline-block;
width: 900;
text-align: center;
margin-top: 5px;
margin-bottom: 5px;
}
#controls2 {
display: inline-block;
width: 900;
margin-left: 20px;
text-align: left;
margin-top: 5px;
}
#controlsleft {
display: inline-block;
width: 200;
margin-left: 10px;
text-align: left;
}
#controlsright {
display: inline-block;
width: 350;
margin-right: 10px;
text-align: right;
}
#instructions {
display: inline-block;
width: 900;
margin-left: 20px;
text-align: left;
margin-bottom: 20px;
margin-top: 10px;
}
#datacontrols {
display: inline-block;
width: 860;
margin-left: 20px;
text-align: left;
background-color: white;
}
#outputblock {
width: 900px;
margin-left: 0px;
margin-top: 10px;
background-color: salmon;
border:1px solid #4e4e4e;
}
#help {
width: 860;
text-align: left;
}
.highlight
{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 18px;
background: yellow;
z-index: -1;
display: none;
}
/* 7-segment LED Binary Data
|--A--|
F B
|--G--|
E C
|--D--|
H - Decimal
HGFEDCBA
0b00000000
*/
</style>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<h1>7-Segment LED Display Animator - 6-Digit Display</h1>
<div id="digitPane">
<svg xmlns="http://www.w3.org/2000/svg" width="140px" height="200px" viewBox="2 -2 10 22">
<g class="digit1">
<polygon id="a" class="off" points=" 1, 1 2, 0 8, 0 9, 1 8, 2 2, 2"/>
<polygon id="b" class="off" points=" 9, 1 10, 2 10, 8 9, 9 8, 8 8, 2"/>
<polygon id="c" class="off" points=" 9, 9 10,10 10,16 9,17 8,16 8,10"/>
<polygon id="d" class="off" points=" 9,17 8,18 2,18 1,17 2,16 8,16"/>
<polygon id="e" class="off" points=" 1,17 0,16 0,10 1, 9 2,10 2,16"/>
<polygon id="f" class="off" points=" 1, 9 0, 8 0, 2 1, 1 2, 2 2, 8"/>
<polygon id="g" class="off" points=" 1, 9 2, 8 8, 8 9, 9 8,10 2,10"/>
<polygon id="h" class="off" points=" 11,16 13,16 13,18 11,18"/>
</g>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" width="140px" height="200px" viewBox="2 -2 10 22">
<g class="digit2">
<polygon id="a" class="off" points=" 1, 1 2, 0 8, 0 9, 1 8, 2 2, 2"/>
<polygon id="b" class="off" points=" 9, 1 10, 2 10, 8 9, 9 8, 8 8, 2"/>
<polygon id="c" class="off" points=" 9, 9 10,10 10,16 9,17 8,16 8,10"/>
<polygon id="d" class="off" points=" 9,17 8,18 2,18 1,17 2,16 8,16"/>
<polygon id="e" class="off" points=" 1,17 0,16 0,10 1, 9 2,10 2,16"/>
<polygon id="f" class="off" points=" 1, 9 0, 8 0, 2 1, 1 2, 2 2, 8"/>
<polygon id="g" class="off" points=" 1, 9 2, 8 8, 8 9, 9 8,10 2,10"/>
<polygon id="h" class="off" points=" 11,16 13,16 13,18 11,18"/>
</g>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" width="140px" height="200px" viewBox="2 -2 10 22">
<g class="digit3">
<polygon id="a" class="off" points=" 1, 1 2, 0 8, 0 9, 1 8, 2 2, 2"/>
<polygon id="b" class="off" points=" 9, 1 10, 2 10, 8 9, 9 8, 8 8, 2"/>
<polygon id="c" class="off" points=" 9, 9 10,10 10,16 9,17 8,16 8,10"/>
<polygon id="d" class="off" points=" 9,17 8,18 2,18 1,17 2,16 8,16"/>
<polygon id="e" class="off" points=" 1,17 0,16 0,10 1, 9 2,10 2,16"/>
<polygon id="f" class="off" points=" 1, 9 0, 8 0, 2 1, 1 2, 2 2, 8"/>
<polygon id="g" class="off" points=" 1, 9 2, 8 8, 8 9, 9 8,10 2,10"/>
<polygon id="h" class="off" points=" 11,16 13,16 13,18 11,18"/>
</g>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" width="140px" height="200px" viewBox="2 -2 10 22">
<g class="digit4">
<polygon id="a" class="off" points=" 1, 1 2, 0 8, 0 9, 1 8, 2 2, 2"/>
<polygon id="b" class="off" points=" 9, 1 10, 2 10, 8 9, 9 8, 8 8, 2"/>
<polygon id="c" class="off" points=" 9, 9 10,10 10,16 9,17 8,16 8,10"/>
<polygon id="d" class="off" points=" 9,17 8,18 2,18 1,17 2,16 8,16"/>
<polygon id="e" class="off" points=" 1,17 0,16 0,10 1, 9 2,10 2,16"/>
<polygon id="f" class="off" points=" 1, 9 0, 8 0, 2 1, 1 2, 2 2, 8"/>
<polygon id="g" class="off" points=" 1, 9 2, 8 8, 8 9, 9 8,10 2,10"/>
<polygon id="h" class="off" points=" 11,16 13,16 13,18 11,18"/>
</g>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" width="140px" height="200px" viewBox="2 -2 10 22">
<g class="digit5">
<polygon id="a" class="off" points=" 1, 1 2, 0 8, 0 9, 1 8, 2 2, 2"/>
<polygon id="b" class="off" points=" 9, 1 10, 2 10, 8 9, 9 8, 8 8, 2"/>
<polygon id="c" class="off" points=" 9, 9 10,10 10,16 9,17 8,16 8,10"/>
<polygon id="d" class="off" points=" 9,17 8,18 2,18 1,17 2,16 8,16"/>
<polygon id="e" class="off" points=" 1,17 0,16 0,10 1, 9 2,10 2,16"/>
<polygon id="f" class="off" points=" 1, 9 0, 8 0, 2 1, 1 2, 2 2, 8"/>
<polygon id="g" class="off" points=" 1, 9 2, 8 8, 8 9, 9 8,10 2,10"/>
<polygon id="h" class="off" points=" 11,16 13,16 13,18 11,18"/>
</g>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" width="140px" height="200px" viewBox="2 -2 10 22">
<g class="digit6">
<polygon id="a" class="off" points=" 1, 1 2, 0 8, 0 9, 1 8, 2 2, 2"/>
<polygon id="b" class="off" points=" 9, 1 10, 2 10, 8 9, 9 8, 8 8, 2"/>
<polygon id="c" class="off" points=" 9, 9 10,10 10,16 9,17 8,16 8,10"/>
<polygon id="d" class="off" points=" 9,17 8,18 2,18 1,17 2,16 8,16"/>
<polygon id="e" class="off" points=" 1,17 0,16 0,10 1, 9 2,10 2,16"/>
<polygon id="f" class="off" points=" 1, 9 0, 8 0, 2 1, 1 2, 2 2, 8"/>
<polygon id="g" class="off" points=" 1, 9 2, 8 8, 8 9, 9 8,10 2,10"/>
<polygon id="h" class="off" points=" 11,16 13,16 13,18 11,18"/>
</g>
</svg>
</div>
<div id="valuePane">
<div id="readings">
<div id="val1">Digit 1</div>
<div id="hex1">HEX</div>
<div id="bin1">BIN</div>
</div>
<div id="readings">
<div id="val2">Digit 2</div>
<div id="hex2">HEX</div>
<div id="bin2">BIN</div>
</div>
<div id="readings">
<div id="val3">Digit 3</div>
<div id="hex3">HEX</div>
<div id="bin3">BIN</div>
</div>
<div id="readings">
<div id="val4">Digit 4</div>
<div id="hex4">HEX</div>
<div id="bin4">BIN</div>
</div>
<div id="readings">
<div id="val5">Digit 5</div>
<div id="hex5">HEX</div>
<div id="bin5">BIN</div>
</div>
<div id="readings">
<div id="val6">Digit 6</div>
<div id="hex6">HEX</div>
<div id="bin6">BIN</div>
</div>
<div id="controls">
<button onclick="saveFrame()">Save Frame</button>
<button onclick="playAnimation()">Playback Animation</button>
<button onclick="clearDisplay()">Clear</button> |
<button onclick="updateFrame()" id="frame">Frame = 0</button>
<button onclick="updateSpeed()" id="delay">Delay = 100</button>
<button onclick="toggleMap()" id="map">HGFEDCBA</button>
</div>
</div>
<div id="instructions">
<div id="help"><b>Instructions:</b> Click segments above to toggle LED state and
click "Save" to record frame. Data for the
animation will be generated below. Click the "Copy Code" button and paste
the data definition into your sketch. See:
<a href="https://github.com/jasonacox/TM1637TinyDisplay" target="_blank" >TM1637TinyDisplay</a>
( Animator:
<a href="https://jasonacox.github.io/TM1637TinyDisplay/examples/7-segment-animator.html">4-Digit</a> |
<a href="https://jasonacox.github.io/TM1637TinyDisplay/examples/7-segment-animator6.html">6-Digit</a> )
</div>
</div>
<br>
<div id="outputblock">
<div id="controls2">
<div id="controlsleft"><b>Animation Data</b></div>
<div id="controlsright">
<button onclick="copyCode()">Copy Code</button>
<button onclick="if(confirmErase('This will clear current animation - Proceed?')) clearData()">Clear Data</button>
<button onclick="formatData()">Format Data</button>
<button onclick="if(confirmErase('This will replace current animation - Proceed?')) exampleData()">Example</button>
</div>
</div>
<pre id="codeOutput" contentEditable="true">
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // Frame 0</pre>
</div>
<script>
// Globals //
var delayTime = 100; // Time between animation frames in ms
var currentFrame = 0; // TODO - Allow editing of frames
var HGFEDCBA = true; // Binary Map - True=HGFEDCBA and False=ABCDEFGH
// Functions //
// Popup to ask user msg to confirm
function confirmErase(msg) {
console.log("size: " + $('#codeOutput').text().length);
if($('#codeOutput').text().length > 39) {
return(window.confirm(msg));
}
else return(true);
}
// Flip binary order to change mapping
function flipByte(digitbin) {
var ret = 0b00000000;
console.log("digitbin: ".concat(digitbin," ret: ", ret));
if((digitbin & 0b00000001)>0) ret += 0b10000000;
if((digitbin & 0b00000010)>0) ret += 0b01000000;
if((digitbin & 0b00000100)>0) ret += 0b00100000;
if((digitbin & 0b00001000)>0) ret += 0b00010000;
if((digitbin & 0b00010000)>0) ret += 0b00001000;
if((digitbin & 0b00100000)>0) ret += 0b00000100;
if((digitbin & 0b01000000)>0) ret += 0b00000010;
if((digitbin & 0b10000000)>0) ret += 0b00000001;
return(ret);
}
// Toggle binary order for segment map
function toggleMap() {
if(HGFEDCBA) {
HGFEDCBA = false;
$('#map').text("ABCDEFGH");
}
else {
HGFEDCBA = true;
$('#map').text("HGFEDCBA");
}
formatData(true);
updateValues();
}
// Refresh button frame number indicator to last frame
function updateFrame() {
formatData();
var last = lastFrame();
showFrame(last);
}
// Toggle through speed options
function updateSpeed() {
switch(delayTime) {
case 200:
delayTime = 100;
break;
case 100:
delayTime = 50;
break;
case 50:
delayTime = 25;
break;
case 25:
delayTime = 500;
break;
default:
delayTime = 200;
break;
}
$('#delay').text("Delay = ".concat(delayTime.toString()));
}
// Copy animation data to users clipboard
function textToClipboard (text) {
var dummy = document.createElement("textarea");
dummy.setAttribute('readonly', '');
dummy.style.position = 'absolute';
dummy.style.left = '-9999px';
document.body.appendChild(dummy);
dummy.value = text;
dummy.select();
document.execCommand("copy");
document.body.removeChild(dummy);
}
// Generate code from animation data and update code output div
function copyCode() {
formatData();
var frame = 0;
var frameNum = lastFrame() + 1;
var buffer = "/* Animation Data - ";
if(HGFEDCBA) buffer = buffer + "HGFEDCBA Map */\n";
else buffer = buffer + "ABCDEFGH Map */\n";
buffer = buffer.concat("const uint8_t ANIMATION[",frameNum,"][6] = {");
// Copy data into code format and add to clipboard
var lines = $('#codeOutput').text().split("\n");
while(frame < lines.length) {
// Each frames to buffer
var line = lines[frame];
if(frame == (lines.length-1)) {
buffer = buffer.concat("\n ",line.replace("},","} "));
}
else if(line.length > 19) {
buffer = buffer.concat("\n ",line);
}
frame++;
}
buffer = buffer.concat("\n};");
console.log(buffer);
textToClipboard(buffer);
$('#codeOutput').text((buffer.concat(" ")).trim());
$("#outputblock").css("background-color","green");
}
// Erase all animation data
function clearData() {
$('#codeOutput').text('');
$('#frame').text("Frame = 0");
$("#outputblock").css("background-color","salmon");
}
// Load en example animation
function exampleData() {
var data = "{ 0x08, 0x00, 0x00, 0x00, 0x00, 0x00 },\n{ 0x00, 0x08, 0x00, 0x00, 0x00, 0x00 },\n{ 0x00, 0x00, 0x08, 0x00, 0x00, 0x00 },\n{ 0x00, 0x00, 0x00, 0x08, 0x00, 0x00 },\n{ 0x00, 0x00, 0x00, 0x00, 0x08, 0x00 },\n{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x08 },\n{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x04 },\n{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 },\n{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },\n{ 0x00, 0x00, 0x00, 0x00, 0x01, 0x00 },\n{ 0x00, 0x00, 0x00, 0x01, 0x00, 0x00 },\n{ 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 },\n{ 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 },\n{ 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 },\n{ 0x20, 0x00, 0x00, 0x00, 0x00, 0x00 },\n{ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00 }";
$('#codeOutput').text(data);
$("#outputblock").css("background-color","salmon");
formatData(!HGFEDCBA);
}
// Return the value of digit dig
function grabValue(dig) {
// Compute binary value based on segment LEDs
var digitbin = 0;
if($(dig.concat(" #a")).attr("class") == "on") digitbin += 0b00000001;
if($(dig.concat(" #b")).attr("class") == "on") digitbin += 0b00000010;
if($(dig.concat(" #c")).attr("class") == "on") digitbin += 0b00000100;
if($(dig.concat(" #d")).attr("class") == "on") digitbin += 0b00001000;
if($(dig.concat(" #e")).attr("class") == "on") digitbin += 0b00010000;
if($(dig.concat(" #f")).attr("class") == "on") digitbin += 0b00100000;
if($(dig.concat(" #g")).attr("class") == "on") digitbin += 0b01000000;
if($(dig.concat(" #h")).attr("class") == "on") digitbin += 0b10000000;
// Note: the svg LED segment values use HGFEDCBA map
if(!HGFEDCBA) return(flipByte(digitbin)); // flip if ABCDEFGH map
else return(digitbin);
}
// Clear all digits
function clearDisplay(){
resetZero(".digit1");
resetZero(".digit2");
resetZero(".digit3");
resetZero(".digit4");
resetZero(".digit5");
resetZero(".digit6");
var frameNum = lastFrame() + 1;
$('#frame').text("Frame = ".concat(frameNum.toString()));
$("#outputblock").css("background-color","salmon");
}
// Clear a single digit dig
function resetZero(dig) {
$(dig.concat(" #a")).attr("class","off");
$(dig.concat(" #b")).attr("class","off");
$(dig.concat(" #c")).attr("class","off");
$(dig.concat(" #d")).attr("class","off");
$(dig.concat(" #e")).attr("class","off");
$(dig.concat(" #f")).attr("class","off");
$(dig.concat(" #g")).attr("class","off");
$(dig.concat(" #h")).attr("class","off");
}
// Set digit dig segment LEDs based on d
function setDigit(dig, d) {
resetZero(dig);
var digitbin = d;
// Note: the svg LED segment values always use HGFEDCBA map
if(!HGFEDCBA) digitbin = flipByte(d); // flip if ABCDEFGH
if((digitbin & 0b00000001) > 0)
$(dig.concat(" #a")).attr("class","on");
if((digitbin & 0b00000010) > 0)
$(dig.concat(" #b")).attr("class","on");
if((digitbin & 0b00000100) > 0)
$(dig.concat(" #c")).attr("class","on");
if((digitbin & 0b00001000) > 0)
$(dig.concat(" #d")).attr("class","on");
if((digitbin & 0b00010000) > 0)
$(dig.concat(" #e")).attr("class","on");
if((digitbin & 0b00100000) > 0)
$(dig.concat(" #f")).attr("class","on");
if((digitbin & 0b01000000) > 0)
$(dig.concat(" #g")).attr("class","on");
if((digitbin & 0b10000000) > 0)
$(dig.concat(" #h")).attr("class","on");
updateValues();
}
// Compute digit values and display values below digits
function updateValues() {
digit1bin = grabValue(".digit1");
$('#hex1').html('0x'.concat(digit1bin.toString(16).padStart(2, '0')));
$('#bin1').html('0b'.concat(digit1bin.toString(2).padStart(8, '0')));
digit2bin = grabValue(".digit2");
$('#hex2').html('0x'.concat(digit2bin.toString(16).padStart(2, '0')));
$('#bin2').html('0b'.concat(digit2bin.toString(2).padStart(8, '0')));
digit3bin = grabValue(".digit3");
$('#hex3').html('0x'.concat(digit3bin.toString(16).padStart(2, '0')));
$('#bin3').html('0b'.concat(digit3bin.toString(2).padStart(8, '0')));
digit4bin = grabValue(".digit4");
$('#hex4').html('0x'.concat(digit4bin.toString(16).padStart(2, '0')));
$('#bin4').html('0b'.concat(digit4bin.toString(2).padStart(8, '0')));
digit5bin = grabValue(".digit5");
$('#hex5').html('0x'.concat(digit5bin.toString(16).padStart(2, '0')));
$('#bin5').html('0b'.concat(digit5bin.toString(2).padStart(8, '0')));
digit6bin = grabValue(".digit6");
$('#hex6').html('0x'.concat(digit6bin.toString(16).padStart(2, '0')));
$('#bin6').html('0b'.concat(digit6bin.toString(2).padStart(8, '0')));
}
// Capture digit values as a new frame and add to animation data
function saveFrame() {
formatData(); // Clean up data before adding new frame
var frameNum = lastFrame() + 1;
var d1 = '0x'.concat(grabValue(".digit1").toString(16).padStart(2, '0'));
var d2 = '0x'.concat(grabValue(".digit2").toString(16).padStart(2, '0'));
var d3 = '0x'.concat(grabValue(".digit3").toString(16).padStart(2, '0'));
var d4 = '0x'.concat(grabValue(".digit4").toString(16).padStart(2, '0'));
var d5 = '0x'.concat(grabValue(".digit5").toString(16).padStart(2, '0'));
var d6 = '0x'.concat(grabValue(".digit6").toString(16).padStart(2, '0'));
var output = $('#codeOutput').text();
if(output.length > 19) output = output + "\n";
else frameNum = 0;
$('#codeOutput').text(''.concat(output,"{ ",d1,", ",d2,", ",d3,", ",d4,", ",d5,", ",d6," }, // Frame ",frameNum.toString()));
$('#frame').text("Frame = ".concat(frameNum.toString()));
}
// Return the number of frames
function lastFrame() {
var lines = $('#codeOutput').text().split("\n");
return(lines.length - 1);
}
// Playback the animation data on digits
function playAnimation() {
formatData(); // clean up data first
var frame = 0;
var lines = $('#codeOutput').text().split("\n");
var step = 1;
var tick = setInterval(function() {
if(frame == (lines.length-1)) { clearInterval(tick) };
var line = lines[frame];
if(line.length > 19) {
$('#frame').text("Frame = ".concat(frame.toString()));
var fields = line.replace("{","").replace("}","").split(",");
if(fields.length >= 6) {
var d1 = parseInt(fields[0].trim());
var d2 = parseInt(fields[1].trim());
var d3 = parseInt(fields[2].trim());
var d4 = parseInt(fields[3].trim());
var d5 = parseInt(fields[4].trim());
var d6 = parseInt(fields[5].trim());
console.log("Frame: ".concat(frame," - Valid Data: ",d1," ",d2," ", d3," ", d4," ", d5," ", d6));
setDigit(".digit1", d1);
setDigit(".digit2", d2);
setDigit(".digit3", d3);
setDigit(".digit4", d4);
setDigit(".digit5", d5);
setDigit(".digit6", d6);
}
else console.log("Frame: ".concat(frame," - Invalid Data"));
}
else {
console.log("Frame: ".concat(frame," - Invalid Data"));
}
frame++;
}, delayTime);
}
// Clean up the animation data
function formatData(flip = false) {
var frame = 0;
var lines = $('#codeOutput').text().split("\n");
var step = 1;
var buffer = [];
var dirty = false;
while(frame < lines.length) {
var line = lines[frame];
if(line.length > 19) {
var fields = line.replace("{","").replace("}","").split(",");
if(fields.length >= 6) {
var d1 = parseInt(fields[0].trim());
var d2 = parseInt(fields[1].trim());
var d3 = parseInt(fields[2].trim());
var d4 = parseInt(fields[3].trim());
var d5 = parseInt(fields[4].trim());
var d6 = parseInt(fields[5].trim());
if(isNaN(d1)) d1=0;
if(isNaN(d2)) d2=0;
if(isNaN(d3)) d3=0;
if(isNaN(d4)) d4=0;
if(isNaN(d5)) d5=0;
if(isNaN(d6)) d6=0;
if(flip) {
d1 = flipByte(d1);
d2 = flipByte(d2);
d3 = flipByte(d3);
d4 = flipByte(d4);
d5 = flipByte(d5);
d6 = flipByte(d6);
}
var nd1 = '0x'.concat(d1.toString(16).padStart(2, '0'));
var nd2 = '0x'.concat(d2.toString(16).padStart(2, '0'));
var nd3 = '0x'.concat(d3.toString(16).padStart(2, '0'));
var nd4 = '0x'.concat(d4.toString(16).padStart(2, '0'));
var nd5 = '0x'.concat(d5.toString(16).padStart(2, '0'));
var nd6 = '0x'.concat(d6.toString(16).padStart(2, '0'));
buffer = "".concat(buffer,"{ ",nd1,", ",nd2,", ",nd3,", ",nd4,", ",nd5,", ",nd6," }, // Frame ",frame.toString());
if(frame < (lines.length-1)) buffer = buffer + "\n";
}
else {
console.log("Format Frame: ".concat(frame," - Invalid Field Number"));
dirty = true;
}
}
else {
console.log("Format Frame: ".concat(frame," - Data Missing"));
}
frame++;
}
$('#codeOutput').text((" ".concat(buffer)).trim());
$('#frame').text("Frame = ".concat(lastFrame().toString()));
$("#outputblock").css("background-color","salmon");
if(dirty) formatData(); // We threw away lines during formatting, run again
}
// Load a single frame into digits
function showFrame(frame) {
var lines = $('#codeOutput').text().split("\n");
if((lines.length-1)<frame) return;
var line = lines[frame];
if(line.length > 19) {
$('#frame').text("Frame = ".concat(frame.toString()));
var fields = line.replace("{","").replace("}","").split(",");
if(fields.length >= 6) {
var d1 = parseInt(fields[0].trim());
var d2 = parseInt(fields[1].trim());
var d3 = parseInt(fields[2].trim());
var d4 = parseInt(fields[3].trim());
var d5 = parseInt(fields[4].trim());
var d6 = parseInt(fields[5].trim());
setDigit(".digit1", d1);
setDigit(".digit2", d2);
setDigit(".digit3", d3);
setDigit(".digit4", d4);
setDigit(".digit5", d5);
setDigit(".digit6", d6);
}
else console.log("Frame: ".concat(frame," - Invalid Data"));
}
else {
console.log("Frame: ".concat(frame," - Invalid Data"));
}
}
// Warn user before leaving page
$(window).bind('beforeunload', function(e){
if($('#codeOutput').text().length > 39) {
return("Animation data will be lost - Proceed?");
}
else e=null;
});
// Detect user interactions
$(document).ready(function() {
// Intercept LED mouse clicks and toggle LED
$(".digit1 polygon").click(function() {
var sSegClass = $(this).attr("class") === "off" ? "on" : "off";
$(this).attr("class", sSegClass);
updateValues();
});
$(".digit2 polygon").click(function() {
var sSegClass = $(this).attr("class") === "off" ? "on" : "off";
$(this).attr("class", sSegClass);
updateValues();
});
$(".digit3 polygon").click(function() {
var sSegClass = $(this).attr("class") === "off" ? "on" : "off";
$(this).attr("class", sSegClass);
updateValues();
});
$(".digit4 polygon").click(function() {
var sSegClass = $(this).attr("class") === "off" ? "on" : "off";
$(this).attr("class", sSegClass);
updateValues();
});
$(".digit5 polygon").click(function() {
var sSegClass = $(this).attr("class") === "off" ? "on" : "off";
$(this).attr("class", sSegClass);
updateValues();
});
$(".digit6 polygon").click(function() {
var sSegClass = $(this).attr("class") === "off" ? "on" : "off";
$(this).attr("class", sSegClass);
updateValues();
});
// Intercept mouse clicks in output section and load frame selected
$( "#codeOutput" ).mousedown(function( event ) {
var line = parseInt(event.offsetY/17);
console.log("Line: "+ line);
showFrame(line);
});
});
updateValues();
</script>
</body>
</html>