Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
SimpleTimer.cpp
00001 /* 00002 * SimpleTimer.cpp 00003 * 00004 * SimpleTimer - A timer library for Arduino. 00005 * Author: mromani@ottotecnica.com 00006 * Copyright (c) 2010 OTTOTECNICA Italy 00007 * 00008 * This library is free software; you can redistribute it 00009 * and/or modify it under the terms of the GNU Lesser 00010 * General Public License as published by the Free Software 00011 * Foundation; either version 2.1 of the License, or (at 00012 * your option) any later version. 00013 * 00014 * This library is distributed in the hope that it will 00015 * be useful, but WITHOUT ANY WARRANTY; without even the 00016 * implied warranty of MERCHANTABILITY or FITNESS FOR A 00017 * PARTICULAR PURPOSE. See the GNU Lesser General Public 00018 * License for more details. 00019 * 00020 * You should have received a copy of the GNU Lesser 00021 * General Public License along with this library; if not, 00022 * write to the Free Software Foundation, Inc., 00023 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00024 */ 00025 00026 #include "mbed.h" 00027 #include "SimpleTimer.h" 00028 00029 00030 // Select time function: 00031 /* 00032 inline unsigned long SimpleTimer::elapsed() 00033 { 00034 return _timer.read_us(); 00035 } 00036 */ 00037 int SimpleTimer::elapsed() 00038 { 00039 return _timer.read_ms(); 00040 } 00041 00042 SimpleTimer::SimpleTimer(Timer &timer) : _timer(timer) 00043 { 00044 int current_millis = elapsed(); 00045 00046 for (int i = 0; i < MAX_TIMERS; i++) { 00047 enabled[i] = false; 00048 callbacks[i] = 0; // If the callback pointer is zero, the slot is free, i.e. doesn't "contain" any timer 00049 prev_millis[i] = current_millis; 00050 numRuns[i] = 0; 00051 } 00052 00053 numTimers = 0; 00054 } 00055 00056 void SimpleTimer::run() 00057 { 00058 int i; 00059 int current_millis; 00060 00061 // get current time 00062 current_millis = elapsed(); 00063 00064 for (i = 0; i < MAX_TIMERS; i++) { 00065 00066 toBeCalled[i] = DEFCALL_DONTRUN; 00067 00068 // no callback == no timer, i.e. jump over empty slots 00069 if (callbacks[i]) { 00070 00071 // is it time to process this timer ? 00072 // see http://arduino.cc/forum/index.php/topic,124048.msg932592.html#msg932592 00073 00074 if (current_millis - prev_millis[i] >= delays[i]) { 00075 00076 // update time 00077 //prev_millis[i] = current_millis; 00078 prev_millis[i] += delays[i]; 00079 00080 // check if the timer callback has to be executed 00081 if (enabled[i]) { 00082 00083 // "run forever" timers must always be executed 00084 if (maxNumRuns[i] == RUN_FOREVER) { 00085 toBeCalled[i] = DEFCALL_RUNONLY; 00086 } 00087 // other timers get executed the specified number of times 00088 else if (numRuns[i] < maxNumRuns[i]) { 00089 toBeCalled[i] = DEFCALL_RUNONLY; 00090 numRuns[i]++; 00091 00092 // after the last run, delete the timer 00093 if (numRuns[i] >= maxNumRuns[i]) { 00094 toBeCalled[i] = DEFCALL_RUNANDDEL; 00095 } 00096 } 00097 } 00098 } 00099 } 00100 } 00101 00102 for (i = 0; i < MAX_TIMERS; i++) { 00103 switch(toBeCalled[i]) { 00104 case DEFCALL_DONTRUN: 00105 break; 00106 00107 case DEFCALL_RUNONLY: 00108 callbacks[i](); 00109 break; 00110 00111 case DEFCALL_RUNANDDEL: 00112 callbacks[i](); 00113 deleteTimer(i); 00114 break; 00115 } 00116 } 00117 } 00118 00119 00120 // find the first available slot 00121 // return -1 if none found 00122 int SimpleTimer::findFirstFreeSlot() 00123 { 00124 int i; 00125 00126 // all slots are used 00127 if (numTimers >= MAX_TIMERS) { 00128 return -1; 00129 } 00130 00131 // return the first slot with no callback (i.e. free) 00132 for (i = 0; i < MAX_TIMERS; i++) { 00133 if (callbacks[i] == 0) { 00134 return i; 00135 } 00136 } 00137 00138 // no free slots found 00139 return -1; 00140 } 00141 00142 00143 int SimpleTimer::setTimer(int d, timer_callback f, int n) 00144 { 00145 int freeTimer; 00146 00147 freeTimer = findFirstFreeSlot(); 00148 if (freeTimer < 0) { 00149 return -1; 00150 } 00151 00152 if (f == NULL) { 00153 return -1; 00154 } 00155 00156 delays[freeTimer] = d; 00157 callbacks[freeTimer] = f; 00158 maxNumRuns[freeTimer] = n; 00159 enabled[freeTimer] = true; 00160 prev_millis[freeTimer] = elapsed(); 00161 00162 numTimers++; 00163 00164 return freeTimer; 00165 } 00166 00167 00168 int SimpleTimer::setInterval(int d, timer_callback f) 00169 { 00170 return setTimer(d, f, RUN_FOREVER); 00171 } 00172 00173 00174 int SimpleTimer::setTimeout(int d, timer_callback f) 00175 { 00176 return setTimer(d, f, RUN_ONCE); 00177 } 00178 00179 00180 void SimpleTimer::deleteTimer(int timerId) 00181 { 00182 if (timerId >= MAX_TIMERS) { 00183 return; 00184 } 00185 00186 // nothing to delete if no timers are in use 00187 if (numTimers == 0) { 00188 return; 00189 } 00190 00191 // don't decrease the number of timers if the 00192 // specified slot is already empty 00193 if (callbacks[timerId] != NULL) { 00194 callbacks[timerId] = 0; 00195 enabled[timerId] = false; 00196 toBeCalled[timerId] = DEFCALL_DONTRUN; 00197 delays[timerId] = 0; 00198 numRuns[timerId] = 0; 00199 00200 // update number of timers 00201 numTimers--; 00202 } 00203 } 00204 00205 00206 // function contributed by code@rowansimms.com 00207 void SimpleTimer::restartTimer(int numTimer) 00208 { 00209 if (numTimer >= MAX_TIMERS) { 00210 return; 00211 } 00212 00213 prev_millis[numTimer] = elapsed(); 00214 } 00215 00216 00217 bool SimpleTimer::isEnabled(int numTimer) 00218 { 00219 if (numTimer >= MAX_TIMERS) { 00220 return false; 00221 } 00222 00223 return enabled[numTimer]; 00224 } 00225 00226 00227 void SimpleTimer::enable(int numTimer) 00228 { 00229 if (numTimer >= MAX_TIMERS) { 00230 return; 00231 } 00232 00233 enabled[numTimer] = true; 00234 } 00235 00236 00237 void SimpleTimer::disable(int numTimer) 00238 { 00239 if (numTimer >= MAX_TIMERS) { 00240 return; 00241 } 00242 00243 enabled[numTimer] = false; 00244 } 00245 00246 00247 void SimpleTimer::toggle(int numTimer) 00248 { 00249 if (numTimer >= MAX_TIMERS) { 00250 return; 00251 } 00252 00253 enabled[numTimer] = !enabled[numTimer]; 00254 } 00255 00256 00257 int SimpleTimer::getNumTimers() 00258 { 00259 return numTimers; 00260 }
Generated on Tue Jul 12 2022 23:11:54 by
