add generic callback struct and use it for brightness and hourFormat24

This commit is contained in:
Nicu Hodos 2025-02-10 12:50:44 +01:00
parent f10c456f68
commit 3df8f48538
2 changed files with 19 additions and 10 deletions

View File

@ -131,12 +131,12 @@ namespace Devices {
} }
void setup() { void setup() {
Display::hourFormat24.changedCallback = []{ Display::hourFormat24.registerCallback([]{
hourFormatMqtt->updateState(Display::hourFormat24); hourFormatMqtt->updateState(Display::hourFormat24);
}; });
Display::brightness.changedCallback = []{ Display::brightness.registerCallback([]{
brightnessMqtt->updateState(Display::brightness); brightnessMqtt->updateState(Display::brightness);
}; });
Display::Timer::remainingCallback = [](int8 current){ Display::Timer::remainingCallback = [](int8 current){
timerRemainingMqtt->updateState(to_string(current).c_str()); timerRemainingMqtt->updateState(to_string(current).c_str());
}; };

View File

@ -14,6 +14,17 @@
#define DISPLAY_SENSOR_ITERATIONS 2+1 #define DISPLAY_SENSOR_ITERATIONS 2+1
#define DISPLAY_DELAY (SECONDS(2)) #define DISPLAY_DELAY (SECONDS(2))
typedef void (*callback_t)();
template <typename T>
struct CallbackAware {
void registerCallback(T f) {
callback = f;
}
protected:
T callback;
};
namespace Display { namespace Display {
void displayTime(); void displayTime();
@ -51,8 +62,7 @@ namespace Display {
tDisplayTime.enable(); tDisplayTime.enable();
}); });
struct HourFormat { struct HourFormat : public CallbackAware<callback_t> {
void (*changedCallback)();
operator bool() { operator bool() {
return format24; return format24;
@ -61,7 +71,7 @@ namespace Display {
void operator=(bool value) { void operator=(bool value) {
format24 = value; format24 = value;
tDisplayTime.restart(); tDisplayTime.restart();
if (changedCallback) changedCallback(); if (callback) callback();
} }
private: private:
bool format24 = false; bool format24 = false;
@ -70,17 +80,16 @@ namespace Display {
// Create display object // Create display object
Adafruit_7segment clockDisplay = Adafruit_7segment(); Adafruit_7segment clockDisplay = Adafruit_7segment();
struct Brightness { struct Brightness : public CallbackAware<callback_t> {
static constexpr uint8 MIN = 0; static constexpr uint8 MIN = 0;
static constexpr uint8 MAX = 15; static constexpr uint8 MAX = 15;
static constexpr uint8 DAY = 11; static constexpr uint8 DAY = 11;
static constexpr uint8 NIGHT = MIN; static constexpr uint8 NIGHT = MIN;
void (*changedCallback)();
void operator=(uint8 value) { void operator=(uint8 value) {
current = value % (MAX + 1); current = value % (MAX + 1);
clockDisplay.setBrightness(current); clockDisplay.setBrightness(current);
if (changedCallback) changedCallback(); if (callback) callback();
} }
operator uint8() { operator uint8() {