forked from mishafarms/LEDs_esp8266
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeds.cpp
1293 lines (1044 loc) · 25.6 KB
/
Leds.cpp
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
#include "Leds.h"
#include <FS.h>
#include <ArduinoJson.h>
#include "artnet.h"
#include <WiFiUdp.h>
#define DBG_OUTPUT_PORT Serial
String modeNames[LAST_MODE + 1] = {
"Stop",
"Color",
"Pattern",
"Pattern_Cycle",
"Artnet",
"Last"
};
extern Leds *myLeds;
// there should probably be one per Led object
static WiFiUDP _artnetUdp;
static Artnet *_artnet;
void onDmxFrame(uint16_t universe, uint16_t length, uint8_t sequence, uint8_t* data);
int startUniverse = 0; // sometimes software may start at 1
// Check if we got all universes
int maxUniverses = 0;
bool universesReceived[MAX_UNIS];
unsigned long artNetRecved = 0;
Leds::Leds()
{
#if NOT_YET
stringCom["Play"] = &Leds::play;
stringCom["Stop"] = &Leds::stop;
stringCom["Pause"] = &Leds::pause;
stringCom["Rewind"] = &Leds::rew;
// stringCom["Record"] = &Leds::changeDiag;
stringCom["FastForward"] = &Leds::ff;
stringCom["Start"] = &Leds::colorDown;
stringCom["Shuffle"] = &Leds::shuffle;
stringCom["End"] = &Leds::colorUp;
#endif
currentHue = CHSV(HUE_BLUE, 240, 255);
// these are the defaults if there is a config file it can be overwritten.
// initial config stuff
startTime = FIVE_PM * MINUTES_PER_HOUR; // by default on at 5pm
stopTime = ONE_AM * MINUTES_PER_HOUR; // by default off at 1am
timeZone_ = (PST_TIME * SECONDS_PER_HOUR);
hueCycleTime = 20; // this is milliseconds
patCycleTime = 60; // this is in seconds
numLeds = DEFAULT_NUM_LEDS;
// set to cycle through the patterns
mode = PATTERN_CYCLE_MODE;
// start with the default color order
colorOrder_ = COLOR_ORDER;
// default the artnetWaitTime, it may get overwritten in the config file
artnetWaitTime = ARTNET_WAIT_TIME;
// build our vector of patterns here
patterns["allChristmasLights"] = &Leds::allChristmasLights;
patterns["wipe"] = &Leds::wipe;
patterns["christmasConfetti"] = &Leds::christmasConfetti;
patterns["chase"] = &Leds::chase;
patterns["christmasLights"] = &Leds::christmasLights;
patterns["sweep"] = &Leds::sweep;
patterns["dark"] = &Leds::dark;
currentPattern = patterns.begin();
// create some palettes
gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::Green, CRGB::Blue);
grPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::Green, CRGB::Green);
// read the config for the leds and then we can uses all the info
readConfig();
shuffleCnt = 0;
num = 3;
// create the default pixel mapping. This is a normal map 0 = 0, 1 = 1, ... 24 = 24.
// add the leds, this is a little cumbersome, but it does work.
switch (colorOrder_) {
case RGB:
{
FastLED.addLeds<LED_TYPE, SPI_DATA, SPI_CLOCK, RGB>(leds, numLeds).setCorrection(TypicalLEDStrip);;
break;
}
case RBG:
{
FastLED.addLeds<LED_TYPE, SPI_DATA, SPI_CLOCK, RBG>(leds, numLeds).setCorrection(TypicalLEDStrip);;
break;
}
case GRB:
{
FastLED.addLeds<LED_TYPE, SPI_DATA, SPI_CLOCK, GRB>(leds, numLeds).setCorrection(TypicalLEDStrip);;
break;
}
case GBR:
{
FastLED.addLeds<LED_TYPE, SPI_DATA, SPI_CLOCK, GBR>(leds, numLeds).setCorrection(TypicalLEDStrip);;
break;
}
case BRG:
{
FastLED.addLeds<LED_TYPE, SPI_DATA, SPI_CLOCK, BRG>(leds, numLeds).setCorrection(TypicalLEDStrip);;
break;
}
case BGR:
{
FastLED.addLeds<LED_TYPE, SPI_DATA, SPI_CLOCK, BGR>(leds, numLeds).setCorrection(TypicalLEDStrip);;
break;
}
default:
{
FastLED.addLeds<LED_TYPE, SPI_DATA, SPI_CLOCK, RGB>(leds, numLeds).setCorrection(TypicalLEDStrip);;
break;
}
}
// start running and we will figure it out later.
running = true;
if (mode == COLOR_MODE)
{
// this will start things
loop(-1);
}
// look and see if we should open the artnet port
if (artnetEnabled)
{
startUniverse = _startUniverse;
maxUniverses = (numLeds + (NUM_UNI_LEDS - 1))/ NUM_UNI_LEDS;
_artnet = new Artnet(_artnetUdp);
_artnet->begin(artnetPort);
_artnet->setArtDmxCallback(onDmxFrame);
}
}
Leds::~Leds()
{
}
bool Leds::isRunning(void)
{
return running;
}
CRGB Leds::color(void)
{
return currentRgb;
}
bool Leds::setColor(CRGB newColor)
{
currentRgb = newColor;
return true;
}
bool Leds::setColor(CHSV newColor)
{
currentHue = newColor;
currentRgb = currentHue;
return true;
}
CHSV Leds::hue(void)
{
return currentHue;
}
String Leds::getMode(void)
{
return modeNames[mode];
}
bool Leds::setMode(enum Modes newMode)
{
if ((newMode < STOP_MODE) || (newMode >= LAST_MODE))
{
// out of bounds
return false;
}
// save the last mode, just in case
lastMode = mode;
mode = newMode;
}
void Leds::setStartTime(int newTime)
{
startTime = newTime;
}
int Leds::getStartTime(void)
{
return startTime;
}
void Leds::setStopTime(int newTime)
{
stopTime = newTime;
}
int Leds::getStopTime(void)
{
return stopTime;
}
/**@brief Function for doing something with the leds when we connect.
*
* @details This function will be called when we are connected to by a device.
*
* @param[in] void * p_data, uint16_t length of p_data (both unused)
*/
void Leds::connected(void *p_data, uint16_t length)
{
(void) p_data;
(void) length;
/* this maybe should not set a mode */
/* flash to let us know it happened */
FastLED.showColor(CRGB::FairyLight);
delay(100);
FastLED.show();
}
/**@brief Function for doing something with the leds when we disconnect.
*
* @details This function will be called when we are disconnected from a device.
*
* @param[in] void
*/
void Leds::disconnected(void)
{
/* this should set a mode, just keep doing what you are doing */
FastLED.showColor(CRGB::Purple);
delay(100);
FastLED.show();
}
/**@brief Function for doing something with the leds when we disconnect.
*
* @details This function will be called when we are disconnected from a device.
*
* @param[in] void
*/
void Leds::sleepMode(void)
{
for(uint8_t x = 0 ; x < 30 ; x++)
{
leds[10] = CRGB::Red;
FastLED.show();
delay(100);
leds[10] = CRGB::Black;
FastLED.show();
delay(100);
leds[10] = CRGB::Blue;
FastLED.show();
delay(100);
leds[10] = CRGB::Black;
FastLED.show();
delay(100);
leds[10] = CRGB::Green;
FastLED.show();
delay(100);
leds[10] = CRGB::Black;
FastLED.show();
delay(100);
}
}
/**@brief Function for moving the led mode forward.
*
* @details This function will be called from the string_com table.
*
* @param[in] void
*/
void Leds::ff(void)
{
if (++currentPattern == patterns.end())
{
currentPattern = patterns.begin();
}
fill_solid(leds, numLeds, CRGB::Black);
}
/**@brief Function for moving the led mode backward.
*
* @details This function will be called from the string_com table.
*
* @param[in] void
*/
void Leds::rew(void)
{
if (currentPattern == patterns.begin())
{
currentPattern = patterns.end();
}
currentPattern--;
fill_solid(leds, numLeds, CRGB::Black);
}
/**@brief Function for doing something with the leds when we receive a play command.
*
* @details This function will be called when e receive a matching string see string_com[] above.
*
* @param[in] void
*/
void Leds::play(void)
{
running = 1;
}
/**@brief Function for doing something with the leds when we receive a play command.
*
* @details This function will be called when e receive a matching string see string_com[] above.
*
* @param[in] void
*/
void Leds::pause(void)
{
running = 0;
}
/**@brief Function for doing something with the leds when we receive a play command.
*
* @details This function will be called when e receive a matching string see string_com[] above.
*
* @param[in] void
*/
void Leds::stop(void)
{
running = 0;
FastLED.clear();
FastLED.show();
}
/**@brief Function for changing the color to a new color. this will move the color by 7
*
* @details This function will be called from the string_com table.
*
* @param[in] void
*/
void Leds::colorDown(void)
{
currentHue.hue -= 7;
currentRgb = currentHue;
}
/**@brief Function for changing the color to a new color. this will move the color by 7
*
* @details This function will be called from the string_com table.
*
* @param[in] void
*/
void Leds::colorUp(void)
{
currentHue.hue += 7;
currentRgb = currentHue;
}
void Leds::shuffle(void)
{
shuffleCnt = (shuffleCnt + 1) % 2;
}
/**@brief Function for each pattern, this one blinks 2 lights.
*
* @details This function will be called tick when it is the active patterns.
*
* @param[in] void
*/
void Leds::blinkSimple2(void)
{
static uint8_t frame = 0;
EVERY_N_MILLISECONDS( 500 )
{
uint8_t x;
if (frame)
{
for( x = 0 ; x < numLeds ; x++ )
{
if ( x % 2 )
{
leds[x] = CRGB::Black;
}
else
{
leds[x] = currentRgb;
}
}
frame = 0;
}
else
{
for( x = 0 ; x < numLeds ; x++ )
{
if ( x % 2 )
{
leds[x] = currentRgb;
}
else
{
leds[x] = CRGB::Black;
}
}
frame = 1;
}
}
FastLED.show();
}
/**@brief Function for each pattern, this one lights the leds all one color.
*
* @details This function will be called tick when it is the active patterns.
*
* @param[in] void
*/
void Leds::simpleColor(void)
{
// go all blue till we get a command or timeout
fill_solid(leds, numLeds, currentRgb);
FastLED.show();
}
/**@brief Function for each pattern, this one blinks 2 lights.
*
* @details This function will be called tick when it is the active patterns.
*
* @param[in] void
*/
void Leds::hueColor(void)
{
CRGB rgb = currentHue;
fill_solid(leds, numLeds, rgb);
FastLED.show();
}
/**@brief Function for each pattern, this one is a 1 light chase.
*
* @details This function will be called tick when it is the active patterns.
*
* @param[in] void
*/
void Leds::chase(void)
{
// simple single led chase
static int pos = 0;
fill_solid(leds, numLeds, CRGB::Black);
leds[pos] = currentRgb;
pos = (pos + 1) % numLeds;
FastLED.show();
}
void Leds::chase2()
{
static int j = 0;
EVERY_N_MILLISECONDS(300) {
for(int x = 0 ; x < numLeds ; x++)
{
leds[x] = CRGB::Black;
}
for(int i = j ; i < numLeds ; i += num)
{
leds[i] = ColorFromPalette(gPal, beatsin8(5));
}
j++;
j = j % num;
}
}
/**@brief Function for each pattern, this one shows a rainbow that cycles.
*
* @details This function will be called tick when it is the active pattern.
*
* @param[in] void
*/
void Leds::rainbow(void)
{
fill_rainbow(leds, numLeds, currentHue.hue, 7);
FastLED.show();
}
void Leds::addGlitter( fract8 chanceOfGlitter)
{
if( random8() < chanceOfGlitter) {
leds[ random16(numLeds) ] += CRGB::White;
}
}
void Leds::rainbowWithGlitter(void)
{
// built-in FastLED rainbow, plus some random sparkly glitter
rainbow();
addGlitter(80);
}
void Leds::gConfetti(void)
{
// random colored speckles that blink in and fade smoothly
fadeToBlackBy( leds, numLeds, 10);
int pos = random16(numLeds);
leds[pos] += CHSV( HUE_GREEN + random8(64), 200, 255);
}
void Leds::rConfetti(void)
{
// random colored speckles that blink in and fade smoothly
fadeToBlackBy( leds, numLeds, 10);
int pos = random16(numLeds);
leds[pos] += CHSV( HUE_RED + random8(64), 200, 255);
}
void Leds::confetti(void)
{
// random colored speckles that blink in and fade smoothly
fadeToBlackBy( leds, numLeds, 10);
int pos = random16(numLeds);
CHSV confetti = currentHue;
confetti.h += random8(64);
leds[pos] += confetti;
}
void Leds::sinelon(void)
{
// a colored dot sweeping back and forth, with fading trails
fadeToBlackBy( leds, numLeds, 20);
int pos = beatsin16(13,0,numLeds);
leds[pos] += ColorFromPalette(gPal, beatsin8(20));
}
void Leds::greenlon(void)
{
// a colored dot sweeping back and forth, with fading trails
fadeToBlackBy( leds, numLeds, 20);
int pos = beatsin16(13,0,numLeds);
leds[pos] += CRGB::Green;
addGlitter(40);
}
void Leds::redlon(void)
{
// a colored dot sweeping back and forth, with fading trails
fadeToBlackBy( leds, numLeds, 20);
int pos = beatsin16(13,0,numLeds);
leds[pos] += CRGB::Red;
addGlitter(40);
}
void Leds::sweep(void)
{
for(int i = 0 ; i < numLeds ; i++)
{
leds[i] = ColorFromPalette(grPal, beatsin8(5));
}
addGlitter(10);
}
void Leds::dark(void)
{
for(int i = 0 ; i < numLeds ; i++)
{
leds[i] = CRGB::Black;
}
}
void Leds::bpm(void)
{
// colored stripes pulsing at a defined Beats-Per-Minute (BPM)
uint8_t BeatsPerMinute = 62;
CRGBPalette16 palette = PartyColors_p;
uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
for( int i = 0; i < numLeds; i++) { //9948
leds[i] = ColorFromPalette(palette, currentHue.hue + (i*2), beat-currentHue.hue + (i*10));
}
}
void Leds::juggle(void) {
// eight colored dots, weaving in and out of sync with each other
fadeToBlackBy( leds, numLeds, 20);
byte dothue = 0;
for( int i = 0; i < 8; i++) {
leds[beatsin16(i+7,0,numLeds)] |= CHSV(dothue, 200, 255);
dothue += 32;
}
}
void Leds::christmasConfetti(void) {
// random colored speckles that blink in and fade smoothly
fadeToBlackBy( leds, numLeds, 10);
int pos = random16(numLeds);
leds[pos] = ColorFromPalette( gPal, random8(255));
}
void Leds::christmasLights(void) {
int x;
CRGB ltColors[] = { CRGB::Red, CRGB::Green, CRGB::Blue, CRGB::Yellow };
CRGB color;
static int times = 0;
EVERY_N_MILLISECONDS(600){
for(int x = 0 ; x < numLeds ; x++)
{
leds[x] = CRGB::Black;
}
color = ltColors[times % (sizeof(ltColors) / sizeof(color))];
for( x = 0 + (times % 4) ; x < numLeds ; x += 4)
{
leds[x] = color;
}
times++;
}
}
void Leds::allChristmasLights(void) {
int x;
CRGB ltColors[] = { CRGB::Red, CRGB::Green, CRGB::Blue, CRGB::Yellow };
static int times = 0;
EVERY_N_MILLISECONDS(600){
for( x = 0 ; x < numLeds; x += 8)
{
for(int y = 0 ; y < 8 ; y++)
{
leds[x + y] = ltColors[(times + (x/8)) % (sizeof(ltColors) / sizeof(CRGB))];
}
}
times++;
}
}
void Leds::wipe(void) {
int x;
static CRGB color;
static int wiping = 0;
if (wiping >= numLeds)
{
wiping = 0;
color = ColorFromPalette( gPal, random8(255));
}
leds[wiping++] = color;
}
/**@brief Function show a test pattern on the LED strip.
*
* @details This function will be called to show a test on the LEDS.
*
* @param[in] void
*/
void Leds::ledTest(void) {
// flash leds
FastLED.showColor(CRGB::Red);
delay(1000);
FastLED.showColor(CRGB::Green);
delay(1000);
FastLED.showColor(CRGB::Blue);
delay(1000);
FastLED.showColor(CRGB::Black);
FastLED.show();
}
String Leds::pattern(void)
{
return(currentPattern->first);
}
std::vector <String> Leds::getPatterns(void)
{
std::vector <String> patStrs;
std::map <String, funcPtr_t>::iterator pat = patterns.begin();
while(pat != patterns.end())
{
patStrs.push_back(pat->first);
pat++;
}
return patStrs;
}
bool Leds::setPattern(String newPattern)
{
// see if we can find the newPattern in our list of patterns, and if so then we can set the current pattern to it.
std::map <String, funcPtr_t>::iterator match = patterns.find(newPattern);
if (match != patterns.end())
{
// we need to set the current pattern to the match.
currentPattern = match;
return(true);
}
return(false);
}
/**@brief Function for doing all the fastled stuff. this happens every 100ms or so.
*
* @details This function will be called each fastled tick.
*
* @param[in] void
*/
void Leds::loop(int nowTime)
{
static bool _inited = false;
if (!_inited && (nowTime > 0)) {
// we should figure out if we are running or not, we start running so only stop us if needed
if ((startTime >= 0) && (stopTime >= 0) && (startTime != stopTime)) {
if (startTime > stopTime) {
if ((nowTime >= stopTime) && (nowTime < startTime)) {
// we are not running
stop();
}
}
else if ((nowTime < startTime) || (nowTime >= stopTime)) {
stop();
}
}
// don't do this again
_inited = true;
}
// only if there are non-negative non-matching times
if (_inited && ((startTime >= 0) && (stopTime >= 0) && (startTime != stopTime)))
{
if ((nowTime == startTime) && !running)
{
play();
}
else if ((nowTime == stopTime) && running)
{
stop();
}
}
if (artnetEnabled)
{
_artnet->read();
}
if (!running)
{
return;
}
switch(mode)
{
case PATTERN_MODE:
case PATTERN_CYCLE_MODE:
{
// call the current pattern
((*this).*currentPattern->second)();
// do some periodic updates
EVERY_N_MILLISECONDS( hueCycleTime )
{
currentHue.hue++;
currentRgb = currentHue;
} // slowly cycle the "base color" through the rainbow
if (mode == PATTERN_CYCLE_MODE)
{
EVERY_N_SECONDS( patCycleTime )
{
ff(); // every 5 minutes change the mode
}
}
break;
}
case COLOR_MODE:
{
simpleColor();
break;
}
case ARTNET_MODE:
{
// if we are in artnet mode and haven't seen a packet in a while, then change back to the old mode
if ((artNetRecved + artnetWaitTime) < millis())
{
mode = lastMode;
}
break;
}
case STOP_MODE:
default:
// do nothing for now
break;
}
}
#if 0
/**@brief Function for parsing the commands to see what to do.
*
* @details This function will be called whenever a change to the mode is needed.
*
* @param[in] char const *command, uint8_t length of command string
*/
void Leds::processCommands(char const *command, uint16_t len)
{
String s(command, len);
std::map <String, funcPtr_t>::iterator match = stringCom.find(s);
if (match != stringCom.end())
{
((*this).*match->second)();
}
}
#endif
int Leds::timezone(void) {
return(timeZone_);
}
static EOrder strToColorOrder(String colorOrder)
{
if (colorOrder == "RBG")
{
return RBG;
}
if (colorOrder == "GRB")
{
return GRB;
}
if (colorOrder == "GBR")
{
return GBR;
}
if (colorOrder == "BRG")
{
return BRG;
}
if (colorOrder == "BGR")
{
return BGR;
}
return RGB;
}
static String colorOrderToStr(EOrder colorOrder)
{
switch (colorOrder)
{
case RGB:
return("RGB");
break;
case RBG:
return("RBG");
break;
case GRB:
return("GRB");
break;
case GBR:
return("GBR");
break;
case BRG:
return("BRG");
break;
case BGR:
return("BGR");
break;
default:
return("RGB");
break;
}
return("");
}
static const char *configFilename = "/led.json";
static const char *tmpConfigFilename = "/led.tmp";
static const char *configBackFilename = "/led.bak.json";
bool Leds::readConfig(void) {
bool status = false;
int configSize = 0;
if (!SPIFFS.exists(configFilename)) {
// if it doesn't exist then return false
DBG_OUTPUT_PORT.print("Config file ");
DBG_OUTPUT_PORT.print(configFilename);
DBG_OUTPUT_PORT.println(" doesn't exist");
return false;
}
// file exists, open it
File configFile = SPIFFS.open(configFilename, "r");
if (!configFile) {
// if we can't open it then return false
DBG_OUTPUT_PORT.print("Config file ");
DBG_OUTPUT_PORT.print(configFilename);
DBG_OUTPUT_PORT.println(" open failed");
return false;
}
// get the size of the file
configSize = configFile.size();
DynamicJsonBuffer jsonBuffer;
// allocate space for the file
char *configJson = (char *) calloc(configSize, 1);
// read in the file
if (configFile.readBytes(configJson, configSize) == 0) {
DBG_OUTPUT_PORT.print("Config file ");
DBG_OUTPUT_PORT.print(configFilename);
DBG_OUTPUT_PORT.print(" read failed size ");
DBG_OUTPUT_PORT.println(configSize);
goto cleanup;
} else {
// we have hopefully a JSON string in memory, we need to parse it
JsonObject& ledJson = jsonBuffer.parseObject(configJson);
if (ledJson.success()) {
// we should be able to do cool things here. But for now let's print it
ledJson.prettyPrintTo(Serial);
Serial.println();
// see if there is a mode
if (ledJson.containsKey("mode"))