From 9a25b41aaff6b0adf246e4c65e183df421f3f619 Mon Sep 17 00:00:00 2001 From: Robert Marshall Date: Thu, 18 Aug 2022 21:46:04 +0100 Subject: [PATCH] Simplify setup and control of LEDs --- src/BluetoothLightControl.cpp | 28 +++++---------------------- src/LedManager.cpp | 36 +++++++++++++++++++++++++++++++++++ src/Timer.cpp | 5 +++-- src/TimerManager.cpp | 13 +++++++------ src/main.cpp | 19 +++++++----------- 5 files changed, 58 insertions(+), 43 deletions(-) create mode 100644 src/LedManager.cpp diff --git a/src/BluetoothLightControl.cpp b/src/BluetoothLightControl.cpp index 636ceaf..53c8e7a 100644 --- a/src/BluetoothLightControl.cpp +++ b/src/BluetoothLightControl.cpp @@ -1,22 +1,17 @@ #include "LED.cpp" -#include "TimerManager.cpp" +#include "LedManager.cpp" #include class BluetoothLightControl{ - std::map _leds; - TimerManager *_timers; + LedManager *_leds; 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; } public: - BluetoothLightControl(TimerManager *timers){ - _timers = timers; - } - - void registerLEDs(int id, LED *leds){ - _leds[id] = leds; + BluetoothLightControl(LedManager *leds){ + _leds = leds; } 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; unsigned long timer = convertBytesToUnsignedLong(data[i + 6], data[i + 7], data[i + 8], data[i + 9]); - if (!_leds.count(id)) continue; - - auto led = _leds.find(id)->second; - - led->setBrightness(brightness); - - - if (on) - led->on(); - else - led->off(); - - if (timer > 0) - _timers->reset(led->getName(), timer); + _leds->setLedProperties(id, on, brightness, timer); } } }; diff --git a/src/LedManager.cpp b/src/LedManager.cpp new file mode 100644 index 0000000..9a6b863 --- /dev/null +++ b/src/LedManager.cpp @@ -0,0 +1,36 @@ +#include "LED.cpp" +#include "TimerManager.cpp" +#include +#include + +class LedManager { + TimerManager *_timers; + std::map _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); + } +}; \ No newline at end of file diff --git a/src/Timer.cpp b/src/Timer.cpp index 007ac3b..7a8933c 100644 --- a/src/Timer.cpp +++ b/src/Timer.cpp @@ -1,13 +1,14 @@ #include +#include class Timer{ private: unsigned long _interval, _lastTick; - void (*_callback)(void); + std::function _callback; bool _running; public: - Timer(void (*callback)(void)){ + Timer(std::function callback){ _callback = callback; _running = false; } diff --git a/src/TimerManager.cpp b/src/TimerManager.cpp index 7b4b04b..3bc2a5e 100644 --- a/src/TimerManager.cpp +++ b/src/TimerManager.cpp @@ -1,19 +1,20 @@ #include +#include #include "Timer.cpp" class TimerManager{ - std::map _timers; + std::map _timers; public: - void add(std::string name, void (*callback)(void)){ - _timers[name] = new Timer(callback); + void add(int id, std::function callback){ + _timers[id] = new Timer(callback); } - void reset(std::string name, unsigned long interval){ - if (!_timers.count(name)) + void reset(int id, unsigned long interval){ + if (!_timers.count(id)) return; - _timers.find(name)->second->reset(interval); + _timers.find(id)->second->reset(interval); } void loop(){ diff --git a/src/main.cpp b/src/main.cpp index 7d73726..a4277c0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,7 +20,8 @@ LEDOutput _cabLedOutput(4); LED _cabLeds("cab", &_cabLedOutput, FADE_IN_DURATION, FADE_OUT_DURATION); TimerManager _timers; -BluetoothLightControl _payloadLightControl(&_timers); +LedManager _leds(&_timers); +BluetoothLightControl _payloadLightControl(&_leds); BluetoothLEDCallback _btCallback(&_payloadLightControl); BluetoothService _btService("Van Lights", &_btCallback); @@ -40,17 +41,11 @@ void setup() { _cabLedOutput.attach(4); - _timers.add(_seatingLeds.getName(), []() { _seatingLeds.off(); }); - _timers.add(_kitchenLeds.getName(), []() { _kitchenLeds.off(); }); - _timers.add(_bathroomLeds.getName(), []() { _bathroomLeds.off(); }); - _timers.add(_awningLeds.getName(), []() { _awningLeds.off(); }); - _timers.add(_cabLeds.getName(), []() { _cabLeds.off(); }); - - _payloadLightControl.registerLEDs(1, &_seatingLeds); - _payloadLightControl.registerLEDs(2, &_kitchenLeds); - _payloadLightControl.registerLEDs(3, &_bathroomLeds); - _payloadLightControl.registerLEDs(4, &_awningLeds); - _payloadLightControl.registerLEDs(5, &_cabLeds); + _leds.registerLEDs(1, &_seatingLeds); + _leds.registerLEDs(2, &_kitchenLeds); + _leds.registerLEDs(3, &_bathroomLeds); + _leds.registerLEDs(4, &_awningLeds); + _leds.registerLEDs(5, &_cabLeds); _btService.init(); _btService.start();