-
Notifications
You must be signed in to change notification settings - Fork 4
/
LedStrip.h
1825 lines (1469 loc) · 43.9 KB
/
LedStrip.h
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
//
// Led Strip configuration for TexTime
//
#include "fonts.h"
#include "textime.h"
#define NROW 10
#define NCOL 11
#define NEDGE 4
#define pVOID Pixel()
#define pBLACK Pixel(RgbColor(0 , 0, 0))
#define pRED Pixel(RgbColor(255, 0, 0))
#define pGREEN Pixel(RgbColor(0 , 255, 0))
#define pBLUE Pixel(RgbColor(0 , 0 , 255))
#define pWHITE Pixel(RgbColor(255, 255, 255))
//typedef NeoPixelBrightnessBus<NeoGrbFeature, NeoEsp8266AsyncUart1Ws2813Method> MyNeoPixelBrightnessBus;
typedef NeoPixelBrightnessBus<NeoGrbFeature, NeoEsp8266Uart1Ws2813Method> MyNeoPixelBrightnessBus;
class Pixel
{
public:
RgbColor color;
bool display;
Pixel()
: color(RgbColor(0, 0, 0))
, display(false)
{
}
Pixel(const RgbColor &c)
{
color = c;
display = true;
}
};
class PixelsArray {
private:
Pixel _pixels[NROW][NCOL];
public:
PixelsArray()
{
clear();
}
void fill(const Pixel &p)
{
for (int i = 0; i < NROW; i++)
{
for (int j = 0; j < NCOL; j++)
{
_pixels[i][j] = p;
}
}
}
void clear()
{
fill(pVOID);
}
void setPixel(const Pixel &p, int row, int col)
{
if (row < 0) return;
if (col < 0) return;
if (row > NROW - 1) return;
if (col > NCOL - 1) return;
_pixels[row][col] = p;
}
Pixel getPixel(int row, int col)
{
if (row < 0) return pVOID;
if (col < 0) return pVOID;
if (row > NROW - 1) return pVOID;
if (col > NCOL - 1) return pVOID;
return _pixels[row][col];
}
};
struct PixelsContainer
{
PixelsArray pixelsArray;
Pixel pixelsEdge[NEDGE];
bool hasChanged;
};
class LedConfiguration {
public:
virtual int ledsByPixelForMatrix() = 0;
virtual int ledsByPixelForEdges() = 0;
virtual int ledsNumber() = 0;
virtual String getName() = 0;
virtual const uint8_t *getLedsMatrixId(int row, int col) = 0;
virtual const uint8_t *getLedsEdgeId(int n) = 0;
virtual ~LedConfiguration() {}
};
class LedConfiguration40x40 : public LedConfiguration {
private:
const uint8_t _matchingPixelsMatrix[NROW][NCOL][1] = {
{ { 0 }, { 1 }, { 5 }, { 6 }, { 14 }, { 15 }, { 27 }, { 28 }, { 44 }, { 45 }, { 64 } },
{ { 2 }, { 4 }, { 7 }, { 13 }, { 16 }, { 26 }, { 29 }, { 43 }, { 46 }, { 63 }, { 65 } },
{ { 3 }, { 8 }, { 12 }, { 17 }, { 25 }, { 30 }, { 42 }, { 47 }, { 62 }, { 66 }, { 81 } },
{ { 9 }, { 11 }, { 18 }, { 24 }, { 31 }, { 41 }, { 48 }, { 61 }, { 67 }, { 80 }, { 82 } },
{ { 10 }, { 19 }, { 23 }, { 32 }, { 40 }, { 49 }, { 60 }, { 68 }, { 79 }, { 83 }, { 94 } },
{ { 20 }, { 22 }, { 33 }, { 39 }, { 50 }, { 59 }, { 69 }, { 78 }, { 84 }, { 93 }, { 95 } },
{ { 21 }, { 34 }, { 38 }, { 51 }, { 58 }, { 70 }, { 77 }, { 85 }, { 92 }, { 96 }, { 103 } },
{ { 35 }, { 37 }, { 52 }, { 57 }, { 71 }, { 76 }, { 86 }, { 91 }, { 97 }, { 102 }, { 104 } },
{ { 36 }, { 53 }, { 56 }, { 72 }, { 75 }, { 87 }, { 90 }, { 98 }, { 101 }, { 105 }, { 108 } },
{ { 54 }, { 55 }, { 73 }, { 74 }, { 88 }, { 89 }, { 99 }, { 100 }, { 106 }, { 107 }, { 109 } }
};
const uint8_t _matchingPixelsEdge[NEDGE][1] = { { 112 }, { 111 }, { 110 }, { 113 } };
public:
virtual String getName()
{
return "40x40@1";
}
int ledsByPixelForMatrix()
{
return 1;
}
int ledsByPixelForEdges()
{
return 1;
}
int ledsNumber()
{
return (NROW * NCOL) + NEDGE;
}
const uint8_t *getLedsMatrixId(int row, int col)
{
if (row < 0) return NULL;
if (col < 0) return NULL;
if (row > NROW - 1) return NULL;
if (col > NCOL - 1) return NULL;
return _matchingPixelsMatrix[row][col];
}
const uint8_t *getLedsEdgeId(int n)
{
if (n < 0) return NULL;
if (n > NEDGE - 1) return NULL;
return _matchingPixelsEdge[n];
}
};
class LedConfiguration100x100_1 : public LedConfiguration {
private:
const uint8_t _matchingPixelsMatrix[NROW][NCOL][1] = {
{ { 21 }, { 19 }, { 17 }, { 15 }, { 13 }, { 11 }, { 9 }, { 7 }, { 5 }, { 3 }, { 1 } },
{ { 24 }, { 26 }, { 28 }, { 30 }, { 32 }, { 34 }, { 36 }, { 38 }, { 40 }, { 42 }, { 44 } },
{ { 67 }, { 65 }, { 63 }, { 61 }, { 59 }, { 57 }, { 55 }, { 53 }, { 51 }, { 49 }, { 47 } },
{ { 70 }, { 72 }, { 74 }, { 76 }, { 78 }, { 80 }, { 82 }, { 84 }, { 86 }, { 88 }, { 90 } },
{ { 113 }, { 111 }, { 109 }, { 107 }, { 105 }, { 103 }, { 101 }, { 99 }, { 97 }, { 95 }, { 93 } },
{ { 116 }, { 118 }, { 120 }, { 122 }, { 124 }, { 126 }, { 128 }, { 130 }, { 132 }, { 134 }, { 136 } },
{ { 159 }, { 157 }, { 155 }, { 153 }, { 151 }, { 149 }, { 147 }, { 145 }, { 143 }, { 141 }, { 139 } },
{ { 162 }, { 164 }, { 166 }, { 168 }, { 170 }, { 172 }, { 174 }, { 176 }, { 178 }, { 180 }, { 182 } },
{ { 205 }, { 203 }, { 201 }, { 199 }, { 197 }, { 195 }, { 193 }, { 191 }, { 189 }, { 187 }, { 185 } },
{ { 208 }, { 210 }, { 212 }, { 214 }, { 216 }, { 218 }, { 220 }, { 222 }, { 224 }, { 226 }, { 228 } }
};
const uint8_t _matchingPixelsEdge[NEDGE][1] = { { 232 }, { 231 }, { 230 }, { 233 } };
public:
virtual String getName()
{
return "100x100@1";
}
int ledsByPixelForMatrix()
{
return 1;
}
int ledsByPixelForEdges()
{
return 1;
}
int ledsNumber()
{
return (NROW * 23) + NEDGE;
}
const uint8_t *getLedsMatrixId(int row, int col)
{
if (row < 0) return NULL;
if (col < 0) return NULL;
if (row > NROW - 1) return NULL;
if (col > NCOL - 1) return NULL;
return _matchingPixelsMatrix[row][col];
}
const uint8_t *getLedsEdgeId(int n)
{
if (n < 0) return NULL;
if (n > NEDGE - 1) return NULL;
return _matchingPixelsEdge[n];
}
};
class LedConfiguration100x100_2 : public LedConfiguration {
private:
const uint8_t _matchingPixelsMatrix[NROW][NCOL][2] = {
{ { 21 , 22 }, { 19 , 20 }, { 17 , 18 }, { 15 , 16 }, { 13 , 14 }, { 11 , 12 }, { 9 , 10 }, { 7 , 8 }, { 5 , 6 }, { 3 , 4 }, { 1 , 2 } },
{ { 25 , 26 }, { 27 , 28 }, { 29 , 30 }, { 31 , 32 }, { 33 , 34 }, { 35 , 36 }, { 37 , 38 }, { 39 , 40 }, { 41 , 42 }, { 43 , 44 }, { 45 , 46 } },
{ { 69 , 70 }, { 67 , 68 }, { 65 , 66 }, { 63 , 64 }, { 61 , 62 }, { 59 , 60 }, { 57 , 58 }, { 55 , 56 }, { 53 , 54 }, { 51 , 52 }, { 49 , 50 } },
{ { 73 , 74 }, { 75 , 76 }, { 77 , 78 }, { 79 , 80 }, { 81 , 82 }, { 83 , 84 }, { 85 , 86 }, { 87 , 88 }, { 89 , 90 }, { 91 , 92 }, { 93 , 94 } },
{ { 117, 118 }, { 115, 116 }, { 113, 114 }, { 111, 112 }, { 109, 110 }, { 107, 108 }, { 105, 106 }, { 103, 104 }, { 101, 102 }, { 99 , 100 }, { 97 , 98 } },
{ { 121, 122 }, { 123, 124 }, { 125, 126 }, { 127, 128 }, { 129, 130 }, { 131, 132 }, { 133, 134 }, { 135, 136 }, { 137, 138 }, { 139, 140 }, { 141, 142 } },
{ { 165, 166 }, { 163, 164 }, { 161, 162 }, { 159, 160 }, { 157, 158 }, { 155, 156 }, { 153, 154 }, { 151, 152 }, { 149, 150 }, { 147, 148 }, { 145, 146 } },
{ { 169, 170 }, { 171, 172 }, { 173, 174 }, { 175, 176 }, { 177, 178 }, { 179, 180 }, { 181, 182 }, { 183, 184 }, { 185, 186 }, { 187, 188 }, { 189, 190 } },
{ { 213, 214 }, { 211, 212 }, { 209, 210 }, { 207, 208 }, { 205, 206 }, { 203, 204 }, { 201, 202 }, { 199, 200 }, { 197, 198 }, { 195, 196 }, { 193, 194 } },
{ { 217, 218 }, { 219, 220 }, { 221, 222 }, { 223, 224 }, { 225, 226 }, { 227, 228 }, { 229, 230 }, { 231, 232 }, { 233, 234 }, { 235, 236 }, { 237, 238 } }
};
const uint8_t _matchingPixelsEdge[NEDGE][1] = { { 242 }, { 241 }, { 240 }, { 243 } };
public:
virtual String getName()
{
return "100x100@2";
}
int ledsByPixelForMatrix()
{
return 2;
}
int ledsByPixelForEdges()
{
return 1;
}
int ledsNumber()
{
return (NROW * 24) + NEDGE;
}
const uint8_t *getLedsMatrixId(int row, int col)
{
if (row < 0) return NULL;
if (col < 0) return NULL;
if (row > NROW - 1) return NULL;
if (col > NCOL - 1) return NULL;
return _matchingPixelsMatrix[row][col];
}
const uint8_t *getLedsEdgeId(int n)
{
if (n < 0) return NULL;
if (n > NEDGE - 1) return NULL;
return _matchingPixelsEdge[n];
}
};
// 0 x
// 0+----------
// |
// |
// |
// y|
//
void copyCharToMatrix(const uint8_t src[FONTROW][FONTCOL], PixelsArray &dst, int posx, int posy, const RgbColor &color)
{
for (int r = 0; r < FONTROW; r++)
{
for (int c = 0; c < FONTCOL; c++)
{
if (src[r][c])
dst.setPixel(Pixel(color), posx + r, posy + c);
}
}
}
// Display number [-9:99]
void copyNumberToMatrix(int n, PixelsArray &dst, const RgbColor &color)
{
int x = 1;
if (n < 0)
::copyCharToMatrix(_font_minus, dst, x, 0, color); // Display "-"
else
{
if (n / 10 != 0)
::copyCharToMatrix(_font_number[abs(n / 10)], dst, x, 0, color); // Display "First number"
else
::copyCharToMatrix(_font_number[0], dst, x, 0, color); // Display "First number"
}
::copyCharToMatrix(_font_number[abs(n % 10)], dst, x, 0 + 6, color); // Display "Second number"
}
enum RandomColorMode
{
ColorRandomNo = 0,
ColorRandomAll,
ColorRandomLetter,
ColorRandomWord
};
class LedStripMode
{
protected:
String _name;
PixelsContainer *_pPixelContainer;
RgbColor _color;
RandomColorMode _colorRandomMode;
void setPixelsColor(const Pixel &p)
{
_pPixelContainer->pixelsArray.fill(p);
for (int i = 0; i < NEDGE; i++)
_pPixelContainer->pixelsEdge[i] = p;
}
void clearPixelsColor()
{
// Clear pixels
setPixelsColor(pVOID);
}
public:
LedStripMode(String name, PixelsContainer *pPixelContainer)
: _name(name)
, _pPixelContainer(pPixelContainer)
, _color(RgbColor(255, 255, 255))
, _colorRandomMode(ColorRandomNo)
{
}
String getName()
{
return _name;
}
void setColor(RgbColor c)
{
_color = c;
}
RgbColor getColor()
{
return _color;
}
void setColorRandom(RandomColorMode c)
{
_colorRandomMode = c;
}
virtual void begin() = 0;
virtual void handle() = 0;
virtual bool allowAnimation() = 0;
};
class LedStripModeNothing : public LedStripMode
{
public:
LedStripModeNothing(PixelsContainer *pPixelContainer)
: LedStripMode("Nothing", pPixelContainer)
{
}
void begin()
{
// Clear display
clearPixelsColor();
_pPixelContainer->hasChanged = true;
}
void handle()
{
}
bool allowAnimation()
{
return true;
}
};
#define LedStripModeTimeName "Time"
class LedStripModeTime : public LedStripMode
{
private:
cl_Lst<TextTime *> _pTexTime;
int _lang;
int _m;
int _h;
public:
LedStripModeTime(PixelsContainer *pPixelContainer)
: LedStripMode(LedStripModeTimeName, pPixelContainer)
, _lang(0)
, _m(-1)
, _h(-1)
{
// Add your language here. It will be added
// to the configuration page automatically
_pTexTime.push_back(new TextTimeFr());
_pTexTime.push_back(new TextTimeEn());
}
cl_Lst<TextTime *> *getLanguagesList()
{
return &_pTexTime;
}
int getLanguage()
{
return _lang;
}
bool setLanguage(int lang)
{
if (lang < 0) return false;
if (lang > _pTexTime.size() - 1) return false;
_lang = lang;
// Force refresh
begin();
return true;
}
void begin()
{
_m = -1;
_h = -1;
}
void handle()
{
int h = _dateTime.hour;
int m = _dateTime.minute;
if (_m == m && _h == h)
return;
_m = m;
_h = h;
TextTimeBlobs b = _pTexTime[_lang]->getBlobsFromTime(h, m);
if (!b.number)
return; // TODO: Display something useful
// Clear display
clearPixelsColor();
RgbColor c = _color;
if (_colorRandomMode == ColorRandomAll)
c = HslColor((float)random(256) / 255.0, 1.0, 0.5);
for (int i = 0; i < b.number; i++)
{
if (_colorRandomMode == ColorRandomWord)
c = HslColor((float)random(256) / 255.0, 1.0, 0.5);
for (int j = 0; j < b.blobs[i]->number; j++)
{
if (_colorRandomMode == ColorRandomLetter)
c = HslColor((float)random(256) / 255.0, 1.0, 0.5);
Pixel p;
p.color = c;
p.display = true;
_pPixelContainer->pixelsArray.setPixel(p, b.blobs[i]->pixels[j].row, b.blobs[i]->pixels[j].col);
}
}
if (_colorRandomMode == ColorRandomWord)
c = HslColor((float)random(256) / 255.0, 1.0, 0.5);
for (int i = 0; i < m % 5; i++)
{
if (_colorRandomMode == ColorRandomLetter)
c = HslColor((float)random(256) / 255.0, 1.0, 0.5);
Pixel p;
p.color = c;
p.display = true;
_pPixelContainer->pixelsEdge[i] = p;
}
_pPixelContainer->hasChanged = true;
}
bool allowAnimation()
{
return true;
}
};
class LedStripModeSeconds : public LedStripMode
{
private:
int _s;
public:
LedStripModeSeconds(PixelsContainer *pPixelContainer)
: LedStripMode("Seconds", pPixelContainer)
, _s(-1)
{
}
void begin()
{
_s = -1;
}
void handle()
{
if (_s == (int)_dateTime.second)
return;
_s = _dateTime.second;
// Clear display
clearPixelsColor();
::copyNumberToMatrix(_s, _pPixelContainer->pixelsArray, _color);
_pPixelContainer->hasChanged = true;
}
bool allowAnimation()
{
return true;
}
};
class LedStripModeDay : public LedStripMode
{
private:
int _s;
public:
LedStripModeDay(PixelsContainer *pPixelContainer)
: LedStripMode("Day", pPixelContainer)
, _s(-1)
{
}
void begin()
{
_s = -1;
}
void handle()
{
if (_s == (int)_dateTime.day)
return;
_s = _dateTime.day;
// Clear display
clearPixelsColor();
::copyNumberToMatrix(_s, _pPixelContainer->pixelsArray, _color);
_pPixelContainer->hasChanged = true;
}
bool allowAnimation()
{
return true;
}
};
class LedStripModeTemperature : public LedStripMode
{
private:
int _s;
public:
LedStripModeTemperature(PixelsContainer *pPixelContainer)
: LedStripMode("Temperature", pPixelContainer)
, _s(-1)
{
}
void begin()
{
_s = -1;
}
void handle()
{
if (_s == (int)_dateTime.second)
return;
_s = _dateTime.second;
// Clear display
clearPixelsColor();
// Display Temperature
if (!RTC.GetIsRunning())
return; // TODO: Display something useful
int8_t t = RTC.GetTemperature().AsFloatDegC();
::copyNumberToMatrix(t, _pPixelContainer->pixelsArray, _color);
_pPixelContainer->hasChanged = true;
}
bool allowAnimation()
{
return true;
}
};
class LedStripModeTestColors : public LedStripMode
{
private:
int _t;
public:
LedStripModeTestColors(PixelsContainer *pPixelContainer)
: LedStripMode("Test Colors", pPixelContainer)
, _t(0)
{
}
void begin()
{
_t = 0;
}
void handle()
{
switch (_t)
{
case 0:
setPixelsColor(pRED);
_pPixelContainer->hasChanged = true;
break;
case 100000:
setPixelsColor(pGREEN);
_pPixelContainer->hasChanged = true;
break;
case 200000:
setPixelsColor(pBLUE);
_pPixelContainer->hasChanged = true;
break;
case 300000:
setPixelsColor(pWHITE);
_pPixelContainer->hasChanged = true;
break;
case 400000:
_t = -1;
break;
}
_t++;
}
bool allowAnimation()
{
return false;
}
};
class LedStripModeTestSpeed : public LedStripMode
{
private:
int _r;
int _c;
int _e;
public:
LedStripModeTestSpeed(PixelsContainer *pPixelContainer)
: LedStripMode("Test Speed", pPixelContainer)
, _r(0)
, _c(0)
, _e(0)
{
}
void begin()
{
_r = 0;
_c = 0;
_e = 0;
}
void handle()
{
if (_pPixelContainer->hasChanged)
return;
if (_c == NCOL) {
_c = 0;
_r++;
}
if (_r == NROW) {
_c = 0;
_r = 0;
_e++;
}
if (_e == NEDGE) {
_e = 0;
}
clearPixelsColor();
_pPixelContainer->pixelsArray.setPixel(pWHITE, _r, _c++);
_pPixelContainer->pixelsEdge[_e] = pWHITE;
_pPixelContainer->hasChanged = true;
}
bool allowAnimation()
{
return true;
}
};
class LedStripModeTestStrip : public LedStripMode
{
private:
MyNeoPixelBrightnessBus **_ppStrip;
Frame _frame;
int _index;
public:
LedStripModeTestStrip(PixelsContainer *pPixelContainer, MyNeoPixelBrightnessBus **ppStrip)
: LedStripMode("Test Strip", pPixelContainer)
, _ppStrip(ppStrip)
, _index(0)
{
}
void begin()
{
_index = 0;
_frame.init(4);
}
void handle()
{
if (!_frame.next())
return;
// Clear display
clearPixelsColor();
// Do not update buffer because this animation write directly to the strip device
_pPixelContainer->hasChanged = false;
if (!(*_ppStrip)->CanShow())
return;
if (_index > (*_ppStrip)->PixelCount())
_index = 0;
(*_ppStrip)->ClearTo(RgbColor(0, 0, 0));
(*_ppStrip)->SetPixelColor(_index++, RgbColor(255, 255, 255));
(*_ppStrip)->Show();
}
bool allowAnimation()
{
return false;
}
};
class MyLedStrip
{
protected:
MyNeoPixelBrightnessBus *_pStrip;
cl_Lst<LedConfiguration *> _ledConfiguration;
int _ledConfigurationIndex;
PixelsContainer _pixels;
bool _automaticBrightness;
cl_Lst<LedStripMode *> _modeList;
int _modeIndex;
bool refresh(PixelsContainer *pPixel)
{
if (!pPixel->hasChanged)
return false;
if (!_pStrip->CanShow())
return false;
// Reset led strip
_pStrip->ClearTo(RgbColor(0, 0, 0));
// Fill leds strip with matrix pixels
for (int r = 0; r < NROW; r++) {
for (int c = 0; c < NCOL; c++) {
Pixel p = pPixel->pixelsArray.getPixel(r, c);
const uint8_t *i = _ledConfiguration[_ledConfigurationIndex]->getLedsMatrixId(r, c);
if (!p.display) continue;
for (int l = 0; l < _ledConfiguration[_ledConfigurationIndex]->ledsByPixelForMatrix(); l++)
_pStrip->SetPixelColor(i[l], p.color);
}
}
// Fill leds strip with edge pixels
for (int e = 0; e < NEDGE; e++) {
Pixel p = pPixel->pixelsEdge[e];
const uint8_t *i = _ledConfiguration[_ledConfigurationIndex]->getLedsEdgeId(e);
if (!p.display) continue;
for (int l = 0; l < _ledConfiguration[_ledConfigurationIndex]->ledsByPixelForEdges(); l++)
_pStrip->SetPixelColor(i[l], p.color);
}
// Refresh display
_pStrip->Show();
pPixel->hasChanged = false;
return true;
}
// Update brightness every 50ms
void handleAutomaticBrightness()
{
static uint64_t p = 0;
if (!_automaticBrightness)
return;
uint64_t v = millis64() / 50;
if (v != p)
{
int sd = _config.brightnessAutoMinDay; // minimum brightness during the day
int sn = _config.brightnessAutoMinNight; // minimum brightness during the night
int sm = 255; // maximum brightness
int lmin = 0; // minimum lux sensitivity allowed
int lmax = _config.luxSensitivity * 10; // maximum lux sensitivity allowed
int s = sn; // night
if (_dateTime.hour > 9 && _dateTime.hour < 21) s = sd; // between 10h and 21h (day)
if (_dateTime.hour == 21) s = map(_dateTime.minute, 0, 59, sd, sn); // during the 21th hour
if (_dateTime.hour == 9) s = map(_dateTime.minute, 0, 59, sn, sd); // during the 9th hour
if (s == 0) s = 1;
int l = getAvgLux();
if (l < lmin) l = lmin;
if (l > lmax) l = lmax;
_pStrip->SetBrightness(map(l, lmin, lmax, s, sm)); // limit
if (_pStrip->CanShow())
_pStrip->Show();
}
p = v;
}
bool handleMode()
{
if (_modeIndex < 0) return false;
if (_modeIndex > _modeList.size() - 1) return false;
_modeList[_modeIndex]->handle();
return true;
}
public:
MyLedStrip()
: _pStrip(NULL)
, _ledConfigurationIndex(0)
, _automaticBrightness(false)
, _modeIndex(0)
{
_ledConfiguration.push_back(new LedConfiguration40x40());
_ledConfiguration.push_back(new LedConfiguration100x100_1());
_ledConfiguration.push_back(new LedConfiguration100x100_2());
_modeList.push_back(new LedStripModeNothing(&_pixels));
_modeList.push_back(new LedStripModeTime(&_pixels));
_modeList.push_back(new LedStripModeSeconds(&_pixels));
_modeList.push_back(new LedStripModeDay(&_pixels));
_modeList.push_back(new LedStripModeTemperature(&_pixels));
_modeList.push_back(new LedStripModeTestColors(&_pixels));
_modeList.push_back(new LedStripModeTestSpeed(&_pixels));
_modeList.push_back(new LedStripModeTestStrip(&_pixels, &_pStrip));
}
~MyLedStrip()
{
end();
_modeList.clear();
}
cl_Lst<TextTime *> *getLanguagesList()
{
for (int i = 0; i < _modeList.size(); i++)
{
if (_modeList[i]->getName().equalsIgnoreCase(LedStripModeTimeName))
return ((LedStripModeTime *)(_modeList[i]))->getLanguagesList();
}
return NULL;
}
cl_Lst<LedStripMode *> *getModesList()
{
return &_modeList;
}
cl_Lst<LedConfiguration *> *getLedConfigurationList()
{
return &_ledConfiguration;
}
void begin()
{
end();
if (!_pStrip)
{
_ledConfigurationIndex = _config.ledConfig;
// Cannot use DMA because DMA GPIO is already used by serial/USB bridge :(
_pStrip = new MyNeoPixelBrightnessBus(_ledConfiguration[_ledConfigurationIndex]->ledsNumber(), D4);
_pStrip->Begin();
}
_pStrip->ClearTo(RgbColor(0, 0, 0));
_pStrip->Show();
}
void end()
{
if (_pStrip)
delete _pStrip;
_pStrip = NULL;
}
void setAutomaticBrightness(int b)
{
_automaticBrightness = b;
}
void setBrightness(uint8_t b)
{
if (b < 1) b = 1;
if (b > 255) b = 255;
if (_automaticBrightness)
return;
_pStrip->SetBrightness(b);
if (_pStrip->CanShow())
_pStrip->Show();
//refresh();
}
uint8_t getBrightness()