Skip to content

Commit d560fb3

Browse files
committed
initial update of examples
1 parent 381b7ef commit d560fb3

File tree

4 files changed

+54
-58
lines changed

4 files changed

+54
-58
lines changed

examples/blink/blink.ino

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
/*
22
* timer_blink
33
*
4-
* Blinks the built-in LED every second using the arduino-timer library.
4+
* Blinks the built-in LED every second using the arduino-timer-cpp17 library.
55
*
66
*/
77

8-
#include <arduino-timer.h>
8+
#include <arduino-timer-cpp17.h>
99

10-
auto timer = timer_create_default(); // create a timer with default settings
10+
auto timerset = Timers::create_default(); // create a TimerSet with default settings
1111

12-
bool toggle_led(void *) {
12+
Timers::HandlerResult toggle_led() {
1313
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // toggle the LED
14-
return true; // repeat? true
14+
return Timers::TimerStatus::repeat;
1515
}
1616

1717
void setup() {
1818
pinMode(LED_BUILTIN, OUTPUT); // set LED pin to OUTPUT
1919

2020
// call the toggle_led function every 1000 millis (1 second)
21-
timer.every(1000, toggle_led);
21+
timerset.every(1000, toggle_led);
2222
}
2323

2424
void loop() {
25-
timer.tick(); // tick the timer
25+
timerset.tick_and_delay(); // tick the timer
2626
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
/*
22
* timer_blink_micros
33
*
4-
* Blinks the built-in LED every second using the arduino-timer library.
4+
* Blinks the built-in LED every second using the arduino-timer-cpp17 library.
55
*
66
*/
77

8-
#include <arduino-timer.h>
8+
#include <arduino-timer-cpp17.h>
99

10-
Timer<1, micros> timer; // create a timer with 1 task and microsecond resolution
10+
Timers::TimerSet<1, micros, delayMicroseconds> timerset; // create a TimerSet with 1 timer and microsecond resolution
1111

12-
bool toggle_led(void *) {
12+
Timers::HandlerResult toggle_led() {
1313
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // toggle the LED
14-
return true; // repeat? true
14+
return Timers::TimerStatus::repeat;
1515
}
1616

1717
void setup() {
1818
pinMode(LED_BUILTIN, OUTPUT); // set LED pin to OUTPUT
1919

2020
// call the toggle_led function every 1000000 micros (1 second)
21-
timer.every(1000000, toggle_led);
21+
timerset.every(1000000, toggle_led);
2222
}
2323

2424
void loop() {
25-
timer.tick(); // tick the timer
25+
timerset.tick_and_delay(); // tick the timer
2626
}

examples/blink_print/blink_print.ino

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,36 @@
22
* timer_blink_print
33
*
44
* Blinks the built-in LED every half second, and prints a messages every
5-
* second using the arduino-timer library.
5+
* second using the arduino-timer-cpp17 library.
66
*
77
*/
88

9-
#include <arduino-timer.h>
9+
#include <arduino-timer-cpp17.h>
1010

11-
auto timer = timer_create_default(); // create a timer with default settings
11+
auto timerset = Timers::create_default(); // create a TimerSet with default settings
1212

13-
bool toggle_led(void *) {
13+
Timers::HandlerResult toggle_led() {
1414
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // toggle the LED
15-
return true; // repeat? true
15+
return Timers::TimerStatus::repeat;
1616
}
1717

18-
bool print_message(void *) {
18+
Timers::HandlerResult print_message() {
1919
Serial.print("print_message: Called at: ");
2020
Serial.println(millis());
21-
return true; // repeat? true
21+
return Timers::TimerStatus::repeat;
2222
}
2323

2424
void setup() {
2525
Serial.begin(9600);
2626
pinMode(LED_BUILTIN, OUTPUT); // set LED pin to OUTPUT
2727

2828
// call the toggle_led function every 500 millis (half second)
29-
timer.every(500, toggle_led);
29+
timerset.every(500, toggle_led);
3030

3131
// call the print_message function every 1000 millis (1 second)
32-
timer.every(1000, print_message);
32+
timerset.every(1000, print_message);
3333
}
3434

3535
void loop() {
36-
timer.tick(); // tick the timer
36+
timerset.tick_and_delay(); // tick the timer
3737
}

examples/full/full.ino

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* timer_full
33
*
4-
* Full example using the arduino-timer library.
4+
* Full example using the arduino-timer-cpp17 library.
55
* Shows:
66
* - Setting a different number of tasks with microsecond resolution
77
* - disabling a repeated function
@@ -10,78 +10,74 @@
1010
*
1111
*/
1212

13-
#include <arduino-timer.h>
13+
#include <arduino-timer-cpp17.h>
1414

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
1717

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;
2120

21+
// create a TimerSet that holds 16 tasks, with millisecond resolution
22+
Timers::TimerSet<16, millis> t_timerset;
2223

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() {
2825
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // toggle the LED
29-
return true; // repeat? true
26+
return Timers::TimerStatus::repeat;
3027
}
3128

32-
bool print_message(const char *m) {
29+
Timers::HandlerResult print_message(const char *message) {
3330
Serial.print("print_message: ");
34-
Serial.println(m);
35-
return true; // repeat? true
31+
Serial.println(message);
32+
return Timers::TimerStatus::repeat;
3633
}
3734

3835
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) {
4237
Serial.print("repeat_x_times: ");
4338
Serial.print(repeat_count);
4439
Serial.print("/");
4540
Serial.println(limit);
4641

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;
4844
}
4945

5046
void setup() {
5147
Serial.begin(9600);
5248
pinMode(LED_BUILTIN, OUTPUT); // set LED pin to OUTPUT
5349

5450
// call the toggle_led function every 500 millis (half second)
55-
timer.every(500, toggle_led);
51+
timerset.every(500, toggle_led);
5652

5753
// 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); });
5955

6056
// call the print_message function every 1000 millis (1 second),
6157
// 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"); });
6359

6460
// 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"); });
6662

6763
// 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"); });
6965

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
7369

7470
// 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"); });
7672

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 */
7975
Serial.println("Failed to add microsecond event - timer full");
8076
}
8177
}
8278

8379
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();
8783
}

0 commit comments

Comments
 (0)