Newer
Older
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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"
/* USER CODE BEGIN Includes */
#include "led_driver.h"
#include "globals.h"
#include <string.h>
#include <math.h>
#include <stdlib.h>
/* USER CODE END Includes */
const uint8_t binary_si[10][11] =
{
{0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0,1,0,0},
{0,0,0,1,0,0,0,1,0,0,0},
{0,0,1,1,1,1,1,1,1,0,0},
{0,1,1,0,1,1,1,0,1,1,0},
{1,1,1,1,1,1,1,1,1,1,1},
{1,0,1,1,1,1,1,1,1,0,1},
{1,0,1,0,0,0,0,0,1,0,1},
{0,0,0,1,1,0,1,1,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0},
};
//#define NB_LEDS_PER_STRIP 60
//#define NB_LINES 20
inline uint32_t getRawOffset(uint32_t column,uint32_t line)
{
return 3*(column+line*NB_COLUMNS);
}
void putSprite(uint8_t *raw, uint8_t offsetx, uint8_t offsety, uint8_t *color)
{
uint32_t raw_offset;
uint32_t line, column;
for (line=0;line<10;line++)
{
for (column=0;column<11;column++)
{
if (binary_si[line][column]==1)
{
raw_offset = getRawOffset(column+offsetx, line+offsety);
raw[raw_offset++] = color[0];
raw[raw_offset++] = color[1];
raw[raw_offset++] = color[2];
}
}
}
}
uint8_t generate3RGBSpaceInvaders(uint8_t *raw)
{
uint8_t offsetx, offsety;
uint8_t color[3];
memset(raw,128,RAW_SIZE);
offsetx = 0;
offsety = 0;
color[0] = 255;
color[1] = 0;
color[2] = 0;
putSprite(raw, offsetx, offsety, color);
offsety = 10;
color[0] = 0;
color[1] = 255;
color[2] = 0;
putSprite(raw, offsetx, offsety, color);
offsetx = 20;
offsety = 0;
color[0] = 0;
color[1] = 255;
color[2] = 255;
putSprite(raw, offsetx, offsety, color);
offsety = 10;
color[0] = 0;
color[1] = 0;
color[2] = 255;
putSprite(raw, offsetx, offsety, color);
offsetx = 40;
offsety = 0;
color[0] = 255;
color[1] = 0;
color[2] = 255;
putSprite(raw, offsetx, offsety, color);
offsety = 10;
color[0] = 255;
color[1] = 255;
color[2] = 0;
putSprite(raw, offsetx, offsety, color);
return 0;
}
// cf file LogoFiltered2.raw & cfromraw.cpp
// from LogoElectrolab.png
// 20 x 20 pixels
// Exploite la symtrie gauche droite => 10x20
const uint8_t logo_electrolab[200] =
{
0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x0D, 0x1A, 0x28, 0x30,
0x09, 0x09, 0x09, 0x09, 0x0A, 0x1F, 0x50, 0x83, 0xA7, 0xB5,
0x09, 0x09, 0x09, 0x0C, 0x3B, 0x91, 0xD6, 0xEF, 0xEB, 0xE1,
0x09, 0x09, 0x0C, 0x4A, 0xB8, 0xEE, 0xD1, 0x93, 0x62, 0x4B,
0x09, 0x0A, 0x3F, 0xBB, 0xEF, 0xA9, 0x49, 0x17, 0x0A, 0x07,
0x09, 0x25, 0x9A, 0xF0, 0xA7, 0x33, 0x01, 0x01, 0x01, 0x01,
0x0F, 0x5B, 0xDF, 0xCE, 0x45, 0x0A, 0x00, 0xA4, 0xC5, 0xC5,
0x1F, 0x91, 0xF3, 0x8B, 0x15, 0x08, 0x00, 0x96, 0xF7, 0xF7,
0x30, 0xB5, 0xE9, 0x59, 0x09, 0x08, 0x00, 0x04, 0x0D, 0x0D,
0x39, 0xC3, 0xDC, 0x42, 0x08, 0x01, 0x00, 0x86, 0xAE, 0xAE,
0x38, 0xC2, 0xDD, 0x44, 0x08, 0x00, 0x00, 0x9F, 0xE8, 0xE8,
0x2E, 0xB1, 0xEB, 0x5F, 0x0A, 0x05, 0x00, 0x04, 0x0D, 0x0D,
0x1C, 0x88, 0xF2, 0x96, 0x19, 0x08, 0x00, 0x6A, 0x99, 0x99,
0x0D, 0x51, 0xD6, 0xD8, 0x54, 0x0C, 0x01, 0xB4, 0xEE, 0xEE,
0x09, 0x1D, 0x8A, 0xEE, 0xB9, 0x44, 0x02, 0x05, 0x0E, 0x0E,
0x09, 0x09, 0x32, 0xA9, 0xF0, 0xBE, 0x60, 0x24, 0x0F, 0x08,
0x09, 0x09, 0x0A, 0x39, 0xA3, 0xE9, 0xE1, 0xAE, 0x7F, 0x66,
0x09, 0x09, 0x09, 0x0A, 0x2B, 0x79, 0xC2, 0xE9, 0xF0, 0xEC,
0x09, 0x09, 0x09, 0x09, 0x09, 0x16, 0x3D, 0x6A, 0x8D, 0x9B,
0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x0A, 0x12, 0x1C, 0x22
};
void putSpriteEL(uint8_t *raw, uint8_t offsetx, uint8_t *color)
{
uint32_t raw_offset;
int line, column;
int j;
j=0;
for (line=0;line<20;line++)
{
for (column=0;column<20;column++)
{
// sequence j 0,1,2..9,9,8,7,...,1,0,0,1,2,...
if (column>=10)
j--;
raw_offset = getRawOffset(column+offsetx, line);
raw[raw_offset++] = (color[0]*logo_electrolab[j])>>8;
raw[raw_offset++] = (color[1]*logo_electrolab[j])>>8;
raw[raw_offset++] = (color[2]*logo_electrolab[j])>>8;
if (column<10)
j++;
}
}
}
// vert vert vert...
uint8_t generateLogoElectrolab(uint8_t *raw, uint8_t intensity)
{
uint8_t offsetx;
uint8_t color[3];
offsetx = 0;
color[0] = 0;
color[1] = intensity;
color[2] = 0;
putSpriteEL(raw, offsetx, color);
offsetx = 20;
color[0] = 0;
color[1] = intensity;
color[2] = 0;
putSpriteEL(raw, offsetx, color);
offsetx = 40;
color[0] = 0;
color[1] = intensity;
color[2] = 0;
putSpriteEL(raw, offsetx, color);
return 0;
}
float mysin[360];
float mycos[360];
void generateSinCosTable(void)
{
int i;
for (i=0;i<360;i++)
{
mysin[i] = sin(1.0*i*M_PI/180);
mycos[i] = cos(1.0*i*M_PI/180);
}
}
float myfsin(float x)
{
int i;
x = x*180.0f/M_PI;
i = fmod(x,360);
if (i>=360)
crash(CC_CODE_SINCOS);
if (i<0)
{
i+=360;
}
if (i<0)
crash(CC_CODE_SINCOS);
return mysin[i];
}
float myfcos(float x)
{
int i;
x = x*180.0f/M_PI;
i = fmod(x,360);
if (i>=360)
crash(CC_CODE_SINCOS);
if (i<0)
{
i+=360;
}
if (i<0)
crash(CC_CODE_SINCOS);
return mycos[i];
}
uint8_t generatePlasma(uint8_t *raw,float u_time, uint8_t plasma_type, uint8_t intensity)
{
uint32_t raw_offset;
uint32_t line, column;
float u_k[2];
float v_coords[2];
float v;
float c[2];
static uint8_t firstCall = 1;
uint8_t x;
uint8_t s1,s2,s4,c1;
if (firstCall)
{
generateSinCosTable();
firstCall = 0;
}
u_k[0] = u_k[1] = 4;
for (line=0;line<NB_LINES;line++)
{
v_coords[1] = 1.0f*line/NB_LINES;
for (column=0;column<NB_COLUMNS;column++)
{
v_coords[0] = 1.0f*column/NB_COLUMNS;
raw_offset = getRawOffset(column, line);
c[0] = u_k[0]*(v_coords[0] - 0.5f);
c[1] = u_k[1]*(v_coords[1] - 0.5f);
v = myfsin( c[0]+u_time);
v += myfsin((c[1]+u_time)*0.5f);
v += myfsin((c[0]+c[1]+u_time)*0.5f);
c[0] += u_k[0]*0.5f * myfsin(u_time*0.33333f);
c[1] += u_k[1]*0.5f * myfcos(u_time*0.5f);
v += myfsin(sqrt(c[0]*c[0]+c[1]*c[1]+1.0f)+u_time);
v = v*0.5f;
switch(plasma_type)
{
// type RGB
case 1:
case 4:
case 19:
case 20:
s1 = 127.5f*myfsin(M_PI*v)+127.5f;
s2 = 127.5f*myfsin(M_PI*v+2.0f/3.0f*M_PI)+127.5f;
s4 = 127.5f*myfsin(M_PI*v+4.0f/3.0f*M_PI)+127.5f;
break;
// type 2 couleurs pures
case 2:
case 16:
case 18:
// type 2 couleurs white
case 3:
case 12:
case 17:
case 14:
s1 = 127.5f*myfsin(M_PI*v)+127.5f;
c1 = 127.5f*myfcos(M_PI*v)+127.5f;
break;
case 13:
case 15:
c[0] = 0.5f+0.5f*myfsin(M_PI*v+4.0f/3.0f*M_PI);
break;
// 5
default :
break;
}
switch(plasma_type)
{
// RGB Type
case 1:
// RGB Type
raw[raw_offset++] = s1;
raw[raw_offset++] = s2;
raw[raw_offset++] = s4;
break;
case 19:
// RGB Type 2
raw[raw_offset++] = s2;
raw[raw_offset++] = s1;
raw[raw_offset++] = s4;
break;
case 20:
// RGB Type 3
raw[raw_offset++] = s4;
raw[raw_offset++] = s2;
raw[raw_offset++] = s1;
break;
// Yellow
case 4:
// White Yellow Type
raw[raw_offset++] = 255; //(0.5f+0.5f*myfsin(M_PI*v))*255;
raw[raw_offset++] = 255; //(0.5f+0.5f*myfsin(M_PI*v+2.0f/3.0f*M_PI))*255;
raw[raw_offset++] = s4;
break;
// 2 Couleurs pures
case 2:
// RED GREEN TYPE
raw[raw_offset++] = s1;
raw[raw_offset++] = c1;
raw[raw_offset++] = 0;
break;
case 16:
// VERT / BLEU
raw[raw_offset++] = 0;
raw[raw_offset++] = c1;
raw[raw_offset++] = s1;
break;
case 18:
// ROUGE / BLEU
raw[raw_offset++] = s1;
raw[raw_offset++] = 0;
raw[raw_offset++] = c1;
break;
// Mix White
case 3:
// YELLOW/MAGENTA/PINK/WHITE TYPE
raw[raw_offset++] = 255;
raw[raw_offset++] = c1;
raw[raw_offset++] = s1;
break;
case 12:
// CYAN/YELLOW/WHITE TYPE
raw[raw_offset++] = s1;
raw[raw_offset++] = 255;
raw[raw_offset++] = c1;
break;
case 17:
// MAGENTA/CYAN/WHITE
raw[raw_offset++] = s1;
raw[raw_offset++] = c1;
raw[raw_offset++] = 255;
break;
//
case 14:
// MAGENTA TYPE
raw[raw_offset++] = 255;
raw[raw_offset++] = s1;
raw[raw_offset++] = 255;
break;
case 5:
// BLACK AND WHITE
x = 127.5f*myfsin(M_PI*4.0f*v)+127.5f;
raw[raw_offset++] = x;
raw[raw_offset++] = x;
raw[raw_offset++] = x;
break;
case 13:
// CYAN TYPE
raw[raw_offset++] = (c[0]*c[0])*intensity;
raw[raw_offset++] = 255;
raw[raw_offset++] = 255;
break;
case 15:
// MAGENTA TYPE DEEP
raw[raw_offset++] = 255;
raw[raw_offset++] = (c[0]*c[0])*intensity;
raw[raw_offset++] = 255;
break;
default :
raw[raw_offset++] = 0x80;
raw[raw_offset++] = 0x80;
raw[raw_offset++] = 0x80;
break;
}
}
}
return 1;
}
uint8_t generateUniformColor(uint8_t *raw,uint8_t *color)
{
uint32_t p=0;
while (p<NB_PIXELS)
{
*raw++=color[0];
*raw++=color[1];
*raw++=color[2];
p++;
}
return 0;
}
uint8_t generateBlack(uint8_t *raw)
{
memset(raw,0,RAW_SIZE);
return 0;
}
uint8_t generateWhite(uint8_t *raw, uint8_t intensity)
{
memset(raw,intensity,RAW_SIZE);
return 0;
}
uint8_t generateYellow(uint8_t *raw, uint8_t intensity)
{
uint8_t color[3];
color[0] = color[1] = intensity;
color[2] = intensity>>2;
generateUniformColor(raw,color);
return 0;
}
/*
uint8_t generateYellow(uint8_t *raw, uint8_t intensity)
{
uint32_t p=0;
while (p<NB_PIXELS)
{
*raw++=intensity;
*raw++=intensity;
*raw++=intensity>>2;
p++;
}
return 0;
}
*/
// GAME OF LIFE CODE START
void myseed(float time)
{
srand(time);
}
uint8_t myrand(void)
{
return 2*rand()/RAND_MAX;
}
// well, ok, ram footprint could be / 8
// but that's ok, plenty of ram
static uint8_t imageff[2][NB_LINES][NB_COLUMNS];
void randfill(void)
{
int line, column;
for (line=0;line<NB_LINES;line++)
{
for (column=0;column<NB_COLUMNS;column++)
{
imageff[0][line][column] = (rand()>=RAND_MAX/2?1:0);
}
}
}
uint8_t get8brothers(uint8_t flip,int line,int column)
{
int cl,cr,lu,ld;
// manage rollover on borders
if (line==0)
lu = NB_LINES-1;
else
lu = line-1;
if (line == NB_LINES-1)
ld = 0;
else
ld = line +1;
if (column==0)
cl = NB_COLUMNS-1;
else
cl = column-1;
if (column==NB_COLUMNS-1)
cr = 0;
else
cr = column+1;
// addup brothers
return
imageff[flip][lu][cl] +
imageff[flip][lu][column] +
imageff[flip][lu][cr] +
imageff[flip][line][cl] +
imageff[flip][line][cr] +
imageff[flip][ld][cl] +
imageff[flip][ld][column] +
imageff[flip][ld][cr];
}
// not a good way to compare, best method is to compute a hash and compare the hashs on more that 2 or 3 cycles.
uint8_t compareGOLImages(void)
{
uint8_t *p0,*p1;
uint32_t p;
p0 = &imageff[0][0][0];
p1 = &imageff[1][0][0];
p = 0;
while (p<NB_PIXELS)
{
if ((*p0)!=(*p1))
return 0;
p0++;
p1++;
p++;
}
return 1;
}
uint8_t generateGameOfLife(uint8_t *raw, float time, uint8_t intensity)
{
static uint8_t initialized = 0;
static uint8_t flip, flop;
int line, column;
static float lastTime = 0;
uint8_t brothers;
uint32_t raw_offset;
static float prepareRestart;
static float autoRestart;
if (((!initialized)||(time-lastTime>2))||((prepareRestart!=0)&&(time-prepareRestart>10))||(time-autoRestart>60))
{
flip = 0;
flop = 1;
srand(time*20);
randfill();
initialized = 1;
prepareRestart = 0;
autoRestart = time;
lastTime = time;
}
else
{
if (time-lastTime<0.3f)
{
return 1;
}
flip = 1-flip;
flop = 1-flop;
}
// calcule de flip vers flop
for (line=0;line<NB_LINES;line++)
{
for (column=0;column<NB_COLUMNS;column++)
{
brothers = get8brothers(flip,line,column);
if (imageff[flip][line][column]==0)
{
if (brothers==3)
imageff[flop][line][column] = 1;
else
imageff[flop][line][column] = 0;
}
else if (imageff[flip][line][column]==1)
{
if ((brothers==2)||(brothers==3))
imageff[flop][line][column] = 1;
else
imageff[flop][line][column] = 0;
}
else
{
crash(0);
}
raw_offset = getRawOffset(column, line);
brothers = intensity*imageff[flop][line][column];
raw[raw_offset++] = brothers;
raw[raw_offset++] = brothers;
raw[raw_offset++] = brothers;
}
}
/*
if (compareGOLImages())
{
prepareRestart = time;
}
*/
lastTime = time;
return 1;
}
// GAME OF LIFE CODE END
// POWER LIMITER CODE START
#define MAX_LEDTUBE_LINE_POWER (NB_COLUMNS*3*POWER_LIMIT)
void powerLimiter(uint8_t *raw)
{
int line, column, i;
uint8_t *p;
uint16_t linePower;
uint16_t maxLinePower;
uint16_t ipowerReduction;
p = raw;
maxLinePower = 0;
for (line=0;line<NB_LINES;line++)
{
linePower = 0;
for (column=0;column<NB_COLUMNS;column++)
{
linePower+= p[0] + p[1] + p[2];
p+=3;
}
if (linePower>maxLinePower)
maxLinePower = linePower;
}
if (maxLinePower>MAX_LEDTUBE_LINE_POWER)
{
ipowerReduction = (uint16_t) (256.0f*MAX_LEDTUBE_LINE_POWER/maxLinePower);
p=raw;
i=RAW_SIZE;
do
{
*p = ((*p)*ipowerReduction) >> 8;
p++;
} while(--i);
}
}
// POWER LIMITER CODE END
uint8_t imageGenerator(uint32_t currentMode, uint8_t *raw, float time, uint8_t intensity)
{
uint8_t dynamic;
switch(currentMode)
{
case 1:
case 2:
case 3:
case 4:
case 5: dynamic = generatePlasma(raw,time,(uint8_t) currentMode, intensity); break;
case 6: dynamic = generate3RGBSpaceInvaders(raw); break;
case 7: dynamic = generateBlack(raw); break;
case 8: dynamic = generateWhite(raw, intensity); break;
case 9: dynamic = generateYellow(raw, intensity); break;
case 10: dynamic = generateLogoElectrolab(raw, intensity); break;
case 11: dynamic = generateGameOfLife(raw, time, intensity); break;
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
case 19:
case 20:
dynamic = generatePlasma(raw,time,(uint8_t) currentMode, intensity); break;
// case 21: dynamic = generateElabLogos(raw);break; // Pas beau
default:return 0;
}
#ifdef POWER_LIMITER
powerLimiter(raw);
#endif
return dynamic;
}