diff --git a/include/devices.h b/include/devices.h index 267b0db..9c1e90c 100644 --- a/include/devices.h +++ b/include/devices.h @@ -137,8 +137,8 @@ namespace Devices { Display::brightness.registerCallback([]{ brightnessMqtt->updateState(Display::brightness); }); - Display::Timer::remainingCallback = [](int8 current){ + Display::Timer::timer.registerCallback([](int8 current){ timerRemainingMqtt->updateState(to_string(current).c_str()); - }; + }); } } \ No newline at end of file diff --git a/include/display.h b/include/display.h index c08791e..6e798e4 100644 --- a/include/display.h +++ b/include/display.h @@ -101,13 +101,38 @@ namespace Display { } brightness; namespace Timer { - int8 timer = 0, current = 0; - void (*remainingCallback)(int8); + typedef void (*remaining_callback_t)(int8); + struct : public CallbackAware { + + void operator=(int8 value) { + initial = remaining = value; + } + + operator int8() { + return remaining; + } + + void start() { + remaining = initial + 1; + } + + void decrease() { + remaining--; + if (callback) callback(remaining); + } + + bool atBeginning() { + return initial == remaining; + } + + private: + int8 initial = 0, remaining = 0; + } timer; Task tDisplayTimer(SECONDS(10), TASK_ONCE + 1, []{ - if (current >= 0) { - clockDisplay.print(current, DEC); + if (timer >= 0) { + clockDisplay.print(timer, DEC); clockDisplay.writeDisplay(); } }, &ts, false, @@ -123,18 +148,17 @@ namespace Display { Task tTimer(MINUTES(1), TASK_FOREVER, []{ static constexpr uint8 threshold = 16; - current--; - if (current == timer) { + timer.decrease(); + if (timer.atBeginning()) { if (timer <= threshold) tDisplayTimer.setIterations(TASK_FOREVER); tDisplayTimer.restart(); - } else if (current == threshold) { + } else if (timer == threshold) { tDisplayTimer.setIterations(TASK_FOREVER); tDisplayTimer.restart(); } - if (remainingCallback) remainingCallback(current); }, &ts, false, []{ - current = timer+1; + timer.start(); return true; }, []{