From 4e9f6c11164ec1f177ba18da8749b333e7152a17 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Mon, 10 Feb 2025 09:49:57 +0100 Subject: [PATCH] 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(); } }