Skip to content

Commit c554da1

Browse files
committed
centralize rainbow/disco mode logic
1 parent 1d1ba29 commit c554da1

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

src/LEDState.h

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -226,17 +226,49 @@ class StatefulLED {
226226
};
227227

228228

229+
// All Binky LEDs respond to global modes (like rainbow)
230+
// The local states must be defined as member variables, and a chooseNextLocalState function
231+
// chooses which state to point to when it is time to find the next one. This ensures
232+
// we only need to handle global behaviors here.
233+
class BinkyLED : public StatefulLED {
234+
public:
235+
SolidColorTimedState m_stOn;
236+
SolidColorState m_stOff;
237+
PartyMode m_partymode;
238+
RainbowState m_stRainbow;
239+
unsigned long m_canFlashMs;
240+
241+
BinkyLED(struct CRGB* leds, int numLEDs, int index) :
242+
StatefulLED(leds, numLEDs, index),
243+
m_stOn(COLOR_WHITE, FLASH_DURATION_MS / 10),
244+
m_stOff(COLOR_BLACK),
245+
m_partymode(PartyMode::Values::rainbow);
246+
m_stRainbow(numLEDs, index),
247+
m_canFlashMs(0)
248+
{}
249+
250+
// the master signal for rainbow mode overrides all others
251+
virtual LEDState* chooseNextState(unsigned long const &millis, const SlaveState &slave) {
252+
if (slave.getMasterSignal(MasterSignal::Values::scrollRainbowEffects)) {
253+
return &m_stRainbow;
254+
} else {
255+
return chooseNextLocalState(millis, slave);
256+
}
257+
}
258+
259+
// what state to pick next
260+
virtual LEDState* chooseNextLocalState(unsigned long const &millis, const SlaveState &slave) = 0;
261+
};
262+
229263
// A simple LED switches between a solid color mode (on/off) and a rainbow mode
230264
// the on/off criteria cna be overridden
231-
class SimpleLED : public StatefulLED {
265+
class SimpleLED : public BinkyLED {
232266
public:
233-
RainbowState m_stRainbow;
234267
SolidColorState m_stOff;
235268
SolidColorState m_stOn;
236269

237270
SimpleLED(struct CRGB* leds, int numLEDs, int index, const struct CHSV &color) :
238-
StatefulLED(leds, numLEDs, index),
239-
m_stRainbow(numLEDs, index),
271+
BinkyLED(leds, numLEDs, index),
240272
m_stOff(COLOR_BLACK),
241273
m_stOn(color)
242274
{}
@@ -246,12 +278,8 @@ class SimpleLED : public StatefulLED {
246278
{}
247279

248280
// the master signal for rainbow mode overrides all others
249-
virtual LEDState* chooseNextState(unsigned long const &millis, const SlaveState &slave) {
250-
if (slave.getMasterSignal(MasterSignal::Values::scrollRainbowEffects)) {
251-
return &m_stRainbow;
252-
} else {
253-
return isOn(millis, slave) ? &m_stOn : &m_stOff;
254-
}
281+
virtual LEDState* chooseNextLocalState(unsigned long const &millis, const SlaveState &slave) {
282+
return isOn(millis, slave) ? &m_stOn : &m_stOff;
255283
}
256284

257285
// by default, always on
@@ -321,7 +349,7 @@ class RearFoggerLED : public SimpleLED {
321349

322350

323351
// This class defines a blinking LED that can blink 2 different colors
324-
class MultiBlinkingLED : public StatefulLED {
352+
class MultiBlinkingLED : public BinkyLED {
325353
public:
326354
SolidColorState m_stSolid;
327355
FlashLoudState m_stFlashRedLoud;
@@ -333,7 +361,7 @@ class MultiBlinkingLED : public StatefulLED {
333361
// the solid state can be interrupted at any time
334362

335363
MultiBlinkingLED(struct CRGB* leds, int numLEDs, int index) :
336-
StatefulLED(leds, numLEDs, index),
364+
BinkyLED(leds, numLEDs, index),
337365
m_stSolid(COLOR_WHITE),
338366
m_stFlashRedLoud(COLOR_RED),
339367
m_stFlashRedQuiet(m_stSolid),
@@ -358,10 +386,8 @@ class MultiBlinkingLED : public StatefulLED {
358386
virtual bool isCritical(const SlaveState &slave) const = 0;
359387

360388
// what state to pick next
361-
virtual LEDState* chooseNextState(unsigned long const &millis, const SlaveState &slave) {
362-
if (slave.getMasterSignal(MasterSignal::Values::scrollRainbowEffects)) {
363-
return &m_stRainbow;
364-
} else if (isCritical(slave)) {
389+
virtual LEDState* chooseNextLocalState(unsigned long const &millis, const SlaveState &slave) {
390+
if (isCritical(slave)) {
365391
seedFlashTiming(millis);
366392
// loud states must transition to quiet to complete the blink. all others go loud immediately
367393
return (inState(m_stFlashRedLoud) || inState(m_stFlashAmberLoud)) ? (LEDState*)&m_stFlashRedQuiet : (LEDState*)&m_stFlashRedLoud;

0 commit comments

Comments
 (0)