make Brightness a class instead of namespace

This commit is contained in:
Nicu Hodos 2025-02-10 09:49:57 +01:00
parent 0010f856aa
commit 4e9f6c1116
2 changed files with 19 additions and 14 deletions

View File

@ -37,7 +37,7 @@ namespace Devices {
auto brightnessMqtt = Builder<Number>::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());

View File

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