add timer and expose:

- duration
- start/stop switch
On start, it will display the remaining time instead of the watch
This commit is contained in:
Nicu Hodos 2025-02-07 08:21:05 +01:00
parent cf581bc03e
commit b7b8fc603d
2 changed files with 49 additions and 4 deletions

View File

@ -46,6 +46,24 @@ namespace Devices {
strcmp("ON", msg) == 0 ? Display::changeHourFormat24(true) : Display::changeHourFormat24(false);
}
}).restoreStateFromCommand().build();
Number* displayTimerMqtt = Builder<Number>::instance(new Number{ "Display imer", "display_timer",
[](const char* msg) {
auto value = String{ msg }.toInt();
Display::Timer::timer = value;
displayTimerMqtt->updateState(value);
}
}).withMin(0).withMax(90).withStep(5).restoreStateFromCommand().build();
Switch* timerMqtt = Builder<Switch>::instance(new Switch{"Timer", "timer",
[](const char* msg) {
if (strcmp("ON", msg) == 0) {
Display::Timer::start();
timerMqtt->updateState(true);
} else {
Display::Timer::stop();
timerMqtt->updateState(false);
}
}
}).restoreStateFromCommand().build();
auto button =
Builder<Button>::instance(new Button{"Restart", "restart",
@ -82,8 +100,10 @@ namespace Devices {
}).build()
)
.addSecondary(ledMqtt)
.addSecondary(timerMqtt)
.addConfiguration(brightnessMqtt)
.addConfiguration(hourFormatMqtt)
.addConfiguration(displayTimerMqtt)
.addPreconfigured(HaESP::heapStats)
.addPreconfigured(HaESP::restartInfo)
.build();

View File

@ -13,7 +13,9 @@
#define BRIGHTNESS_STEP 1
#define BRIGHTNESS_NIGHT BRIGHTNESS_MIN
#define BRIGHTNESS_DAY 11
#define SECONDS(value) value*1000
#define MILLISECONDS(value) value*TASK_MILLISECOND
#define SECONDS(value) value*TASK_SECOND
#define MINUTES(value) value*TASK_MINUTE
#define DISPLAY_SENSOR_ITERATIONS 2
#define DISPLAY_DELAY (SECONDS(2))
@ -25,7 +27,7 @@ namespace Display {
void displayHumidity();
void drawTime();
void drawColon(bool);
Task tDisplayTime(500, TASK_FOREVER, displayTime, &ts, false,
Task tDisplayTime(MILLISECONDS(500), TASK_FOREVER, displayTime, &ts, false,
[]() {
drawTime();
return true;
@ -33,7 +35,7 @@ namespace Display {
[]() {
drawColon(false);
});
Task tDisplaySensor(5 * TASK_SECOND, DISPLAY_SENSOR_ITERATIONS, displayTemp, &ts, false,
Task tDisplaySensor(SECONDS(5), DISPLAY_SENSOR_ITERATIONS, displayTemp, &ts, false,
[]() {
tDisplayTime.disable();
return true;
@ -49,7 +51,7 @@ namespace Display {
return true;
},
[]() {
tDisplayTime.enableDelayed(5 * TASK_SECOND);
tDisplayTime.enableDelayed(SECONDS(5));
});
bool hourFormat24 = false;
@ -73,6 +75,29 @@ namespace Display {
}
}
namespace Timer {
int8 timer = 0, currentTimer = 0;
void start() {
currentTimer = timer;
auto displayTimer = []{
if (currentTimer >= 0) {
clockDisplay.print(currentTimer--, DEC);
clockDisplay.writeDisplay();
}
};
tDisplayTime.setInterval(MINUTES(1));
tDisplayTime.setCallback(displayTimer);
tDisplayTime.restart();
}
void stop() {
tDisplayTime.setInterval(MILLISECONDS(500));
tDisplayTime.setCallback(displayTime);
tDisplayTime.restart();
}
}
void drawTime() {
int displayHour = hourFormat24 ? hour() : hourFormat12();
int displayMinute = minute();