From 4e9f6c11164ec1f177ba18da8749b333e7152a17 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Mon, 10 Feb 2025 09:49:57 +0100 Subject: [PATCH 1/6] make Brightness a class instead of namespace --- include/devices.h | 6 +++--- include/display.h | 27 ++++++++++++++++----------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/include/devices.h b/include/devices.h index 1bd3fe5..9ef1228 100644 --- a/include/devices.h +++ b/include/devices.h @@ -37,7 +37,7 @@ namespace Devices { auto brightnessMqtt = Builder::instance(new Number{ "Brightness", "brightness", [](const char* msg) { - Display::Brightness::set(String{ msg }.toInt()); + Display::brightness = String{ msg }.toInt(); } }).withMin(BRIGHTNESS_MIN).withMax(BRIGHTNESS_MAX).withStep(BRIGHTNESS_STEP).restoreStateFromCommand().build(); @@ -134,8 +134,8 @@ namespace Devices { Display::hourFormatChangedCallback = []{ hourFormatMqtt->updateState(Display::hourFormat24); }; - Display::Brightness::brightnessChangedCallback = []{ - brightnessMqtt->updateState(Display::Brightness::current); + Display::brightness.changedCallback = []{ + brightnessMqtt->updateState(Display::brightness); }; Display::Timer::remainingCallback = [](int8 current){ timerRemainingMqtt->updateState(to_string(current).c_str()); diff --git a/include/display.h b/include/display.h index dce6e48..28b50f1 100644 --- a/include/display.h +++ b/include/display.h @@ -62,20 +62,25 @@ namespace Display { // Create display object Adafruit_7segment clockDisplay = Adafruit_7segment(); - namespace Brightness { - uint8_t current = BRIGHTNESS_NIGHT; - void (*brightnessChangedCallback)(); + class Brightness { + uint8 current = BRIGHTNESS_NIGHT; + public: + void (*changedCallback)(); - void set(uint8_t value) { + void operator=(uint8 value) { current = value % (BRIGHTNESS_MAX + 1); clockDisplay.setBrightness(current); - if (brightnessChangedCallback) brightnessChangedCallback(); + if (changedCallback) changedCallback(); + } + + operator uint8() { + return current; } void change(bool increase) { - increase ? set(current + BRIGHTNESS_STEP) : set(current - BRIGHTNESS_STEP); + increase ? *this = current + BRIGHTNESS_STEP : *this = current - BRIGHTNESS_STEP; } - } + } brightness; namespace Timer { int8 timer = 0, current = 0; @@ -155,11 +160,11 @@ namespace Display { Ntp::tUpdateTime.restart(); } if (currentHour == 8) { - Brightness::set(BRIGHTNESS_DAY); + brightness = BRIGHTNESS_DAY; Wifi::tConnect.enable(); } if (currentHour == 17) { - Brightness::set(BRIGHTNESS_NIGHT); + brightness = BRIGHTNESS_NIGHT; } } static int currentMin = -1; @@ -194,7 +199,7 @@ namespace Display { tDisplaySensor.setCallback(&displayTemp); } - void displayValue(uint8_t value) { + void displayValue(uint8 value) { tDisplayTime.disable(); clockDisplay.print(value, HEX); clockDisplay.writeDisplay(); @@ -232,7 +237,7 @@ namespace Display { void setup() { clockDisplay.begin(DISPLAY_ADDRESS); - clockDisplay.setBrightness(Brightness::current); + clockDisplay.setBrightness(brightness); tDisplayTime.enable(); } } From 6e608da1f035068946044516e85292902e415d3f Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Mon, 10 Feb 2025 10:27:07 +0100 Subject: [PATCH 2/6] move brightness defines inside the struct --- include/devices.h | 2 +- include/display.h | 25 +++++++++++++------------ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/include/devices.h b/include/devices.h index 9ef1228..3d0a1d0 100644 --- a/include/devices.h +++ b/include/devices.h @@ -39,7 +39,7 @@ namespace Devices { [](const char* msg) { Display::brightness = String{ msg }.toInt(); } - }).withMin(BRIGHTNESS_MIN).withMax(BRIGHTNESS_MAX).withStep(BRIGHTNESS_STEP).restoreStateFromCommand().build(); + }).withMin(Display::Brightness::MIN).withMax(Display::Brightness::MAX).withStep(Display::Brightness::STEP).restoreStateFromCommand().build(); auto hourFormatMqtt = Builder::instance(new Switch{ "Format 24h", "format_24h", [](const char* msg) { diff --git a/include/display.h b/include/display.h index 28b50f1..b2b5c2c 100644 --- a/include/display.h +++ b/include/display.h @@ -8,11 +8,6 @@ #include "bme.h" #define DISPLAY_ADDRESS 0x70 -#define BRIGHTNESS_MIN 0 -#define BRIGHTNESS_MAX 15 -#define BRIGHTNESS_STEP 1 -#define BRIGHTNESS_NIGHT BRIGHTNESS_MIN -#define BRIGHTNESS_DAY 11 #define MILLISECONDS(value) value*TASK_MILLISECOND #define SECONDS(value) value*TASK_SECOND #define MINUTES(value) value*TASK_MINUTE @@ -62,13 +57,16 @@ namespace Display { // Create display object Adafruit_7segment clockDisplay = Adafruit_7segment(); - class Brightness { - uint8 current = BRIGHTNESS_NIGHT; - public: + struct Brightness { + static constexpr uint8 MIN = 0; + static constexpr uint8 MAX = 15; + static constexpr uint8 STEP = 1; + static constexpr uint8 DAY = 11; + static constexpr uint8 NIGHT = MIN; void (*changedCallback)(); void operator=(uint8 value) { - current = value % (BRIGHTNESS_MAX + 1); + current = value % (MAX + 1); clockDisplay.setBrightness(current); if (changedCallback) changedCallback(); } @@ -78,8 +76,11 @@ namespace Display { } void change(bool increase) { - increase ? *this = current + BRIGHTNESS_STEP : *this = current - BRIGHTNESS_STEP; + increase ? *this = current + STEP : *this = current - STEP; } + + private: + uint8 current = NIGHT; } brightness; namespace Timer { @@ -160,11 +161,11 @@ namespace Display { Ntp::tUpdateTime.restart(); } if (currentHour == 8) { - brightness = BRIGHTNESS_DAY; + brightness = Brightness::DAY; Wifi::tConnect.enable(); } if (currentHour == 17) { - brightness = BRIGHTNESS_NIGHT; + brightness = Brightness::NIGHT; } } static int currentMin = -1; From dbaae667ec560f785334870076c08710ae588f75 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Mon, 10 Feb 2025 11:26:59 +0100 Subject: [PATCH 3/6] remove unused incremental increase/decrease of brightness --- include/devices.h | 2 +- include/display.h | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/include/devices.h b/include/devices.h index 3d0a1d0..10f28eb 100644 --- a/include/devices.h +++ b/include/devices.h @@ -39,7 +39,7 @@ namespace Devices { [](const char* msg) { Display::brightness = String{ msg }.toInt(); } - }).withMin(Display::Brightness::MIN).withMax(Display::Brightness::MAX).withStep(Display::Brightness::STEP).restoreStateFromCommand().build(); + }).withMin(Display::Brightness::MIN).withMax(Display::Brightness::MAX).withStep(1).restoreStateFromCommand().build(); auto hourFormatMqtt = Builder::instance(new Switch{ "Format 24h", "format_24h", [](const char* msg) { diff --git a/include/display.h b/include/display.h index b2b5c2c..cfbccf8 100644 --- a/include/display.h +++ b/include/display.h @@ -60,7 +60,6 @@ namespace Display { struct Brightness { static constexpr uint8 MIN = 0; static constexpr uint8 MAX = 15; - static constexpr uint8 STEP = 1; static constexpr uint8 DAY = 11; static constexpr uint8 NIGHT = MIN; void (*changedCallback)(); @@ -75,10 +74,6 @@ namespace Display { return current; } - void change(bool increase) { - increase ? *this = current + STEP : *this = current - STEP; - } - private: uint8 current = NIGHT; } brightness; From f10c456f689fc1987a070d48b34f3f4d5f8bdbf3 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Mon, 10 Feb 2025 12:00:31 +0100 Subject: [PATCH 4/6] encapsulate hourFormat24 under a struct --- include/devices.h | 4 ++-- include/display.h | 23 +++++++++++++++-------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/include/devices.h b/include/devices.h index 10f28eb..10bdf83 100644 --- a/include/devices.h +++ b/include/devices.h @@ -43,7 +43,7 @@ namespace Devices { auto hourFormatMqtt = Builder::instance(new Switch{ "Format 24h", "format_24h", [](const char* msg) { - strcmp("ON", msg) == 0 ? Display::changeHourFormat24(true) : Display::changeHourFormat24(false); + Display::hourFormat24 = (strcmp("ON", msg) == 0); } }).restoreStateFromCommand().build(); Number* displayTimerMqtt = Builder::instance(new Number{ "Timer duration", "timer_duration", @@ -131,7 +131,7 @@ namespace Devices { } void setup() { - Display::hourFormatChangedCallback = []{ + Display::hourFormat24.changedCallback = []{ hourFormatMqtt->updateState(Display::hourFormat24); }; Display::brightness.changedCallback = []{ diff --git a/include/display.h b/include/display.h index cfbccf8..5cdf099 100644 --- a/include/display.h +++ b/include/display.h @@ -51,8 +51,21 @@ namespace Display { tDisplayTime.enable(); }); - bool hourFormat24 = false; - void (*hourFormatChangedCallback)(); + struct HourFormat { + void (*changedCallback)(); + + operator bool() { + return format24; + } + + void operator=(bool value) { + format24 = value; + tDisplayTime.restart(); + if (changedCallback) changedCallback(); + } + private: + bool format24 = false; + } hourFormat24; // Create display object Adafruit_7segment clockDisplay = Adafruit_7segment(); @@ -225,12 +238,6 @@ namespace Display { clockDisplay.writeDisplay(); } - void changeHourFormat24(bool format24) { - hourFormat24 = format24; - tDisplayTime.restart(); - if (hourFormatChangedCallback) hourFormatChangedCallback(); - } - void setup() { clockDisplay.begin(DISPLAY_ADDRESS); clockDisplay.setBrightness(brightness); From 3df8f48538147cfb84ceb15e862fe57edc7e2676 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Mon, 10 Feb 2025 12:50:44 +0100 Subject: [PATCH 5/6] add generic callback struct and use it for brightness and hourFormat24 --- include/devices.h | 8 ++++---- include/display.h | 21 +++++++++++++++------ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/include/devices.h b/include/devices.h index 10bdf83..267b0db 100644 --- a/include/devices.h +++ b/include/devices.h @@ -131,12 +131,12 @@ namespace Devices { } void setup() { - Display::hourFormat24.changedCallback = []{ + Display::hourFormat24.registerCallback([]{ hourFormatMqtt->updateState(Display::hourFormat24); - }; - Display::brightness.changedCallback = []{ + }); + Display::brightness.registerCallback([]{ brightnessMqtt->updateState(Display::brightness); - }; + }); Display::Timer::remainingCallback = [](int8 current){ timerRemainingMqtt->updateState(to_string(current).c_str()); }; diff --git a/include/display.h b/include/display.h index 5cdf099..c08791e 100644 --- a/include/display.h +++ b/include/display.h @@ -14,6 +14,17 @@ #define DISPLAY_SENSOR_ITERATIONS 2+1 #define DISPLAY_DELAY (SECONDS(2)) +typedef void (*callback_t)(); + +template +struct CallbackAware { + void registerCallback(T f) { + callback = f; + } +protected: + T callback; +}; + namespace Display { void displayTime(); @@ -51,8 +62,7 @@ namespace Display { tDisplayTime.enable(); }); - struct HourFormat { - void (*changedCallback)(); + struct HourFormat : public CallbackAware { operator bool() { return format24; @@ -61,7 +71,7 @@ namespace Display { void operator=(bool value) { format24 = value; tDisplayTime.restart(); - if (changedCallback) changedCallback(); + if (callback) callback(); } private: bool format24 = false; @@ -70,17 +80,16 @@ namespace Display { // Create display object Adafruit_7segment clockDisplay = Adafruit_7segment(); - struct Brightness { + struct Brightness : public CallbackAware { static constexpr uint8 MIN = 0; static constexpr uint8 MAX = 15; static constexpr uint8 DAY = 11; static constexpr uint8 NIGHT = MIN; - void (*changedCallback)(); void operator=(uint8 value) { current = value % (MAX + 1); clockDisplay.setBrightness(current); - if (changedCallback) changedCallback(); + if (callback) callback(); } operator uint8() { From daf6c876a8085cb9d62d410bf08c49ee21620d63 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Mon, 10 Feb 2025 13:09:23 +0100 Subject: [PATCH 6/6] use struct for timer and use generic callback --- include/devices.h | 4 ++-- include/display.h | 42 +++++++++++++++++++++++++++++++++--------- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/include/devices.h b/include/devices.h index 267b0db..9c1e90c 100644 --- a/include/devices.h +++ b/include/devices.h @@ -137,8 +137,8 @@ namespace Devices { Display::brightness.registerCallback([]{ brightnessMqtt->updateState(Display::brightness); }); - Display::Timer::remainingCallback = [](int8 current){ + Display::Timer::timer.registerCallback([](int8 current){ timerRemainingMqtt->updateState(to_string(current).c_str()); - }; + }); } } \ No newline at end of file diff --git a/include/display.h b/include/display.h index c08791e..6e798e4 100644 --- a/include/display.h +++ b/include/display.h @@ -101,13 +101,38 @@ namespace Display { } brightness; namespace Timer { - int8 timer = 0, current = 0; - void (*remainingCallback)(int8); + typedef void (*remaining_callback_t)(int8); + struct : public CallbackAware { + + void operator=(int8 value) { + initial = remaining = value; + } + + operator int8() { + return remaining; + } + + void start() { + remaining = initial + 1; + } + + void decrease() { + remaining--; + if (callback) callback(remaining); + } + + bool atBeginning() { + return initial == remaining; + } + + private: + int8 initial = 0, remaining = 0; + } timer; Task tDisplayTimer(SECONDS(10), TASK_ONCE + 1, []{ - if (current >= 0) { - clockDisplay.print(current, DEC); + if (timer >= 0) { + clockDisplay.print(timer, DEC); clockDisplay.writeDisplay(); } }, &ts, false, @@ -123,18 +148,17 @@ namespace Display { Task tTimer(MINUTES(1), TASK_FOREVER, []{ static constexpr uint8 threshold = 16; - current--; - if (current == timer) { + timer.decrease(); + if (timer.atBeginning()) { if (timer <= threshold) tDisplayTimer.setIterations(TASK_FOREVER); tDisplayTimer.restart(); - } else if (current == threshold) { + } else if (timer == threshold) { tDisplayTimer.setIterations(TASK_FOREVER); tDisplayTimer.restart(); } - if (remainingCallback) remainingCallback(current); }, &ts, false, []{ - current = timer+1; + timer.start(); return true; }, []{