simplify timer by using first iteration functionality of the task - avoid using start and decreasing value on first iteration

This commit is contained in:
Nicu Hodos 2025-09-29 10:47:34 +02:00
parent 5421e52bf4
commit 2c0346680d
2 changed files with 12 additions and 22 deletions

View File

@ -85,22 +85,20 @@ namespace Display {
[]{ []{
if (!displayTask.isPerm(tDisplayTimer)) displayTask.restorePerm(); if (!displayTask.isPerm(tDisplayTimer)) displayTask.restorePerm();
}); });
constexpr uint8 threshold = 16;
Task tTimer(MINUTES(1), TASK_FOREVER, Task tTimer(MINUTES(1), TASK_FOREVER,
[]{ []{
static constexpr uint8 threshold = 16; if (tTimer.isFirstIteration()) {
timer.decrease();
if (timer.atBeginning()) {
if (timer <= threshold) tDisplayTimer.setIterations(TASK_FOREVER); if (timer <= threshold) tDisplayTimer.setIterations(TASK_FOREVER);
displayTask.activate(tDisplayTimer); displayTask.activate(tDisplayTimer);
} else if (timer == threshold) { } else {
tDisplayTimer.setIterations(TASK_FOREVER); timer.decrease();
displayTask.activate(tDisplayTimer); if (timer == threshold) {
tDisplayTimer.setIterations(TASK_FOREVER);
displayTask.activate(tDisplayTimer);
}
} }
}, &ts, false, }, &ts, false, nullptr,
[]{
timer.start();
return true;
},
[]{ []{
tDisplayTimer.setIterations(TASK_ONCE + 1); tDisplayTimer.setIterations(TASK_ONCE + 1);
displayTask.activate(tDisplayTime); displayTask.activate(tDisplayTime);

View File

@ -7,25 +7,17 @@ struct : public CallbackAware<remaining_callback_t> {
void operator=(int8 value) { void operator=(int8 value) {
remaining = value; remaining = value;
if (callback) callback(remaining);
} }
operator int8() { operator int8() {
return remaining; return remaining;
} }
void start() {
initial = remaining++;
}
void decrease() { void decrease() {
remaining--; *this = remaining - 1;
if (callback) callback(remaining);
}
bool atBeginning() {
return initial == remaining;
} }
private: private:
int8 initial = 0, remaining = 0; int8 remaining = 0;
} timer; } timer;