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() {
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());
};

View File

@ -14,6 +14,17 @@
#define DISPLAY_SENSOR_ITERATIONS 2+1
#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 {
void displayTime();
@ -51,8 +62,7 @@ namespace Display {
tDisplayTime.enable();
});
struct HourFormat {
void (*changedCallback)();
struct HourFormat : public CallbackAware<callback_t> {
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<callback_t> {
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() {