Non-blocking library for delaying function calls
Non-blocking library for delaying function calls
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.
Include the library and create a Timer instance.
#include
auto timer = timer_create_default();Or using the Timer constructors for different task limits / time resolution
Timer timer; // 10 concurrent tasks, using millis as resolution
Timer timer; // 10 concurrent tasks, using micros as resolution
Timer timer; // 10 concurrent tasks, using micros as resolution, with handler argument of type intCall timer.tick() in the loop function
void loop() {
timer.tick();
}Make a function to call when the Timer expires
bool function_to_call(void *argument /* optional argument given to in/at/every */) {
return true; // to repeat the action - false to stop
}Call function_to_call in delay units of time (unit of time defaults to milliseconds).
timer.in(delay, function_to_call);
timer.in(delay, function_to_call, argument); // or with an optional argument for function_to_callCall function_to_call at a specific time.
timer.at(time, function_to_call);
timer.at(time, function_to_call, argument); // with argumentCall function_to_call every interval units of time.
timer.every(interval, function_to_call);
timer.every(interval, function_to_call, argument); // with argumentTo cancel a Task
auto task = timer.in(delay, function_to_call);
timer.cancel(task);To cancel all Tasks
timer.cancel();Check if a timer is empty - no active Tasks
if (timer.empty()) { /* no active tasks */ }Get the number of active Tasks
auto active_tasks = timer.size();Be fancy with lambdas
timer.in(1000, [](void*) -> bool { return false; });
timer.in(1000, [](void *argument) -> bool { return argument; }, argument);Getting the number of ticks until the next Task
auto ticks = timer.ticks(); // usefull for sleeping until the next taskvoid loop {
auto ticks = timer.tick(); // returns the number of ticks
}Avoiding ticks calculation inside of tick
void loop {
timer.tick(); // avoids ticks() calculation
}…Check out the instructions from Arduino.
OR copy src/arduino-timer.h into your project folder (you won't get managed updates this way).
Found in the examples/ folder.
The simplest example, blinking an LED every second (from examples/blink):
#include
auto timer = timer_create_default(); // create a timer with default settings
bool toggle_led(void *) {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // toggle the LED
return true; // keep timer active? true
}
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // set LED pin to OUTPUT
// call the toggle_led function every 1000 millis (1 second)
timer.every(1000, toggle_led);
}
void loop() {
timer.tick(); // tick the timer
}Check the LICENSE file - 3-Clause BSD License
Currently only a software timer. Any blocking code delaying timer.tick() will prevent the timer from moving forward and calling any functions.
The library does not do any dynamic memory allocation.
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.
A Task value is valid only for the timer that created it, and only for the lifetime of that timer.
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.
If you find this project useful, consider becoming a sponsor.
No open issues yet, or sync has not completed.