From 6e6f9bd243f455c95e0b8db6b09968d01aac3230 Mon Sep 17 00:00:00 2001 From: Greg Date: Fri, 21 Jan 2022 20:58:35 +0200 Subject: [PATCH 1/3] Overloaded ticks() function, getting the ticks left for a specific task Signed-off-by: Greg --- src/arduino-timer.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/arduino-timer.h b/src/arduino-timer.h index c87b06b..b4bbe4b 100644 --- a/src/arduino-timer.h +++ b/src/arduino-timer.h @@ -114,6 +114,30 @@ class Timer { } } + /* Left until the task ends */ + unsigned long + ticks(const Task &task) + { + auto lft = 0ul; + if (!task) + return lft; + + timer_foreach_task(t) { + if (t->handler && task_id(t) == task) { + const unsigned long now = time_func(); + const unsigned long duration = now - t->start; + + if (duration >= t->expires) + break; + + lft = t->expires - duration; + break; + } + } + + return lft; + } + /* Ticks the timer forward - call this function in loop() */ unsigned long tick() From 3124d31dee99395c76ebd01661d435182c35a312 Mon Sep 17 00:00:00 2001 From: Greg Date: Fri, 4 Feb 2022 10:54:52 +0200 Subject: [PATCH 2/3] Renamed ticks() to left(), minor style update Signed-off-by: Greg --- src/arduino-timer.h | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/arduino-timer.h b/src/arduino-timer.h index b4bbe4b..56164ae 100644 --- a/src/arduino-timer.h +++ b/src/arduino-timer.h @@ -116,8 +116,8 @@ class Timer { /* Left until the task ends */ unsigned long - ticks(const Task &task) - { + left(const Task &task) + { auto lft = 0ul; if (!task) return lft; @@ -127,10 +127,8 @@ class Timer { const unsigned long now = time_func(); const unsigned long duration = now - t->start; - if (duration >= t->expires) - break; - - lft = t->expires - duration; + if (duration < t->expires) + lft = t->expires - duration; break; } } From da7b82142965bd4cdbe33dbcbe57c4d26400e435 Mon Sep 17 00:00:00 2001 From: Greg Date: Fri, 4 Feb 2022 11:06:10 +0200 Subject: [PATCH 3/3] Change left(),task_id() to const --- src/arduino-timer.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/arduino-timer.h b/src/arduino-timer.h index 56164ae..fdf23c0 100644 --- a/src/arduino-timer.h +++ b/src/arduino-timer.h @@ -116,13 +116,13 @@ class Timer { /* Left until the task ends */ unsigned long - left(const Task &task) + left(const Task &task) const { auto lft = 0ul; if (!task) return lft; - timer_foreach_task(t) { + timer_foreach_const_task(t) { if (t->handler && task_id(t) == task) { const unsigned long now = time_func(); const unsigned long duration = now - t->start; @@ -245,7 +245,7 @@ class Timer { inline Task - task_id(const struct task * const t) + task_id(const struct task * const t) const { const Task id = reinterpret_cast(t);