Skip to content

Commit 612ab5e

Browse files
committed
Add SimpleTimer library used for Retry-example
1 parent 1d94772 commit 612ab5e

File tree

4 files changed

+414
-0
lines changed

4 files changed

+414
-0
lines changed

libraries/SimpleTimer/README.markdown

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## SimpleTimer - A timer library for Arduino.
2+
3+
### Uploader
4+
Derek Chafin
5+
Jun 16, 2012
6+
info<a href="http://www.google.com/recaptcha/mailhide/d?k=01OlINdKCVggEULcwNY4NTTg==&amp;c=NuRwkCvdkiHnX97lENoQ4aUrQPIek0qZKoNXiOiv8uE=" onclick="window.open('http://www.google.com/recaptcha/mailhide/d?k\07501OlINdKCVggEULcwNY4NTTg\75\75\46c\75NuRwkCvdkiHnX97lENoQ4aUrQPIek0qZKoNXiOiv8uE\075', '', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=300'); return false;" title="Reveal this e-mail address">...</a>@gmail.com
7+
8+
###Disclaimer
9+
I am pushing this library to github because I like git better than I like the Windows clipboard.
10+
11+
For reference this library can also be found at http://playground.arduino.cc/Code/SimpleTimer.
12+
13+
### Original Author
14+
Marcello Romani
15+
mrom<a href="http://www.google.com/recaptcha/mailhide/d?k=01OlINdKCVggEULcwNY4NTTg==&amp;c=35t9v0kxxmeXywbgEkq2W9ogg7J-gbHsx2HD-cJD7Rc=" onclick="window.open('http://www.google.com/recaptcha/mailhide/d?k\07501OlINdKCVggEULcwNY4NTTg\75\75\46c\07535t9v0kxxmeXywbgEkq2W9ogg7J-gbHsx2HD-cJD7Rc\075', '', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=300'); return false;" title="Reveal this e-mail address">...</a>@ottotecnica.com
16+
Copyright (c) 2010 OTTOTECNICA Italy
17+
18+
### License - GNU LGPL 2.1+
19+
This library is free software; you can redistribute it
20+
and/or modify it under the terms of the GNU Lesser
21+
General Public License as published by the Free Software
22+
Foundation; either version 2.1 of the License, or (at
23+
your option) any later version.
24+
25+
This library is distributed in the hope that it will
26+
be useful, but WITHOUT ANY WARRANTY; without even the
27+
implied warranty of MERCHANTABILITY or FITNESS FOR A
28+
PARTICULAR PURPOSE. See the GNU Lesser General Public
29+
License for more details.
30+
31+
You should have received a copy of the GNU Lesser
32+
General Public License along with this library; if not,
33+
write to the Free Software Foundation, Inc.,
34+
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

libraries/SimpleTimer/SimpleTimer.cpp

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
/*
2+
* SimpleTimer.cpp
3+
*
4+
* SimpleTimer - A timer library for Arduino.
5+
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 [email protected]
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+
}

libraries/SimpleTimer/SimpleTimer.h

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* SimpleTimer.h
3+
*
4+
* SimpleTimer - A timer library for Arduino.
5+
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+
28+
#ifndef SIMPLETIMER_H
29+
#define SIMPLETIMER_H
30+
31+
#if defined(ARDUINO) && ARDUINO >= 100
32+
#include <Arduino.h>
33+
#else
34+
#include <WProgram.h>
35+
#endif
36+
37+
typedef void (*timer_callback)(void);
38+
39+
class SimpleTimer {
40+
41+
public:
42+
// maximum number of timers
43+
const static int MAX_TIMERS = 10;
44+
45+
// setTimer() constants
46+
const static int RUN_FOREVER = 0;
47+
const static int RUN_ONCE = 1;
48+
49+
// constructor
50+
SimpleTimer();
51+
52+
// this function must be called inside loop()
53+
void run();
54+
55+
// call function f every d milliseconds
56+
int setInterval(long d, timer_callback f);
57+
58+
// call function f once after d milliseconds
59+
int setTimeout(long d, timer_callback f);
60+
61+
// call function f every d milliseconds for n times
62+
int setTimer(long d, timer_callback f, int n);
63+
64+
// destroy the specified timer
65+
void deleteTimer(int numTimer);
66+
67+
// restart the specified timer
68+
void restartTimer(int numTimer);
69+
70+
// returns true if the specified timer is enabled
71+
boolean isEnabled(int numTimer);
72+
73+
// enables the specified timer
74+
void enable(int numTimer);
75+
76+
// disables the specified timer
77+
void disable(int numTimer);
78+
79+
// enables the specified timer if it's currently disabled,
80+
// and vice-versa
81+
void toggle(int numTimer);
82+
83+
// returns the number of used timers
84+
int getNumTimers();
85+
86+
// returns the number of available timers
87+
int getNumAvailableTimers() { return MAX_TIMERS - numTimers; };
88+
89+
private:
90+
// deferred call constants
91+
const static int DEFCALL_DONTRUN = 0; // don't call the callback function
92+
const static int DEFCALL_RUNONLY = 1; // call the callback function but don't delete the timer
93+
const static int DEFCALL_RUNANDDEL = 2; // call the callback function and delete the timer
94+
95+
// find the first available slot
96+
int findFirstFreeSlot();
97+
98+
// value returned by the millis() function
99+
// in the previous run() call
100+
unsigned long prev_millis[MAX_TIMERS];
101+
102+
// pointers to the callback functions
103+
timer_callback callbacks[MAX_TIMERS];
104+
105+
// delay values
106+
long delays[MAX_TIMERS];
107+
108+
// number of runs to be executed for each timer
109+
int maxNumRuns[MAX_TIMERS];
110+
111+
// number of executed runs for each timer
112+
int numRuns[MAX_TIMERS];
113+
114+
// which timers are enabled
115+
boolean enabled[MAX_TIMERS];
116+
117+
// deferred function call (sort of) - N.B.: this array is only used in run()
118+
int toBeCalled[MAX_TIMERS];
119+
120+
// actual number of timers in use
121+
int numTimers;
122+
};
123+
124+
#endif

0 commit comments

Comments
 (0)