Skip to content

Commit 88cdd9b

Browse files
committed
Merge branch 'mc/parameterize-handler-argument'
* mc/parameterize-handler-argument: examples:full: add parametrized timer example README: update with template handler usage src/timer: parameterize handler argument type
2 parents fd02d24 + 215666e commit 88cdd9b

File tree

3 files changed

+35
-24
lines changed

3 files changed

+35
-24
lines changed

README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Or using the *Timer* constructors for different task limits / time resolution
1515
```cpp
1616
Timer<10> timer; // 10 concurrent tasks, using millis as resolution
1717
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
1819
```
1920

2021
Call *timer*.**tick()** in the loop function
@@ -65,33 +66,36 @@ timer.in(1000, [](void *argument) -> bool { return argument; }, argument);
6566
6667
```cpp
6768
/* Constructors */
68-
/* Create a timer object with default settings - millis resolution, TIMER_MAX_TASKS (=16) task slots */
69+
/* Create a timer object with default settings:
70+
millis resolution, TIMER_MAX_TASKS (=16) task slots, T = void *
71+
*/
6972
Timer<> timer_create_default(); // auto timer = timer_create_default();
7073
7174
/* Create a timer with max_tasks slots and time_func resolution */
72-
Timer<size_t max_tasks = TIMER_MAX_TASKS, unsigned long (*time_func)(void) = millis> timer;
75+
Timer<size_t max_tasks = TIMER_MAX_TASKS, unsigned long (*time_func)(void) = millis, typename T = void *> timer;
7376
Timer<> timer; // Equivalent to: auto timer = timer_create_default()
7477
Timer<10> timer; // Timer with 10 task slots
7578
Timer<10, micros> timer; // timer with 10 task slots and microsecond resolution
79+
Timer<10, micros, int> timer; // timer with 10 task slots, microsecond resolution, and handler argument type int
7680
77-
/* Signature for handler functions */
78-
bool handler(void *argument);
81+
/* Signature for handler functions - T = void * by default */
82+
bool handler(T argument);
7983
8084
/* Timer Methods */
8185
/* Ticks the timer forward, returns the ticks until next event, or 0 if none */
82-
unsigned int tick(); // call this function in loop()
86+
unsigned long tick(); // call this function in loop()
8387
8488
/* Calls handler with opaque as argument in delay units of time */
8589
Timer<>::Task
86-
in(unsigned long delay, handler_t handler, void *opaque = NULL);
90+
in(unsigned long delay, handler_t handler, T opaque = T());
8791
8892
/* Calls handler with opaque as argument at time */
8993
Timer<>::Task
90-
at(unsigned long time, handler_t handler, void *opaque = NULL);
94+
at(unsigned long time, handler_t handler, T opaque = T());
9195
9296
/* Calls handler with opaque as argument every interval units of time */
9397
Timer<>::Task
94-
every(unsigned long interval, handler_t handler, void *opaque = NULL);
98+
every(unsigned long interval, handler_t handler, T opaque = T());
9599
96100
/* Cancel a timer task */
97101
void cancel(Timer<>::Task &task);

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
}

src/timer.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,32 +47,33 @@
4747

4848
template <
4949
size_t max_tasks = TIMER_MAX_TASKS, /* max allocated tasks */
50-
unsigned long (*time_func)() = millis /* time function for timer */
50+
unsigned long (*time_func)() = millis, /* time function for timer */
51+
typename T = void * /* handler argument type */
5152
>
5253
class Timer {
5354
public:
5455

5556
typedef uintptr_t Task; /* public task handle */
56-
typedef bool (*handler_t)(void *opaque); /* task handler func signature */
57+
typedef bool (*handler_t)(T opaque); /* task handler func signature */
5758

5859
/* Calls handler with opaque as argument in delay units of time */
5960
Task
60-
in(unsigned long delay, handler_t h, void *opaque = NULL)
61+
in(unsigned long delay, handler_t h, T opaque = T())
6162
{
6263
return task_id(add_task(time_func(), delay, h, opaque));
6364
}
6465

6566
/* Calls handler with opaque as argument at time */
6667
Task
67-
at(unsigned long time, handler_t h, void *opaque = NULL)
68+
at(unsigned long time, handler_t h, T opaque = T())
6869
{
6970
const unsigned long now = time_func();
7071
return task_id(add_task(now, time - now, h, opaque));
7172
}
7273

7374
/* Calls handler with opaque as argument every interval units of time */
7475
Task
75-
every(unsigned long interval, handler_t h, void *opaque = NULL)
76+
every(unsigned long interval, handler_t h, T opaque = T())
7677
{
7778
return task_id(add_task(time_func(), interval, h, opaque, interval));
7879
}
@@ -129,7 +130,7 @@ class Timer {
129130

130131
struct task {
131132
handler_t handler; /* task handler callback func */
132-
void *opaque; /* argument given to the callback handler */
133+
T opaque; /* argument given to the callback handler */
133134
unsigned long start,
134135
expires; /* when the task expires */
135136
size_t repeat, /* repeat task */
@@ -141,7 +142,7 @@ class Timer {
141142
remove(struct task *task)
142143
{
143144
task->handler = NULL;
144-
task->opaque = NULL;
145+
task->opaque = T();
145146
task->start = 0;
146147
task->expires = 0;
147148
task->repeat = 0;
@@ -172,7 +173,7 @@ class Timer {
172173
inline
173174
struct task *
174175
add_task(unsigned long start, unsigned long expires,
175-
handler_t h, void *opaque, bool repeat = 0)
176+
handler_t h, T opaque, bool repeat = 0)
176177
{
177178
struct task * const slot = next_task_slot();
178179

0 commit comments

Comments
 (0)