Repository navigation
Expand file tree
/
Copy pathTopLeds.cpp
More file actions
312 lines (259 loc) · 7.5 KB
/
Copy pathTopLeds.cpp
File metadata and controls
312 lines (259 loc) · 7.5 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
#include "TopLeds.h"
TopLeds::TopLeds(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_TOP;
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->theater_chase_q = 0;
this->currentEffect = 0;
this->currentColor = LedConstants::DEFAULT_COLOR_BLUE;
this->autoChange = false;
this->bounceDirection = 1;
// Validation
if (!validatePointers()) {
#ifdef DEBUG_MODE
Serial.println(F("ERROR: TopLeds - Invalid LED pointer"));
#endif
}
if (!validateNumLeds()) {
#ifdef DEBUG_MODE
Serial.print(F("WARNING: TopLeds - NumLEDs out of range: "));
Serial.println(numleds);
#endif
}
}
void TopLeds::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->bounceDirection = 1;
if (effect == 99) {
this->autoChange = true;
this->currentEffect = 0;
} else {
this->autoChange = false;
}
}
void TopLeds::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_SIDE) {
this->currentEffect++;
this->idx = 0;
this->bounceDirection = 1;
this->effectChangeTime = currentTime;
if (this->currentEffect >= LedConstants::MAX_EFFECT_TOP) {
this->currentEffect = 1; // Skip 0 (off) in auto mode
}
}
switch (this->currentEffect) {
case 0: // Off
safeFillSolid(CRGB::Black);
break;
case 1: // Left run
this->leftrun();
break;
case 2: // Left-right
this->leftright();
break;
case 3: // To center
this->tocenter();
break;
case 4: // Sparkle
commonSparkle();
break;
case 5: // Comet
this->comet();
break;
case 6: // Chase
this->chase();
break;
case 7: // Rainbow
commonRainbow();
break;
case 8: // Pulse
this->pulseEffect();
break;
case 9: // Bounce
this->bounce();
break;
case 10: // Fill from center
this->fillFromCenter();
break;
case 11: // Knight Rider
this->knightRider();
break;
case 12: // Twinkle
commonTwinkle();
break;
case 13: // Theater Chase (common)
commonTheaterChase();
break;
case 14: // Bounce with Trail
commonBounceWithTrail();
break;
case 15: // Color Gradient
commonColorGradient();
break;
}
this->lastUpdate = currentTime;
}
void TopLeds::leftrun()
{
if (!validatePointers()) return;
safeFillSolid(CRGB::Black);
if (isValidIndex(this->idx)) {
this->leds[this->idx] = getCurrentColor();
}
this->idx++;
if (this->idx >= this->numleds) {
this->idx = 0;
}
}
void TopLeds::leftright()
{
if (!validatePointers()) return;
safeFillSolid(CRGB::Black);
int left = this->idx;
int right = (this->numleds - 1) - this->idx;
if (isValidIndex(left)) {
this->leds[left] = getCurrentColor();
}
if (isValidIndex(right)) {
this->leds[right] = getCurrentColor();
}
this->idx++;
if (this->idx >= LedConstants::INDEX_SIX) {
this->idx = 0;
}
}
void TopLeds::tocenter()
{
if (!validatePointers()) return;
safeFillSolid(CRGB::Black);
int left = this->idx;
int right = (this->numleds - 1) - this->idx;
if (isValidIndex(left)) {
this->leds[left] = getCurrentColor();
}
if (isValidIndex(right)) {
this->leds[right] = getCurrentColor();
}
this->idx++;
if (this->idx >= LedConstants::INDEX_FOUR) {
this->idx = 0;
}
}
void TopLeds::comet()
{
if (!validatePointers()) return;
fadeToBlackBy(this->leds, this->numleds, LedConstants::FADE_AMOUNT_STANDARD);
int pos = beatsin16(LedConstants::BEATSIN_FREQUENCY_BREATHE, 0, this->numleds - 1);
if (isValidIndex(pos)) {
this->leds[pos] = getCurrentColor();
// Add trail
if (isValidIndex(pos - 1)) {
this->leds[pos - 1] = getCurrentColor();
this->leds[pos - 1].fadeToBlackBy(LedConstants::FADE_MULTIPLIER_THEATER);
}
if (isValidIndex(pos + 1)) {
this->leds[pos + 1] = getCurrentColor();
this->leds[pos + 1].fadeToBlackBy(LedConstants::FADE_MULTIPLIER_THEATER);
}
}
}
void TopLeds::chase()
{
if (!validatePointers()) return;
fadeToBlackBy(this->leds, this->numleds, LedConstants::FADE_AMOUNT_STANDARD);
if (isValidIndex(this->idx)) {
this->leds[this->idx] = getCurrentColor();
}
this->idx++;
if (this->idx >= this->numleds) {
this->idx = 0;
}
}
void TopLeds::pulseEffect()
{
if (!validatePointers()) return;
uint8_t brightness = beatsin8(LedConstants::BEATSIN_FREQUENCY_PULSE,
LedConstants::PULSE_VALUE_MIN,
LedConstants::PULSE_VALUE_MAX);
CRGB color = getCurrentColor();
for(int i = 0; i < this->numleds; i++) {
this->leds[i] = color;
this->leds[i].nscale8(brightness);
}
}
void TopLeds::bounce()
{
if (!validatePointers()) return;
safeFillSolid(CRGB::Black);
if (isValidIndex(this->idx)) {
this->leds[this->idx] = getCurrentColor();
// Add slight trail
if (this->bounceDirection > 0 && isValidIndex(this->idx - 1)) {
this->leds[this->idx - 1] = getCurrentColor();
this->leds[this->idx - 1].fadeToBlackBy(LedConstants::FADE_MULTIPLIER_WAVE);
} else if (this->bounceDirection < 0 && isValidIndex(this->idx + 1)) {
this->leds[this->idx + 1] = getCurrentColor();
this->leds[this->idx + 1].fadeToBlackBy(LedConstants::FADE_MULTIPLIER_WAVE);
}
}
this->idx += this->bounceDirection;
if (this->idx >= this->numleds - 1 || this->idx <= 0) {
this->bounceDirection = -this->bounceDirection;
}
}
void TopLeds::fillFromCenter()
{
if (!validatePointers()) return;
safeFillSolid(CRGB::Black);
int center = this->numleds / 2;
// Fill from center outward
for(int i = 0; i <= this->idx; i++) {
if (isValidIndex(center + i)) {
this->leds[center + i] = getCurrentColor();
}
if (isValidIndex(center - i)) {
this->leds[center - i] = getCurrentColor();
}
}
this->idx++;
if (this->idx > center) {
this->idx = 0;
}
}
void TopLeds::knightRider()
{
if (!validatePointers()) return;
fadeToBlackBy(this->leds, this->numleds, LedConstants::SCANNER_FADE_AMOUNT);
// Draw the scanner
for(int i = 0; i < LedConstants::SCANNER_WIDTH; i++) {
int pos = this->idx + i - (LedConstants::SCANNER_WIDTH / 2);
if (isValidIndex(pos)) {
this->leds[pos] = getCurrentColor();
// Center is brightest
if (i != LedConstants::SCANNER_WIDTH / 2) {
this->leds[pos].fadeToBlackBy(LedConstants::FADE_AMOUNT_VERY_HEAVY);
}
}
}
this->idx += this->bounceDirection;
if (this->idx >= this->numleds - 1 || this->idx <= 0) {
this->bounceDirection = -this->bounceDirection;
}
}