|
| 1 | +/** |
| 2 | + arduino-timer - library for delaying function calls |
| 3 | +
|
| 4 | + Copyright (c) 2018, Michael Contreras |
| 5 | + All rights reserved. |
| 6 | +
|
| 7 | + Redistribution and use in source and binary forms, with or without |
| 8 | + modification, are permitted provided that the following conditions are |
| 9 | + met: |
| 10 | +
|
| 11 | + 1. Redistributions of source code must retain the above copyright |
| 12 | + notice, this list of conditions and the following disclaimer. |
| 13 | +
|
| 14 | + 2. Redistributions in binary form must reproduce the above copyright |
| 15 | + notice, this list of conditions and the following disclaimer in the |
| 16 | + documentation and/or other materials provided with the distribution. |
| 17 | +
|
| 18 | + 3. Neither the name of the copyright holder nor the names of its |
| 19 | + contributors may be used to endorse or promote products derived from |
| 20 | + this software without specific prior written permission. |
| 21 | +
|
| 22 | + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 23 | + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 24 | + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
| 25 | + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 | + HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 | + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 28 | + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 29 | + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 30 | + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 31 | + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 32 | + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | +*/ |
| 34 | + |
| 35 | +#ifndef _CM_ARDUINO_TIMER_H__ |
| 36 | +#define _CM_ARDUINO_TIMER_H__ |
| 37 | + |
| 38 | +#if defined(ARDUINO) && ARDUINO >= 100 |
| 39 | +#include <Arduino.h> |
| 40 | +#else |
| 41 | +#include <WProgram.h> |
| 42 | +#endif |
| 43 | + |
| 44 | +#ifndef TIMER_MAX_TASKS |
| 45 | + #define TIMER_MAX_TASKS 0x10 |
| 46 | +#endif |
| 47 | + |
| 48 | +template < |
| 49 | + size_t max_tasks = TIMER_MAX_TASKS, /* max allocated tasks */ |
| 50 | + unsigned long (*time_func)() = millis, /* time function for timer */ |
| 51 | + typename T = void * /* handler argument type */ |
| 52 | +> |
| 53 | +class Timer { |
| 54 | + public: |
| 55 | + |
| 56 | + typedef uintptr_t Task; /* public task handle */ |
| 57 | + typedef bool (*handler_t)(T opaque); /* task handler func signature */ |
| 58 | + |
| 59 | + /* Calls handler with opaque as argument in delay units of time */ |
| 60 | + Task |
| 61 | + in(unsigned long delay, handler_t h, T opaque = T()) |
| 62 | + { |
| 63 | + return task_id(add_task(time_func(), delay, h, opaque)); |
| 64 | + } |
| 65 | + |
| 66 | + /* Calls handler with opaque as argument at time */ |
| 67 | + Task |
| 68 | + at(unsigned long time, handler_t h, T opaque = T()) |
| 69 | + { |
| 70 | + const unsigned long now = time_func(); |
| 71 | + return task_id(add_task(now, time - now, h, opaque)); |
| 72 | + } |
| 73 | + |
| 74 | + /* Calls handler with opaque as argument every interval units of time */ |
| 75 | + Task |
| 76 | + every(unsigned long interval, handler_t h, T opaque = T()) |
| 77 | + { |
| 78 | + return task_id(add_task(time_func(), interval, h, opaque, interval)); |
| 79 | + } |
| 80 | + |
| 81 | + /* Cancel the timer task */ |
| 82 | + void |
| 83 | + cancel(Task &task) |
| 84 | + { |
| 85 | + if (!task) return; |
| 86 | + |
| 87 | + for (size_t i = 0; i < max_tasks; ++i) { |
| 88 | + struct task * const t = &tasks[i]; |
| 89 | + |
| 90 | + if (t->handler && (t->id ^ task) == (uintptr_t)t) { |
| 91 | + remove(t); |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + task = (Task)NULL; |
| 97 | + } |
| 98 | + |
| 99 | + /* Ticks the timer forward - call this function in loop() */ |
| 100 | + unsigned long |
| 101 | + tick() |
| 102 | + { |
| 103 | + unsigned long ticks = (unsigned long)-1; |
| 104 | + |
| 105 | + for (size_t i = 0; i < max_tasks; ++i) { |
| 106 | + struct task * const task = &tasks[i]; |
| 107 | + |
| 108 | + if (task->handler) { |
| 109 | + const unsigned long t = time_func(); |
| 110 | + const unsigned long duration = t - task->start; |
| 111 | + |
| 112 | + if (duration >= task->expires) { |
| 113 | + task->repeat = task->handler(task->opaque) && task->repeat; |
| 114 | + |
| 115 | + if (task->repeat) task->start = t; |
| 116 | + else remove(task); |
| 117 | + } else { |
| 118 | + const unsigned long remaining = task->expires - duration; |
| 119 | + ticks = remaining < ticks ? remaining : ticks; |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return ticks == (unsigned long)-1 ? 0 : ticks; |
| 125 | + } |
| 126 | + |
| 127 | + private: |
| 128 | + |
| 129 | + size_t ctr; |
| 130 | + |
| 131 | + struct task { |
| 132 | + handler_t handler; /* task handler callback func */ |
| 133 | + T opaque; /* argument given to the callback handler */ |
| 134 | + unsigned long start, |
| 135 | + expires; /* when the task expires */ |
| 136 | + size_t repeat, /* repeat task */ |
| 137 | + id; |
| 138 | + } tasks[max_tasks]; |
| 139 | + |
| 140 | + inline |
| 141 | + void |
| 142 | + remove(struct task *task) |
| 143 | + { |
| 144 | + task->handler = NULL; |
| 145 | + task->opaque = T(); |
| 146 | + task->start = 0; |
| 147 | + task->expires = 0; |
| 148 | + task->repeat = 0; |
| 149 | + task->id = 0; |
| 150 | + } |
| 151 | + |
| 152 | + inline |
| 153 | + Task |
| 154 | + task_id(const struct task * const t) |
| 155 | + { |
| 156 | + const Task id = (Task)t; |
| 157 | + |
| 158 | + return id ? id ^ t->id : id; |
| 159 | + } |
| 160 | + |
| 161 | + inline |
| 162 | + struct task * |
| 163 | + next_task_slot() |
| 164 | + { |
| 165 | + for (size_t i = 0; i < max_tasks; ++i) { |
| 166 | + struct task * const slot = &tasks[i]; |
| 167 | + if (slot->handler == NULL) return slot; |
| 168 | + } |
| 169 | + |
| 170 | + return NULL; |
| 171 | + } |
| 172 | + |
| 173 | + inline |
| 174 | + struct task * |
| 175 | + add_task(unsigned long start, unsigned long expires, |
| 176 | + handler_t h, T opaque, bool repeat = 0) |
| 177 | + { |
| 178 | + struct task * const slot = next_task_slot(); |
| 179 | + |
| 180 | + if (!slot) return NULL; |
| 181 | + |
| 182 | + if (++ctr == 0) ++ctr; // overflow |
| 183 | + |
| 184 | + slot->id = ctr; |
| 185 | + slot->handler = h; |
| 186 | + slot->opaque = opaque; |
| 187 | + slot->start = start; |
| 188 | + slot->expires = expires; |
| 189 | + slot->repeat = repeat; |
| 190 | + |
| 191 | + return slot; |
| 192 | + } |
| 193 | +}; |
| 194 | + |
| 195 | +/* create a timer with the default settings */ |
| 196 | +inline Timer<> |
| 197 | +timer_create_default() |
| 198 | +{ |
| 199 | + return Timer<>(); |
| 200 | +} |
| 201 | + |
| 202 | +#endif |
0 commit comments