-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSpinTimer.h
260 lines (229 loc) · 8.53 KB
/
SpinTimer.h
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* SpinTimer.h
*
* Created on: 13.08.2013
* Author: niklausd
*/
#ifndef SPINTIMER_H_
#define SPINTIMER_H_
/**
* Schedule all timers, check their expiration states.
* @see SpinTimerContext::handleTick()
*/
void scheduleTimers();
/**
* Delay the caller by the mentioned time while all timers are kept being scheduled in the meanwhile.
* @param delayMillis Time to wait in [ms]
*
* This function is kept for backward compatibility, you can use the arduino delay() function instead.
*/
void delayAndSchedule(unsigned long delayMillis);
/**
* Action Interface, will notify timeExpired() event.
* Implementations derived from this interface can be injected into a Timer object.
* The Timer then will call out the specific action's timeExpired() method.
*/
class SpinTimerAction
{
public:
/**
* Time expired event. To be implemented by specific Timer Action class.
*/
virtual void timeExpired() = 0;
protected:
SpinTimerAction() { }
public:
virtual ~SpinTimerAction() { }
private: // forbidden functions
SpinTimerAction(const SpinTimerAction& src); // copy constructor
SpinTimerAction& operator = (const SpinTimerAction& src); // assignment operator
};
/**
* Universal Timer.
*
* Features:
* - intended use: encapsulate recurring and non-recurring timed actions with a non-busy wait approach for time spans in a range of 0 to thousands of milliseconds, such as:
* - debouncing push-button and switch signals (recommended interval: 50 ms)
* - blink some LEDs
* - schedule some sequences where time accuracy is not crucial
* - configurable to be either
* - recurring (timer automatically restarts after the interval) or
* - non-recurring (timer stops after timeout period is over)
* - timer interval/timeout time configurable ([ms])
* - automatically attaches to SpinTimerContext's linked list of SpinTimer objects. As long as the
* SpinTimerContext::handleTick() will be called (use global function scheduleTimers() to do so),
* this will periodically update the timers' states and thus perform the timers' expire evaluations
* - based on system uptime (number of milliseconds since the system began running the current program,
* i.e. Arduino: millis() function or STM32: HAL_GetTick() function),
* - handles system time overflows correctly (unsigned long int type, occurring around every 50 hours)
*
* Integration:
*
* (shown on the basis of a simple Arduino Framework application toggling the board's built-in LED)
*
* - Include
*
* #include "SpinTimer.h"
* #include "Arduino.h"
*
* - Timer interval constant definition
*
* const unsigned int BLINK_TIME_MILLIS = 200;
*
* - specific SpinTimerAction implementation, periodically toggling the built-in LED
*
* class BlinkTimerAction : public SpinTimerAction
* {
* public:
* void timeExpired()
* {
* digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
* }
* };
*
* - Setup: set LED pin to output; create recurring Timer, inject specific TimerAction
*
* // The setup function is called once at startup of the sketch
* void setup()
* {
* pinMode(LED_BUILTIN, OUTPUT);
* new SpinTimer(BLINK_TIME_MILLIS, new BlinkTimerAction(), SpinTimer::IS_RECURRING, SpinTimer::IS_AUTOSTART);
* }
*
* - Loop: call scheduleTimers()
*
* // The loop function is called in an endless loop
* void loop()
* {
* scheduleTimers();
* }
*
* .
*/
class SpinTimer
{
friend class SpinTimerContext;
public:
/**
* Timer constructor.
* @param timeMillis Time out or interval time to be set for the timer [ms]; 0 will make the timer expire as soon as possible.
* @param action SpinTimerAction, is able to emit a timer expired event to any specific listener, default: 0 (no event will be sent)
* @param isRecurring Operation mode, true: recurring, false: non-recurring, default: false
* @param isAutostart Autostart mode, true: autostart enabled, false: autostart disabled, default: false
*/
SpinTimer(unsigned long timeMillis, SpinTimerAction* action = 0, bool isRecurring = false, bool isAutostart = false);
/**
* Timer destructor.
* Will detach itself from TimerContext.
*/
virtual ~SpinTimer();
/**
* Attach specific SpinTimerAction, acts as dependency injection. @see SpinTimerAction interface.
* @param action Specific SpinTimerAction
*/
void attachAction(SpinTimerAction* action);
/**
* SpinTimer Action accessor method.
* @return SpinTimerAction object pointer or 0 if no action is attached.
*/
SpinTimerAction* action() const;
protected:
/**
* Get next SpinTimer object pointer out of the linked list containing timers.
* @return SpinTimer object pointer or 0 if current object is the trailing list element.
*/
SpinTimer* next() const;
/**
* Set next SpinTimer object of the linked list containing timers.
* @param timer SpinTimer object pointer to be set as the next element of the list.
*/
void setNext(SpinTimer* timer);
public:
/**
* Start or restart the timer with a specific time out or interval time.
* @param timeMillis Time out or interval time to be set for the timer [ms]; 0 will make the timer expire as soon as possible.
*/
void start(unsigned long timeMillis);
/**
* Start or restart the timer.
* The timer will expire after the specified time set with the constructor or start(timeMillis) before.
*/
void start();
/**
* Cancel the timer and stop. No time expired event will be sent out after the specified time would have been elapsed.
* Subsequent isExpired() queries will return false.
*/
void cancel();
/**
* Poll method to get the timer expire status, recalculates whether the timer has expired before.
* This method could be used in a pure polling mode, where tick() has not to get called
* (by the TimerContext::handleTick() method), but also a mixed operation in combination with
* calling tick() periodically is possible.
* Subsequent isExpired() queries will return false after the first one returned true,
* as long as the time did not expire again in case of a recurring timer.
* @return true if the timer has expired.
*/
bool isExpired();
/**
* Indicates whether the timer is currently running.
* @return true if timer is running.
*/
bool isRunning() const;
/**
* Returns the current interval of the timer.
* @return Timer interval/timeout time [ms].
*/
unsigned long getInterval() const;
/**
* Sets the operation mode
* @param isRecurring Operation mode, true: recurring, false: non-recurring
*/
void setIsRecurring(bool isRecurring);
/**
* Kick the Timer.
* Recalculates whether the timer has expired.
*/
void tick();
private:
/**
* Internal tick method, evaluates the expired state.
*/
void internalTick();
/**
* Starts time interval measurement, calculates the expiration trigger time.
* Manages to avoid unsigned long int overflow issues occurring around every 50 hours.
*/
void startInterval();
public:
/**
* Constant for isRecurring parameter of the constructor (@see SpinTimer()), to create a one shot timer.
*/
static const bool IS_NON_RECURRING;
/**
* Constant for isRecurring parameter of the constructor (@see SpinTimer()), to create a recurring timer.
*/
static const bool IS_RECURRING;
/**
* Constant for isAutostart parameter of the constructor (@see SpinTimer()), to create a timer which does not start.
*/
static const bool IS_NON_AUTOSTART;
/**
* Constant for isAutostart parameter of the constructor (@see SpinTimer()), to create a timer which does start after creation.
*/
static const bool IS_AUTOSTART;
private:
bool m_isRunning; /// Timer is running flag, true: timer is running, false: timer is stopped.
bool m_isRecurring; /// Timer mode flag, true: timer will automatically restart after expiration.
bool m_isExpiredFlag; /// Timer expiration flag.
bool m_willOverflow; /// UptimeInfo::Instance()->tMillis() will overflow during new started interval.
unsigned long m_currentTimeMillis; /// interval time measurement base, updated every internalTick(), called either by tick() or by isExpired()
unsigned long m_triggerTimeMillis;
unsigned long m_triggerTimeMillisUpperLimit;
unsigned long m_delayMillis;
SpinTimerAction* m_action;
SpinTimer* m_next;
private: // forbidden default functions
SpinTimer& operator = (const SpinTimer& src); // assignment operator
SpinTimer(const SpinTimer& src); // copy constructor
};
#endif /* SPINTIMER_H_ */