From ddea52be65c33cb233a908cb077f5627e124cec3 Mon Sep 17 00:00:00 2001 From: Jean-Francois Turcot Date: Sun, 24 Oct 2010 15:47:59 -0400 Subject: [PATCH] Initial import of the Arduino's SimpleTimer library --- .gitignore | 0 SimpleTimer.cpp | 165 ++++++++++++++++++++++++++++++++++++++++++++++++ SimpleTimer.h | 101 +++++++++++++++++++++++++++++ 3 files changed, 266 insertions(+) create mode 100644 .gitignore create mode 100644 SimpleTimer.cpp create mode 100644 SimpleTimer.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/SimpleTimer.cpp b/SimpleTimer.cpp new file mode 100644 index 0000000..39ebb7f --- /dev/null +++ b/SimpleTimer.cpp @@ -0,0 +1,165 @@ +/* + * SimpleTimer.cpp + * + * SimpleTimer - A timer library for Arduino. + * Author: mromani@ottotecnica.com + * Copyright (c) 2010 OTTOTECNICA Italy + * + * This library is free software; you can redistribute it + * and/or modify it under the terms of the GNU Lesser + * General Public License as published by the Free Software + * Foundation; either version 2.1 of the License, or (at + * your option) any later version. + * + * This library is distributed in the hope that it will + * be useful, but WITHOUT ANY WARRANTY; without even the + * implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser + * General Public License along with this library; if not, + * write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "SimpleTimer.h" + + +SimpleTimer::SimpleTimer() { + long current_millis = millis(); + + for (int i = 0; i < MAX_TIMERS; i++) { + enabled[i] = false; + callbacks[i] = 0; + prev_millis[i] = current_millis; + } + + numTimers = 0; +} + + +void SimpleTimer::run() { + int i; + long current_millis; + + // get current time + current_millis = millis(); + + for (i = 0; i < MAX_TIMERS; i++) { + + // only process active timers + if (callbacks[i] && enabled[i]) { + + // is it time to process this timer ? + if (current_millis - prev_millis[i] >= delays[i]) { + + // update time + prev_millis[i] = current_millis; + + // "run forever" timers must always be executed + if (maxNumRuns[i] == RUN_FOREVER) { + (*callbacks[i])(); + } + // other timers get executed the specified number of times + else if (numRuns[i] < maxNumRuns[i]) { + (*callbacks[i])(); + numRuns[i]++; + + // after the last run, delete the timer + // to save some cycles + if (numRuns[i] >= maxNumRuns[i]) { + deleteTimer(i); + } + } + } + } + } +} + + +int SimpleTimer::setTimer(long d, timer_callback f, int n) { + if (numTimers >= MAX_TIMERS) { + return -1; + } + + delays[numTimers] = d; + callbacks[numTimers] = f; + maxNumRuns[numTimers] = n; + enabled[numTimers] = true; + numRuns[numTimers] = 0; + + numTimers++; + + return (numTimers - 1); +} + + +int SimpleTimer::setInterval(long d, timer_callback f) { + return setTimer(d, f, RUN_FOREVER); +} + + +int SimpleTimer::setTimeout(long d, timer_callback f) { + return setTimer(d, f, RUN_ONCE); +} + + +void SimpleTimer::deleteTimer(int numTimer) { + if (numTimer >= MAX_TIMERS) { + return; + } + + // nothing to disable if no timers are in use + if (numTimers == 0) { + return; + } + + callbacks[numTimer] = 0; + enabled[numTimer] = false; + delays[numTimer] = 0; + + // update number of timers + numTimers--; +} + + +boolean SimpleTimer::isEnabled(int numTimer) { + if (numTimer >= MAX_TIMERS) { + return false; + } + + return enabled[numTimer]; +} + + +void SimpleTimer::enable(int numTimer) { + if (numTimer >= MAX_TIMERS) { + return; + } + + enabled[numTimer] = true; +} + + +void SimpleTimer::disable(int numTimer) { + if (numTimer >= MAX_TIMERS) { + return; + } + + enabled[numTimer] = false; +} + + +void SimpleTimer::toggle(int numTimer) { + if (numTimer >= MAX_TIMERS) { + return; + } + + enabled[numTimer] = !enabled[numTimer]; +} + + +int SimpleTimer::getNumTimers() { + return numTimers; +} \ No newline at end of file diff --git a/SimpleTimer.h b/SimpleTimer.h new file mode 100644 index 0000000..7936c5d --- /dev/null +++ b/SimpleTimer.h @@ -0,0 +1,101 @@ +/* + * SimpleTimer.h + * + * SimpleTimer - A timer library for Arduino. + * Author: mromani@ottotecnica.com + * Copyright (c) 2010 OTTOTECNICA Italy + * + * This library is free software; you can redistribute it + * and/or modify it under the terms of the GNU Lesser + * General Public License as published by the Free Software + * Foundation; either version 2.1 of the License, or (at + * your option) any later version. + * + * This library is distributed in the hope that it will + * be useful, but WITHOUT ANY WARRANTY; without even the + * implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser + * General Public License along with this library; if not, + * write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef SIMPLETIMER_H +#define SIMPLETIMER_H + +#include + +typedef void (*timer_callback)(void); + +class SimpleTimer { + +public: + // maximum number of timers + const static int MAX_TIMERS = 10; + + // setTimer() constants + const static int RUN_FOREVER = 0; + const static int RUN_ONCE = 1; + + // constructor + SimpleTimer(); + + // this function must be called inside loop() + void run(); + + // call function f every d milliseconds + int setInterval(long d, timer_callback f); + + // call function f once after d milliseconds + int setTimeout(long d, timer_callback f); + + // call function f every d milliseconds for n times + int setTimer(long d, timer_callback f, int n); + + // destroy the specified timer + void deleteTimer(int numTimer); + + // returns true if the specified timer is enabled + boolean isEnabled(int numTimer); + + // enables the specified timer + void enable(int numTimer); + + // disables the specified timer + void disable(int numTimer); + + // enables or disables the specified timer + // based on its current state + void toggle(int numTimer); + + // returns the number of used timers + int getNumTimers(); + +private: + // value returned by the millis() function + // in the previous run() call + long prev_millis[MAX_TIMERS]; + + // pointers to the callback functions + timer_callback callbacks[MAX_TIMERS]; + + // delay values + long delays[MAX_TIMERS]; + + // number of runs to be executed for each timer + int maxNumRuns[MAX_TIMERS]; + + // number of executed runs for each timer + int numRuns[MAX_TIMERS]; + + // which timers are enabled + boolean enabled[MAX_TIMERS]; + + // actual number of timers in use + int numTimers; +}; + +#endif \ No newline at end of file