From f986478e62c46eb2f100ed1c7186efe461208fa6 Mon Sep 17 00:00:00 2001 From: Philip Johansson Date: Fri, 19 Jun 2020 14:12:15 +0200 Subject: [PATCH] save color on OFF --- include/myled.h | 26 ++++++++++++++++++++++---- src/main.cpp | 18 ++++++++++++++++++ src/myled.cpp | 31 ++++++++++++++++++++++++++----- 3 files changed, 66 insertions(+), 9 deletions(-) diff --git a/include/myled.h b/include/myled.h index 204c026..46cac94 100644 --- a/include/myled.h +++ b/include/myled.h @@ -4,6 +4,16 @@ class MyLed { public: + typedef std::array ColorArr; + + typedef std::function CallbackFunc; + + typedef struct { + ColorArr color; + uint8_t brightness; + bool isOn; + } State; + MyLed(int ledPin, int ledCount, bool effects = false); void initialize(); @@ -14,7 +24,7 @@ public: void toggle(); - //! @param state Json package + //! @param jsonState Json package //! { //! "brightness": 255, //! "color": { @@ -26,8 +36,13 @@ public: //! } void set(String jsonState); + void setState(State); + + //! @return a Json string on the same format as for MyLed::set(jsonState) String get(); + State getState(); + //! @param brightness in [0, 1] void setBrightness(float brightness); @@ -38,10 +53,11 @@ public: void run(); -private: - void _apply(); + //! @param CallbackFunc will be called when the LED is turned off + void registerOffCallback(CallbackFunc); - typedef std::array ColorArr; +private: + void apply(); bool _useEffects; @@ -54,4 +70,6 @@ private: const ColorArr _defColor; ColorArr _color; + + CallbackFunc _callback; }; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 95f057d..8031ba7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -159,6 +159,21 @@ void mqttCallback(char* topic, byte* payload, unsigned int length) } } +void offCallback() +{ + auto state = myLed.getState(); + if (config.data.color == state.color && + config.data.brightness == state.brightness) + { + return; + } else + { + config.data.color = state.color; + config.data.brightness = state.brightness; + config.write(); + } +} + void setup() { pinMode(boardLedPin, OUTPUT); @@ -169,8 +184,11 @@ void setup() Serial.begin(9600); myLed.initialize(); + myLed.registerOffCallback(offCallback); config.load(); + myLed.setState( + MyLed::State({config.data.color, config.data.brightness, true})); WiFi.begin(config.data.ssid, config.data.pass); WiFi.hostname(config.data.hostname); Serial.print("Connecting"); diff --git a/src/myled.cpp b/src/myled.cpp index f501637..01d1f88 100644 --- a/src/myled.cpp +++ b/src/myled.cpp @@ -14,6 +14,7 @@ MyLed::MyLed(int ledPin, int ledCount, bool effects) , _isOn(true) , _defColor({255, 147, 41}) , _color(_defColor) +, _callback(nullptr) {} void MyLed::initialize() @@ -31,11 +32,14 @@ void MyLed::setOnState(bool on) Serial.println(on ? "Turning on" : "Turning off"); if (on) { - _apply(); + apply(); } else { _strip.fill({}); _strip.show(); + + if (_callback) + _callback(); }; } @@ -80,6 +84,13 @@ void MyLed::set(String jsonState) } } +void MyLed::setState(State s) +{ + setColor(s.color.at(0), s.color.at(1), s.color.at(2)); + setBrightness(static_cast(s.brightness) / 255.0f); + setOnState(s.isOn); +} + String MyLed::get() { return "{\"brightness\": " + String(_brightness * 255.0f) + @@ -89,6 +100,11 @@ String MyLed::get() String(_isOn ? "ON" : "OFF") + "\"}"; } +MyLed::State MyLed::getState() +{ + return {_color, static_cast(_brightness * 255.0f), _isOn}; +} + void MyLed::setBrightness(float brightness) { brightness = max(minBrightness, brightness); @@ -99,7 +115,7 @@ void MyLed::setBrightness(float brightness) } else { _brightness = brightness; - _apply(); + apply(); } } @@ -112,7 +128,7 @@ void MyLed::adjustBrightness(float diff) void MyLed::setColor(uint8_t r, uint8_t g, uint8_t b) { _color = {r, g, b}; - _apply(); + apply(); } void MyLed::run() @@ -124,11 +140,16 @@ void MyLed::run() const float mixFactor = 0.2; _brightness = _brightness * (1 - mixFactor) + _targetBrightness * mixFactor; - _apply(); + apply(); } } -void MyLed::_apply() +void MyLed::registerOffCallback(CallbackFunc callback) +{ + _callback = callback; +} + +void MyLed::apply() { _strip.fill(_strip.Color(static_cast(_color.at(0) * _brightness), static_cast(_color.at(1) * _brightness),