Skip to content

Commit 215666e

Browse files
committed
examples:full: add parametrized timer example
1 parent acbbbd3 commit 215666e

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

examples/full/full.ino

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,20 @@ auto timer = timer_create_default(); // create a timer with default settings
1616
Timer<> default_timer; // save as above
1717

1818
// create a timer that can hold 1 concurrent task, with microsecond resolution
19-
Timer<1, micros> u_timer;
19+
// and a custom handler type of 'const char *
20+
Timer<1, micros, const char *> u_timer;
21+
22+
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;
2026

2127
bool toggle_led(void *) {
2228
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // toggle the LED
2329
return true; // repeat? true
2430
}
2531

26-
bool print_message(void *msg) {
27-
const char *m = (const char *)msg;
32+
bool print_message(const char *m) {
2833
Serial.print("print_message: ");
2934
Serial.println(m);
3035
return true; // repeat? true
@@ -54,28 +59,29 @@ void setup() {
5459

5560
// call the print_message function every 1000 millis (1 second),
5661
// passing it an argument string
57-
timer.every(1000, print_message, (void *)"called every second");
62+
t_timer.every(1000, print_message, "called every second");
5863

5964
// call the print_message function in five seconds
60-
timer.in(5000, print_message, (void *)"delayed five seconds");
65+
t_timer.in(5000, print_message, "delayed five seconds");
6166

6267
// call the print_message function at time + 10 seconds
63-
timer.at(millis() + 10000, print_message, (void *)"call at millis() + 10 seconds");
68+
t_timer.at(millis() + 10000, print_message, "call at millis() + 10 seconds");
6469

6570
// call the toggle_led function every 500 millis (half second)
6671
auto task = timer.every(500, toggle_led);
6772
timer.cancel(task); // this task is now cancelled, and will not run
6873

6974
// call print_message in 2 seconds, but with microsecond resolution
70-
u_timer.in(2000000, print_message, (void *)"delayed two seconds using microseconds");
75+
u_timer.in(2000000, print_message, "delayed two seconds using microseconds");
7176

72-
if (!u_timer.in(5000, print_message, (void *)"never printed")) {
77+
if (!u_timer.in(5000, print_message, "never printed")) {
7378
/* this fails because we created u_timer with only 1 concurrent task slot */
7479
Serial.println("Failed to add microsecond event - timer full");
7580
}
7681
}
7782

7883
void loop() {
7984
timer.tick(); // tick the timer
85+
t_timer.tick();
8086
u_timer.tick();
8187
}

0 commit comments

Comments
 (0)