-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLedControl.cpp
170 lines (136 loc) · 4.41 KB
/
LedControl.cpp
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
#include "LedControl.h"
CHSVPalette16 LedControl::_palette = CHSVPalette16(
CHSV(RED, 255, 255),
CHSV(RED, 255, 255),
CHSV(ORANGE, 255, 255),
CHSV(ORANGE, 255, 255),
CHSV(ORANGE, 255, 255),
CHSV(ORANGE, 255, 255),
CHSV(ORANGE, 255, 255),
CHSV(ORANGE, 255, 255),
CHSV(ORANGE, 255, 255),
CHSV(ORANGE, 255, 255),
CHSV(ORANGE, 255, 255),
CHSV(ORANGE, 255, 255),
CHSV(ORANGE, 255, 255),
CHSV(YELLOW, 255, 255),
CHSV(YELLOW, 255, 255),
CHSV(YELLOW, 255, 255));
CRGB LedControl::_leds[NUM_LEDS];
FlameMode LedControl::_flameMode;
FadeState LedControl::_fadeState;
int LedControl::_fadeDuration;
int LedControl::_fadeDelay;
long LedControl::_fadeStart;
void LedControl::setup()
{
FastLED.addLeds<WS2812B, LED_PIN, GRB>(_leds, NUM_LEDS).setCorrection(TypicalPixelString);
FastLED.setBrightness(MAX_BRIGHTNESS);
for (int i = 0; i < NUM_LEDS; i++)
{
_leds[i] = ColorFromPalette(_palette, random8());
}
}
uint8_t offset;
void LedControl::update()
{
switch (_flameMode)
{
case FlameMode::Flame:
EVERY_N_MILLISECONDS(100)
{
_leds[random8(NUM_LEDS)] = ColorFromPalette(_palette, random8());
}
EVERY_N_MILLISECONDS(10)
{
// Flicker LEDs
fadeToBlackBy(_leds, NUM_LEDS, 1);
}
break;
case FlameMode::Party:
EVERY_N_MILLISECONDS(5)
{
fill_rainbow(_leds, NUM_LEDS, offset++);
}
break;
case FlameMode::Offline:
uint8_t intensity;
EVERY_N_MILLISECONDS(5)
{
uint8_t intensity = cubicwave8(offset++);
fill_solid(_leds, NUM_LEDS, CHSV(OFFLINE, 255, intensity));
}
break;
}
uint8_t brightness = getBrightness();
resetFadeState(brightness);
FastLED.setBrightness(brightness);
FastLED.show();
}
void LedControl::resetFadeState(uint8_t brightness)
{
if (brightness == 0 && _fadeState == FadeState::FadeOut)
{
Serial.println("[LedControl] Resetting state to off after fade out");
off();
}
else if (brightness == MAX_BRIGHTNESS && _fadeState == FadeState::FadeIn)
{
Serial.println("[LedControl] Resetting state to on after fade in");
on();
}
}
uint8_t LedControl::getBrightness()
{
long elapsedMillis = millis() - _fadeStart;
switch (_fadeState)
{
case FadeState::FadeOut:
if (elapsedMillis < _fadeDelay)
{
return FastLED.getBrightness();
}
return constrain(MAX_BRIGHTNESS - round((elapsedMillis - _fadeDelay) / (float)_fadeDuration * MAX_BRIGHTNESS), 0, MAX_BRIGHTNESS);
case FadeState::FadeIn:
if (elapsedMillis < _fadeDelay)
{
return FastLED.getBrightness();
}
return constrain(round((elapsedMillis - _fadeDelay) / (float)_fadeDuration * MAX_BRIGHTNESS), 0, MAX_BRIGHTNESS);
default:
return FastLED.getBrightness();
}
}
void LedControl::fadeOut(int delay, int duration)
{
Serial.printf("[LedControl] Fading out for %dms with %d delay\n", duration, delay);
_fadeState = FadeState::FadeOut;
_fadeDuration = duration;
_fadeDelay = delay;
_fadeStart = millis();
}
void LedControl::fadeIn(int delay, int duration)
{
Serial.printf("[LedControl] Fading in for %dms with %d delay\n", duration, delay);
_fadeState = FadeState::FadeIn;
_fadeDuration = duration;
_fadeDelay = delay;
_fadeStart = millis();
}
void LedControl::on()
{
Serial.println("[LedControl] Turning on");
_fadeState = FadeState::On;
FastLED.setBrightness(MAX_BRIGHTNESS);
}
void LedControl::off()
{
Serial.println("[LedControl] Turning off");
_fadeState = FadeState::Off;
FastLED.setBrightness(0);
}
void LedControl::setMode(FlameMode mode)
{
Serial.printf("[LedControl] Setting mode to %d\n", mode);
_flameMode = mode;
}