-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYetAnotherLedMatrixEffectBox.ino
More file actions
1232 lines (1053 loc) · 31.2 KB
/
YetAnotherLedMatrixEffectBox.ino
File metadata and controls
1232 lines (1053 loc) · 31.2 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
/**************************************************************************
(yet another) LED Matrix Effect Box
Created by Istvan Klezli, 2022-Feb-05
MIT License
Copyright (c) 2022 Istvan Klezli
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**************************************************************************/
/*
HW components
- ESP8266 board, e.g. WEMOS D1 mini (clone)
- 1x 0,91" OLED I2C Display Module SSD1306 128x32
- 1x Rotary encoder
- 24x RGB LED WS2812 or compatible
- power connector, e.g. USB micro socket
Note: Current needed for the 24 LEDs (all R, G, B LEDs permanent ON!)
+ ESP8266 is ca. 1200mA!
HW wiring (in case of WEMOS D1 mini board, ESP8266-based)
* A0 (ADC0) - n.c.
* D0 (GPIO16) - n.c.
* D5 (GPIO14) - rotary encoder in1
* D6 (GPIO12) - rotary encoder in2
* D7 (GPIO13) - rotary encoder button
* D8 (GPIO15) - n.c.
* TX (GPIO1) - n.c.
* RX (GPIO3) - n.c.
* D1 (GPIO5) - OLED 128x32 I2C SCL
* D2 (GPIO4) - OLED 128x32 I2C SDA
* D3 (GPIO0) - WS2812 neopixel LED
* D4 (GPIO2) - n.c.
Note: you can use more LEDs, other microcontroller or display
if you adapt the SW. Do not forget to consider the power consumption!
Used Software Libraries (hope listed all of them)
- Adafruit_SSD1306.h
- Adafruit_GFX.h
- Adafruit_NeoPixel.h
- RotaryEncoder by Matthias Hertel
- Wire
- ESP8266WiFi.h + ArduinoOTA.h (if enabled, see OTA_UPDATE_ACTIVE_TIME_IN_MINS)
Software:
...hmmm... improve/refactor/adapt/extend it if you have free time, thanks!
I wanted to provide just one file (*.ino), however, this source could be
easily slit up into small C++ modules/libraries.
Do not forget to add your router's name and password to wlan_settings.h
if you want to use OTA (over the air) update!
Maybe you would like to fork the code and implement a fire, matrix
(falling symbols) or a fancy rainbow effect? Or maybe implement a random
effect sequence mode? Let's go!
*/
// C++ stuff
#include <vector>
#include <tuple>
#include <string>
// Arduino libraries
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_NeoPixel.h>
#include <RotaryEncoder.h>
// IMPORTANT:
// OTA update is possible in the first 10 minutes after startup.
// 0 means OTA and also the wifi functions are disabled!
#define OTA_UPDATE_ACTIVE_TIME_IN_MINS 10
#if (OTA_UPDATE_ACTIVE_TIME_IN_MINS > 0)
#include <ESP8266WiFi.h>
#include <ArduinoOTA.h>
//{ WLAN CONSTANTS, VARIABLES
#include "wlan_settings.h" // THIS MUST BE CUSTOMIZED!!!
// The unit will habve the following name LEDBOARD6x4_<MAC-address>
String strUniqueDeviceName = PRODUCT_NAME + String(ESP.getChipId(), HEX);
#define MAC_ADDR_BYTES 6
uint8_t G_macAddress[MAC_ADDR_BYTES] = {0};
IPAddress G_IpAddr;
//}
#endif
//{ OLED Display related declarations
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C // See datasheet for Address
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//}
//{ addressable RGB LED strip related declarations (needs to be customized?)
#define LED_PIN D3
// How many NeoPixels/WS2812/etc. LEDs are attached to the Arduino?
const byte LED_ROWS = 4;
const byte LED_COLS = 6;
constexpr byte LED_COUNT = LED_ROWS*LED_COLS;
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number
// Argument 3 = Pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
const uint32_t MAX_HUE = 65535L;
//}
//{ Rotary encoder related declarations
// input signals from Rotary Encoder
#define PIN_IN1 D6
#define PIN_IN2 D5
#define BUTTON_PIN D7
// Setup the RotaryEncoder
RotaryEncoder encoder(PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::FOUR3);
//}
// forward declarations
void printMsg(String msg, bool bBigFont = false);
void printMenu(String strMenuText, String strParamName, String strParamValue);
/// --------------------------------------------------------------------------
/// Base class for handling a parameter of a light effect
class SingleParamBase
{
public:
SingleParamBase(String name, String unit, int iInitialValue, bool bRotateThrough)
: m_srtParamName{name},
m_srtParamUnit{unit},
m_iValue{iInitialValue},
m_rotateThrough{bRotateThrough}
{
};
virtual String GetParamName() const
{
return m_srtParamName;
};
virtual int GetVal() const
{
return m_iValue;
};
virtual String GetParamText()
{
}
virtual void Next()
{
}
virtual void Prev()
{
}
protected:
String m_srtParamName;
String m_srtParamUnit;
int m_iValue;
bool m_rotateThrough;
};
/// --------------------------------------------------------------------------
/// Class for handling a single integer parameter of a light effect
class SingleParamInt : public SingleParamBase
{
public:
SingleParamInt(String name, String unit, int iInitialValue, bool bRotateThrough, std::tuple<int, int> limits)
: SingleParamBase(name, unit, iInitialValue, bRotateThrough), m_Limits{limits}
{
};
virtual String GetParamText() override
{
return String(m_iValue) + m_srtParamUnit;
}
virtual void Next() override
{
if (m_iValue >= std::get<1>(m_Limits))
{
if (m_rotateThrough)
m_iValue = std::get<0>(m_Limits);
return;
}
++m_iValue;
};
virtual void Prev() override
{
if (m_iValue <= std::get<0>(m_Limits))
{
if (m_rotateThrough)
m_iValue = std::get<1>(m_Limits);
return;
}
--m_iValue;
};
protected:
std::tuple<int, int> m_Limits;
};
/// --------------------------------------------------------------------------
/// Class for handling a single option parameter of a light effect
class SingleParamNamedOpts : public SingleParamBase
{
public:
// todo: beside the option name, store also a code (e.g. enum) to the option in order
// to avoid comparing the option name against literals to device on caller's side which
// option is set (string compare vs. integer compare)
SingleParamNamedOpts(String name, String unit, int iInitialValue, bool bRotateThrough, const std::vector<String>& optNames)
: SingleParamBase(name, unit, iInitialValue, bRotateThrough)
, m_vecOptNames{optNames}, m_currOpt{m_vecOptNames.begin()}
{
};
virtual String GetParamText() override
{
return *m_currOpt;
};
virtual void Next() override
{
if (m_currOpt == std::prev(m_vecOptNames.end()))
{
if (m_rotateThrough)
m_currOpt = m_vecOptNames.begin();
return;
}
++m_currOpt;
};
virtual void Prev() override
{
if (m_currOpt == m_vecOptNames.begin())
{
if (m_rotateThrough)
m_currOpt = std::prev(m_vecOptNames.end());
return;
}
--m_currOpt;
};
protected:
std::vector<String> m_vecOptNames;
std::vector<String>::iterator m_currOpt;
};
enum LightProgramType
{
PROG_WHITE,
PROG_MONOCHROME,
PROG_TRANSITION,
PROG_GRADIENT,
PROG_EFFECT,
};
class LightProg
{
public:
static const byte COLOR_PARAM_MAX = 59; // color 0 to 59, i.e. 60 diff. colors.
// It is better when this number can be divided by
// LED_COLS and LED_ROWS if Gradient/Rows or
// Gradient/Columns effects used (color1 is set to 0
// and color2 is set to 3/4 or 5/6 of the whole range).
static const byte SPEED_PARAM_MAX = 10;
static const byte LEVEL_PARAM_MAX = 100;
LightProg(LightProgramType progCode, String name, std::vector<SingleParamBase*>* params)
: m_progCode{progCode}
, m_strProgName{name}
, m_vecParams{params}
, m_currParam{m_vecParams->begin()}
, m_bParamSettingMode{false}
, m_bLedsAlreadyCalculated{false}
{
};
virtual bool NextParam()
{
if (m_currParam == std::prev(m_vecParams->end()))
return false;
++m_currParam;
return true;
}
virtual void GoToFirstParam()
{
m_currParam = m_vecParams->begin();
}
virtual void Next()
{
if (m_currParam == m_vecParams->end())
return;
(*m_currParam)->Next();
};
virtual void Prev()
{
if (m_currParam == m_vecParams->end())
return;
(*m_currParam)->Prev();
};
virtual LightProgramType GetProgCode() const
{
return m_progCode;
}
virtual void Print() const
{
String strParamName =
(m_bParamSettingMode && m_currParam != m_vecParams->end()
? (*m_currParam)->GetParamName()
: "");
String strParamText =
(m_bParamSettingMode && m_currParam != m_vecParams->end()
? (*m_currParam)->GetParamText()
: "");
printMenu(m_strProgName, strParamName, strParamText);
}
virtual void SetParamSettingMode(bool bParamSettingMode)
{
m_bParamSettingMode = bParamSettingMode;
}
virtual void Init()
{
m_currParam = m_vecParams->begin();
m_bParamSettingMode = false;
Off();
}
virtual void Start()
{
m_bLedsAlreadyCalculated = false;
}
virtual byte GetSpeed()
{
}
virtual void Tick()
{
if (IsTimeToUpdate())
Update();
}
virtual void Off()
{
strip.clear();
strip.show();
}
virtual void setColor(uint32_t color)
{
for(int i=0; i<LED_COUNT; i++)
strip.setPixelColor(i, color);
strip.show();
}
virtual void Update()
{
}
virtual bool IsTimeToUpdate()
{
static uint32_t lastUpdate = millis();
byte speed = GetSpeed();
// speed 0 means no update at all, however, 1st time the scene must be calculated
if (speed == 0 && m_bLedsAlreadyCalculated)
return false;
// speed max. means updates as fast as possible
if (SPEED_PARAM_MAX == speed)
return true;
// otherwise calculate the time delay and invoke update (in Tick()) if the
// time elapsed since the last update is bigger than the time delay calculated
// from the speed
uint32_t timeDelay = (1L << (SPEED_PARAM_MAX - speed)); // 10..0 -> 1024..1
uint32_t now = millis();
if (now > lastUpdate + timeDelay)
{
lastUpdate = now;
return true;
}
return false;
}
protected:
String m_strProgName;
LightProgramType m_progCode;
std::vector<SingleParamBase*>* m_vecParams;
std::vector<SingleParamBase*>::iterator m_currParam;
bool m_bParamSettingMode;
uint32_t m_currColor;
bool m_bLedsAlreadyCalculated;
};
class LightProgWhite : public LightProg
{
public:
LightProgWhite(LightProgramType progCode, String name, std::vector<SingleParamBase*>* params)
: LightProg(progCode, name, params)
{
};
virtual bool IsTimeToUpdate() override
{
return true;
}
virtual void Update() override
{
static byte levelParam = 0;
if (levelParam != m_vecParams->at(0)->GetVal())
{
levelParam = m_vecParams->at(0)->GetVal();
byte level = map(levelParam, 0, LEVEL_PARAM_MAX, 0, 255);
m_currColor = strip.Color(level, level, level);
setColor(m_currColor);
m_bLedsAlreadyCalculated = true;
}
}
};
class LightProgMonochrome : public LightProg
{
public:
LightProgMonochrome(LightProgramType progCode, String name, std::vector<SingleParamBase*>* params)
: LightProg(progCode, name, params)
{
};
virtual bool IsTimeToUpdate() override
{
return true;
}
virtual void Update() override
{
static byte colorParam = 0;
static byte levelParam = 0;
if (colorParam != m_vecParams->at(0)->GetVal()
|| levelParam != m_vecParams->at(1)->GetVal())
{
colorParam = m_vecParams->at(0)->GetVal();
levelParam = m_vecParams->at(1)->GetVal();
uint32_t color = map(colorParam, 0, COLOR_PARAM_MAX, 0, MAX_HUE);
byte saturation = 255;
byte level = map(levelParam, 0, LEVEL_PARAM_MAX, 0, 255);
m_currColor = strip.ColorHSV(color, 255, level);
setColor(m_currColor);
m_bLedsAlreadyCalculated = true;
}
}
};
class LightProgGradient : public LightProg
{
public:
enum GradientType
{
GRADIENT_ROWS,
GRADIENT_COLS,
GRADIENT_COLS2,
GRADIENT_PIXELS,
GRADIENT_RANDOM,
};
LightProgGradient(LightProgramType progCode, String name, std::vector<SingleParamBase*>* params)
: LightProg(progCode, name, params)
{
};
virtual void Start() override
{
m_bLedsAlreadyCalculated = false;
m_vecRandomColors.clear();
m_color1 = map(m_vecParams->at(1)->GetVal(), 0, COLOR_PARAM_MAX, 0, MAX_HUE);
m_color2 = map(m_vecParams->at(2)->GetVal(), 0, COLOR_PARAM_MAX, 0, MAX_HUE);
if (m_vecParams->at(3)->GetParamText() == "Rows") // oh, get rid of this, see todo in SingleParamNamedOpts
m_gradType = GRADIENT_ROWS;
else if (m_vecParams->at(3)->GetParamText() == "Columns")
m_gradType = GRADIENT_COLS;
else if (m_vecParams->at(3)->GetParamText() == "Columns 2")
m_gradType = GRADIENT_COLS2;
else if (m_vecParams->at(3)->GetParamText() == "Pixels")
m_gradType = GRADIENT_PIXELS;
else if (m_vecParams->at(3)->GetParamText() == "Random")
{
m_gradType = GRADIENT_RANDOM;
std::vector<byte> vecColorOrder;
for (byte pos = 0; pos<LED_COUNT; ++pos)
vecColorOrder.push_back(pos);
byte vecSize = 0;
while ((vecSize = vecColorOrder.size()) > 0)
{
byte ledToConfigure = random(vecSize);
byte order = vecColorOrder.at(ledToConfigure);
uint32_t color = map(order, 0, LED_COUNT, m_color1, m_color2);
m_vecRandomColors.push_back(color);
vecColorOrder.erase(vecColorOrder.begin() + ledToConfigure);
}
}
}
virtual byte GetSpeed() override
{
return m_vecParams->at(0)->GetVal();
}
virtual void Update() override
{
const uint32_t HUE_INCREMENT = 128;
static uint32_t hueOffset = 0;
for (int i=0; i<LED_COUNT; ++i)
{
switch (m_gradType)
{
case GRADIENT_PIXELS:
m_currColor = map(i, 0, LED_COUNT-1, m_color1, m_color2);
break;
case GRADIENT_ROWS:
m_currColor = map(i / LED_COLS, 0, LED_ROWS-1, m_color1, m_color2);
break;
case GRADIENT_COLS:
m_currColor = map(
( (i / LED_COLS) % 2 > 0) ? (i % LED_COLS) : LED_COLS - (i % LED_COLS) - 1,
0, LED_COLS - 1, m_color1, m_color2);
break;
case GRADIENT_COLS2:
m_currColor = map(i % LED_COLS, 0, LED_COLS-1, m_color1, m_color2);
break;
case GRADIENT_RANDOM:
m_currColor = m_vecRandomColors.at(i);
break;
}
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(m_currColor + hueOffset, 255, 255)));
}
m_bLedsAlreadyCalculated = true;
strip.show();
hueOffset += HUE_INCREMENT;
}
protected:
GradientType m_gradType = GRADIENT_ROWS;
std::vector<uint32_t> m_vecRandomColors;
uint32_t m_color1;
uint32_t m_color2;
};
class LightProgEffects : public LightProg
{
public:
enum GradientType
{
EFFECTS_RAINDROPS,
EFFECTS_MATRIX,
EFFECTS_STROBO,
EFFECTS_FIRE,
};
LightProgEffects(LightProgramType progCode, String name, std::vector<SingleParamBase*>* params)
: LightProg(progCode, name, params)
{
};
virtual void Start() override
{
m_bLedsAlreadyCalculated = false;
m_vecRandomColors.clear();
m_color1 = map(m_vecParams->at(1)->GetVal(), 0, COLOR_PARAM_MAX, 0, MAX_HUE);
m_color2 = map(m_vecParams->at(2)->GetVal(), 0, COLOR_PARAM_MAX, 0, MAX_HUE);
if (m_vecParams->at(3)->GetParamText() == "Raindrops") // oh, get rid of this, see todo in SingleParamNamedOpts
m_gradType = EFFECTS_RAINDROPS;
else if (m_vecParams->at(3)->GetParamText() == "Matrix")
m_gradType = EFFECTS_MATRIX;
else if (m_vecParams->at(3)->GetParamText() == "Strobo")
m_gradType = EFFECTS_STROBO;
else if (m_vecParams->at(3)->GetParamText() == "Fire")
m_gradType = EFFECTS_FIRE;
}
virtual byte GetSpeed() override
{
return m_vecParams->at(0)->GetVal();
}
virtual void Update() override
{
const byte HUE_INCREMENT = 1;
const byte RANDOM_LED_SELECTION_PERIOD_MIN = 1;
const byte RANDOM_LED_SELECTION_PERIOD_MAX = 100;
static uint8_t iRandomLedIdx = 0;
static uint32_t iCounter = RANDOM_LED_SELECTION_PERIOD_MAX;
static uint32_t colorBuff[LED_COUNT][2];
const byte COLOR_BUFF_HUE_IDX = 0;
const byte COLOR_BUFF_LEVEL_IDX = 1;
static byte hueOffset = 0;
bool bDecreaseBrigtness = false;
for (int i=0; i<LED_COUNT; ++i)
{
switch (m_gradType)
{
case EFFECTS_RAINDROPS:
if (--iCounter == 0)
{
iCounter = random(RANDOM_LED_SELECTION_PERIOD_MIN, RANDOM_LED_SELECTION_PERIOD_MAX);
iRandomLedIdx = random (LED_COUNT);
colorBuff[iRandomLedIdx][COLOR_BUFF_LEVEL_IDX] = 255;
colorBuff[iRandomLedIdx][COLOR_BUFF_HUE_IDX] = map(hueOffset, 0, 255, m_color1, m_color2);
}
colorBuff[i][COLOR_BUFF_LEVEL_IDX] = (15L * colorBuff[i][COLOR_BUFF_LEVEL_IDX]) >> 4; // ca. 6% lower brightness
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(
colorBuff[i][COLOR_BUFF_HUE_IDX],
255,
colorBuff[i][COLOR_BUFF_LEVEL_IDX])));
break;
case EFFECTS_MATRIX:
case EFFECTS_STROBO:
case EFFECTS_FIRE:
printMsg("Please implement", true);
break;
}
}
m_bLedsAlreadyCalculated = true;
strip.show();
hueOffset += HUE_INCREMENT;
}
protected:
GradientType m_gradType = EFFECTS_RAINDROPS;
std::vector<uint32_t> m_vecRandomColors;
uint32_t m_color1;
uint32_t m_color2;
};
class LightProgTransition : public LightProg
{
public:
enum TransientType
{
REPEAT,
REBOUND,
};
LightProgTransition(LightProgramType progCode, String name, std::vector<SingleParamBase*>* params)
: LightProg(progCode, name, params)
{
};
virtual void Start() override
{
m_bLedsAlreadyCalculated = false;
if (m_vecParams->at(3)->GetParamText() == "Repeat")
m_transType = REPEAT;
else
m_transType = REBOUND;
}
virtual byte GetSpeed() override
{
return m_vecParams->at(0)->GetVal();
}
virtual void Update() override
{
const uint32_t MAX_STEPS = 256;
static uint32_t step = 0;
static byte color1Param = 0;
static byte color2Param = 0;
static uint32_t color1 = 0;
static uint32_t color2 = 0;
if (color1Param != m_vecParams->at(1)->GetVal()
|| color2Param != m_vecParams->at(2)->GetVal())
{
color1Param = m_vecParams->at(1)->GetVal();
color2Param = m_vecParams->at(2)->GetVal();
color1 = map(color1Param, 0, COLOR_PARAM_MAX, 0, MAX_HUE);
color2 = map(color2Param, 0, COLOR_PARAM_MAX, 0, MAX_HUE);
}
uint32_t currColor = map(step, 0, MAX_STEPS-1, color1, color2);
m_currColor = strip.ColorHSV(currColor, 255, 255);
setColor(m_currColor);
m_bLedsAlreadyCalculated = true;
step += m_increasing ? 1 : -1;
if (step == 0 || step == MAX_STEPS)
{
if (m_transType == REBOUND)
m_increasing = !m_increasing;
else
step = m_increasing ? 0 : MAX_STEPS-1;
}
}
protected:
TransientType m_transType = REPEAT;
bool m_increasing = true;
};
class ProgHandler
{
public:
typedef std::vector<LightProg*> LightProgSet;
enum State
{
STATE_INIT,
STATE_PROGSELECT,
STATE_PARAMSETTING,
STATE_WAIT_FOR_START,
STATE_RUNNING,
};
ProgHandler(LightProgSet* pProgSet)
: m_pProgSet{pProgSet}
, m_pCurrLightProg{m_pProgSet->begin()}
, m_state{STATE_INIT}
{
(*m_pCurrLightProg)->SetParamSettingMode(false);
};
void Tick()
{
if (m_state == STATE_RUNNING)
(*m_pCurrLightProg)->Tick();
}
void Click()
{
switch (m_state)
{
case STATE_INIT:
case STATE_RUNNING:
(*m_pCurrLightProg)->Init();
m_state = STATE_PROGSELECT;
Print();
break;
case STATE_PROGSELECT:
m_state = STATE_PARAMSETTING;
(*m_pCurrLightProg)->SetParamSettingMode(true);
Print();
break;
case STATE_PARAMSETTING:
if (!(*m_pCurrLightProg)->NextParam())
{
m_state = STATE_WAIT_FOR_START;
printMsg("Click to START!", true);
}
else
Print();
break;
case STATE_WAIT_FOR_START:
m_state = STATE_RUNNING;
printMsg("Click to STOP!", true);
(*m_pCurrLightProg)->GoToFirstParam();
(*m_pCurrLightProg)->Start();
break;
}
};
void Next()
{
switch (m_state)
{
case STATE_PROGSELECT:
++m_pCurrLightProg;
if (m_pCurrLightProg == m_pProgSet->end())
m_pCurrLightProg = m_pProgSet->begin();
break;
case STATE_PARAMSETTING:
case STATE_RUNNING:
(*m_pCurrLightProg)->Next();
break;
default:
return;
}
Print();
};
void Prev()
{
switch (m_state)
{
case STATE_PROGSELECT:
if (m_pCurrLightProg == m_pProgSet->begin())
m_pCurrLightProg = std::prev(m_pProgSet->end());
else
--m_pCurrLightProg;
break;
case STATE_PARAMSETTING:
case STATE_RUNNING:
(*m_pCurrLightProg)->Prev();
break;
default:
return;
}
Print();
};
LightProgramType GetCurrProg() const
{
return (*m_pCurrLightProg)->GetProgCode();
}
bool IsToBeConfirmed() const
{
return m_state == STATE_WAIT_FOR_START;
}
void Print() const
{
(*m_pCurrLightProg)->Print();
};
private:
LightProgSet* m_pProgSet;
std::vector<LightProg*>::iterator m_pCurrLightProg;
State m_state;
};
/// --------------------------------------------------------------------------
/// LIGHT PROGRAMS
/// concerning "new", feel free to change it :)
LightProgWhite programWhite
{
PROG_WHITE,
"White",
new std::vector<SingleParamBase*> {
new SingleParamInt{"Level", "%", 100, true, std::tuple<int, int>{0,LightProg::LEVEL_PARAM_MAX}},
}
};
LightProgMonochrome programMonochrome
{
PROG_MONOCHROME,
"Monochrom",
new std::vector<SingleParamBase*> {
new SingleParamInt{"Color ", "", 0, true, std::tuple<int, int>{0,LightProg::COLOR_PARAM_MAX}},
new SingleParamInt{"Level", "%", 100, true, std::tuple<int, int>{0,LightProg::LEVEL_PARAM_MAX}},
}
};
LightProgTransition programTransition
{
PROG_TRANSITION,
"Transitn.",
new std::vector<SingleParamBase*> {
new SingleParamInt{"Speed", "", 0, false, std::tuple<int, int>{0,LightProg::SPEED_PARAM_MAX}},
new SingleParamInt{"Color1", "", 0, true, std::tuple<int, int>{0,LightProg::COLOR_PARAM_MAX}},
new SingleParamInt{"Color2", "", LightProg::COLOR_PARAM_MAX, true, std::tuple<int, int>{0,LightProg::COLOR_PARAM_MAX}},
new SingleParamNamedOpts{"", "", 0, true, std::vector<String>{"Repeat", "Rebound"}},
}
};
LightProgGradient programGradient
{
PROG_GRADIENT,
"Gradient",
new std::vector<SingleParamBase*> {
new SingleParamInt{"Speed", "", 0, false, std::tuple<int, int>{0,LightProg::SPEED_PARAM_MAX}},
new SingleParamInt{"Color1", "", 0, true, std::tuple<int, int>{0,LightProg::COLOR_PARAM_MAX}},
new SingleParamInt{"Color2", "", LightProg::COLOR_PARAM_MAX, true, std::tuple<int, int>{0,LightProg::COLOR_PARAM_MAX}},
new SingleParamNamedOpts{"", "", 0, true, std::vector<String>{"Rows", "Columns", "Columns 2", "Pixels", "Random"}},
}
};
LightProgEffects programEffects
{
PROG_EFFECT,
"Effects",
new std::vector<SingleParamBase*> {
new SingleParamInt{"Speed", "", 0, false, std::tuple<int, int>{0,LightProg::SPEED_PARAM_MAX}},
new SingleParamInt{"Color1", "", 0, true, std::tuple<int, int>{0,LightProg::COLOR_PARAM_MAX}},
new SingleParamInt{"Color2", "", LightProg::COLOR_PARAM_MAX, true, std::tuple<int, int>{0,LightProg::COLOR_PARAM_MAX}},
new SingleParamNamedOpts{"", "", 0, true, std::vector<String>{"Raindrops", "Matrix", "Strobo", "Fire"}},
}
};
ProgHandler::LightProgSet programSet
{
{
&programWhite,
&programMonochrome,
&programTransition,
&programGradient,
&programEffects,
}
};
ProgHandler PROGRAMHANDLER{&programSet};
/// --------------------------------------------------------------------------
/// Some global scope functions...
/// Debounce algorithm for the button part of the
/// rotary encoder. (func. found in internet, originally from Jack G. Ganssle?)
bool debounce()
{
static uint32_t state = 0;
state = (state<<1) | digitalRead(BUTTON_PIN) | 0xe0000000;
return (state == 0xf0000000);
}
/// Displays a text on the OLED display (font size 1 or 2)
void printMsg(String msg, bool bBigFont)
{
display.clearDisplay();
display.setTextSize(bBigFont ? 2 : 1);
display.setTextColor(SSD1306_WHITE, SSD1306_BLACK);
display.setCursor(0, 0);
display.println(msg);
display.display();
}
/// Shows a "structured" text on the OLED display.
/// (a) Either a (menu) text alone will be shown
/// (b) or a (menu) text and a paramter + value will be displayed.
/// (a) (b)
/// 1st row: (<menu text>) <menu text>
/// 2nd row: <empty> <param> (<value>)
/// Note: <menu text> (case a) and <value> (case b) are highlighted,
/// i.e. written in inverse mode: black font on top of a white
/// rounded box in order to represent the text in focus.
/// 1st and 2nd line should be max. 9-char long.
/// Optimized for display with 128x32 resolution!
void printMenu(String strMenuText, String strParamName, String strParamValue)
{
const byte CHAR_X_SPACING = 12;
const byte CHAR_Y_SPACING = 16;