forked from thoralt/esp8266wordclock
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathledfunctions.cpp
More file actions
1352 lines (1242 loc) · 45.4 KB
/
ledfunctions.cpp
File metadata and controls
1352 lines (1242 loc) · 45.4 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
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
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ESP8266 Wordclock
// Copyright (C) 2016 Thoralt Franz, https://github.com/thoralt
//
// This module implements functions to manage the WS2812B LEDs. Two buffers contain
// color information with current state and fade target state and are updated by
// either simple set operations or integrated screensavers (matrix, stars, heart).
// Also contains part of the data and logic for the hourglass animation.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "ledfunctions.h"
//hallo
//---------------------------------------------------------------------------------------
#if 1 // variables
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
// global instance
//---------------------------------------------------------------------------------------
LEDFunctionsClass LED = LEDFunctionsClass();
//---------------------------------------------------------------------------------------
// variables in PROGMEM (mapping table, images)
//---------------------------------------------------------------------------------------
#include "hourglass_animation.h"
// This defines the LED output for different minutes
// param0 controls whether the hour has to be incremented for the given minutes
// param1 is the matching minimum minute count (inclusive)
// param2 is the matching maximum minute count (inclusive)
#if 1 // code folding minutes template
const std::vector<leds_template_t> LEDFunctionsClass::minutesTemplate =
{
{ 0, 0, 4,{ 106, 107, 108 } }, // UHR
{ 0, 5, 9,{ 7, 8, 9, 10, 34, 35, 36, 37 } }, // FüNF NACH
{ 0, 10, 14,{ 11, 12, 13, 14, 34, 35, 36, 37 } }, // ZEHN NACH
{ 0, 15, 19,{ 15, 16, 17, 18, 19, 20, 21, 34, 35, 36, 37 } }, // VIERTEL NACH
{ 0, 20, 24,{ 22, 23, 24, 25, 26, 27, 28, 34, 35, 36, 37 } }, // ZWANZIG NACH
{ 1, 25, 29,{ 7, 8, 9, 10, 30, 31, 32, 39, 40, 41, 42 } }, // FüNF VOR HALB
{ 1, 30, 34,{ 39, 40, 41, 42 } }, // HALB
{ 1, 35, 39,{ 7, 8, 9, 10, 34, 35, 36, 37, 39, 40, 41, 42 } }, // FüNF NACH HALB
{ 1, 40, 44,{ 22, 23, 24, 25, 26, 27, 28, 30, 31, 32 } }, // ZWANZIG VOR
{ 1, 45, 49,{ 15, 16, 17, 18, 19, 20, 21, 30, 31, 32 } }, // VIERTEL VOR
{ 1, 50, 54,{ 11, 12, 13, 14, 30, 31, 32 } }, // ZEHN VOR
{ 1, 55, 59,{ 7, 8, 9, 10, 30, 31, 32 } } // FüNF VOR
};
#endif
// This defines the LED output for different hours
// param0 deals with special cases:
// = 0: matches hour in param1 and param2
// = 1: matches hour in param1 and param2 whenever minute is < 5
// = 2: matches hour in param1 and param2 whenever minute is >= 5
// param1: hour to match
// param2: alternative hour to match
#if 1 // code folding hours template
const std::vector<leds_template_t> LEDFunctionsClass::hoursTemplate =
{
{ 0, 0, 12,{ 100, 101, 102, 103, 104 } }, // ZWÖLF
{ 1, 1, 13,{ 44, 45, 46 } }, // EIN
{ 2, 1, 13,{ 44, 45, 46, 47 } }, // EINS
{ 0, 2, 14,{ 49, 50, 51, 52 } }, // ZWEI
{ 0, 3, 15,{ 56, 57, 58, 59 } }, // DREI
{ 0, 4, 16,{ 61, 62, 63, 64 } }, // VIER
{ 0, 5, 17,{ 66, 67, 68, 69 } }, // FÜNF
{ 0, 6, 18,{ 71, 72, 73, 74, 75 } }, // SECHS
{ 0, 7, 19,{ 78, 79, 80, 81, 82, 83 } }, // SIEBEN
{ 0, 8, 20,{ 84, 85, 86, 87 } }, // ACHT
{ 0, 9, 21,{ 88, 89, 90, 91 } }, // NEUN
{ 0, 10, 22,{ 92, 93, 94, 95 } }, // ZEHN
{ 0, 11, 23,{ 96, 97, 98 } }, // ELF
};
#endif
#if 1 // code folding mapping table
// this mapping table maps the linear memory buffer structure used throughout the
// project to the physical layout of the LEDs
const uint32_t PROGMEM LEDFunctionsClass::mapping[NUM_PIXELS] = {
0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ,
21 , 20 , 19 , 18 , 17 , 16 , 15 , 14 , 13 , 12 , 11 ,
22 , 23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 , 31 , 32 ,
43 , 42 , 41 , 40 , 39 , 38 , 37 , 36 , 35 , 34 , 33 ,
44 , 45 , 46 , 47 , 48 , 49 , 50 , 51 , 52 , 53 , 54 ,
65 , 64 , 63 , 62 , 61 , 60 , 59 , 58 , 57 , 56 , 55 ,
66 , 67 , 68 , 69 , 70 , 71 , 72 , 73 , 74 , 75 , 76 ,
87 , 86 , 85 , 84 , 83 , 82 , 81 , 80 , 79 , 78 , 77 ,
88 , 89 , 90 , 91 , 92 , 93 , 94 , 95 , 96 , 97 , 98 ,
109 , 108 , 107 , 106 , 105 , 104 , 103 , 102 , 101 , 100 , 99 ,
110, 111, 112, 113
};
#endif
#if 1 // code folding brightness adjust tables
const uint32_t PROGMEM LEDFunctionsClass::brightnessCurveSelect[NUM_PIXELS] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0
/* 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1,
0, 1, 0, 1*/
};
const uint32_t PROGMEM LEDFunctionsClass::brightnessCurvesR[256 * NUM_BRIGHTNESS_CURVES] = {
// LED type 1, 1:1 mapping (neutral)
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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,
// LED type 2
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 5, 5, 5, 5, 5, 5, 7, 7, 7, 7,
7, 7, 9, 9, 9, 9, 11, 11, 11, 11, 13, 13, 13, 13, 15, 15, 15, 15,
17, 17, 17, 17, 19, 19, 21, 21, 23, 23, 25, 25, 27, 27, 29, 29, 31,
31, 33, 33, 35, 35, 37, 37, 39, 39, 41, 41, 43, 43, 45, 45, 47, 47,
49, 49, 50, 50, 53, 53, 53, 53, 55, 55, 57, 57, 59, 59, 61, 61, 63,
63, 65, 65, 67, 67, 69, 69, 71, 71, 73, 73, 75, 75, 77, 77, 79, 79,
81, 81, 83, 83, 83, 83, 85, 85, 87, 87, 89, 89, 91, 91, 93, 93, 95,
95, 97, 97, 99, 99, 101, 101, 103, 103, 105, 105, 107, 107, 109, 109,
111, 111, 113, 113, 115, 115, 115, 115, 117, 117, 119, 119, 121, 121,
123, 123, 125, 125, 127, 127, 130, 130, 132, 132, 134, 134, 136, 136,
138, 138, 140, 140, 142, 142, 144, 144, 144, 144, 146, 146, 148, 148,
150, 150, 152, 152, 153, 153, 155, 155, 156, 156, 158, 158, 160, 160,
162, 162, 164, 164, 166, 166, 168, 168, 170, 170, 172, 172, 174, 174,
176, 176, 178, 178, 180, 180, 182, 182, 184, 184, 184, 184, 186, 186,
188, 188, 190, 190, 192, 192, 194, 194, 196, 196, 198, 198, 200, 200,
202, 202, 204, 204, 206, 206, 208, 208, 210, 210, 212, 212, 214, 214,
216, 216, 218, 218
};
const uint32_t PROGMEM LEDFunctionsClass::brightnessCurvesG[256 * NUM_BRIGHTNESS_CURVES] = {
// LED type 1, 1:1 mapping (neutral)
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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,
// LED type 2
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 5, 5, 5, 5, 5, 5, 7, 7, 7, 7, 7,
7, 9, 9, 9, 9, 11, 11, 11, 11, 13, 13, 13, 13, 15, 15, 15, 15, 17,
17, 17, 17, 19, 19, 21, 21, 23, 23, 25, 25, 27, 27, 29, 29, 31, 31,
33, 33, 35, 35, 37, 37, 39, 39, 41, 41, 43, 43, 45, 45, 47, 47, 49,
49, 51, 51, 53, 53, 54, 54, 56, 56, 57, 57, 59, 59, 61, 61, 63, 63,
66, 66, 68, 68, 70, 70, 72, 72, 74, 74, 76, 76, 78, 78, 80, 80, 80,
80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80,
80, 80, 80, 80, 80, 80, 80, 80, 108, 108, 110, 110, 112, 112, 113,
113, 116, 116, 117, 117, 119, 119, 122, 122, 124, 124, 126, 126, 127,
127, 131, 131, 132, 132, 135, 135, 137, 137, 139, 139, 141, 141, 143,
143, 145, 145, 147, 147, 149, 149, 151, 151, 153, 153, 155, 155, 157,
157, 159, 159, 160, 160, 163, 163, 165, 165, 167, 167, 169, 169, 171,
171, 173, 173, 175, 175, 177, 177, 179, 179, 181, 181, 183, 183, 185,
185, 187, 187, 189, 189, 191, 191, 194, 194, 197, 197, 198, 198, 201,
201, 203, 203, 205, 205, 208, 208, 210, 210, 212, 212, 215, 215, 218,
218, 220, 220, 222, 222, 224, 224, 226, 226, 228, 228, 230, 230, 232,
232, 234, 234
};
const uint32_t PROGMEM LEDFunctionsClass::brightnessCurvesB[256 * NUM_BRIGHTNESS_CURVES] = {
// LED type 1, 1:1 mapping (neutral)
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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,
// LED type 2
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 5, 5, 5, 5, 5, 5, 7, 7, 7, 7, 9,
9, 9, 9, 9, 9, 11, 11, 11, 11, 13, 13, 13, 13, 15, 15, 15, 15, 17,
17, 19, 19, 20, 20, 21, 21, 23, 23, 25, 25, 27, 27, 29, 29, 31, 31,
33, 33, 35, 35, 37, 37, 39, 39, 41, 41, 42, 42, 43, 43, 45, 45, 47,
47, 49, 49, 51, 51, 53, 53, 55, 55, 57, 57, 59, 59, 61, 61, 63, 63,
64, 64, 66, 66, 68, 68, 72, 72, 72, 72, 74, 74, 76, 76, 78, 78, 80,
80, 82, 82, 84, 84, 86, 86, 88, 88, 90, 90, 91, 91, 93, 93, 94, 94,
96, 96, 98, 98, 100, 100, 102, 102, 104, 104, 106, 106, 108, 108,
110, 110, 112, 112, 114, 114, 116, 116, 118, 118, 119, 119, 121, 121,
123, 123, 124, 124, 127, 127, 129, 129, 131, 131, 133, 133, 135, 137,
137, 139, 139, 141, 141, 143, 143, 145, 145, 147, 147, 149, 149, 151,
151, 153, 153, 155, 155, 157, 157, 160, 160, 161, 161, 163, 163, 166,
166, 168, 168, 170, 170, 171, 171, 174, 174, 176, 176, 178, 178, 180,
180, 183, 183, 184, 184, 187, 187, 188, 188, 191, 191, 194, 194, 196,
196, 198, 198, 200, 200, 202, 202, 204, 204, 206, 206, 209, 209, 210,
210, 212, 212, 215, 215, 217, 217, 219, 219, 222, 222, 224, 224, 226,
226, 228, 228, 230
};
#endif
#endif
//---------------------------------------------------------------------------------------
#if 1 // getters, setters, data flow
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
// ~LEDFunctionsClass
//
// Destructor
//
// -> --
// <- --
//---------------------------------------------------------------------------------------
LEDFunctionsClass::~LEDFunctionsClass()
{
// for(MatrixObject* m : this->matrix_objects) delete m;
}
//---------------------------------------------------------------------------------------
// LEDFunctionsClass
//
// Constructor, initializes data structures
//
// -> --
// <- --
//---------------------------------------------------------------------------------------
LEDFunctionsClass::LEDFunctionsClass()
{
// initialize matrix objects with random coordinates
for (int i = 0; i < NUM_MATRIX_OBJECTS; i++)
{
this->matrix.push_back(MatrixObject());
}
// initialize star objects with default coordinates
for (int i = 0; i < NUM_STARS; i++) this->stars.push_back(StarObject());
// set random coordinates with minimum distance to other star objects
for (StarObject& s : this->stars) s.randomize(this->stars);
}
//---------------------------------------------------------------------------------------
// begin
//
// Initializes the LED driver
//
// -> pin: hardware pin to use for WS2812B data output
// <- --
//---------------------------------------------------------------------------------------
void LEDFunctionsClass::begin(int pin)
{
this->pixels = new Adafruit_NeoPixel(NUM_PIXELS, pin, NEO_GRB + NEO_KHZ800);
this->pixels->begin();
}
//---------------------------------------------------------------------------------------
// process
//
// Drives internal data flow, should be called repeatedly from main loop()
//
// -> --
// <- --
//---------------------------------------------------------------------------------------
void LEDFunctionsClass::process()
{
if (Config.debugMode) return;
// check time values against boundaries
if (this->h > 23 || this->h < 0) this->h = 0;
if (this->m > 59 || this->m < 0) this->m = 0;
if (this->s > 59 || this->s < 0) this->s = 0;
if (this->ms > 999 || this->ms < 0) this->ms = 0;
// load palette colors from configuration
palette_entry palette[] = {
{ Config.bg.r, Config.bg.g, Config.bg.b },
{ Config.fg.r, Config.fg.g, Config.fg.b },
{ Config.s.r, Config.s.g, Config.s.b }
};
uint8_t buf[NUM_PIXELS];
switch (this->mode)
{
case DisplayMode::wifiManager:
this->renderWifiManager();
break;
case DisplayMode::yellowHourglass:
this->renderHourglass(Config.hourglassState, false);
break;
case DisplayMode::greenHourglass:
this->renderHourglass(Config.hourglassState, true);
break;
case DisplayMode::update:
this->renderUpdate();
break;
case DisplayMode::updateComplete:
this->renderUpdateComplete();
break;
case DisplayMode::updateError:
this->renderUpdateError();
break;
case DisplayMode::red:
this->renderRed();
break;
case DisplayMode::green:
this->renderGreen();
break;
case DisplayMode::blue:
this->renderBlue();
break;
case DisplayMode::flyingLettersVerticalUp:
case DisplayMode::flyingLettersVerticalDown:
this->renderFlyingLetters();
break;
case DisplayMode::explode:
this->renderExplosion();
break;
case DisplayMode::matrix:
this->renderMatrix();
break;
case DisplayMode::heart:
this->renderHeart();
break;
case DisplayMode::stars:
this->renderStars();
break;
case DisplayMode::fade:
this->renderTime(buf, this->h, this->m, this->s, this->ms);
this->set(buf, palette, false);
this->fade();
break;
case DisplayMode::random: // TODO: Implement random display mode
case DisplayMode::plain:
default:
this->renderTime(buf, this->h, this->m, this->s, this->ms);
this->set(buf, palette, true);
break;
}
// transfer this->currentValues to LEDs
this->show();
}
//---------------------------------------------------------------------------------------
// setBrightness
//
// Sets the brightness for the WS2812 values. Will be multiplied with each color
// component when sending data to WS2812.
//
// -> brightness: [0...256]
// <- --
//---------------------------------------------------------------------------------------
void LEDFunctionsClass::setBrightness(int brightness)
{
this->brightness = brightness;
}
//---------------------------------------------------------------------------------------
// setTime
//
// Sets the time which will be used to drive the LED matrix. The internal time is _not_
// updated, this function must be called repeatedly if the time changes.
//
// -> h, m, s, ms: Time in hours, minutes, seconds, milliseconds
// <- --
//---------------------------------------------------------------------------------------
void LEDFunctionsClass::setTime(int h, int m, int s, int ms)
{
this->h = h;
this->m = m;
this->s = s;
this->ms = ms;
}
//---------------------------------------------------------------------------------------
// setMode
//
// Sets the display mode to one of the members of the DisplayMode enum and thus changes
// what will be shown on the display during the next calls of LEDFunctionsClass.process()
//
// -> newMode: mode to be set
// <- --
//---------------------------------------------------------------------------------------
void LEDFunctionsClass::setMode(DisplayMode newMode)
{
uint8_t buf[NUM_PIXELS];
DisplayMode previousMode = this->mode;
this->mode = newMode;
// if we changed to an animated letters mode, then start animation
// even if the current time did not yet change
if (newMode != previousMode &&
(newMode == DisplayMode::flyingLettersVerticalUp ||
newMode == DisplayMode::flyingLettersVerticalDown))
{
this->renderTime(buf, this->h, this->m, this->s, this->ms);
this->prepareFlyingLetters(buf);
}
// if we changed to exploding letters mode, then start animation
// even if the current time did not yet change
if (newMode != previousMode && newMode == DisplayMode::explode)
{
this->renderTime(buf, this->h, this->m, this->s, this->ms);
this->prepareExplosion(buf);
}
this->process();
}
//---------------------------------------------------------------------------------------
// set
//
// Sets the internal LED buffer to new values based on an indexed source buffer and an
// associated palette. Does not display colors immediately, fades to new colors instead.
//
// Attention: If buf is PROGMEM, make sure it is aligned at 32 bit and its size is
// a multiple of 4 bytes!
//
// -> buf: indexed source buffer
// palette: color definition for source buffer
// <- --
//---------------------------------------------------------------------------------------
void LEDFunctionsClass::set(const uint8_t *buf, palette_entry palette[])
{
this->set(buf, palette, false);
}
//---------------------------------------------------------------------------------------
// set
//
// Sets the internal LED buffer to new values based on an indexed source buffer and an
// associated palette.
//
// Attention: If buf is PROGMEM, make sure it is aligned at 32 bit and its size is
// a multiple of 4 bytes!
//
// -> buf: indexed source buffer
// palette: color definition for source buffer
// immediately: if true, display buffer immediately; fade to new colors if false
// <- --
//---------------------------------------------------------------------------------------
void LEDFunctionsClass::set(const uint8_t *buf, palette_entry palette[],
bool immediately)
{
this->setBuffer(this->targetValues, buf, palette);
if (immediately)
{
this->setBuffer(this->currentValues, buf, palette);
}
}
//---------------------------------------------------------------------------------------
// setBuffer
//
// Fills a buffer (e. g. this->targetValues) with color data based on indexed source
// pixels and a palette. Pays attention to 32 bit boundaries, so use with PROGMEM is
// safe.
//
// -> target: color buffer, e. g. this->targetValues or this->currentValues
// source: buffer with color indexes
// palette: colors for indexed source buffer
// <- --
//---------------------------------------------------------------------------------------
void LEDFunctionsClass::setBuffer(uint8_t *target, const uint8_t *source,
palette_entry palette[])
{
uint32_t mapping, palette_index, curveOffset;
// cast source to 32 bit pointer to ensure 32 bit aligned access
uint32_t *buf = (uint32_t*)source;
// this holds the current 4 bytes
uint32_t currentDWord;
// this is a pointer to the current 4 bytes for access as single bytes
uint8_t *currentBytes = (uint8_t*)¤tDWord;
// this counts bytes from 0...3
uint32_t byteCounter = 0;
for (int i = 0; i < NUM_PIXELS; i++)
{
// get next 4 bytes
if (byteCounter == 0) currentDWord = buf[i >> 2];
palette_index = currentBytes[byteCounter];
mapping = LEDFunctionsClass::mapping[i] * 3;
curveOffset = LEDFunctionsClass::brightnessCurveSelect[i] << 8;
// select color value using palette and brightness correction curves
target[mapping + 0] = brightnessCurvesR[curveOffset + palette[palette_index].r];
target[mapping + 1] = brightnessCurvesG[curveOffset + palette[palette_index].g];
target[mapping + 2] = brightnessCurvesB[curveOffset + palette[palette_index].b];
byteCounter = (byteCounter + 1) & 0x03;
}
}
//---------------------------------------------------------------------------------------
// fade
//
// Fade one step of the color values from this->currentValues[i] to
// this->targetValues[i]. Uses non-linear fade speed depending on distance to target
// value.
//
// -> --
// <- --
//---------------------------------------------------------------------------------------
void LEDFunctionsClass::fade()
{
static int prescaler = 0;
if (++prescaler < 2) return;
prescaler = 0;
int delta;
for (int i = 0; i < NUM_PIXELS * 3; i++)
{
delta = this->targetValues[i] - this->currentValues[i];
if (delta > 64) this->currentValues[i] += 8;
else if (delta > 16) this->currentValues[i] += 4;
else if (delta > 0) this->currentValues[i]++;
else if (delta < -64) this->currentValues[i] -= 8;
else if (delta < -16) this->currentValues[i] -= 4;
else if (delta < 0) this->currentValues[i]--;
}
}
//---------------------------------------------------------------------------------------
// show
//
// Internal method, copies this->currentValues to WS2812 object while applying brightness
//
// -> --
// <- --
//---------------------------------------------------------------------------------------
void LEDFunctionsClass::show()
{
uint8_t *data = this->currentValues;
int ofs = 0;
// copy current color values to LED object and display it
for (int i = 0; i < NUM_PIXELS; i++)
{
this->pixels->setPixelColor(i,
pixels->Color(((int)data[ofs + 0] * this->brightness) >> 8,
((int)data[ofs + 1] * this->brightness) >> 8,
((int)data[ofs + 2] * this->brightness) >> 8));
ofs += 3;
}
this->pixels->show();
}
//---------------------------------------------------------------------------------------
// getOffset
//
// Calculates the offset of a given RGB triplet inside the LED buffer.
// Does range checking for x and y, uses the internal mapping table.
//
// -> x: x coordinate
// y: y coordinate
// <- offset for given coordinates
//---------------------------------------------------------------------------------------
int LEDFunctionsClass::getOffset(int x, int y)
{
if (x >= 0 && y >= 0 && x < LEDFunctionsClass::width && y < LEDFunctionsClass::height)
{
return LEDFunctionsClass::mapping[x + y * LEDFunctionsClass::width] * 3;
}
else
{
return 0;
}
}
//---------------------------------------------------------------------------------------
// fillBackground
//
// Initializes the buffer with either background (=0) or seconds progress (=2),
// part of the background will be illuminated with color 2 depending on current
// seconds/milliseconds value, whole screen is backlit when seconds = 59
//
// -> seconds, milliseconds: Time value which the fill process will base on
// buf: destination buffer
// <- --
//---------------------------------------------------------------------------------------
void LEDFunctionsClass::fillBackground(int seconds, int milliseconds, uint8_t *buf)
{
int pos = (((seconds * 1000 + milliseconds) * 110) / 60000) + 1;
for (int i = 0; i < NUM_PIXELS; i++) buf[i] = (i < pos) ? 2 : 0;
}
#endif
//---------------------------------------------------------------------------------------
#if 1 // rendering methods
//---------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------
// renderTime
//
// Loads internal buffers with the current time representation
//
// -> target: If NULL, a local buffer is used and time is actually being displayed
// if not null, time is not being displayed, but the given buffer is being
// filled with palette indexes representing the time
// <- --
//---------------------------------------------------------------------------------------
void LEDFunctionsClass::renderTime(uint8_t *target, int h, int m, int s, int ms)
{
this->fillBackground(s, ms, target);
// set static LEDs
target[0] = 1; // E
target[1] = 1; // S
target[3] = 1; // I
target[4] = 1; // S
target[5] = 1; // T
// minutes 1...4 for the corners
for (int i = 0; i <= ((m % 5) - 1); i++) target[10 * 11 + i] = 1;
// iterate over minutes_template
int adjust_hour = 0;
for (leds_template_t t : LEDFunctionsClass::minutesTemplate)
{
// test if this template matches the current minute
if (m >= t.param1 && m <= t.param2)
{
// set all LEDs defined in this template
for (int i : t.LEDs) target[i] = 1;
adjust_hour = t.param0;
break;
}
}
// adjust hour display if necessary (e. g. 09:45 = quarter to *TEN* instead of NINE)
h += adjust_hour;
if (h > 23) h -= 24;
// iterate over hours template
for (leds_template_t t : LEDFunctionsClass::hoursTemplate)
{
// test if this template matches the current hour
if ((t.param1 == h || t.param2 == h) &&
((t.param0 == 1 && m < 5) || // special case full hour
(t.param0 == 2 && m >= 5) || // special case hour + minutes
(t.param0 == 0))) // normal case
{
// set all LEDs defined in this template
for (int i : t.LEDs) target[i] = 1;
break;
}
}
// DEBUG
static int last_minutes = -1;
if (last_minutes != this->m)
{
last_minutes = this->m;
Serial.printf("h=%i, m=%i, s=%i\r\n", this->h, this->m, this->s);
for (int y = 0; y < 10; y++)
{
for (int x = 0; x < 11; x++)
{
Serial.print(target[y * 11 + x]);
Serial.print(' ');
}
Serial.println(' ');
}
}
}
//---------------------------------------------------------------------------------------
// renderHourglass
//
// Immediately displays an animation step of the hourglass animation.
// ATTENTION: Animation frames must start at 32 bit boundary each!
//
// -> animationStep: Number of current frame [0...HOURGLASS_ANIMATION_FRAMES]
// green: Flag to switch the palette color 3 to green instead of yellow (used in the
// second half of the hourglass animation to indicate the short wait-for-OTA
// window)
// <- --
//---------------------------------------------------------------------------------------
void LEDFunctionsClass::renderHourglass(uint8_t animationStep, bool green)
{
// colors in palette: black, white, yellow
palette_entry p[] = { { 0, 0, 0 },{ 255, 255, 255 },{ 255, 255, 0 },{ 255, 255, 0 } };
// delete red component in palette entry 3 to make this color green
if (green) p[3].r = 0;
if (animationStep >= HOURGLASS_ANIMATION_FRAMES) animationStep = 0;
this->set(hourglass_animation[animationStep], p, true);
}
//---------------------------------------------------------------------------------------
// renderMatrix
//
// Renders one frame of the matrix animation and displays it immediately
//
// -> --
// <- --
//---------------------------------------------------------------------------------------
void LEDFunctionsClass::renderMatrix()
{
// clear buffer
memset(this->currentValues, 0, sizeof(this->currentValues));
// sort by y coordinate for correct overlapping
std::sort(matrix.begin(), matrix.end());
// iterate over all matrix objects, move and render them
for (MatrixObject &m : this->matrix) m.render(this->currentValues);
}
//---------------------------------------------------------------------------------------
// renderStars
//
// Renders one frame of the stars animation and displays it immediately
//
// -> --
// <- --
//---------------------------------------------------------------------------------------
void LEDFunctionsClass::renderStars()
{
// clear buffer
memset(this->currentValues, 0, sizeof(this->currentValues));
for (StarObject &s : this->stars) s.render(this->currentValues, this->stars);
}
//---------------------------------------------------------------------------------------
// renderHeart
//
// Renders one frame of the heart animation and displays it immediately
//
// -> --
// <- --
//---------------------------------------------------------------------------------------
void LEDFunctionsClass::renderHeart()
{
palette_entry palette[2];
uint8_t heart[] = {
0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0,
1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0,
0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0,
0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1
};
palette[0] = { 0, 0, 0 };
palette[1] = { (uint8_t)this->heartBrightness, 0, 0 };
this->set(heart, palette, true);
switch (this->heartState)
{
case 0:
if (this->heartBrightness >= 255) this->heartState = 1;
else this->heartBrightness += 32;
break;
case 1:
if (this->heartBrightness < 128) this->heartState = 2;
else this->heartBrightness -= 32;
break;
case 2:
if (this->heartBrightness >= 255) this->heartState = 3;
else this->heartBrightness += 32;
break;
case 3:
default:
if (this->heartBrightness <= 0) this->heartState = 0;
else this->heartBrightness -= 4;
break;
}
if (this->heartBrightness > 255) this->heartBrightness = 255;
if (this->heartBrightness < 0) this->heartBrightness = 0;
}
//---------------------------------------------------------------------------------------
// prepareExplosion
//
// Prepare particles based on current screen state
//
//
// -> source: buffer to read the currently active LEDs from
// <- --
//---------------------------------------------------------------------------------------
void LEDFunctionsClass::prepareExplosion(uint8_t *source)
{
#define PARTICLE_COUNT 16
#define PARTICLE_SPEED 0.15f
float vx, vy, angle;
int ofs = 0;
Particle *p;
int delay;
// compute angle increment
float angle_increment = 2.0f * 3.141592654f / (float)(PARTICLE_COUNT);
// iterate over every position in the screen buffer
for (int y = 0; y < LEDFunctionsClass::height; y++)
{
for (int x = 0; x < LEDFunctionsClass::width; x++)
{
// create entry in particles vector if current pixel is foreground
if (source[ofs++] == 1)
{
// add a random delay of zero to approx. 3 seconds to each
// explosion
delay = random(300);
// start with angle of zero radians, assign velocity vector
// placed on a circle to each particle
angle = 0;
for (int i = 0; i < PARTICLE_COUNT; i++)
{
// calculate particle speed vector based on angle and
// absolute speed value
vx = PARTICLE_SPEED * sin(angle);
vy = PARTICLE_SPEED * cos(angle);
// create new particle and add it to particles list
p = new Particle(x, y, vx, vy, delay);
this->particles.push_back(p);
angle += angle_increment;
}
}
}
}
}
//---------------------------------------------------------------------------------------
// renderExplosion
//
// Renders the exploding letters animation
//
// -> --
// <- --
//---------------------------------------------------------------------------------------
void LEDFunctionsClass::renderExplosion()
{
std::vector<Particle*> particlesToKeep;
uint8_t buf[NUM_PIXELS];
// load palette colors from configuration
palette_entry palette[] = {
{ Config.bg.r, Config.bg.g, Config.bg.b },
{ Config.fg.r, Config.fg.g, Config.fg.b },
{ Config.s.r, Config.s.g, Config.s.b }
};
// check if the displayed time has changed
if ((this->m / 5 != this->lastM / 5) || (this->h != this->lastH))
{
// prepare new animation with old time
this->renderTime(buf, this->lastH, this->lastM, 0, 0);
this->prepareExplosion(buf);
}
this->lastM = this->m;
this->lastH = this->h;
// create empty buffer filled with seconds color
this->fillBackground(this->s, this->ms, buf);
// minutes 1...4 for the corners
for (int i = 0; i <= ((this->m % 5) - 1); i++) buf[10 * 11 + i] = 1;
// Do we have something to explode?
if (this->particles.size() > 0)
{
// transfer background created by fillBackground to target buffer
this->set(buf, palette, true);
// iterate over all particles
for (Particle *p : this->particles)
{
// move and render current particle
p->render(this->currentValues, palette);
// if particle is still active, keep it; kill it otherwise
if (p->alive) particlesToKeep.push_back(p); else delete p;
}
// only keep active particles, discard the rest
// -> use particlesToKeep as new list
this->particles.swap(particlesToKeep);
}
else
{
// present the current time in boring mode with simple fading
this->renderTime(buf, this->h, this->m, this->s, this->ms);
this->set(buf, palette, false);
this->fade();
}
}
//---------------------------------------------------------------------------------------
// prepareFlyingLetters
//
// Sets the current buffer as target state for flying letters, initializes current
// positions of the letters below the visible area with some random jitter
//
// -> source: buffer to read the currently active LEDs from
// <- --
//---------------------------------------------------------------------------------------
void LEDFunctionsClass::prepareFlyingLetters(uint8_t *source)
{
// transfer the previous flying letters in the leaving letters vector to prepare for
// outgoing animation
this->leavingLetters.clear();
for (xy_t &p : this->arrivingLetters)
{
// delay every letter depending on its position
// and set new target coordinate
if (this->mode == DisplayMode::flyingLettersVerticalUp)
{
p.delay = p.y * 2 + p.x + 1 + random(5);
p.yTarget = -1;
}
else
{
p.delay = (LEDFunctionsClass::height - p.y - 1) * 2 + p.x + 1 + random(5);
p.yTarget = LEDFunctionsClass::height;
}
this->leavingLetters.push_back(p);
}
// initialize arriving letters from scratch
this->arrivingLetters.clear();
int ofs = 0;
// iterate over every position in the screen buffer
for (int y = 0; y < LEDFunctionsClass::height; y++)
{