Repository navigation
Expand file tree
/
Copy pathBackLeds.cpp
More file actions
148 lines (124 loc) · 3.56 KB
/
Copy pathBackLeds.cpp
File metadata and controls
148 lines (124 loc) · 3.56 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
#include "BackLeds.h"
BackLeds::BackLeds(CRGB *leds, int numleds)
{
// Initialize base class members
this->leds = leds;
this->numleds = numleds;
this->lastUpdate = millis();
this->effectChangeTime = millis();
this->speed = LedConstants::DEFAULT_SPEED_BACK;
this->idx = 0;
this->pulse_speed = LedConstants::PULSE_SPEED_DEFAULT;
this->pulse = LedConstants::PULSE_VALUE_DEFAULT;
this->pulse_offset = LedConstants::PULSE_OFFSET_DEFAULT;
this->strobe_ind = false;
this->currentEffect = 0;
this->currentColor = 0; // Red
this->autoChange = false;
this->alternatePhase = false;
// Validation
if (!validatePointers()) {
#ifdef DEBUG_MODE
Serial.println(F("ERROR: BackLeds - Invalid LED pointer"));
#endif
}
if (!validateNumLeds()) {
#ifdef DEBUG_MODE
Serial.print(F("WARNING: BackLeds - NumLEDs out of range: "));
Serial.println(numleds);
#endif
}
}
void BackLeds::setEffect(int effect) {
this->currentEffect = effect;
this->idx = 0;
this->pulse = LedConstants::PULSE_VALUE_DEFAULT;
this->pulse_offset = LedConstants::PULSE_OFFSET_DEFAULT;
this->strobe_ind = false;
this->alternatePhase = false;
if (effect == 99) {
this->autoChange = true;
this->currentEffect = 0;
} else {
this->autoChange = false;
}
}
void BackLeds::update(unsigned long currentTime)
{
if (!validatePointers()) return;
if ((currentTime - this->lastUpdate) < this->speed) return;
// Auto-change mode
if (this->autoChange && (currentTime - this->effectChangeTime) > LedConstants::AUTO_CHANGE_INTERVAL_BACK) {
this->currentEffect++;
this->idx = 0;
this->alternatePhase = false;
this->effectChangeTime = currentTime;
if (this->currentEffect >= LedConstants::MAX_EFFECT_BACK) {
this->currentEffect = 1; // Skip 0 (off) in auto mode
}
}
switch(this->currentEffect) {
case 0: // Off
safeFillSolid(CRGB::Black);
break;
case 1: // Random Red/Blue (original)
this->randomB();
break;
case 2: // Random with selected color
this->randomColored();
break;
case 3: // All on with selected color
this->allOn();
break;
case 4: // Alternate between two colors
this->alternateColors();
break;
case 5: // Sparkle effect
commonSparkle(LedConstants::RANDOM_THRESHOLD_MEDIUM);
break;
}
this->lastUpdate = currentTime;
}
void BackLeds::randomB() {
if (!validatePointers()) return;
safeFillSolid(CRGB::Black);
for(int i = 0; i < this->numleds; i++) {
if (isValidIndex(i)) {
int nr = random(0, 2);
if (nr) {
this->leds[i] = CRGB::Red;
}
else {
this->leds[i] = CRGB::Blue;
}
}
}
}
void BackLeds::randomColored() {
if (!validatePointers()) return;
for(int i = 0; i < this->numleds; i++) {
if (isValidIndex(i)) {
if (random(0, 2)) {
this->leds[i] = getCurrentColor();
} else {
this->leds[i] = CRGB::Black;
}
}
}
}
void BackLeds::allOn() {
safeFillSolid(getCurrentColor());
}
void BackLeds::alternateColors() {
if (!validatePointers()) return;
for(int i = 0; i < this->numleds; i++) {
if (isValidIndex(i)) {
if ((i % 2) == this->alternatePhase) {
this->leds[i] = getCurrentColor();
} else {
this->leds[i] = colorMap[(this->currentColor + 1) % LedConstants::COLOR_WRAP_MODULO];
}
}
}
this->alternatePhase = !this->alternatePhase;
}