Skip to content

Overloaded ticks() function, getting the ticks left for a specific task #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/arduino-timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,28 @@ class Timer {
}
}

/* Left until the task ends */
unsigned long
left(const Task &task) const
{
auto lft = 0ul;
if (!task)
return lft;

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;

if (duration < t->expires)
lft = t->expires - duration;
break;
}
}

return lft;
}

/* Ticks the timer forward - call this function in loop() */
unsigned long
tick()
Expand Down Expand Up @@ -223,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<Task>(t);

Expand Down