-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathleddisplay_test.c
911 lines (802 loc) · 31.7 KB
/
leddisplay_test.c
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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
/* *********************************************************************************************** */
/* leddisplay test
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
/* *********************************************************************************************** */
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <esp_log.h>
#include <soc/rtc.h>
#include <esp_timer.h>
#include <leddisplay.h>
#include "mon.h"
#include "nyan_64x32.h"
/* *********************************************************************************************** */
// local logging
#define LOGNAME "leddisplay_test"
#define ERROR(fmt, ...) ESP_LOGE(LOGNAME, fmt, ## __VA_ARGS__)
#define WARNING(fmt, ...) ESP_LOGW(LOGNAME, fmt, ## __VA_ARGS__)
#define INFO(fmt, ...) ESP_LOGI(LOGNAME, fmt, ## __VA_ARGS__)
#define DEBUG(fmt, ...) ESP_LOGD(LOGNAME, fmt, ## __VA_ARGS__)
#define TRACE(fmt, ...) ESP_LOGV(LOGNAME, fmt, ## __VA_ARGS__)
// useful macros
#define MS2TICKS(ms) ((ms) / portTICK_PERIOD_MS)
#define TICKS2MS(ticks) ((ticks) * portTICK_PERIOD_MS)
#define osTicks() xTaskGetTickCount()
#define osSleep(ms) vTaskDelay(MS2TICKS(ms));
#define osDelayUntil(prev, inc) vTaskDelayUntil(prev, MS2TICKS(inc));
#define NUMOF(x) (sizeof(x)/sizeof(*(x)))
/* *********************************************************************************************** */
// forward declarations
static void sLeddisplayTestTask(void *pParam);
static void sHsvToRgb(const uint8_t hue, const uint8_t sat, const uint8_t val, uint8_t *red, uint8_t *green, uint8_t *blue);
static void sLedfxConcentricHueFlow(leddisplay_frame_t *pFrame, const bool init, const int8_t step, uint8_t *r0);
static void sLedfxPlasma(leddisplay_frame_t *pFrame, const bool init, float *r0);
static void sAnimNyan(leddisplay_frame_t *p_frame, uint32_t delay, int frame);
static void sTicTocInit(const uint32_t reg, const char *name);
static void sTic(const uint32_t reg);
static void sToc(const uint32_t reg);
static void sTicTocStats(const uint32_t reg);
static void sDumpMemInfo(void);
void app_main(void)
{
monStart();
//monSetPeriod(1000);
osSleep(2000);
printf("\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n");
INFO("Hello, hello, good morning, good evening!");
rtc_cpu_freq_config_t cpuCfg;
rtc_clk_cpu_freq_get_config(&cpuCfg);
DEBUG("CPU: %uMHz", cpuCfg.freq_mhz);
BaseType_t res = xTaskCreatePinnedToCore(
sLeddisplayTestTask, "leddisplay_test", 8192 / sizeof(StackType_t), NULL, 10, NULL, 1);
assert(res == pdPASS);
}
static void sLeddisplayTestTask(void *pParam)
{
INFO("testing start...");
const uint32_t delay = 100;
static leddisplay_frame_t sDispFrame;
// run tests forever
while (true)
{
/* ***** initialise display ************************************************************** */
INFO("init display");
sDumpMemInfo();
if (leddisplay_init()!= ESP_OK)
{
ERROR(":-(");
osSleep(2000);
continue;
}
sDumpMemInfo();
/* ***** pixel based API ***************************************************************** */
INFO("ghosting test 1 (pixel)");
{
const int oldBrightness = leddisplay_set_brightness(100);
for (uint16_t x = 0; x < LEDDISPLAY_WIDTH; x++)
{
leddisplay_pixel_xy_rgb(x, 0, 255, 255, 255);
}
leddisplay_pixel_update(0);
osSleep(20 * delay);
leddisplay_set_brightness(oldBrightness);
}
INFO("ghosting test 2 (pixel)");
{
const int oldBrightness = leddisplay_set_brightness(100);
for (uint16_t xy = 0; xy < LEDDISPLAY_HEIGHT; xy++)
{
leddisplay_pixel_xy_rgb(xy, xy, 255, 255, 255);
}
leddisplay_pixel_update(0);
osSleep(20 * delay);
leddisplay_set_brightness(oldBrightness);
}
// -----------------------------------------------------------------------------------------
INFO("fill (pixel)");
for (int which = 1; which <= 7; which++)
{
DEBUG("which=%d (%c%c%c)", which,
(which & 0x1) != 0 ? 'R' : '.',
(which & 0x2) != 0 ? 'G' : '.',
(which & 0x4) != 0 ? 'B' : '.');
for (uint16_t x = 0; x < LEDDISPLAY_WIDTH; x++)
{
for (uint16_t y = 0; y < LEDDISPLAY_HEIGHT; y++)
{
leddisplay_pixel_xy_rgb(x, y,
(which & 0x1) != 0 ? 255 : 0,
(which & 0x2) != 0 ? 255 : 0,
(which & 0x4) != 0 ? 255 : 0);
}
}
leddisplay_pixel_update(0);
osSleep(20 * delay);
}
// -----------------------------------------------------------------------------------------
INFO("fade (pixel)");
for (int which = 1; which <= 7; which++)
{
DEBUG("which=%d (%c%c%c)", which,
(which & 0x1) != 0 ? 'R' : '.',
(which & 0x2) != 0 ? 'G' : '.',
(which & 0x4) != 0 ? 'B' : '.');
const float maxDist = sqrtf( (float)( (LEDDISPLAY_WIDTH - 1) * (LEDDISPLAY_WIDTH - 1)) +
(float)( (LEDDISPLAY_HEIGHT - 1) * (LEDDISPLAY_HEIGHT - 1)) );
for (uint16_t x = 0; x < LEDDISPLAY_WIDTH; x++)
{
for (uint16_t y = 0; y < LEDDISPLAY_HEIGHT; y++)
{
const float dist = sqrtf( (float)(x * x) + (float)(y * y) );
const float r = floorf( 1.0f + (254.0f * dist / maxDist) + 0.5 );
const uint8_t rgb = r;
leddisplay_pixel_xy_rgb(x, y,
(which & 0x1) != 0 ? rgb : 0,
(which & 0x2) != 0 ? rgb : 0,
(which & 0x4) != 0 ? rgb : 0);
}
}
leddisplay_pixel_update(0);
osSleep(20 * delay);
}
// -----------------------------------------------------------------------------------------
INFO("some pixels (pixel)");
leddisplay_pixel_fill_rgb(0, 0, 0);
leddisplay_pixel_xy_rgb(1, 2, 255, 0, 0);
leddisplay_pixel_xy_rgb(3, 4, 0, 255, 0);
leddisplay_pixel_xy_rgb(5, 6, 0, 0, 255);
leddisplay_pixel_update(0);
osSleep(10 * delay);
// -----------------------------------------------------------------------------------------
INFO("individual pixels (pixel)");
{
int n = 25;
sTicTocInit(0, "setpixel");
sTicTocInit(1, "update");
uint8_t hue = 0;
while (n > 0)
{
uint8_t red = 0, green = 0, blue = 0;
sHsvToRgb(hue, 255, 255, &red, &green, &blue);
sTic(0);
for (uint16_t y = 0; y < LEDDISPLAY_HEIGHT; y++)
{
for (uint16_t x = 0; x < LEDDISPLAY_WIDTH; x++)
{
leddisplay_pixel_xy_rgb(x, y, red, green, blue);
}
}
sToc(0);
sTic(1);
leddisplay_pixel_update(0);
sToc(1);
n--;
hue += 10;
osSleep(2 * delay);
}
sTicTocStats(0);
sTicTocStats(1);
}
// -----------------------------------------------------------------------------------------
INFO("clear pixels (pixel)");
leddisplay_pixel_fill_rgb(0, 0, 0);
leddisplay_pixel_update(0);
osSleep(10 * delay);
// -----------------------------------------------------------------------------------------
INFO("hue flow fx (pixel)");
{
uint32_t now = osTicks();
uint8_t r0;
const uint8_t step = 4;
sLedfxConcentricHueFlow(NULL, true, 0, &r0);
leddisplay_pixel_update(0);
osDelayUntil(&now, delay);
int n = 256 / step;
while (n > 0)
{
sLedfxConcentricHueFlow(NULL, false, step, &r0);
leddisplay_pixel_update(0);
osDelayUntil(&now, delay);
n--;
}
}
// -----------------------------------------------------------------------------------------
INFO("plasma fx (pixel)");
{
float r0;
sLedfxPlasma(NULL, true, &r0);
leddisplay_pixel_update(0);
osSleep(10);
int n = 70;
sTicTocInit(0, "plasma");
sTicTocInit(1, "update");
while (n > 0)
{
sTic(0);
sLedfxPlasma(NULL, false, &r0);
sToc(0);
sTic(1);
leddisplay_pixel_update(0);
sToc(1);
osSleep(10);
n--;
}
sTicTocStats(0);
sTicTocStats(1);
}
// -----------------------------------------------------------------------------------------
INFO("animation (pixel)");
{
int n = 7;
while (n > 0)
{
sAnimNyan(NULL, delay, -1);
n--;
}
}
// -----------------------------------------------------------------------------------------
INFO("animation + brightness (pixel)");
{
const int oldBrightness = leddisplay_get_brightness();
const int delta = 2;
int n = 100 * 2 / delta;
int aniFrame = 0;
int brightness = 0;
int dir = delta;
while (n > 0)
{
leddisplay_set_brightness(brightness);
sAnimNyan(NULL, 2 * delay, aniFrame);
n--;
aniFrame++;
aniFrame %= 12;
brightness += dir;
if (brightness >= 100)
{
dir = -delta;
brightness -= 2 * delta;
}
else if (brightness <= 0)
{
dir = +delta;
brightness += 2 * delta;
}
}
leddisplay_set_brightness(oldBrightness);
}
/* ***** frame based API ***************************************************************** */
INFO("ghosting test 1 (frame)");
{
const int oldBrightness = leddisplay_set_brightness(100);
leddisplay_frame_fill_rgb(&sDispFrame, 0, 0, 0);
for (uint16_t x = 0; x < LEDDISPLAY_WIDTH; x++)
{
leddisplay_frame_xy_rgb(&sDispFrame, x, 0, 255, 255, 255);
}
leddisplay_frame_update(&sDispFrame);
leddisplay_set_brightness(oldBrightness);
osSleep(20 * delay);
}
INFO("ghosting test 2 (frame)");
{
const int oldBrightness = leddisplay_set_brightness(100);
leddisplay_frame_fill_rgb(&sDispFrame, 0, 0, 0);
for (uint16_t xy = 0; xy < LEDDISPLAY_HEIGHT; xy++)
{
leddisplay_frame_xy_rgb(&sDispFrame, xy, xy, 255, 255, 255);
}
leddisplay_frame_update(&sDispFrame);
leddisplay_set_brightness(oldBrightness);
osSleep(20 * delay);
}
// -----------------------------------------------------------------------------------------
INFO("fill (frame)");
for (int which = 1; which <= 7; which++)
{
DEBUG("which=%d (%c%c%c)", which,
(which & 0x1) != 0 ? 'R' : '.',
(which & 0x2) != 0 ? 'G' : '.',
(which & 0x4) != 0 ? 'B' : '.');
for (uint16_t x = 0; x < LEDDISPLAY_WIDTH; x++)
{
for (uint16_t y = 0; y < LEDDISPLAY_HEIGHT; y++)
{
leddisplay_frame_xy_rgb(&sDispFrame, x, y,
(which & 0x1) != 0 ? 255 : 0,
(which & 0x2) != 0 ? 255 : 0,
(which & 0x4) != 0 ? 255 : 0);
}
}
leddisplay_frame_update(&sDispFrame);
osSleep(20 * delay);
}
// -----------------------------------------------------------------------------------------
INFO("fade (frame)");
for (int which = 1; which <= 7; which++)
{
DEBUG("which=%d (%c%c%c)", which,
(which & 0x1) != 0 ? 'R' : '.',
(which & 0x2) != 0 ? 'G' : '.',
(which & 0x4) != 0 ? 'B' : '.');
const float maxDist = sqrtf( (float)( (LEDDISPLAY_WIDTH - 1) * (LEDDISPLAY_WIDTH - 1)) +
(float)( (LEDDISPLAY_HEIGHT - 1) * (LEDDISPLAY_HEIGHT - 1)) );
for (uint16_t x = 0; x < LEDDISPLAY_WIDTH; x++)
{
for (uint16_t y = 0; y < LEDDISPLAY_HEIGHT; y++)
{
const float dist = sqrtf( (float)(x * x) + (float)(y * y) );
const float r = floorf( 1.0f + (254.0f * dist / maxDist) + 0.5 );
const uint8_t rgb = r;
leddisplay_frame_xy_rgb(&sDispFrame, x, y,
(which & 0x1) != 0 ? rgb : 0,
(which & 0x2) != 0 ? rgb : 0,
(which & 0x4) != 0 ? rgb : 0);
}
}
leddisplay_frame_update(&sDispFrame);
osSleep(20 * delay);
}
// -----------------------------------------------------------------------------------------
INFO("fade colour and brightness (frame)");
{
const int oldBrightness = leddisplay_get_brightness();
const float maxDist = sqrtf( (float)( (LEDDISPLAY_WIDTH - 1) * (LEDDISPLAY_WIDTH - 1)) +
(float)( (LEDDISPLAY_HEIGHT - 1) * (LEDDISPLAY_HEIGHT - 1)) );
for (int which = 1; which <= 7; which++)
{
DEBUG("which=%d (%c%c%c)", which,
(which & 0x1) != 0 ? 'R' : '.',
(which & 0x2) != 0 ? 'G' : '.',
(which & 0x4) != 0 ? 'B' : '.');
for (int brightness = 1; brightness <= 100; brightness++)
{
leddisplay_set_brightness(brightness);
for (uint16_t x = 0; x < LEDDISPLAY_WIDTH; x++)
{
for (uint16_t y = 0; y < LEDDISPLAY_HEIGHT; y++)
{
const float dist = sqrtf( (float)(x * x) + (float)(y * y) );
const float r = floorf( 1.0f + (254.0f * dist / maxDist) + 0.5 );
const uint8_t rgb = r;
leddisplay_frame_xy_rgb(&sDispFrame, x, y,
(which & 0x1) != 0 ? rgb : 0,
(which & 0x2) != 0 ? rgb : 0,
(which & 0x4) != 0 ? rgb : 0);
}
}
leddisplay_frame_update(&sDispFrame);
osSleep(delay / 5);
}
}
leddisplay_set_brightness(oldBrightness);
}
// -----------------------------------------------------------------------------------------
INFO("some pixels (frame)");
leddisplay_frame_fill_rgb(&sDispFrame, 0, 0, 0);
leddisplay_frame_xy_rgb(&sDispFrame, 1, 2, 255, 0, 0);
leddisplay_frame_xy_rgb(&sDispFrame, 3, 4, 0, 255, 0);
leddisplay_frame_xy_rgb(&sDispFrame, 5, 6, 0, 0, 255);
leddisplay_frame_update(&sDispFrame);
osSleep(10 * delay);
// -----------------------------------------------------------------------------------------
INFO("individual pixels (frame)");
{
int n = 25;
sTicTocInit(0, "setpixel");
sTicTocInit(1, "update");
uint8_t hue = 0;
while (n > 0)
{
uint8_t red = 0, green = 0, blue = 0;
sHsvToRgb(hue, 255, 255, &red, &green, &blue);
sTic(0);
for (uint16_t y = 0; y < LEDDISPLAY_HEIGHT; y++)
{
for (uint16_t x = 0; x < LEDDISPLAY_WIDTH; x++)
{
leddisplay_frame_xy_rgb(&sDispFrame, x, y, red, green, blue);
}
}
sToc(0);
sTic(1);
leddisplay_frame_update(&sDispFrame);
sToc(1);
n--;
hue += 10;
osSleep(2 * delay);
}
sTicTocStats(0);
sTicTocStats(1);
}
// -----------------------------------------------------------------------------------------
INFO("clear pixels (frame)");
leddisplay_frame_clear(&sDispFrame);
leddisplay_frame_update(&sDispFrame);
osSleep(10 * delay);
// -----------------------------------------------------------------------------------------
INFO("hue flow fx (frame)");
{
uint32_t now = osTicks();
uint8_t r0;
const uint8_t step = 4;
sLedfxConcentricHueFlow(&sDispFrame, true, 0, &r0);
leddisplay_frame_update(&sDispFrame);
osDelayUntil(&now, delay);
int n = 256 / step;
while (n > 0)
{
sLedfxConcentricHueFlow(&sDispFrame, false, step, &r0);
leddisplay_frame_update(&sDispFrame);
osDelayUntil(&now, delay);
n--;
}
}
// -----------------------------------------------------------------------------------------
INFO("plasma fx (frame)");
{
float r0;
sLedfxPlasma(&sDispFrame, true, &r0);
leddisplay_frame_update(&sDispFrame);
osSleep(10);
int n = 70;
sTicTocInit(0, "plasma");
sTicTocInit(1, "update");
while (n > 0)
{
sTic(0);
sLedfxPlasma(&sDispFrame, false, &r0);
sToc(0);
sTic(1);
leddisplay_frame_update(&sDispFrame);
sToc(1);
osSleep(10);
n--;
}
sTicTocStats(0);
sTicTocStats(1);
}
// -----------------------------------------------------------------------------------------
INFO("animation (frame)");
{
int n = 7;
while (n > 0)
{
sAnimNyan(&sDispFrame, delay, -1);
n--;
}
}
// -----------------------------------------------------------------------------------------
INFO("animation + brightness (frame)");
{
const int oldBrightness = leddisplay_get_brightness();
const int delta = 2;
int n = 100 * 2 / delta;
int aniFrame = 0;
int brightness = 0;
int dir = delta;
while (n > 0)
{
leddisplay_set_brightness(brightness);
sAnimNyan(&sDispFrame, 2 * delay, aniFrame);
n--;
aniFrame++;
aniFrame %= 12;
brightness += dir;
if (brightness >= 100)
{
dir = -delta;
brightness -= 2 * delta;
}
else if (brightness <= 0)
{
dir = +delta;
brightness += 2 * delta;
}
}
leddisplay_set_brightness(oldBrightness);
}
// -----------------------------------------------------------------------------------------
INFO("random fill (frame)");
{
int n = 200;
while (n--)
{
esp_fill_random(&sDispFrame, sizeof(sDispFrame));
leddisplay_frame_update(&sDispFrame);
osSleep(delay / 10);
n--;
}
}
/* ***** other ************************************************************************** */
INFO("test frame refresh rate");
for (int dummy = 0; dummy < 5; dummy++)
{
int n = 100;
sTicTocInit(0, "maxrefresh");
while (n > 0)
{
sTic(0);
const uint8_t rgb = (n % 2) == 0 ? 50 : 100;
// assuming this is faster than the frame rate..
leddisplay_frame_fill_rgb(&sDispFrame, rgb, rgb, rgb);
// ..and this will block for a moment
leddisplay_frame_update(&sDispFrame);
sToc(0);
n--;
}
leddisplay_frame_fill_rgb(&sDispFrame, 0, 0, 0);
leddisplay_frame_update(&sDispFrame);
sTicTocStats(0);
}
/* ***** shutdown display *************************************************************** */
INFO("shutdown display");
sDumpMemInfo();
leddisplay_shutdown();
sDumpMemInfo();
printf("\r\n\r\n\r\n");
osSleep(5000);
}
}
/* *********************************************************************************************** */
// classic HSV2RGB code (#HSV2RGB_METHOD 1 and 2) à la Wikipedia
static void sHsvToRgb(const uint8_t hue, const uint8_t sat, const uint8_t val, uint8_t *red, uint8_t *green, uint8_t *blue)
{
const uint32_t s = (6 * (uint32_t)hue) >> 8; /* the segment 0..5 (360/60 * [0..255] / 256) */
const uint32_t t = (6 * (uint32_t)hue) & 0xff; /* within the segment 0..255 (360/60 * [0..255] % 256) */
const uint32_t l = ((uint32_t)val * (255 - (uint32_t)sat)) >> 8; /* lower level */
const uint32_t r = ((uint32_t)val * (uint32_t)sat * t) >> 16; /* ramp */
switch (s)
{
case 0: *red = (uint8_t)val; *green = (uint8_t)(l + r); *blue = (uint8_t)l; break;
case 1: *red = (uint8_t)(val - r); *green = (uint8_t)val; *blue = (uint8_t)l; break;
case 2: *red = (uint8_t)l; *green = (uint8_t)val; *blue = (uint8_t)(l + r); break;
case 3: *red = (uint8_t)l; *green = (uint8_t)(val - r); *blue = (uint8_t)val; break;
case 4: *red = (uint8_t)(l + r); *green = (uint8_t)l; *blue = (uint8_t)val; break;
case 5: *red = (uint8_t)val; *green = (uint8_t)l; *blue = (uint8_t)(val - r); break;
}
}
static void sLedfxConcentricHueFlow(leddisplay_frame_t *pFrame, const bool init, const int8_t step, uint8_t *r0)
{
if (init)
{
*r0 = 0;
}
else
{
(*r0) += step;
}
const int16_t x0 = LEDDISPLAY_WIDTH / 2;
const int16_t y0 = LEDDISPLAY_HEIGHT / 2;
const int16_t hueMax = 256/2;
const uint8_t sat = 255;
const uint8_t val = 255;
int16_t dX = x0 + 1;
while (dX--)
{
int16_t dY = y0 + 1;
while (dY--)
{
const uint8_t hue = (uint8_t)(((dX*dX) + (dY*dY))
* hueMax / ((x0*x0) + (y0*y0))) + *r0;
//DEBUG("ledfxConcentricHueFlow() %2i %2i %2i %2i %3u -> %2i %2i %2i %2i %2i %2i %2i %2i",
// dx, dy, X0, Y0, hue,
// X0 + dx, dy, X0 - dx, dy, dx, Y0 + dy, dx, Y0 - dy);
uint8_t red = 0, green = 0, blue = 0;
sHsvToRgb(hue, sat, val, &red, &green, &blue);
if (pFrame == NULL)
{
leddisplay_pixel_xy_rgb(x0 + dX, y0 + dY, red, green, blue);
leddisplay_pixel_xy_rgb(x0 - dX, y0 + dY, red, green, blue);
leddisplay_pixel_xy_rgb(x0 + dX, y0 - dY, red, green, blue);
leddisplay_pixel_xy_rgb(x0 - dX, y0 - dY, red, green, blue);
}
else
{
leddisplay_frame_xy_rgb(pFrame, x0 + dX, y0 + dY, red, green, blue);
leddisplay_frame_xy_rgb(pFrame, x0 - dX, y0 + dY, red, green, blue);
leddisplay_frame_xy_rgb(pFrame, x0 + dX, y0 - dY, red, green, blue);
leddisplay_frame_xy_rgb(pFrame, x0 - dX, y0 - dY, red, green, blue);
}
}
}
}
// The formulas used in this function are based on code floating the internet in various code
// (google "colorduino", "shiftbrite", et al.). While the original source is never properly
// referenced it all seems to be attributed to: copyright (c) 2011 Sam C. Lin, 2009 Ben Combee,
// 2009 Ken Corey, 2008 Windell H. Oskay \todo find original reference for this
static inline float sDist(const float a, const float b, const float c, const float d)
{
const float cma = c - a;
const float dmb = d - b;
return sqrtf( (cma * cma) + (dmb * dmb) );
}
static void sLedfxPlasma(leddisplay_frame_t *pFrame, const bool init, float *r0)
{
const uint8_t sat = 255;
const uint8_t val = 255;
if (init)
{
*r0 = (float)(esp_random() % 128000);
}
for (uint16_t y = 0; y < LEDDISPLAY_HEIGHT; y++)
{
for (uint16_t x = 0; x < LEDDISPLAY_WIDTH; x++)
{
// based on source: see progPlasma() docu
const float value =
sinf( sDist((float)x + *r0, y, 128.0f, 128.0) * (1.0f / 8.0f)) +
sinf( sDist(x, y, 64.0f, 64.0f) * (1.0f / 8.0f) ) +
sinf( sDist(x, (float)y + (*r0 / 7.0f), 192.0f, 64.0f) * (1.0f / 7.0f) ) +
sinf( sDist(x, y, 192.0f, 100.0f) * (1.0 / 8.0) );
const uint8_t hue = (uint8_t)((uint32_t)(value * 128.0) & 0xff);
uint8_t red = 0, green = 0, blue = 0;
sHsvToRgb(hue, sat, val, &red, &green, &blue);
if (pFrame == NULL)
{
leddisplay_pixel_xy_rgb(x, y, red, green, blue);
}
else
{
leddisplay_frame_xy_rgb(pFrame, x, y, red, green, blue);
}
}
}
(*r0) -= 0.25; // smooth (the original code used -= 1)
}
/* *********************************************************************************************** */
typedef struct anim_frame_s
{
uint8_t yx[32][64][3];
} anim_frame_t;
static void sAnimNyan(leddisplay_frame_t *p_frame, uint32_t delay, int frame)
{
// get animation frame
int nFrames;
const uint8_t *animData = get_nyan_64x32(&nFrames);
//DEBUG("test %s delay=%u frame=%d nFrames=%d brightness=%d",
// p_frame == NULL ? "pixel" : "frame", delay, frame, nFrames, leddisplay_get_brightness());
// clear display
leddisplay_pixel_fill_rgb(0, 0, 0);
// which frames to play?
int startFrame = (frame < 0) || (frame >= nFrames) ? 0 : frame;
int endFrame = (frame < 0) || (frame >= nFrames) ? (nFrames - 1) : frame;
// offset animation if display is smaller than the animation
const int16_t nxOffs = LEDDISPLAY_WIDTH < 64 ? (64 - LEDDISPLAY_WIDTH) / 2 : 0;
const int16_t nyOffs = LEDDISPLAY_HEIGHT < 32 ? (32 - LEDDISPLAY_HEIGHT) / 2 : 0;
// play each frame
uint32_t prevTick = xTaskGetTickCount();
for (frame = startFrame; frame <= endFrame; frame++)
{
// copy frame data to display buffer
const anim_frame_t *data = (const anim_frame_t *)&animData[64 * 32 * 3 * frame];
// pixel based
if (p_frame == NULL)
{
for (uint16_t x = 0; x < LEDDISPLAY_WIDTH; x++)
{
for (uint16_t y = 0; y < LEDDISPLAY_HEIGHT; y++)
{
const uint8_t *rgb = data->yx[(nyOffs + y) % 32][(nxOffs + x) % 64];
leddisplay_pixel_xy_rgb(x, y, rgb[0], rgb[1], rgb[2]);
}
}
}
// frame based
else
{
for (uint16_t x = 0; x < LEDDISPLAY_WIDTH; x++)
{
for (uint16_t y = 0; y < LEDDISPLAY_HEIGHT; y++)
{
const uint8_t *rgb = data->yx[(nyOffs + y) % 32][(nxOffs + x) % 64];
leddisplay_frame_xy_rgb(p_frame, x, y, rgb[0], rgb[1], rgb[2]);
}
}
}
// flush display buffer to display
if (p_frame == NULL)
{
leddisplay_pixel_update(true);
}
else
{
leddisplay_frame_update(p_frame);
}
// delay
if (delay > 0)
{
vTaskDelayUntil(&prevTick, delay / portTICK_PERIOD_MS);
//vTaskDelay();
}
}
}
/* *********************************************************************************************** */
typedef struct TICTOC_s
{
const char *name;
uint32_t last;
uint32_t meas[100];
uint32_t ix;
} TICTOC_t;
static TICTOC_t sTicToc[5];
static void sTicTocInit(const uint32_t reg, const char *name)
{
if (reg > (NUMOF(sTicToc) - 1))
{
return;
}
memset(&sTicToc[reg], 0, sizeof(sTicToc[reg]));
sTicToc[reg].name = name;
}
static void sTic(const uint32_t reg)
{
if (reg > (NUMOF(sTicToc) - 1))
{
return;
}
sTicToc[reg].last = esp_timer_get_time();
}
static void sToc(const uint32_t reg)
{
if (reg > (NUMOF(sTicToc) - 1))
{
return;
}
sTicToc[reg].meas[ sTicToc[reg].ix ] = esp_timer_get_time() - sTicToc[reg].last;
sTicToc[reg].ix++;
sTicToc[reg].ix %= NUMOF(sTicToc[reg].meas);
}
static void sTicTocStats(const uint32_t reg)
{
if (reg > (NUMOF(sTicToc) - 1))
{
return;
}
uint64_t sum = 0;
uint32_t n = 0;
uint32_t min = UINT32_MAX;
uint32_t max = 0;
for (int ix = 0; ix < NUMOF(sTicToc[reg].meas); ix++)
{
if (sTicToc[reg].meas[ix] > 0)
{
n++;
sum += sTicToc[reg].meas[ix];
if (sTicToc[reg].meas[ix] < min) { min = sTicToc[reg].meas[ix]; }
if (sTicToc[reg].meas[ix] > max) { max = sTicToc[reg].meas[ix]; }
}
}
if (n > 0)
{
uint32_t avg = sum / n;
double rate = 1000000.0 / (double)avg;
DEBUG("tictoc[%u]=%s: n=%u, avg=%u, rate=%.1f, min=%u, max=%u",
reg, sTicToc[reg].name == NULL ? "???" : sTicToc[reg].name,
n, avg, rate, min, max);
}
else
{
DEBUG("tictoc[%u]=%s: no meas",
reg, sTicToc[reg].name == NULL ? "???" : sTicToc[reg].name);
}
memset(&sTicToc[reg], 0, sizeof(sTicToc[reg]));
}
/* *********************************************************************************************** */
static void sDumpMemInfo(void)
{
DEBUG("heap: EXEC free=%u (min=%u, largest=%u), 32BIT free=%u (min=%u, largest=%u), 8BIT free=%u (min=%u, largest=%u), DMA free=%u (min=%u, largest=%u)",
heap_caps_get_free_size(MALLOC_CAP_EXEC), heap_caps_get_minimum_free_size(MALLOC_CAP_EXEC), heap_caps_get_largest_free_block(MALLOC_CAP_EXEC),
heap_caps_get_free_size(MALLOC_CAP_32BIT), heap_caps_get_minimum_free_size(MALLOC_CAP_32BIT), heap_caps_get_largest_free_block(MALLOC_CAP_32BIT),
heap_caps_get_free_size(MALLOC_CAP_8BIT), heap_caps_get_minimum_free_size(MALLOC_CAP_8BIT), heap_caps_get_largest_free_block(MALLOC_CAP_8BIT),
heap_caps_get_free_size(MALLOC_CAP_DMA), heap_caps_get_minimum_free_size(MALLOC_CAP_DMA), heap_caps_get_largest_free_block(MALLOC_CAP_DMA));
}
/* *********************************************************************************************** */