- Notifications
You must be signed in to change notification settings - Fork52
Non-blocking library for delaying function calls
License
contrem/arduino-timer
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Simplenon-blocking timer library for calling functionsin / at / every specified units of time. Supports millis, micros, time rollover, and compile time configurable number of tasks.
Include the library and create aTimer instance.
#include<arduino-timer.h>auto timer = timer_create_default();
Or using theTimer constructors for different task limits / time resolution
Timer<10> timer;// 10 concurrent tasks, using millis as resolutionTimer<10, micros> timer;// 10 concurrent tasks, using micros as resolutionTimer<10, micros,int> timer;// 10 concurrent tasks, using micros as resolution, with handler argument of type int
Calltimer.tick() in the loop function
voidloop() { timer.tick();}
Make a function to call when theTimer expires
boolfunction_to_call(void *argument/* optional argument given to in/at/every*/) {returntrue;// to repeat the action - false to stop}
Callfunction_to_callindelay 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_call
Callfunction_to_callat a specifictime.
timer.at(time, function_to_call);timer.at(time, function_to_call, argument);// with argument
Callfunction_to_calleveryinterval units of time.
timer.every(interval, function_to_call);timer.every(interval, function_to_call, argument);// with argument
Tocancel aTask
auto task = timer.in(delay, function_to_call);timer.cancel(task);
Tocancel allTasks
timer.cancel();
Check if a timer isempty - no activeTasks
if (timer.empty()) {/* no active tasks*/ }
Get the number of activeTasks
auto active_tasks = timer.size();
Be fancy withlambdas
timer.in(1000, [](void*) ->bool {returnfalse; });timer.in(1000, [](void *argument) ->bool {return argument; }, argument);
Getting the number ofticks until the nextTask
auto ticks = timer.ticks();// usefull for sleeping until the next task
void loop {auto ticks = timer.tick();// returns the number of ticks}
Avoidingticks calculation inside oftick
void loop { timer.tick<void>();// avoids ticks() calculation}
/* Constructors*//* Create a timer object with default settings: millis resolution, TIMER_MAX_TASKS (=16) task slots, T = void **/Timer<>timer_create_default();// auto timer = timer_create_default();/* Create a timer with max_tasks slots and time_func resolution*/Timer<size_t max_tasks = TIMER_MAX_TASKS,unsignedlong (*time_func)(void) = millis, typename T = void *> timer;Timer<> timer;// Equivalent to: auto timer = timer_create_default()Timer<10> timer;// Timer with 10 task slotsTimer<10, micros> timer;// timer with 10 task slots and microsecond resolutionTimer<10, micros,int> timer;// timer with 10 task slots, microsecond resolution, and handler argument type int/* Signature for handler functions - T = void * by default*/boolhandler(T argument);/* Timer Methods*//* Ticks the timer forward, returns the ticks until next event, or 0 if none*/unsignedlongtick();// call this function in loop()/* Calls handler with opaque as argument in delay units of time*/Timer<>::Taskin(unsignedlong delay,handler_t handler, T opaque = T());/* Calls handler with opaque as argument at time*/Timer<>::Taskat(unsignedlong time,handler_t handler, T opaque = T());/* Calls handler with opaque as argument every interval units of time*/Timer<>::Taskevery(unsignedlong interval,handler_t handler, T opaque = T());/* Cancel a timer task*/boolcancel(Timer<>::Task &task);/* Cancel all tasks*/voidcancel();/* Returns the ticks until next event, or 0 if none*/unsignedlongticks();/* Number of active tasks in the timer*/size_tsize()const;/* True if there are no active tasks*/boolempty()const;
Check out the instructions from Arduino.
OR copysrc/arduino-timer.h into your project folder(you won't get managed updates this way).
Found in theexamples/ folder.
The simplest example, blinking an LED every second(from examples/blink):
#include<arduino-timer.h>auto timer = timer_create_default();// create a timer with default settingsbooltoggle_led(void *) {digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));// toggle the LEDreturntrue;// keep timer active? true}voidsetup() {pinMode(LED_BUILTIN, OUTPUT);// set LED pin to OUTPUT// call the toggle_led function every 1000 millis (1 second) timer.every(1000, toggle_led);}voidloop() { timer.tick();// tick the timer}
Check the LICENSE file - 3-Clause BSD License
Currently only a software timer. Any blocking code delayingtimer.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. Thein / at / every functions returnNULL if theTimer is full.
ATask 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 theTimer constructors. Save memory by reducing the number, increase memory use by having more. The default isTIMER_MAX_TASKS which is currently 16.
If you find this project useful,consider becoming a sponsor.
About
Non-blocking library for delaying function calls