|
| 1 | +/* |
| 2 | + * SimpleTimer.cpp |
| 3 | + * |
| 4 | + * SimpleTimer - A timer library for Arduino. |
| 5 | + * Author: mromani@ottotecnica.com |
| 6 | + * Copyright (c) 2010 OTTOTECNICA Italy |
| 7 | + * |
| 8 | + * This library is free software; you can redistribute it |
| 9 | + * and/or modify it under the terms of the GNU Lesser |
| 10 | + * General Public License as published by the Free Software |
| 11 | + * Foundation; either version 2.1 of the License, or (at |
| 12 | + * your option) any later version. |
| 13 | + * |
| 14 | + * This library is distributed in the hope that it will |
| 15 | + * be useful, but WITHOUT ANY WARRANTY; without even the |
| 16 | + * implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 17 | + * PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 18 | + * License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU Lesser |
| 21 | + * General Public License along with this library; if not, |
| 22 | + * write to the Free Software Foundation, Inc., |
| 23 | + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 24 | + */ |
| 25 | + |
| 26 | + |
| 27 | +#include "SimpleTimer.h" |
| 28 | + |
| 29 | + |
| 30 | +SimpleTimer::SimpleTimer() { |
| 31 | + unsigned long current_millis = millis(); |
| 32 | + |
| 33 | + for (int i = 0; i < MAX_TIMERS; i++) { |
| 34 | + enabled[i] = false; |
| 35 | + callbacks[i] = 0; // if the callback pointer is zero, the slot is free, i.e. doesn't "contain" any timer |
| 36 | + prev_millis[i] = current_millis; |
| 37 | + numRuns[i] = 0; |
| 38 | + } |
| 39 | + |
| 40 | + numTimers = 0; |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +void SimpleTimer::run() { |
| 45 | + int i; |
| 46 | + unsigned long current_millis; |
| 47 | + |
| 48 | + // get current time |
| 49 | + current_millis = millis(); |
| 50 | + |
| 51 | + for (i = 0; i < MAX_TIMERS; i++) { |
| 52 | + |
| 53 | + toBeCalled[i] = DEFCALL_DONTRUN; |
| 54 | + |
| 55 | + // no callback == no timer, i.e. jump over empty slots |
| 56 | + if (callbacks[i]) { |
| 57 | + |
| 58 | + // is it time to process this timer ? |
| 59 | + if (current_millis - prev_millis[i] >= delays[i]) { |
| 60 | + |
| 61 | + // update time |
| 62 | + prev_millis[i] = current_millis; |
| 63 | + |
| 64 | + // check if the timer callback has to be executed |
| 65 | + if (enabled[i]) { |
| 66 | + |
| 67 | + // "run forever" timers must always be executed |
| 68 | + if (maxNumRuns[i] == RUN_FOREVER) { |
| 69 | + toBeCalled[i] = DEFCALL_RUNONLY; |
| 70 | + } |
| 71 | + // other timers get executed the specified number of times |
| 72 | + else if (numRuns[i] < maxNumRuns[i]) { |
| 73 | + toBeCalled[i] = DEFCALL_RUNONLY; |
| 74 | + numRuns[i]++; |
| 75 | + |
| 76 | + // after the last run, delete the timer |
| 77 | + if (numRuns[i] >= maxNumRuns[i]) { |
| 78 | + toBeCalled[i] = DEFCALL_RUNANDDEL; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + for (i = 0; i < MAX_TIMERS; i++) { |
| 87 | + switch(toBeCalled[i]) { |
| 88 | + case DEFCALL_DONTRUN: |
| 89 | + break; |
| 90 | + |
| 91 | + case DEFCALL_RUNONLY: |
| 92 | + (*callbacks[i])(); |
| 93 | + break; |
| 94 | + |
| 95 | + case DEFCALL_RUNANDDEL: |
| 96 | + (*callbacks[i])(); |
| 97 | + deleteTimer(i); |
| 98 | + break; |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +// find the first available slot |
| 105 | +// return -1 if none found |
| 106 | +int SimpleTimer::findFirstFreeSlot() { |
| 107 | + int i; |
| 108 | + |
| 109 | + // all slots are used |
| 110 | + if (numTimers >= MAX_TIMERS) { |
| 111 | + return -1; |
| 112 | + } |
| 113 | + |
| 114 | + // return the first slot with no callback (i.e. free) |
| 115 | + for (i = 0; i < MAX_TIMERS; i++) { |
| 116 | + if (callbacks[i] == 0) { |
| 117 | + return i; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // we should never reach this point... |
| 122 | + return -1; |
| 123 | +} |
| 124 | + |
| 125 | + |
| 126 | +int SimpleTimer::setTimer(long d, timer_callback f, int n) { |
| 127 | + int freeTimer; |
| 128 | + |
| 129 | + freeTimer = findFirstFreeSlot(); |
| 130 | + if (freeTimer < 0) { |
| 131 | + return -1; |
| 132 | + } |
| 133 | + |
| 134 | + delays[freeTimer] = d; |
| 135 | + callbacks[freeTimer] = f; |
| 136 | + maxNumRuns[freeTimer] = n; |
| 137 | + enabled[freeTimer] = true; |
| 138 | + prev_millis[freeTimer] = millis(); |
| 139 | + |
| 140 | + numTimers++; |
| 141 | + |
| 142 | + return freeTimer; |
| 143 | +} |
| 144 | + |
| 145 | + |
| 146 | +int SimpleTimer::setInterval(long d, timer_callback f) { |
| 147 | + return setTimer(d, f, RUN_FOREVER); |
| 148 | +} |
| 149 | + |
| 150 | + |
| 151 | +int SimpleTimer::setTimeout(long d, timer_callback f) { |
| 152 | + return setTimer(d, f, RUN_ONCE); |
| 153 | +} |
| 154 | + |
| 155 | + |
| 156 | +void SimpleTimer::deleteTimer(int numTimer) { |
| 157 | + if (numTimer >= MAX_TIMERS) { |
| 158 | + return; |
| 159 | + } |
| 160 | + |
| 161 | + // nothing to delete if no timers are in use |
| 162 | + if (numTimers == 0) { |
| 163 | + return; |
| 164 | + } |
| 165 | + |
| 166 | + callbacks[numTimer] = 0; |
| 167 | + enabled[numTimer] = false; |
| 168 | + delays[numTimer] = 0; |
| 169 | + numRuns[numTimer] = 0; |
| 170 | + |
| 171 | + // update number of timers |
| 172 | + numTimers--; |
| 173 | +} |
| 174 | + |
| 175 | + |
| 176 | +// function contributed by code@rowansimms.com |
| 177 | +void SimpleTimer::restartTimer(int numTimer) { |
| 178 | + if (numTimer >= MAX_TIMERS) { |
| 179 | + return; |
| 180 | + } |
| 181 | + |
| 182 | + prev_millis[numTimer] = millis(); |
| 183 | +} |
| 184 | + |
| 185 | + |
| 186 | +boolean SimpleTimer::isEnabled(int numTimer) { |
| 187 | + if (numTimer >= MAX_TIMERS) { |
| 188 | + return false; |
| 189 | + } |
| 190 | + |
| 191 | + return enabled[numTimer]; |
| 192 | +} |
| 193 | + |
| 194 | + |
| 195 | +void SimpleTimer::enable(int numTimer) { |
| 196 | + if (numTimer >= MAX_TIMERS) { |
| 197 | + return; |
| 198 | + } |
| 199 | + |
| 200 | + enabled[numTimer] = true; |
| 201 | +} |
| 202 | + |
| 203 | + |
| 204 | +void SimpleTimer::disable(int numTimer) { |
| 205 | + if (numTimer >= MAX_TIMERS) { |
| 206 | + return; |
| 207 | + } |
| 208 | + |
| 209 | + enabled[numTimer] = false; |
| 210 | +} |
| 211 | + |
| 212 | + |
| 213 | +void SimpleTimer::toggle(int numTimer) { |
| 214 | + if (numTimer >= MAX_TIMERS) { |
| 215 | + return; |
| 216 | + } |
| 217 | + |
| 218 | + enabled[numTimer] = !enabled[numTimer]; |
| 219 | +} |
| 220 | + |
| 221 | + |
| 222 | +int SimpleTimer::getNumTimers() { |
| 223 | + return numTimers; |
| 224 | +} |
0 commit comments