From 2c0346680de3bb14528b9b9ed7043e9d66d91ab4 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Mon, 29 Sep 2025 10:47:34 +0200 Subject: [PATCH] simplify timer by using first iteration functionality of the task - avoid using start and decreasing value on first iteration --- include/display.h | 20 +++++++++----------- include/timer.h | 14 +++----------- 2 files changed, 12 insertions(+), 22 deletions(-) diff --git a/include/display.h b/include/display.h index b7a0d85..41f7d22 100644 --- a/include/display.h +++ b/include/display.h @@ -85,22 +85,20 @@ namespace Display { []{ if (!displayTask.isPerm(tDisplayTimer)) displayTask.restorePerm(); }); + constexpr uint8 threshold = 16; Task tTimer(MINUTES(1), TASK_FOREVER, []{ - static constexpr uint8 threshold = 16; - timer.decrease(); - if (timer.atBeginning()) { + if (tTimer.isFirstIteration()) { if (timer <= threshold) tDisplayTimer.setIterations(TASK_FOREVER); displayTask.activate(tDisplayTimer); - } else if (timer == threshold) { - tDisplayTimer.setIterations(TASK_FOREVER); - displayTask.activate(tDisplayTimer); + } else { + timer.decrease(); + if (timer == threshold) { + tDisplayTimer.setIterations(TASK_FOREVER); + displayTask.activate(tDisplayTimer); + } } - }, &ts, false, - []{ - timer.start(); - return true; - }, + }, &ts, false, nullptr, []{ tDisplayTimer.setIterations(TASK_ONCE + 1); displayTask.activate(tDisplayTime); diff --git a/include/timer.h b/include/timer.h index 061582f..ec4792d 100644 --- a/include/timer.h +++ b/include/timer.h @@ -7,25 +7,17 @@ struct : public CallbackAware { void operator=(int8 value) { remaining = value; + if (callback) callback(remaining); } operator int8() { return remaining; } - void start() { - initial = remaining++; - } - void decrease() { - remaining--; - if (callback) callback(remaining); - } - - bool atBeginning() { - return initial == remaining; + *this = remaining - 1; } private: - int8 initial = 0, remaining = 0; + int8 remaining = 0; } timer;