Merge branch 'timer'

This commit is contained in:
Nicu Hodos 2025-02-10 11:11:32 +01:00
commit 0010f856aa
2 changed files with 100 additions and 10 deletions

View File

@ -46,6 +46,26 @@ namespace Devices {
strcmp("ON", msg) == 0 ? Display::changeHourFormat24(true) : Display::changeHourFormat24(false);
}
}).restoreStateFromCommand().build();
Number* displayTimerMqtt = Builder<Number>::instance(new Number{ "Timer duration", "timer_duration",
[](const char* msg) {
auto value = String{ msg }.toInt();
Display::Timer::timer = value;
displayTimerMqtt->updateState(value);
}
}).withMin(0).withMax(90).withStep(5).restoreStateFromCommand().build();
Sensor* timerRemainingMqtt = Builder<Sensor>::instance(new Sensor("Timer remaining", "timer_remaining"))
.withUnitMeasure("min").withPrecision(0).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",
@ -74,6 +94,15 @@ namespace Devices {
}
}).build()
)
.addSecondary(
Builder<Button>::instance(new Button{"Display remaining timer", "display_remaining_timer",
[](const char* msg) {
if (strcmp("PRESS", msg) == 0 && !Display::Timer::tDisplayTimer.isEnabled()) {
Display::Timer::tDisplayTimer.restart();
}
}
}).build()
)
.addSecondary(
Builder<Button>::instance(new Button{"Update time", "update_time",
[](const char* msg) {
@ -82,8 +111,11 @@ namespace Devices {
}).build()
)
.addSecondary(ledMqtt)
.addSecondary(timerMqtt)
.addSecondary(timerRemainingMqtt)
.addConfiguration(brightnessMqtt)
.addConfiguration(hourFormatMqtt)
.addConfiguration(displayTimerMqtt)
.addPreconfigured(HaESP::heapStats)
.addPreconfigured(HaESP::restartInfo)
.build();
@ -105,5 +137,8 @@ namespace Devices {
Display::Brightness::brightnessChangedCallback = []{
brightnessMqtt->updateState(Display::Brightness::current);
};
Display::Timer::remainingCallback = [](int8 current){
timerRemainingMqtt->updateState(to_string(current).c_str());
};
}
}

View File

@ -13,8 +13,10 @@
#define BRIGHTNESS_STEP 1
#define BRIGHTNESS_NIGHT BRIGHTNESS_MIN
#define BRIGHTNESS_DAY 11
#define SECONDS(value) value*1000
#define DISPLAY_SENSOR_ITERATIONS 2
#define MILLISECONDS(value) value*TASK_MILLISECOND
#define SECONDS(value) value*TASK_SECOND
#define MINUTES(value) value*TASK_MINUTE
#define DISPLAY_SENSOR_ITERATIONS 2+1
#define DISPLAY_DELAY (SECONDS(2))
namespace Display {
@ -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,23 +35,25 @@ namespace Display {
[]() {
drawColon(false);
});
Task tDisplaySensor(5 * TASK_SECOND, DISPLAY_SENSOR_ITERATIONS, displayTemp, &ts, false,
Task tDisplaySensor(SECONDS(5), DISPLAY_SENSOR_ITERATIONS, nullptr, &ts, false,
[]() {
if (!tDisplayTime.isEnabled()) return false;
tDisplayTime.disable();
tDisplaySensor.setCallback(displayTemp);
return true;
},
[]() {
tDisplaySensor.setIterations(DISPLAY_SENSOR_ITERATIONS * 2);
tDisplayTime.enableDelayed(tDisplaySensor.getInterval());
tDisplaySensor.setIterations((DISPLAY_SENSOR_ITERATIONS - 1) * 2 + 1);
tDisplayTime.enable();
});
Task tDisplayDate(TASK_IMMEDIATE, TASK_ONCE, displayDate, &ts, false,
Task tDisplayDate(SECONDS(5), TASK_ONCE + 1, displayDate, &ts, false,
[]() {
if (!tDisplayTime.isEnabled()) return false;
tDisplayTime.disable();
tDisplaySensor.disable();
return true;
},
[]() {
tDisplayTime.enableDelayed(5 * TASK_SECOND);
tDisplayTime.enable();
});
bool hourFormat24 = false;
@ -73,6 +77,56 @@ namespace Display {
}
}
namespace Timer {
int8 timer = 0, current = 0;
void (*remainingCallback)(int8);
Task tDisplayTimer(SECONDS(10), TASK_ONCE + 1,
[]{
if (current >= 0) {
clockDisplay.print(current, DEC);
clockDisplay.writeDisplay();
}
}, &ts, false,
[]{
if (!tDisplayTime.isEnabled()) return false;
tDisplayTime.disable();
return true;
},
[]{
tDisplayTimer.setIterations(TASK_ONCE + 1);
tDisplayTime.enable();
});
Task tTimer(MINUTES(1), TASK_FOREVER,
[]{
static constexpr uint8 threshold = 16;
current--;
if (current == timer) {
if (timer <= threshold) tDisplayTimer.setIterations(TASK_FOREVER);
tDisplayTimer.restart();
} else if (current == threshold) {
tDisplayTimer.setIterations(TASK_FOREVER);
tDisplayTimer.restart();
}
if (remainingCallback) remainingCallback(current);
}, &ts, false,
[]{
current = timer+1;
return true;
},
[]{
tDisplayTimer.disable();
});
void start() {
tTimer.restart();
}
void stop() {
tTimer.disable();
}
}
void drawTime() {
int displayHour = hourFormat24 ? hour() : hourFormat12();
int displayMinute = minute();
@ -172,12 +226,13 @@ namespace Display {
void changeHourFormat24(bool format24) {
hourFormat24 = format24;
drawTime();
tDisplayTime.restart();
if (hourFormatChangedCallback) hourFormatChangedCallback();
}
void setup() {
clockDisplay.begin(DISPLAY_ADDRESS);
clockDisplay.setBrightness(Brightness::current);
tDisplayTime.enable();
}
}