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

View file

@ -1,22 +1,17 @@
#include "LED.cpp" #include "LED.cpp"
#include "TimerManager.cpp" #include "LedManager.cpp"
#include <map> #include <map>
class BluetoothLightControl{ class BluetoothLightControl{
std::map<int, LED*> _leds; LedManager *_leds;
TimerManager *_timers;
unsigned long convertBytesToUnsignedLong(byte byte1, byte byte2, byte byte3, byte byte4) { unsigned long convertBytesToUnsignedLong(byte byte1, byte byte2, byte byte3, byte byte4) {
return (unsigned long)byte1 << 24 | (unsigned long)byte2 << 16 | (unsigned long)byte3 << 8 | (unsigned long)byte4; return (unsigned long)byte1 << 24 | (unsigned long)byte2 << 16 | (unsigned long)byte3 << 8 | (unsigned long)byte4;
} }
public: public:
BluetoothLightControl(TimerManager *timers){ BluetoothLightControl(LedManager *leds){
_timers = timers; _leds = leds;
}
void registerLEDs(int id, LED *leds){
_leds[id] = leds;
} }
void action(std::string input){ void action(std::string input){
@ -30,20 +25,7 @@ class BluetoothLightControl{
float brightness = convertBytesToUnsignedLong(data[i + 2], data[i + 3], data[i + 4], data[i + 5])/100.0f; float brightness = convertBytesToUnsignedLong(data[i + 2], data[i + 3], data[i + 4], data[i + 5])/100.0f;
unsigned long timer = convertBytesToUnsignedLong(data[i + 6], data[i + 7], data[i + 8], data[i + 9]); unsigned long timer = convertBytesToUnsignedLong(data[i + 6], data[i + 7], data[i + 8], data[i + 9]);
if (!_leds.count(id)) continue; _leds->setLedProperties(id, on, brightness, timer);
auto led = _leds.find(id)->second;
led->setBrightness(brightness);
if (on)
led->on();
else
led->off();
if (timer > 0)
_timers->reset(led->getName(), timer);
} }
} }
}; };

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);
}
};

View file

@ -1,13 +1,14 @@
#include <Arduino.h> #include <Arduino.h>
#include <functional>
class Timer{ class Timer{
private: private:
unsigned long _interval, _lastTick; unsigned long _interval, _lastTick;
void (*_callback)(void); std::function<void()> _callback;
bool _running; bool _running;
public: public:
Timer(void (*callback)(void)){ Timer(std::function<void()> callback){
_callback = callback; _callback = callback;
_running = false; _running = false;
} }

View file

@ -1,19 +1,20 @@
#include <map> #include <map>
#include <functional>
#include "Timer.cpp" #include "Timer.cpp"
class TimerManager{ class TimerManager{
std::map<std::string, Timer*> _timers; std::map<int, Timer*> _timers;
public: public:
void add(std::string name, void (*callback)(void)){ void add(int id, std::function<void()> callback){
_timers[name] = new Timer(callback); _timers[id] = new Timer(callback);
} }
void reset(std::string name, unsigned long interval){ void reset(int id, unsigned long interval){
if (!_timers.count(name)) if (!_timers.count(id))
return; return;
_timers.find(name)->second->reset(interval); _timers.find(id)->second->reset(interval);
} }
void loop(){ void loop(){

View file

@ -20,7 +20,8 @@ LEDOutput _cabLedOutput(4);
LED _cabLeds("cab", &_cabLedOutput, FADE_IN_DURATION, FADE_OUT_DURATION); LED _cabLeds("cab", &_cabLedOutput, FADE_IN_DURATION, FADE_OUT_DURATION);
TimerManager _timers; TimerManager _timers;
BluetoothLightControl _payloadLightControl(&_timers); LedManager _leds(&_timers);
BluetoothLightControl _payloadLightControl(&_leds);
BluetoothLEDCallback _btCallback(&_payloadLightControl); BluetoothLEDCallback _btCallback(&_payloadLightControl);
BluetoothService _btService("Van Lights", &_btCallback); BluetoothService _btService("Van Lights", &_btCallback);
@ -40,17 +41,11 @@ void setup() {
_cabLedOutput.attach(4); _cabLedOutput.attach(4);
_timers.add(_seatingLeds.getName(), []() { _seatingLeds.off(); }); _leds.registerLEDs(1, &_seatingLeds);
_timers.add(_kitchenLeds.getName(), []() { _kitchenLeds.off(); }); _leds.registerLEDs(2, &_kitchenLeds);
_timers.add(_bathroomLeds.getName(), []() { _bathroomLeds.off(); }); _leds.registerLEDs(3, &_bathroomLeds);
_timers.add(_awningLeds.getName(), []() { _awningLeds.off(); }); _leds.registerLEDs(4, &_awningLeds);
_timers.add(_cabLeds.getName(), []() { _cabLeds.off(); }); _leds.registerLEDs(5, &_cabLeds);
_payloadLightControl.registerLEDs(1, &_seatingLeds);
_payloadLightControl.registerLEDs(2, &_kitchenLeds);
_payloadLightControl.registerLEDs(3, &_bathroomLeds);
_payloadLightControl.registerLEDs(4, &_awningLeds);
_payloadLightControl.registerLEDs(5, &_cabLeds);
_btService.init(); _btService.init();
_btService.start(); _btService.start();