Skip to content

Commit 730836a

Browse files
committed
Merge branch 'mc/ticks-until'
* mc/ticks-until: README: document tick() return value src:timer: return ticks until next event
2 parents 0869c22 + c54703e commit 730836a

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ Timer<10, micros> timer; // timer with 10 task slots and microsecond resolution
7878
bool handler(void *argument);
7979
8080
/* Timer Methods */
81-
/* Ticks the timer forward - call this function in loop() */
82-
void tick();
81+
/* Ticks the timer forward, returns the ticks until next event, or 0 if none */
82+
unsigned int tick(); // call this function in loop()
8383
8484
/* Calls handler with opaque as argument in delay units of time */
8585
Timer<>::Task

src/timer.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,11 @@ class Timer {
9696
}
9797

9898
/* Ticks the timer forward - call this function in loop() */
99-
void
99+
unsigned long
100100
tick()
101101
{
102+
unsigned long ticks = (unsigned long)-1;
103+
102104
for (size_t i = 0; i < max_tasks; ++i) {
103105
struct task * const task = &tasks[i];
104106

@@ -111,9 +113,14 @@ class Timer {
111113

112114
if (task->repeat) task->start = t;
113115
else remove(task);
116+
} else {
117+
const unsigned long remaining = task->expires - duration;
118+
ticks = remaining < ticks ? remaining : ticks;
114119
}
115120
}
116121
}
122+
123+
return ticks == (unsigned long)-1 ? 0 : ticks;
117124
}
118125

119126
/* DEPRECATED */

0 commit comments

Comments
 (0)