Skip to content

Commit 9418ae9

Browse files
committed
start updating docs
1 parent ebf7197 commit 9418ae9

File tree

4 files changed

+93
-55
lines changed

4 files changed

+93
-55
lines changed

README.md

Lines changed: 72 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,100 @@
1-
# arduino-timer - library for delaying function calls
1+
# arduino-timer-cpp17 - library for scheduling function calls
22

3-
Simple *non-blocking* timer library for calling functions **in / at / every** specified units of time. Supports millis, micros, time rollover, and compile time configurable number of tasks.
3+
Simple *non-blocking* timer library for calling functions **in / at / every** specified units of time. Supports millis, micros, time rollover, and compile-time configurable number of timers.
4+
5+
This library was inspired by [Michael Contreras' arduino-timer library](https://github.com/sponsors/contrem/arduino-timer), but has been rewritten to make use of 'modern C++' types and
6+
functionality. As a result this library requires that the Arduino IDE toolchain be manually configured for "gnu++17" mode.
47

58
### Use It
69

7-
Include the library and create a *Timer* instance.
10+
Include the library and create a *TimerSet* instance.
811
```cpp
912
#include <arduino-timer.h>
1013

11-
auto timer = timer_create_default();
14+
auto timerset = Timers::create_default();
1215
```
1316

14-
Or using the *Timer* constructors for different task limits / time resolution
17+
Or using the *TimerSet* constructors for different timer limits / time resolution.
1518
```cpp
16-
Timer<10> timer; // 10 concurrent tasks, using millis as resolution
17-
Timer<10, micros> timer; // 10 concurrent tasks, using micros as resolution
18-
Timer<10, micros, int> timer; // 10 concurrent tasks, using micros as resolution, with handler argument of type int
19+
Timers::TimerSet<10> timerset; // 10 concurrent times, using millis as resolution
20+
Timers::TimerSet<10, micros, delayMicroseconds> timerset; // 10 concurrent timers, using micros as resolution
1921
```
2022

21-
Call *timer*.**tick()** in the loop function
23+
Call *timerset*.**tick_and_delay()** in the ```loop``` function to execute handlers for any timers
24+
which have expired and then delay until the next scheduled timer expiration.
2225
```cpp
2326
void loop() {
24-
timer.tick();
27+
timerset.tick_and_delay();
2528
}
2629
```
2730

28-
Make a function to call when the *Timer* expires
31+
Call *timerset*.**tick()** in the ```loop``` function to execute handlers for any timers
32+
which have expired, and then return so additional processing can be handled in the loop function.
2933
```cpp
30-
bool function_to_call(void *argument /* optional argument given to in/at/every */) {
31-
return true; // to repeat the action - false to stop
34+
void loop() {
35+
timerset.tick;
36+
}
37+
```
38+
39+
Make a function to call (without arguments) when a *Timer* expires.
40+
```cpp
41+
Timers::HandlerResult function_to_call() {
42+
return { Timers::TimerStatus::repeat, 0 }; // to repeat the action - 'completed' to stop
43+
}
44+
```
45+
46+
Make a function to call (with an argument) when a *Timer* expires.
47+
```cpp
48+
Timers::HandlerResult function_to_call_with_arg(int value) {
49+
return { Timers::TimerStatus::completed, 0 }; // to stop the timer - 'repeat' to repeat the action
50+
}
51+
```
52+
53+
Make a function to call (without arguments) when a *Timer* expires, which can reschedule itself based
54+
on a digital input. In this example, if digital input 4 is active when the function is called, it will
55+
change its own repeat interval to 5 seconds (5000 millis).
56+
```cpp
57+
Timers::HandlerResult function_to_call_and_reschedule() {
58+
if (digitalRead(4)) {
59+
return { Timers::TimerStatus::reschedule, 5000 }; // to change the repeat interval to 5000 ms
60+
} else {
61+
return { Timers::TimerStatus::repeat, 0 }; // to repeat the action - 'completed' to stop
62+
}
3263
}
3364
```
3465

3566
Call *function\_to\_call* **in** *delay* units of time *(unit of time defaults to milliseconds)*.
3667
```cpp
37-
timer.in(delay, function_to_call);
38-
timer.in(delay, function_to_call, argument); // or with an optional argument for function_to_call
68+
timerset.in(delay, function_to_call);
69+
```
70+
71+
Call *function\_to\_call|_with|_arg* **in** *delay* units of time *(unit of time defaults to milliseconds)*.
72+
```cpp
73+
timerset.in(delay, [](){ return function_to_call_with_arg(42); });
3974
```
4075
41-
Call *function\_to\_call* **at** a specific *time*.
76+
Call functions **at** a specific *time*.
4277
```cpp
43-
timer.at(time, function_to_call);
44-
timer.at(time, function_to_call, argument); // with argument
78+
timerset.at(time, function_to_call);
79+
timerset.at(time, [](){ return function_to_call_with_arg(42); });
4580
```
4681

47-
Call *function\_to\_call* **every** *interval* units of time.
82+
Call functions **every** *interval* units of time.
4883
```cpp
49-
timer.every(interval, function_to_call);
50-
timer.every(interval, function_to_call, argument); // with argument
84+
timerset.every(interval, function_to_call);
85+
timerset.every(interval, [](){ return function_to_call_with_arg(42); });
5186
```
5287
53-
To **cancel** a *Task*
88+
Call functions **now** and **every** *interval* units of time.
5489
```cpp
55-
auto task = timer.in(delay, function_to_call);
56-
timer.cancel(task);
90+
timerset.now_and_every(interval, function_to_call);
91+
timerset.now_and_every(interval, [](){ return function_to_call_with_arg(42); });
5792
```
5893

59-
Be fancy with **lambdas**
94+
To **cancel** a *Timer*
6095
```cpp
61-
timer.in(1000, [](void*) -> bool { return false; });
62-
timer.in(1000, [](void *argument) -> bool { return argument; }, argument);
96+
auto timer = timerset.in(delay, function_to_call);
97+
timerset.cancel(timer);
6398
```
6499

65100
### API
@@ -116,22 +151,22 @@ The simplest example, blinking an LED every second *(from examples/blink)*:
116151
```cpp
117152
#include <arduino-timer.h>
118153
119-
auto timer = timer_create_default(); // create a timer with default settings
154+
auto timerset = Timers::create_default(); // create a timerset with default settings
120155
121-
bool toggle_led(void *) {
156+
Timers::HandlerResult toggle_led() {
122157
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // toggle the LED
123-
return true; // keep timer active? true
158+
return { Timers::TimerStatus::repeat, 0 }; // keep timer active? true
124159
}
125160
126161
void setup() {
127162
pinMode(LED_BUILTIN, OUTPUT); // set LED pin to OUTPUT
128163
129164
// call the toggle_led function every 1000 millis (1 second)
130-
timer.every(1000, toggle_led);
165+
timerset.every(1000, toggle_led);
131166
}
132167
133168
void loop() {
134-
timer.tick(); // tick the timer
169+
timerset.tick_and_delay(); // tick the timer
135170
}
136171
```
137172

@@ -141,14 +176,15 @@ Check the LICENSE file - 3-Clause BSD License
141176

142177
### Notes
143178

144-
Currently only a software timer. Any blocking code delaying *timer*.**tick()** will prevent the timer from moving forward and calling any functions.
179+
Currently only a software timer. Any blocking code delaying *timerset*.**tick()** will prevent the TimerSet from moving forward and calling any functions.
145180

146181
The library does not do any dynamic memory allocation.
147182

148-
The number of concurrent tasks is a compile time constant, meaning there is a limit to the number of concurrent tasks. The **in / at / every** functions return **NULL** if the *Timer* is full.
183+
The number of concurrent timers is a compile time constant, meaning there is a limit to the number of concurrent timers. The **in / at / every / now_and_every**
184+
functions return a TimerHandle which evaluates to ```false``` if the TimerSet is full.
149185

150-
A *Task* value is valid only for the timer that created it, and only for the lifetime of that timer.
186+
A *TimerHandle* value is valid only for the TimerSet that created it, and only for the lifetime of that timer.
151187

152-
Change the number of concurrent tasks using the *Timer* constructors. Save memory by reducing the number, increase memory use by having more. The default is **TIMER_MAX_TASKS** which is currently 16.
188+
Change the number of concurrent timers using the *Timer* constructors. Save memory by reducing the number, increase memory use by having more. The default is **TIMERSET_DEFAULT_TIMERS** which is currently 16.
153189

154190
If you find this project useful, [consider becoming a sponsor.](https://github.com/sponsors/contrem)

keywords.txt

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,24 @@
66
# Datatypes (KEYWORD1)
77
#######################################
88

9-
Timer KEYWORD1
10-
Task KEYWORD1
11-
handler_t KEYWORD1
9+
HandlerResult KEYWORD1
10+
Timepoint KEYWORD1
11+
Timer KEYWORD1
12+
TimerHandle KEYWORD1
13+
TimerSet KEYWORD1
14+
TimerStatus KEYWORD1
1215

1316
#######################################
1417
# Methods and Functions (KEYWORD2)
1518
#######################################
1619

17-
in KEYWORD2
18-
at KEYWORD2
19-
every KEYWORD2
20-
tick KEYWORD2
21-
cancel KEYWORD2
20+
in KEYWORD2
21+
at KEYWORD2
22+
every KEYWORD2
23+
now_and_every KEYWORD2
24+
tick KEYWORD2
25+
cancel KEYWORD2
26+
tick_and_delay KEYWORD2
2227

2328
#######################################
2429
# Instances (KEYWORD2)
@@ -28,4 +33,4 @@ cancel KEYWORD2
2833
# Constants (LITERAL1)
2934
#######################################
3035

31-
TIMER_MAX_TASKS LITERAL1
36+
TIMERSET_DEFAULT_TIMERS LITERAL1

library.properties

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
name=arduino-timer
2-
version=2.1.0
1+
name=arduino-timer-cpp17
2+
version=3.0.0
33

4-
author=Michael Contreras
5-
maintainer=Michael Contreras
6-
sentence=Timer library for delaying function calls
7-
paragraph=Simple non-blocking timer library for calling functions in / at / every specified units of time. Supports millis, micros, time rollover, and compile time configurable number of tasks.
4+
author=Kevin P. Fleming
5+
maintainer=Kevin P. Fleming
6+
sentence=Timer library for scheduling function calls
7+
paragraph=Simple non-blocking timer library for calling functions in / at / every specified units of time. Supports millis, micros, time rollover, and compile-time configurable number of timers.
88
category=Timing
9-
url=https://github.com/contrem/arduino-timer
9+
url=https://github.com/kpfleming/arduino-timer-cpp17
1010
architectures=*
1111
includes=arduino-timer.h

src/timer.h

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)