Simplify setup and control of LEDs

This commit is contained in:
Robert Marshall 2022-08-18 21:46:04 +01:00
parent e03aae9d4d
commit 9a25b41aaf
5 changed files with 58 additions and 43 deletions

36
src/LedManager.cpp Normal file
View file

@ -0,0 +1,36 @@
#include "LED.cpp"
#include "TimerManager.cpp"
#include <map>
#include <functional>
class LedManager {
TimerManager *_timers;
std::map<int, LED *> _leds;
public:
LedManager(TimerManager *timers) {
_timers = timers;
}
void registerLEDs(int id, LED *leds) {
_leds[id] = leds;
_timers->add(id, [&]() { leds->off(); });
}
void setLedProperties(int id, bool on, float brightness, unsigned long timer) {
if (!_leds.count(id))
return;
auto led = _leds.find(id)->second;
led->setBrightness(brightness);
if (on)
led->on();
else
led->off();
if (timer > 0)
_timers->reset(id, timer);
}
};