Repository navigation
Expand file tree
/
Copy pathSideLeds.cpp
More file actions
182 lines (158 loc) · 4.62 KB
/
Copy pathSideLeds.cpp
File metadata and controls
182 lines (158 loc) · 4.62 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
#include "SideLeds.h"
SideLeds::SideLeds(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_SIDE;
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 = 0; // Red
this->autoChange = false;
// Initialize fire heat array
for(int i = 0; i < LedConstants::MAX_LEDS_PER_STRIP; i++) {
this->heat[i] = 0;
}
// Validation
if (!validatePointers()) {
#ifdef DEBUG_MODE
Serial.println(F("ERROR: SideLeds - Invalid LED pointer"));
#endif
}
if (!validateNumLeds()) {
#ifdef DEBUG_MODE
Serial.print(F("WARNING: SideLeds - NumLEDs out of range: "));
Serial.println(numleds);
#endif
}
}
void SideLeds::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;
if (effect == 99) {
this->autoChange = true;
this->currentEffect = 0;
} else {
this->autoChange = false;
}
}
void SideLeds::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->speed = LedConstants::DEFAULT_SPEED_SIDE;
this->idx = 0;
this->pulse = LedConstants::PULSE_VALUE_DEFAULT;
this->pulse_offset = LedConstants::PULSE_OFFSET_DEFAULT;
this->strobe_ind = false;
this->effectChangeTime = currentTime;
if (this->currentEffect >= LedConstants::MAX_EFFECT_SIDE) {
this->currentEffect = 1; // Skip 0 (off) in auto mode
}
}
switch(this->currentEffect) {
case 0: // Off
safeFillSolid(CRGB::Black);
break;
case 1: // Pulse
this->pulseEffect();
break;
case 2: // CW run 1
this->cw_run(1);
break;
case 3: // CW run 2
this->cw_run(2);
break;
case 4: // CW run 3
this->cw_run(3);
break;
case 5: // Strobe
commonStrobe();
break;
case 6: // Breathe
this->breathe();
break;
case 7: // Fire
commonFire(this->heat, LedConstants::MAX_LEDS_PER_STRIP);
break;
case 8: // Sparkle
commonSparkle();
break;
case 9: // Rainbow
commonRainbow();
break;
case 10: // Twinkle
commonTwinkle();
break;
case 11: // Theater Chase (common)
commonTheaterChase();
break;
case 12: // Bounce with Trail
commonBounceWithTrail();
break;
case 13: // Color Gradient
commonColorGradient();
break;
}
this->lastUpdate = currentTime;
}
void SideLeds::pulseEffect()
{
if (!validatePointers()) return;
CHSV hsv = rgb2hsv_approximate(getCurrentColor());
hsv.v = this->idx;
for(int i = 0; i < this->numleds; i++) {
this->leds[i] = hsv;
}
this->idx += this->pulse_offset;
if (this->idx >= LedConstants::PULSE_VALUE_MAX || this->idx <= LedConstants::PULSE_VALUE_MIN) {
this->pulse_offset = -this->pulse_offset;
this->idx += this->pulse_offset;
}
}
void SideLeds::cw_run(int pt)
{
if (!validatePointers()) return;
safeFillSolid(CRGB::Black);
// Always keep center LED on with current color (last LED = center)
int centerLed = this->numleds - 1;
if (isValidIndex(centerLed)) {
this->leds[centerLed] = getCurrentColor();
}
// Running LEDs in circle (LEDs 0 to numleds-2)
for(int x = 0; x < pt; x++) {
int y = (this->idx + x) % LedConstants::MAIN_LED_CIRCLE_SIZE;
if (isValidIndex(y)) {
this->leds[y] = getCurrentColor();
}
}
this->idx++;
if (this->idx >= LedConstants::MAIN_LED_CIRCLE_SIZE) {
this->idx = 0;
}
}
void SideLeds::breathe()
{
if (!validatePointers()) return;
uint8_t breath = beatsin8(LedConstants::BEATSIN_FREQUENCY_BREATHE,
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(breath);
}
}