Repository navigation
Expand file tree
/
Copy pathBaseLeds.cpp
More file actions
223 lines (176 loc) · 6.17 KB
/
Copy pathBaseLeds.cpp
File metadata and controls
223 lines (176 loc) · 6.17 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
#include "BaseLeds.h"
// ============================================
// Common Method Implementations
// ============================================
void BaseLeds::setColor(int color) {
if (color >= 0 && color <= LedConstants::MAX_COLOR_INDEX) {
this->currentColor = color;
}
}
void BaseLeds::setSpeed(int speed) {
if (speed > 0) {
this->speed = speed;
this->pulse_speed = speed / 5;
if (this->pulse_speed < LedConstants::PULSE_SPEED_MINIMUM) {
this->pulse_speed = LedConstants::PULSE_SPEED_MINIMUM;
}
}
}
// ============================================
// Common Effects (Eliminates Code Duplication)
// ============================================
void BaseLeds::commonFire(byte* heat, int heatSize) {
if (!validatePointers() || !heat) return;
// Bounds check
int effectiveSize = min(heatSize, this->numleds);
// Cool down every cell a little
for(int i = 0; i < effectiveSize; i++) {
heat[i] = qsub8(heat[i], random8(0, ((LedConstants::FIRE_COOLING_BASE * LedConstants::FIRE_COOLING_MULTIPLIER) / effectiveSize) + 2));
}
// Heat from each cell drifts up and diffuses slightly
for(int k = effectiveSize - 1; k >= 2; k--) {
heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3;
}
// Randomly ignite new sparks near bottom
if(random8() < LedConstants::FIRE_IGNITION_THRESHOLD) {
int maxRange = min((int)LedConstants::FIRE_SPARK_RANGE, effectiveSize - 1);
int y = random8(maxRange);
heat[y] = qadd8(heat[y], random8(LedConstants::FIRE_HEAT_MIN, LedConstants::FIRE_HEAT_MAX));
}
// Map from heat cells to LED colors
for(int j = 0; j < effectiveSize; j++) {
CRGB color = HeatColor(heat[j]);
this->leds[j] = color;
}
}
void BaseLeds::commonRainbow() {
if (!validatePointers()) return;
static uint8_t hue = 0;
fill_rainbow(this->leds, this->numleds, hue, 255 / this->numleds);
// Speed determines how fast the rainbow rotates
uint8_t hueShift = map(this->speed, LedConstants::FADE_AMOUNT_STANDARD, 200,
LedConstants::RAINBOW_HUE_SHIFT_FAST, LedConstants::RAINBOW_HUE_SHIFT_SLOW);
hue += hueShift;
}
void BaseLeds::commonSparkle(uint8_t threshold) {
if (!validatePointers()) return;
fadeToBlackBy(this->leds, this->numleds, LedConstants::FADE_AMOUNT_LIGHT);
if (random8() < threshold) {
int pos = random8(this->numleds);
if (isValidIndex(pos)) {
this->leds[pos] = getCurrentColor();
}
}
}
void BaseLeds::commonStrobe() {
if (!validatePointers()) return;
if (this->strobe_ind) {
safeFillSolid(getCurrentColor());
} else {
safeFillSolid(CRGB::Black);
}
this->strobe_ind = !this->strobe_ind;
}
void BaseLeds::commonPulseAll() {
if (!validatePointers()) return;
CHSV hsv = rgb2hsv_approximate(getCurrentColor());
hsv.v = this->pulse;
for(int i = 0; i < this->numleds; i++) {
this->leds[i] = hsv;
}
this->pulse += this->pulse_offset;
if (this->pulse >= LedConstants::PULSE_VALUE_MAX || this->pulse <= LedConstants::PULSE_VALUE_MIN) {
this->pulse_offset = -(this->pulse_offset);
}
}
void BaseLeds::commonTwinkle() {
if (!validatePointers()) return;
// Randomly fade all LEDs
for(int i = 0; i < this->numleds; i++) {
this->leds[i].fadeToBlackBy(random8(5, 20));
}
// Randomly light new ones (probability based on speed)
uint8_t probability = map(this->speed, 20, 400, 200, 50); // Higher speed = more twinkles
if(random8() < probability) {
int pos = random8(this->numleds);
if (isValidIndex(pos)) {
this->leds[pos] = getCurrentColor();
this->leds[pos].maximizeBrightness();
}
}
}
void BaseLeds::commonTheaterChase() {
if (!validatePointers()) return;
// Clear all
safeFillSolid(CRGB::Black);
// Light every 3rd LED based on current position
for(int i = 0; i < this->numleds; i += 3) {
int ledIndex = i + this->theater_chase_q;
if (isValidIndex(ledIndex)) {
this->leds[ledIndex] = getCurrentColor();
}
}
// Cycle through 0, 1, 2 positions
this->theater_chase_q++;
if (this->theater_chase_q >= 3) {
this->theater_chase_q = 0;
}
}
void BaseLeds::commonBounceWithTrail() {
if (!validatePointers()) return;
// Fade all LEDs for trail effect
fadeToBlackBy(this->leds, this->numleds, 40);
// Calculate bounce position using beatsin
uint8_t bpm = map(this->speed, 20, 400, 10, 60);
int pos = beatsin16(bpm, 0, this->numleds - 1);
// Set current position
if (isValidIndex(pos)) {
this->leds[pos] = getCurrentColor();
}
// Add blur for smoother trail
blur1d(this->leds, this->numleds, 128);
}
void BaseLeds::commonColorGradient() {
if (!validatePointers()) return;
// Create gradient from current color to its complement
CRGB color1 = getCurrentColor();
CHSV hsv = rgb2hsv_approximate(color1);
// Opposite hue for complement color
hsv.hue += 128;
CRGB color2 = hsv;
// Fill with gradient
fill_gradient_RGB(this->leds, 0, color1, this->numleds - 1, color2);
// Rotate hue over time for animation
EVERY_N_MILLISECONDS(50) {
for(int i = 0; i < this->numleds; i++) {
this->leds[i] += CHSV(this->idx, 0, 0);
}
this->idx++;
}
}
// ============================================
// Utility Methods
// ============================================
bool BaseLeds::isValidIndex(int index) const {
return (index >= 0 && index < this->numleds);
}
void BaseLeds::safeFillSolid(CRGB color) {
if (validatePointers()) {
fill_solid(this->leds, this->numleds, color);
}
}
CRGB BaseLeds::getCurrentColor() const {
if (this->currentColor >= 0 && this->currentColor <= LedConstants::MAX_COLOR_INDEX) {
return colorMap[this->currentColor];
}
return CRGB::White; // Fallback
}
// ============================================
// Validation Methods
// ============================================
bool BaseLeds::validatePointers() const {
return (this->leds != nullptr && this->numleds > 0);
}
bool BaseLeds::validateNumLeds() const {
return (this->numleds > 0 && this->numleds <= LedConstants::MAX_LEDS_PER_STRIP);
}