From f10c456f689fc1987a070d48b34f3f4d5f8bdbf3 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Mon, 10 Feb 2025 12:00:31 +0100 Subject: [PATCH] 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);