Skip to content

Commit 23d60f3

Browse files
Merge pull request #11 from marcelloromani/feature/simpleTimerPlus
Feature/simple timer plus
2 parents a71c054 + 70712d5 commit 23d60f3

4 files changed

Lines changed: 301 additions & 102 deletions

File tree

SimpleTimer/SimpleTimer.cpp

100644100755
Lines changed: 71 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
* Author: mromani@ottotecnica.com
66
* Copyright (c) 2010 OTTOTECNICA Italy
77
*
8+
* Callback function parameters added & compiler warnings
9+
* removed by Bill Knight <billk@rosw.com> 20March2017
10+
*
811
* This library is free software; you can redistribute it
912
* and/or modify it under the terms of the GNU Lesser
1013
* General Public License as published by the Free Software
@@ -36,10 +39,8 @@ SimpleTimer::SimpleTimer() {
3639
unsigned long current_millis = elapsed();
3740

3841
for (int i = 0; i < MAX_TIMERS; i++) {
39-
enabled[i] = false;
40-
callbacks[i] = 0; // if the callback pointer is zero, the slot is free, i.e. doesn't "contain" any timer
41-
prev_millis[i] = current_millis;
42-
numRuns[i] = 0;
42+
memset(&timer[i], 0, sizeof (timer_t));
43+
timer[i].prev_millis = current_millis;
4344
}
4445

4546
numTimers = 0;
@@ -55,35 +56,34 @@ void SimpleTimer::run() {
5556

5657
for (i = 0; i < MAX_TIMERS; i++) {
5758

58-
toBeCalled[i] = DEFCALL_DONTRUN;
59+
timer[i].toBeCalled = DEFCALL_DONTRUN;
5960

6061
// no callback == no timer, i.e. jump over empty slots
61-
if (callbacks[i]) {
62+
if (timer[i].callback != NULL) {
6263

6364
// is it time to process this timer ?
6465
// see http://arduino.cc/forum/index.php/topic,124048.msg932592.html#msg932592
6566

66-
if (current_millis - prev_millis[i] >= delays[i]) {
67+
if ((current_millis - timer[i].prev_millis) >= timer[i].delay) {
6768

6869
// update time
69-
//prev_millis[i] = current_millis;
70-
prev_millis[i] += delays[i];
70+
timer[i].prev_millis += timer[i].delay;
7171

7272
// check if the timer callback has to be executed
73-
if (enabled[i]) {
73+
if (timer[i].enabled) {
7474

7575
// "run forever" timers must always be executed
76-
if (maxNumRuns[i] == RUN_FOREVER) {
77-
toBeCalled[i] = DEFCALL_RUNONLY;
76+
if (timer[i].maxNumRuns == RUN_FOREVER) {
77+
timer[i].toBeCalled = DEFCALL_RUNONLY;
7878
}
7979
// other timers get executed the specified number of times
80-
else if (numRuns[i] < maxNumRuns[i]) {
81-
toBeCalled[i] = DEFCALL_RUNONLY;
82-
numRuns[i]++;
80+
else if (timer[i].numRuns < timer[i].maxNumRuns) {
81+
timer[i].toBeCalled = DEFCALL_RUNONLY;
82+
timer[i].numRuns++;
8383

8484
// after the last run, delete the timer
85-
if (numRuns[i] >= maxNumRuns[i]) {
86-
toBeCalled[i] = DEFCALL_RUNANDDEL;
85+
if (timer[i].numRuns >= timer[i].maxNumRuns) {
86+
timer[i].toBeCalled = DEFCALL_RUNANDDEL;
8787
}
8888
}
8989
}
@@ -92,36 +92,31 @@ void SimpleTimer::run() {
9292
}
9393

9494
for (i = 0; i < MAX_TIMERS; i++) {
95-
switch(toBeCalled[i]) {
96-
case DEFCALL_DONTRUN:
97-
break;
98-
99-
case DEFCALL_RUNONLY:
100-
(*callbacks[i])();
101-
break;
102-
103-
case DEFCALL_RUNANDDEL:
104-
(*callbacks[i])();
105-
deleteTimer(i);
106-
break;
107-
}
95+
if (timer[i].toBeCalled == DEFCALL_DONTRUN)
96+
continue;
97+
98+
if (timer[i].hasParam)
99+
(*(timer_callback_p)timer[i].callback)(timer[i].param);
100+
else
101+
(*(timer_callback)timer[i].callback)();
102+
103+
if (timer[i].toBeCalled == DEFCALL_RUNANDDEL)
104+
deleteTimer(i);
108105
}
109106
}
110107

111108

112109
// find the first available slot
113110
// return -1 if none found
114111
int SimpleTimer::findFirstFreeSlot() {
115-
int i;
116-
117112
// all slots are used
118113
if (numTimers >= MAX_TIMERS) {
119114
return -1;
120115
}
121116

122117
// return the first slot with no callback (i.e. free)
123-
for (i = 0; i < MAX_TIMERS; i++) {
124-
if (callbacks[i] == 0) {
118+
for (int i = 0; i < MAX_TIMERS; i++) {
119+
if (timer[i].callback == NULL) {
125120
return i;
126121
}
127122
}
@@ -131,7 +126,7 @@ int SimpleTimer::findFirstFreeSlot() {
131126
}
132127

133128

134-
int SimpleTimer::setTimer(long d, timer_callback f, int n) {
129+
int SimpleTimer::setupTimer(unsigned long d, void* f, void* p, boolean h, unsigned n) {
135130
int freeTimer;
136131

137132
freeTimer = findFirstFreeSlot();
@@ -143,30 +138,46 @@ int SimpleTimer::setTimer(long d, timer_callback f, int n) {
143138
return -1;
144139
}
145140

146-
delays[freeTimer] = d;
147-
callbacks[freeTimer] = f;
148-
maxNumRuns[freeTimer] = n;
149-
enabled[freeTimer] = true;
150-
prev_millis[freeTimer] = elapsed();
141+
timer[freeTimer].delay = d;
142+
timer[freeTimer].callback = f;
143+
timer[freeTimer].param = p;
144+
timer[freeTimer].hasParam = h;
145+
timer[freeTimer].maxNumRuns = n;
146+
timer[freeTimer].enabled = true;
147+
timer[freeTimer].prev_millis = elapsed();
151148

152149
numTimers++;
153150

154151
return freeTimer;
155152
}
156153

157154

158-
int SimpleTimer::setInterval(long d, timer_callback f) {
159-
return setTimer(d, f, RUN_FOREVER);
155+
int SimpleTimer::setTimer(unsigned long d, timer_callback f, unsigned n) {
156+
return setupTimer(d, (void *)f, NULL, false, n);
160157
}
161158

159+
int SimpleTimer::setTimer(unsigned long d, timer_callback_p f, void* p, unsigned n) {
160+
return setupTimer(d, (void *)f, p, true, n);
161+
}
162+
163+
int SimpleTimer::setInterval(unsigned long d, timer_callback f) {
164+
return setupTimer(d, (void *)f, NULL, false, RUN_FOREVER);
165+
}
166+
167+
int SimpleTimer::setInterval(unsigned long d, timer_callback_p f, void* p) {
168+
return setupTimer(d, (void *)f, p, true, RUN_FOREVER);
169+
}
170+
171+
int SimpleTimer::setTimeout(unsigned long d, timer_callback f) {
172+
return setupTimer(d, (void *)f, NULL, false, RUN_ONCE);
173+
}
162174

163-
int SimpleTimer::setTimeout(long d, timer_callback f) {
164-
return setTimer(d, f, RUN_ONCE);
175+
int SimpleTimer::setTimeout(unsigned long d, timer_callback_p f, void* p) {
176+
return setupTimer(d, (void *)f, p, true, RUN_ONCE);
165177
}
166178

167179

168-
void SimpleTimer::deleteTimer(int timerId) {
169-
// ignore invalid argument
180+
void SimpleTimer::deleteTimer(unsigned timerId) {
170181
if (timerId >= MAX_TIMERS) {
171182
return;
172183
}
@@ -178,12 +189,9 @@ void SimpleTimer::deleteTimer(int timerId) {
178189

179190
// don't decrease the number of timers if the
180191
// specified slot is already empty
181-
if (callbacks[timerId] != NULL) {
182-
callbacks[timerId] = 0;
183-
enabled[timerId] = false;
184-
toBeCalled[timerId] = DEFCALL_DONTRUN;
185-
delays[timerId] = 0;
186-
numRuns[timerId] = 0;
192+
if (timer[timerId].callback != NULL) {
193+
memset(&timer[timerId], 0, sizeof (timer_t));
194+
timer[timerId].prev_millis = elapsed();
187195

188196
// update number of timers
189197
numTimers--;
@@ -192,55 +200,51 @@ void SimpleTimer::deleteTimer(int timerId) {
192200

193201

194202
// function contributed by code@rowansimms.com
195-
void SimpleTimer::restartTimer(int numTimer) {
196-
// ignore invalid argument
203+
void SimpleTimer::restartTimer(unsigned numTimer) {
197204
if (numTimer >= MAX_TIMERS) {
198205
return;
199206
}
200207

201-
prev_millis[numTimer] = elapsed();
208+
timer[numTimer].prev_millis = elapsed();
202209
}
203210

204211

205-
boolean SimpleTimer::isEnabled(int numTimer) {
212+
boolean SimpleTimer::isEnabled(unsigned numTimer) {
206213
if (numTimer >= MAX_TIMERS) {
207214
return false;
208215
}
209216

210-
return enabled[numTimer];
217+
return timer[numTimer].enabled;
211218
}
212219

213220

214-
void SimpleTimer::enable(int numTimer) {
215-
// ignore invalid argument
221+
void SimpleTimer::enable(unsigned numTimer) {
216222
if (numTimer >= MAX_TIMERS) {
217223
return;
218224
}
219225

220-
enabled[numTimer] = true;
226+
timer[numTimer].enabled = true;
221227
}
222228

223229

224-
void SimpleTimer::disable(int numTimer) {
225-
// ignore invalid argument
230+
void SimpleTimer::disable(unsigned numTimer) {
226231
if (numTimer >= MAX_TIMERS) {
227232
return;
228233
}
229234

230-
enabled[numTimer] = false;
235+
timer[numTimer].enabled = false;
231236
}
232237

233238

234-
void SimpleTimer::toggle(int numTimer) {
235-
// ignore invalid argument
239+
void SimpleTimer::toggle(unsigned numTimer) {
236240
if (numTimer >= MAX_TIMERS) {
237241
return;
238242
}
239243

240-
enabled[numTimer] = !enabled[numTimer];
244+
timer[numTimer].enabled = !timer[numTimer].enabled;
241245
}
242246

243247

244-
int SimpleTimer::getNumTimers() {
248+
unsigned SimpleTimer::getNumTimers() {
245249
return numTimers;
246250
}

0 commit comments

Comments
 (0)