-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7segments-anim.html
585 lines (463 loc) · 14.9 KB
/
7segments-anim.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
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript" src="https://getbootstrap.com/dist/js/bootstrap.js"></script>
<link type="text/css" rel="stylesheet" href="https://getbootstrap.com/dist/css/bootstrap.css"/>
<title>7-segments Animation generator</title>
<style>
body {
padding-top: 51px;
}
.text-center {
padding-top: 20px;
}
.col-xs-12 {
background-color: #fff;
}
#sidebar {
height: 100%;
padding-right: 0;
padding-top: 20px;
}
#sidebar .nav {
width: 95%;
}
#sidebar li {
border:0 #f2f2f2 solid;
border-bottom-width:1px;
}
/* collapsed sidebar styles */
@media screen and (max-width: 767px) {
.row-offcanvas {
position: relative;
-webkit-transition: all 0.25s ease-out;
-moz-transition: all 0.25s ease-out;
transition: all 0.25s ease-out;
}
.row-offcanvas-right
.sidebar-offcanvas {
right: -41.6%;
}
.row-offcanvas-left
.sidebar-offcanvas {
left: -41.6%;
}
.row-offcanvas-right.active {
right: 41.6%;
}
.row-offcanvas-left.active {
left: 41.6%;
}
.sidebar-offcanvas {
position: absolute;
top: 0;
width: 41.6%;
}
#sidebar {
padding-top:0;
}
}
.display7{
display:inline-block;
background-color: #000;
padding: 1em;
font: Monaco, "Panic Sans", Consolas, "Courier New", monospace;
font-size: 12pt;
color: transparent;
}
.display7:hover{
color: #666;
}
.display7 .a
.display7 .b,
.display7 .c,
.display7 .d,
.display7 .e,
.display7 .f,
.display7 .g,{
text-align: center;
}
.display7 div{
background-color: #121;
text-align: center;
cursor: pointer;
}
.display7 div:hover{
outline: 1px outset #0F0;
}
.a, .g, .g{
width: 4em;
}
.b, .c, .e, .f{
width: 1em;
height: 4em;
}
.display7 div.on{
background-color: #CF0;
}
#panel{
text-align: center;
background: #999;
padding: 1em;
}
#animationPanel, #storagePanel{
display: inline-block;
/*border: 2px solid gray;*/
height: 100%;
}
textarea{
text-align: left;
}
</style>
</head>
<body>
<div class="page-container">
<!-- top navbar -->
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="offcanvas" data-target=".sidebar-nav">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">7-segments Animation generator</a>
</div>
</div>
</div>
<div class="container">
<div class="well">
<div id="digit1" class="display7">
<table>
<tr>
<td></td>
<td><div class="a">A</div></td>
<td></td>
</tr>
<tr>
<td><div class="f">F</div></td>
<td></td>
<td><div class="b">B</div></td>
</tr>
<tr>
<td></td>
<td><div class="g">G</div></td>
<td></td>
</tr>
<tr>
<td><div class="e">E</div></td>
<td></td>
<td><div class="c">C</div></td>
</tr>
<tr>
<td></td>
<td><div class="d">D</div></td>
<td></td>
</tr>
</table>
</div>
<div id="digit2" class="display7">
<table>
<tr>
<td></td>
<td><div class="a">A</div></td>
<td></td>
</tr>
<tr>
<td><div class="f">F</div></td>
<td></td>
<td><div class="b">B</div></td>
</tr>
<tr>
<td></td>
<td><div class="g">G</div></td>
<td></td>
</tr>
<tr>
<td><div class="e">E</div></td>
<td></td>
<td><div class="c">C</div></td>
</tr>
<tr>
<td></td>
<td><div class="d">D</div></td>
<td></td>
</tr>
</table>
</div>
<br />
<div>
<input id="dispData" type="text"><button onclick="writeDisplay(dispData.value)"><i class="glyphicon glyphicon-arrow-up"></i> SET</button><br>
<button onclick="pushB()"><i class="glyphicon glyphicon-plus"></i> Add Frame</button>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading"><i class="glyphicon glyphicon-film"></i> Animation
<span class="pull-right">
<input type="text" id="animName" placeholder="Animation Name"/>
<button onclick="saveAnim()"><i class="glyphicon glyphicon-floppy-disk"></i> Save</button>
</span>
</div>
<div class="panel-body">
<p>
<button onclick="play()"><i class="glyphicon glyphicon-play"></i> PLAY</button>
<button onclick="stop()"><i class="glyphicon glyphicon-stop"></i> STOP</button>
<span>
<label><i class="glyphicon glyphicon-dashboard"></i> Interval <input type="number" id="speed" value="250" size="3" style="width: 4em;" /></label> ms
<label><i class="glyphicon glyphicon-repeat"></i> Repeat <input type="number" id="repeat" value="5" size="2" style="width: 4em;"/></label> times
</span>
</p>
<textarea id="anim" rows="20" placeholder="Frames" style="width:100%;"></textarea>
</div>
</div>
</div>
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading"><i class="glyphicon glyphicon-inbox"></i> Saved Animations</div>
<div class="panel-body">
<p>
<button onclick="loadAnim()"><i class="glyphicon glyphicon-floppy-open"></i> Load</button>
<button onclick="deleteAnim()"><i class="glyphicon glyphicon-minus-sign"></i> Delete</button>
</p>
<select id="savedList" size="20" onchange="loadAnim()" style="width:100%;"></select>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading"><i class="glyphicon glyphicon-apple"></i> Arduino Code
<button onclick="generateCode()" class="pull-right"><i class="glyphicon glyphicon-flash"></i>Generate Code</button> </div>
<div class="panel-body">
<textarea id="outputCode" cols="80" rows="25" style="width:100%;" placeholder="Click on the [Generate Code] button"></textarea>
</div>
</div>
</div><!--/.container-->
</div><!--/.page-container-->
<script type="text/javascript">
var playing, t, d1, d2;
var savedAnimations = {};
function saveAnim(){
var animObj = {
name: animName.value,
interval: speed.value,
repeat: repeat.value,
data: anim.value
}
savedAnimations[animObj.name] = animObj;
savedList.innerHTML = "";
for(var a in savedAnimations){
var opt = document.createElement('option');
opt.innerHTML = a;
opt.value = a;
savedList.appendChild(opt);
}
window.localStorage.setItem('savedAnimations', JSON.stringify(savedAnimations) );
}
function loadAnim(){
var a = savedAnimations[savedList.value];
animName.value = a.name;
speed.value = a.interval;
repeat.value = a.repeat;
anim.value = a.data;
}
function deleteAnim(){
if(savedList.value){
var a = savedAnimations[savedList.value];
delete savedAnimations[a.name];
savedList.innerHTML = "";
for(var a in savedAnimations){
var opt = document.createElement('option');
opt.innerHTML = a;
opt.value = a;
savedList.appendChild(opt);
}
window.localStorage.setItem('savedAnimations', JSON.stringify(savedAnimations) );
}
}
function pushB(){
anim.value += d1.getValue()+" "+d2.getValue()+"\n";
}
function play(){
if(!playing==true){
var sequence = anim.value.split(/\n/);
var i=0;
var r = parseInt(repeat.value);
console.log('animation started');
t = setInterval( function(){
if(sequence[i]){
console.log('animation frame', i, sequence[i]);
playing = true;
var bytes = sequence[i].split(" ");
writeDisplay(bytes);
++i;
}
else{
if(r>=0){
i=0;
--r;
console.log('animation repeat');
}else{
clearInterval(t);
playing = false;
console.log('animation ended.');
}
}
}, parseInt(speed.value));
}
else console.log('already playing!');
}
function stop(){
playing=false;
clearInterval(t);
console.log('animation canceled');
}
/* JAVASCRIPT GOES HERE */
window.onload=function(){
d1 = new Display(digit1);
d2 = new Display(digit2);
writeDisplay("1111111 1111111".split(" "));
d1.onChange = d2.onChange = function(){
dispData.value = d1.getValue() +" "+ d2.getValue();
}
//Load saved animations to memory
var storage = window.localStorage.getItem('savedAnimations');
if(storage){
savedAnimations = JSON.parse(storage);
}
else{
savedAnimations = {"filling bar":{"name":"filling bar","interval":"50","repeat":"5","data":"0001000 0001000\n0001001 0001001\n1001001 1001001\n1000001 1000001\n1000000 1000000\n0000000 0000000"},"rising sides":{"name":"rising sides","interval":"50","repeat":"5","data":"0001000 0001000\n0001100 0011000\n0001110 0111000\n1001110 1111000\n1000110 1110000\n1000010 1100000\n1000000 1000000\n0000000 0000000"},"rising dash":{"name":"rising dash","interval":"50","repeat":"5","data":"0001000 0001000\n0000001 0000001\n1000000 1000000\n0000000 0000000"},"rotating segment":{"name":"rotating segment","interval":"50","repeat":"5","data":"0000000 0001000\n0001000 0000000\n0000100 0000000\n0000010 0000000\n1000000 0000000\n0000000 1000000\n0000000 0100000\n0000000 0010000"},"drawing circle":{"name":"drawing circle","interval":"50","repeat":"5","data":"0000000 0001000\n0001000 0001000\n0001100 0001000\n0001110 0001000\n1001110 0001000\n1001110 1001000\n1001110 1101000\n1001110 1111000\n1001110 1110000\n1000110 1110000\n1000010 1110000\n1000000 1110000\n0000000 1110000\n0000000 0110000\n0000000 0010000\n0000000 0000000"},"spinner":{"name":"spinner","interval":"50","repeat":"5","data":"1000000 0001000\n0001000 1000000\n0000100 0100000\n0000010 0010000\n"},"spinner2":{"name":"spinner2","interval":"40","repeat":"5","data":"1000000 0001000\n1001000 1001000\n0001000 1000000\n0001100 1100000\n0000100 0100000\n0000110 0110000\n0000010 0010000\n1000010 0011000"},"cmd":{"name":"cmd","interval":"200","repeat":"5","data":"1000000 0001000\n0100000 0000100\n0010000 0000010\n0001000 1000000\n0000100 0100000\n0000001 0000001\n0000000 0000000\n0000010 0010000"},"command symbol":{"name":"command symbol","interval":"50","repeat":"5","data":"0000000 0010000\n0000000 0001000\n0000000 0000100\n0000000 0000010\n0000000 1000000\n0000000 0100000\n0000000 0000001\n0000001 0000000\n0000010 0000000\n1000000 0000000\n0100000 0000000\n0010000 0000000\n0001000 0000000\n0000100 0000000\n0000001 0000000\n0000000 0000001"},"fast blink UP":{"name":"fast blink UP","interval":"20","repeat":"5","data":"0111110 1100111\n0000000 0000000"},"fast blink dn":{"name":"fast blink dn","interval":"20","repeat":"5","data":"0111101 0010101\n0000000 0000000\n"},"balance":{"name":"balance","interval":"350","repeat":"5","data":"0000111 0110001\n0000111 0110001\n0000111 0110001\n0000111 0110001\n0010110 0110100\n0010100 0110100\n0010000 0110100\n0000000 0110100\n0000000 0110000\n0000000 0100000\n0000000 0110000\n0000000 0110100\n0010000 0110100\n0010100 0110100\n0010110 0110100\n0000111 0110001\n0010110 0110100\n0010110 0010100\n0010110 0000100\n0010110 0000000\n0000110 0000000\n0000010 0000000\n"}};
}
for(var a in savedAnimations){
var opt = document.createElement('option');
opt.innerHTML = a;
opt.value = a;
savedList.appendChild(opt);
}
};
function writeDisplay(bytes){
d1.setValue(bytes[0]);
d2.setValue(bytes[1])
dispData.value = bytes.join(" ");
}
/*
* Counter.js - written by Victor N - 22/Nov/2013 - www.vitim.us
*/
function Display(displayElement){
var self = this;
this.pos;
this.values = [0,1,2,3,4,5,6,7,8,9];
this.options = {
mousewheel: true,
digitHeight: 0,
inverted: false
}
this.DOM = {
display : displayElement,
segments: [
displayElement.querySelector('.a'),
displayElement.querySelector('.b'),
displayElement.querySelector('.c'),
displayElement.querySelector('.d'),
displayElement.querySelector('.e'),
displayElement.querySelector('.f'),
displayElement.querySelector('.g')
]
}
this.DOM.display.onclick=function(){
}
for(var i=0; i<this.DOM.segments.length; ++i){
this.DOM.segments[i].onclick = function(){
this.classList.toggle("on");
if(self.onChange) self.onChange();
}
}
/*
//Initial Values
if(this.DOM.counter.innerHTML.indexOf('|')>-1){
this.values = this.DOM.counter.innerHTML.split('|');
}
this.DOM.counter.innerHTML = "";
this.DOM.counter.classList.add('counter');
this.DOM.wheel.classList.add('wheel');
this.DOM.digitAbove.classList.add('digit');
this.DOM.digitCenter.classList.add('digit');
this.DOM.digitBelow.classList.add('digit');
this.DOM.digitAbove.classList.add('above');
this.DOM.digitCenter.classList.add('center');
this.DOM.digitBelow.classList.add('below');
this.DOM.counter.appendChild(this.DOM.wheel);
this.DOM.wheel.appendChild(this.DOM.digitAbove);
this.DOM.wheel.appendChild(this.DOM.digitCenter);
this.DOM.wheel.appendChild(this.DOM.digitBelow);
//compute digit height
//this.options.digitHeight = this.DOM.digitCenter.offsetHeight;
this.options.digitHeight = parseInt(window.getComputedStyle(this.DOM.digitCenter, null).getPropertyValue("height"));
this.setPos(0);
this.DOM.counter.onmousewheel = this.mouseWheel();
*/
}
Display.prototype.setValue = function(value){
if(typeof value=="number")
value = value.toString();
for(var i=0; i<this.DOM.segments.length; ++i){
var b = value.charAt(i);
if(b=="1")
this.DOM.segments[i].classList.add("on");
else
this.DOM.segments[i].classList.remove("on");
}
}
Display.prototype.getValue = function(value){
var byte=[];
for(var i=0; i<this.DOM.segments.length; ++i){
byte.push( this.DOM.segments[i].classList.contains("on")?1:0)
}
return byte.join('');
}
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function toLowerCamelCase(str) {
return str.replace(/[^A-Za-z0-9]/g, ' ').replace(/^\w|[A-Z]|\b\w|\s+/g, function (match, index) {
if (+match === 0 || match === '-' || match === '.' ) {
return ""; // or if (/\s+/.test(match)) for white spaces
}
return index === 0 ? match.toLowerCase() : match.toUpperCase();
});
}
function generateCode(){
var segmentsNum,
framesNum;
var name = animName.value.replace(/[^\w\d]+/g, " ");
name = toLowerCamelCase(name);
name = capitalizeFirstLetter(name);
if(!name){ alert("Type a name for the animation"); animName.focus(); return; }
var animation = anim.value;
var frames = animation.split(/\n/);
frames = frames.filter(function(line){
return line.trim()!=="";
});
frames = frames.map(function(segments){
var bytes = segments.split(/\s+/);
segmentsNum = bytes.length;
bytes = bytes.map(function(s){ return s?('B'+s):undefined; });
return bytes;
});
if(!segmentsNum){ alert("Add frames to the animation"); dispData.focus(); return; }
var code = "const byte animation"+name+"[]["+segmentsNum+"] = {\n";
frames = frames.map(function(bytes){
bytes = bytes.join(', ');
if(bytes) return " {"+bytes+"}";
});
code += frames.join(",\n");
code += '\n};';
outputCode.value = code;
outputCode.focus();
outputCode.select();
}
</script>
</body>
</html>