|
| 1 | +# arduino-timer - library for delaying function calls |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +### Use It |
| 6 | + |
| 7 | +Include the library and create a *Timer* instance. |
| 8 | +```cpp |
| 9 | +#include <timer.h> |
| 10 | + |
| 11 | +auto timer = timer_create_default(); |
| 12 | +``` |
| 13 | + |
| 14 | +Or using the *Timer* constructors for different task limits / time resolution |
| 15 | +```cpp |
| 16 | +Timer<10> timer; // 10 concurrent tasks, using millis as resolution |
| 17 | +Timer<10, micros> timer; // 10 concurrent tasks, using micros as resolution |
| 18 | +``` |
| 19 | + |
| 20 | +Call *timer*.**tick()** in the loop function |
| 21 | +```cpp |
| 22 | +void loop() { |
| 23 | + timer.tick(); |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +Make a function to call when the *Timer* expires |
| 28 | +```cpp |
| 29 | +bool function_to_call(void *argument /* optional argument given to in/at/every */) { |
| 30 | + return false; // to keep the timer active - true removes it (for timer.every()) |
| 31 | +} |
| 32 | +``` |
| 33 | +
|
| 34 | +Call *function\_to\_call* **in** *delay* units of time *(unit of time defaults to milliseconds)*. |
| 35 | +```cpp |
| 36 | +timer.in(delay, function_to_call); |
| 37 | +timer.in(delay, function_to_call, argument); // or with an optional argument for function_to_call |
| 38 | +``` |
| 39 | + |
| 40 | +Call *function\_to\_call* **at** a specific *time*. |
| 41 | +```cpp |
| 42 | +timer.at(time, function_to_call); |
| 43 | +timer.at(time, function_to_call, argument); // with argument |
| 44 | +``` |
| 45 | + |
| 46 | +Call *function\_to\_call* **every** *interval* units of time. |
| 47 | +```cpp |
| 48 | +timer.every(interval, function_to_call); |
| 49 | +timer.every(interval, function_to_call, argument); // with argument |
| 50 | +``` |
| 51 | + |
| 52 | +Be fancy with **lambdas** |
| 53 | +```cpp |
| 54 | +timer.in(1000, [](void*) -> bool { return false; }); |
| 55 | +timer.in(1000, [](void *argument) -> bool { return argument; }, argument); |
| 56 | +``` |
| 57 | +
|
| 58 | +### API |
| 59 | +
|
| 60 | +```cpp |
| 61 | +/* Constructors */ |
| 62 | +/* Create a timer object with default settings - millis resolution, TIMER_MAX_TASKS (=16) task slots */ |
| 63 | +Timer<> timer_create_default(); // auto timer = timer_create_default(); |
| 64 | +
|
| 65 | +/* Create a timer with max_tasks slots and time_func resolution */ |
| 66 | +Timer<size_t max_tasks = TIMER_MAX_TASKS, unsigned long (*time_func)(void) = millis> timer; |
| 67 | +Timer<> timer; // Equivalent to: auto timer = timer_create_default() |
| 68 | +Timer<10> timer; // Timer with 10 task slots |
| 69 | +Timer<10, micros> timer; // timer with 10 task slots and microsecond resolution |
| 70 | +
|
| 71 | +/* Signature for handler functions */ |
| 72 | +bool handler(void *argument); |
| 73 | +
|
| 74 | +/* Timer Methods */ |
| 75 | +/* Ticks the timer forward - call this function in loop() */ |
| 76 | +void tick(); |
| 77 | +
|
| 78 | +/* Calls handler with opaque as argument in delay units of time */ |
| 79 | +bool in(unsigned long delay, handler_t handler, void *opaque = NULL); |
| 80 | +
|
| 81 | +/* Calls handler with opaque as argument at time */ |
| 82 | +bool at(unsigned long time, handler_t handler, void *opaque = NULL); |
| 83 | +
|
| 84 | +/* Calls handler with opaque as argument every interval units of time */ |
| 85 | +bool every(unsigned long interval, handler_t handler, void *opaque = NULL); |
| 86 | +``` |
| 87 | + |
| 88 | +### Installation |
| 89 | + |
| 90 | +[Check out the instructions](https://www.arduino.cc/en/Guide/Libraries) from Arduino. |
| 91 | + |
| 92 | +**OR** copy **src/timer.h** into your project folder *(you won't get managed updates this way)*. |
| 93 | + |
| 94 | +### Examples |
| 95 | + |
| 96 | +Found in the **examples/** folder. |
| 97 | + |
| 98 | +The simplest example, blinking an LED every second *(from examples/blink)*: |
| 99 | + |
| 100 | +```cpp |
| 101 | +#include <timer.h> |
| 102 | + |
| 103 | +auto timer = timer_create_default(); // create a timer with default settings |
| 104 | + |
| 105 | +bool toggle_led(void *) { |
| 106 | + digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // toggle the LED |
| 107 | + return false; // stop repeating? false |
| 108 | +} |
| 109 | + |
| 110 | +void setup() { |
| 111 | + pinMode(LED_BUILTIN, OUTPUT); // set LED pin to OUTPUT |
| 112 | + |
| 113 | + // call the toggle_led function every 1000 millis (1 second) |
| 114 | + timer.every(1000, toggle_led); |
| 115 | +} |
| 116 | + |
| 117 | +void loop() { |
| 118 | + timer.tick(); // tick the timer |
| 119 | +} |
| 120 | +``` |
| 121 | +
|
| 122 | +### LICENSE |
| 123 | +
|
| 124 | +Check the LICENSE file - 3-Clause BSD License |
| 125 | +
|
| 126 | +### Notes |
| 127 | +
|
| 128 | +Currently only a software timer. Any blocking code delaying *timer*.**tick()** will prevent the timer from moving forward and calling any functions. |
| 129 | +
|
| 130 | +The library does not do any dynamic memory allocation. |
| 131 | +
|
| 132 | +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 **false** if the *Timer* is full. |
| 133 | +
|
| 134 | +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. |
0 commit comments