Skip to content

Commit ef5d1eb

Browse files
committed
Merge branch 'mc/timer-size-checks'
* mc/timer-size-checks: aruino-timer:keywords: add size and empty methods README: document size() and empty() extras:tests:unitTes: size() and empty() unit tests src:arduino-timer: empty() function src:arduino-timer: size() function
2 parents a5ecffe + 3ec10c3 commit ef5d1eb

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ To **cancel** all *Task*s
6161
timer.cancel();
6262
```
6363

64+
Check if a timer is **empty** - no active *Task*s
65+
```cpp
66+
if (timer.empty()) { /* no active tasks */ }
67+
```
68+
69+
Get the number of active *Task*s
70+
```cpp
71+
auto active_tasks = timer.size();
72+
```
73+
6474
Be fancy with **lambdas**
6575
```cpp
6676
timer.in(1000, [](void*) -> bool { return false; });
@@ -126,6 +136,12 @@ void cancel();
126136

127137
/* Returns the ticks until next event, or 0 if none */
128138
unsigned long ticks();
139+
140+
/* Number of active tasks in the timer */
141+
size_t size() const;
142+
143+
/* True if there are no active tasks */
144+
bool empty() const;
129145
```
130146
131147
### Installation

extras/tests/unitTest/unitTest.ino

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,49 @@ test(timer_ticks) {
360360
assertEqual(big, timer.ticks()); // big pending again
361361
}
362362

363+
test(timer_size) {
364+
pre_test();
365+
366+
Timer<0x2, CLOCK::millis, Task *> timer;
367+
368+
assertEqual(timer.size(), 0UL);
369+
370+
auto t = make_task();
371+
372+
auto r = timer.in(0UL, handler, &t);
373+
374+
assertNotEqual((unsigned long)r, 0UL);
375+
assertEqual(timer.size(), 1UL);
376+
377+
r = timer.in(0UL, handler, &t);
378+
379+
assertNotEqual((unsigned long)r, 0UL);
380+
assertEqual(timer.size(), 2UL);
381+
382+
timer.cancel();
383+
384+
assertEqual(timer.size(), 0UL);
385+
}
386+
387+
test(timer_empty) {
388+
pre_test();
389+
390+
Timer<0x1, CLOCK::millis, Task *> timer;
391+
392+
assertEqual(timer.empty(), true);
393+
394+
auto t = make_task();
395+
396+
auto r = timer.in(0UL, handler, &t);
397+
398+
assertNotEqual((unsigned long)r, 0UL);
399+
assertEqual(timer.empty(), false);
400+
401+
timer.cancel();
402+
403+
assertEqual(timer.empty(), true);
404+
}
405+
363406
test(timer_rollover_every) {
364407
pre_test();
365408

keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ every KEYWORD2
2020
tick KEYWORD2
2121
ticks KEYWORD2
2222
cancel KEYWORD2
23+
size KEYWORD2
24+
empty KEYWORD2
2325

2426
#######################################
2527
# Instances (KEYWORD2)

src/arduino-timer.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,30 @@ class Timer {
168168
return ticks;
169169
}
170170

171+
/* Number of active tasks in the timer */
172+
size_t
173+
size() const
174+
{
175+
size_t s = 0;
176+
177+
timer_foreach_const_task(task) {
178+
if (task->handler) ++s;
179+
}
180+
181+
return s;
182+
}
183+
184+
/* True if there are no active tasks */
185+
bool
186+
empty() const
187+
{
188+
timer_foreach_const_task(task) {
189+
if (task->handler) return false;
190+
}
191+
192+
return true;
193+
}
194+
171195
Timer() : ctr(0), tasks{} {}
172196

173197
private:

0 commit comments

Comments
 (0)