|
1 | 1 | /* |
2 | 2 | * timer_full |
3 | 3 | * |
4 | | - * Full example using the arduino-timer library. |
| 4 | + * Full example using the arduino-timer-cpp17 library. |
5 | 5 | * Shows: |
6 | 6 | * - Setting a different number of tasks with microsecond resolution |
7 | 7 | * - disabling a repeated function |
|
10 | 10 | * |
11 | 11 | */ |
12 | 12 |
|
13 | | -#include <arduino-timer.h> |
| 13 | +#include <arduino-timer-cpp17.h> |
14 | 14 |
|
15 | | -auto timer = timer_create_default(); // create a timer with default settings |
16 | | -Timer<> default_timer; // save as above |
| 15 | +auto timerset = Timers::create_default(); // create a TimerSet with default settings |
| 16 | +Timers::TimerSet<> default_timerset; // same as above |
17 | 17 |
|
18 | | -// create a timer that can hold 1 concurrent task, with microsecond resolution |
19 | | -// and a custom handler type of 'const char * |
20 | | -Timer<1, micros, const char *> u_timer; |
| 18 | +// create a TimerSet that can hold 1 concurrent task, with microsecond resolution |
| 19 | +Timers::TimerSet<1, micros, delayMicroseconds> microtimerset; |
21 | 20 |
|
| 21 | +// create a TimerSet that holds 16 tasks, with millisecond resolution |
| 22 | +Timers::TimerSet<16, millis> t_timerset; |
22 | 23 |
|
23 | | -// create a timer that holds 16 tasks, with millisecond resolution, |
24 | | -// and a custom handler type of 'const char * |
25 | | -Timer<16, millis, const char *> t_timer; |
26 | | - |
27 | | -bool toggle_led(void *) { |
| 24 | +Timers::HandlerResult toggle_led() { |
28 | 25 | digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // toggle the LED |
29 | | - return true; // repeat? true |
| 26 | + return Timers::TimerStatus::repeat; |
30 | 27 | } |
31 | 28 |
|
32 | | -bool print_message(const char *m) { |
| 29 | +Timers::HandlerResult print_message(const char *message) { |
33 | 30 | Serial.print("print_message: "); |
34 | | - Serial.println(m); |
35 | | - return true; // repeat? true |
| 31 | + Serial.println(message); |
| 32 | + return Timers::TimerStatus::repeat; |
36 | 33 | } |
37 | 34 |
|
38 | 35 | size_t repeat_count = 1; |
39 | | -bool repeat_x_times(void *opaque) { |
40 | | - size_t limit = (size_t)opaque; |
41 | | - |
| 36 | +Timers::HandlerResult repeat_x_times(size_t limit) { |
42 | 37 | Serial.print("repeat_x_times: "); |
43 | 38 | Serial.print(repeat_count); |
44 | 39 | Serial.print("/"); |
45 | 40 | Serial.println(limit); |
46 | 41 |
|
47 | | - return ++repeat_count <= limit; // remove this task after limit reached |
| 42 | + // remove this task after limit reached |
| 43 | + return ++repeat_count <= limit ? Timers::TimerStatus::repeat : Timers::TimerStatus::completed; |
48 | 44 | } |
49 | 45 |
|
50 | 46 | void setup() { |
51 | 47 | Serial.begin(9600); |
52 | 48 | pinMode(LED_BUILTIN, OUTPUT); // set LED pin to OUTPUT |
53 | 49 |
|
54 | 50 | // call the toggle_led function every 500 millis (half second) |
55 | | - timer.every(500, toggle_led); |
| 51 | + timerset.every(500, toggle_led); |
56 | 52 |
|
57 | 53 | // call the repeat_x_times function every 1000 millis (1 second) |
58 | | - timer.every(1000, repeat_x_times, (void *)10); |
| 54 | + timerset.every(1000, [](){ return repeat_x_times(10); }); |
59 | 55 |
|
60 | 56 | // call the print_message function every 1000 millis (1 second), |
61 | 57 | // passing it an argument string |
62 | | - t_timer.every(1000, print_message, "called every second"); |
| 58 | + t_timerset.every(1000, [](){ return print_message("called every second"); }); |
63 | 59 |
|
64 | 60 | // call the print_message function in five seconds |
65 | | - t_timer.in(5000, print_message, "delayed five seconds"); |
| 61 | + t_timerset.in(5000, [](){ return print_message("delayed five seconds"); }); |
66 | 62 |
|
67 | 63 | // call the print_message function at time + 10 seconds |
68 | | - t_timer.at(millis() + 10000, print_message, "call at millis() + 10 seconds"); |
| 64 | + t_timerset.at(millis() + 10000, [](){ return print_message("call at millis() + 10 seconds"); }); |
69 | 65 |
|
70 | | - // call the toggle_led function every 500 millis (half second) |
71 | | - auto task = timer.every(500, toggle_led); |
72 | | - timer.cancel(task); // this task is now cancelled, and will not run |
| 66 | + // call the toggle_led function every 500 millis (half-second) |
| 67 | + auto timer = timerset.every(500, toggle_led); |
| 68 | + timerset.cancel(timer); // this task is now cancelled, and will not run |
73 | 69 |
|
74 | 70 | // call print_message in 2 seconds, but with microsecond resolution |
75 | | - u_timer.in(2000000, print_message, "delayed two seconds using microseconds"); |
| 71 | + microtimerset.in(2000000, [](){ return print_message("delayed two seconds using microseconds"); }); |
76 | 72 |
|
77 | | - if (!u_timer.in(5000, print_message, "never printed")) { |
78 | | - /* this fails because we created u_timer with only 1 concurrent task slot */ |
| 73 | + if (!microtimerset.in(5000, [](){ return print_message("never printed"); })) { |
| 74 | + /* this fails because we created microtimerset with only 1 concurrent timer slot */ |
79 | 75 | Serial.println("Failed to add microsecond event - timer full"); |
80 | 76 | } |
81 | 77 | } |
82 | 78 |
|
83 | 79 | void loop() { |
84 | | - timer.tick(); // tick the timer |
85 | | - t_timer.tick(); |
86 | | - u_timer.tick(); |
| 80 | + timerset.tick(); |
| 81 | + t_timerset.tick(); |
| 82 | + microtimerset.tick(); |
87 | 83 | } |
0 commit comments