forked from JChristensen/Timer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.cpp
More file actions
243 lines (215 loc) · 5.52 KB
/
Timer.cpp
File metadata and controls
243 lines (215 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
Code by Simon Monk
http://www.simonmonk.org
* * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// For Arduino 1.0 and earlier
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "Timer.h"
Timer::Timer(void)
{
}
int8_t Timer::every(
unsigned long period,
void (*callback)(),
std::function<void(void)> stdCallback,
int repeatCount)
{
int8_t i = findFreeEventIndex();
if (i == -1) return -1;
_events[i].eventType = EVENT_EVERY;
_events[i].period = period;
_events[i].repeatCount = repeatCount;
_events[i].callback = callback;
_events[i].stdCallback = stdCallback;
// Timers all start upon the first loop of Timer.update();
// Otherwise they start 'now'
if (m_bStarted) {
_events[i].lastEventTime = millis();
}
else {
_events[i].lastEventTime = 0;
}
_events[i].count = 0;
_events[i].paused = false;
return i;
}
int8_t Timer::every(unsigned long period, void (*callback)(), int repeatCount)
{
return every(period, callback, NULL, repeatCount);
}
int8_t Timer::every(unsigned long period, void (*callback)())
{
return every(period, callback, NULL, -1); // - means forever
}
int8_t Timer::after(unsigned long period, void (*callback)())
{
return every(period, callback, NULL, 1);
}
int8_t Timer::every(unsigned long period, std::function<void(void)> stdCallback, int repeatCount)
{
return every(period, NULL, stdCallback, repeatCount);
}
int8_t Timer::every(unsigned long period, std::function<void(void)> stdCallback)
{
return every(period, NULL, stdCallback, -1);
}
int8_t Timer::after(unsigned long period, std::function<void(void)> stdCallback)
{
return every(period, NULL, stdCallback, 1);
}
int8_t Timer::oscillate(uint8_t pin, unsigned long period, uint8_t startingValue, int repeatCount)
{
int8_t i = findFreeEventIndex();
if (i == NO_TIMER_AVAILABLE) return NO_TIMER_AVAILABLE;
_events[i].eventType = EVENT_OSCILLATE;
_events[i].pin = pin;
_events[i].period = period;
_events[i].pinState = startingValue;
digitalWrite(pin, startingValue);
_events[i].repeatCount = repeatCount * 2; // full cycles not transitions
// Timers all start upon the first loop of Timer.update();
// Otherwise they start 'now'
if (m_bStarted) {
_events[i].lastEventTime = millis();
}
else {
_events[i].lastEventTime = 0;
}
_events[i].count = 0;
_events[i].paused = false;
return i;
}
int8_t Timer::oscillate(uint8_t pin, unsigned long period, uint8_t startingValue)
{
return oscillate(pin, period, startingValue, -1); // forever
}
/**
* This method will generate a pulse of !startingValue, occuring period after the
* call of this method and lasting for period. The Pin will be left in !startingValue.
*/
int8_t Timer::pulse(uint8_t pin, unsigned long period, uint8_t startingValue)
{
return oscillate(pin, period, startingValue, 1); // once
}
/**
* This method will generate a pulse of startingValue, starting immediately and of
* length period. The pin will be left in the !startingValue state
*/
int8_t Timer::pulseImmediate(uint8_t pin, unsigned long period, uint8_t pulseValue)
{
int8_t id(oscillate(pin, period, pulseValue, 1));
// now fix the repeat count
if (id >= 0 && id < MAX_NUMBER_OF_EVENTS) {
_events[id].repeatCount = 1;
}
return id;
}
void Timer::startImmediate()
{
m_bStarted = true;
}
void Timer::stop(int8_t id)
{
if (id >= 0 && id < MAX_NUMBER_OF_EVENTS) {
_events[id].eventType = EVENT_NONE;
}
}
void Timer::stop()
{
m_bPaused = true;
m_bStopped = true;
}
void Timer::pause()
{
m_bPaused = true;
}
void Timer::pause(int8_t id)
{
_events[id].paused = true;
}
void Timer::unpause()
{
if (m_bStopped) {
Serial.println("Cannot unpause Timer in stopped state!");
return;
}
m_bPaused = false;
}
void Timer::unpause(int8_t id)
{
_events[id].paused = false;
}
void Timer::update(void)
{
if (m_bPaused) {
return;
}
if (!m_bStarted) {
m_bStarted = true;
}
for (int8_t i = 0; i < MAX_NUMBER_OF_EVENTS; i++)
{
if (!m_bPaused && _events[i].eventType != EVENT_NONE && !_events[i].paused)
{
unsigned long now = millis();
_events[i].update(now);
}
}
}
void Timer::update(unsigned long now)
{
if (m_bPaused) {
return;
}
if (!m_bStarted) {
m_bStarted = true;
}
for (int8_t i = 0; i < MAX_NUMBER_OF_EVENTS; i++)
{
if (!m_bPaused && _events[i].eventType != EVENT_NONE && !_events[i].paused)
{
_events[i].update(now);
}
}
}
void Timer::updatePeriod(int8_t id, unsigned long newPeriod)
{
if (id >= 0 && id < MAX_NUMBER_OF_EVENTS)
{
if (_events[id].eventType != EVENT_NONE)
{
_events[id].period = newPeriod;
}
}
}
int8_t Timer::findFreeEventIndex(void)
{
for (int8_t i = 0; i < MAX_NUMBER_OF_EVENTS; i++)
{
if (_events[i].eventType == EVENT_NONE)
{
return i;
}
}
return NO_TIMER_AVAILABLE;
}