renderbug-cpp/firmware/inputs/ColorCycle.h
2021-03-28 14:50:31 -07:00

42 lines
1.0 KiB
C++

#include "../Figments/Figments.h"
using CRGB = NSFastLED::CRGB;
template<int Period>
class ColorSequenceInput: public InputSource {
public:
ColorSequenceInput(const std::vector<CRGB> &colors, const char* name, Task::State initialState)
: InputSource(name, initialState), m_colors(colors) {}
InputEvent read() override {
if (m_reset) {
m_reset = false;
m_override = false;
return InputEvent{InputEvent::SetColor, m_colors[m_idx]};
}
if (!m_override) {
EVERY_N_SECONDS(Period) {
m_idx %= m_colors.size();
return InputEvent{InputEvent::SetColor, m_colors[m_idx++]};
}
}
return InputEvent{};
}
void handleEvent(const InputEvent& event) {
if (event.intent == InputEvent::SetColor) {
m_override = true;
}
}
void onStart() override {
m_reset = true;
}
private:
std::vector<CRGB> m_colors;
int m_idx = 0;
bool m_reset = true;
bool m_override = false;
};