-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPollingTimer.h
308 lines (271 loc) · 10.1 KB
/
PollingTimer.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#pragma once
#ifndef HT_POLLINGTIMER_H
#define HT_POLLINGTIMER_H
#ifdef ARDUINO
#include <Arduino.h>
#include <ArxTypeTraits.h>
#include "util/TeensyDirtySTLErrorSolution/TeensyDirtySTLErrorSolution.h"
#define MICROS() micros()
#elif defined(OF_VERSION_MAJOR)
#include "ofMain.h"
#include <functional>
#define MICROS() ofGetElapsedTimeMicros()
#else
#error THIS PLATFORM IS NOT SUPPORTED
#endif
class PollingTimer {
protected:
static constexpr int64_t UINT32_NUMERIC_LIMIT_MAX {0x00000000FFFFFFFF};
static constexpr uint32_t UINT32_NUMERIC_LIMIT_HALF {0x7FFFFFFF};
bool running {false};
bool prev_running {false};
uint32_t prev_us32 {0};
int64_t prev_us64 {0};
int64_t origin {0};
uint32_t ovf {0};
int64_t offset {0};
int64_t duration {0};
bool b_loop {false};
std::function<void(void)> cb_start;
std::function<void(void)> cb_pause;
std::function<void(void)> cb_stop;
public:
virtual ~PollingTimer() {}
virtual void start() {
startFromForUsec64(0, 0, false);
}
void startFromSec(const double from_sec) {
startFromForUsec64(from_sec * 1000000., 0, false);
}
void startFromMsec(const double from_ms) {
startFromForUsec64(from_ms * 1000., 0, false);
}
void startFromUsec(const double from_us) {
startFromForUsec64(from_us, 0, false);
}
void startForSec(const double for_sec, const bool loop = false) {
startFromForUsec64(0, for_sec * 1000000., loop);
}
void startForMsec(const double for_ms, const bool loop = false) {
startFromForUsec64(0, for_ms * 1000., loop);
}
void startForUsec(const double for_us, const bool loop = false) {
startFromForUsec64(0, for_us, loop);
}
void startFromForSec(const double from_sec, const double for_sec, const bool loop = false) {
startFromForUsec64(from_sec * 1000000., for_sec * 1000000., loop);
}
void startFromForMsec(const double from_ms, const double for_ms, const bool loop = false) {
startFromForUsec64(from_ms * 1000., for_ms * 1000., loop);
}
void startFromForUsec(const double from_us, const double for_us, const bool loop = false) {
startFromForUsec64(from_us, for_us, loop);
}
void startFromForUsec64(const int64_t from_us, const int64_t for_us, const bool loop = false) {
prev_running = running;
running = true;
prev_us32 = MICROS();
origin = prev_us64 = (int64_t)prev_us32;
ovf = 0;
offset = from_us;
duration = for_us;
b_loop = loop;
}
virtual void stop() {
prev_running = running;
running = false;
prev_us32 = 0;
origin = prev_us64 = 0;
ovf = 0;
// offset
// duration
// b_loop
}
virtual void play() {
if (isPausing()) {
prev_running = running;
running = true;
const uint32_t curr_us32 = MICROS();
int64_t diff = 0;
if (curr_us32 > prev_us32)
diff = (int64_t)(curr_us32 - prev_us32);
else
diff = UINT32_NUMERIC_LIMIT_MAX - (int64_t)(prev_us32 - curr_us32);
origin += diff;
prev_us64 += diff;
prev_us32 = curr_us32;
// No need to change
// ovf
// offset
// duration
// b_loop
} else if (isRunning())
;
else
restart();
}
virtual void pause() {
if (isRunning()) {
this->update();
prev_running = running;
running = false;
// No need to change
// origin
// prev_us32
// prev_us64
// ovf
// offset
// duration
// b_loop
}
}
virtual void restart() {
stop();
startFromForUsec64(offset, duration, b_loop);
}
virtual void clear() {
prev_running = false;
running = false;
prev_us32 = 0;
origin = prev_us64 = 0;
ovf = 0;
offset = 0;
duration = 0;
b_loop = false;
}
int64_t update() {
if (isRunning()) {
if (cb_start && hasStarted()) cb_start();
prev_running = true;
int64_t t = elapsed() + offset;
if ((t >= duration) && (duration != 0)) {
if (b_loop) {
t = 0;
restart();
} else {
t = prev_us64 - origin + offset;
stop();
}
}
return t;
} else if (isPausing()) {
if (cb_pause && hasPaused()) cb_pause();
prev_running = false;
return prev_us64 - origin + offset;
} else {
if (cb_stop && hasStopped()) cb_stop();
prev_running = false;
return 0;
}
}
bool isRunning() const { return running; }
bool isPausing() const { return (!running && (origin != 0)); }
bool isStopping() const { return (!running && (origin == 0)); }
bool hasStarted() const { return isRunning() && !prev_running; }
bool hasPaused() const { return isPausing() && prev_running; }
bool hasStopped() const { return isStopping() && prev_running; }
void releaseEventTrigger() { prev_running = running; }
int64_t usec64() { return microsec(); }
double usec() { return (double)microsec(); }
double msec() { return usec() * 0.001; }
double sec() { return usec() * 0.000001; }
double getOrigin() const { return (double)origin; }
uint32_t getOverflow() const { return ovf; }
double getOffsetUsec64() const { return offset; }
double getOffsetUsec() const { return (double)offset; }
double getOffsetMsec() const { return (double)offset * 0.001; }
double getOffsetSec() const { return (double)offset * 0.000001; }
int64_t getDurationUsec64() const { return duration; }
double getDurationUsec() const { return (double)duration; }
double getDurationMsec() const { return (double)duration * 0.001; }
double getDurationSec() const { return (double)duration * 0.000001; }
double getRemainingTime() { return (double)duration - usec(); }
double getRemainingLife() { return (double)duration - usec() / (double)duration; }
void setOffsetUsec64(const int64_t us) { offset = us; }
void setOffsetUsec(const double us) { setOffsetUsec64(int64_t(us)); }
void setOffsetMsec(const double ms) { setOffsetUsec64(int64_t(1000. * ms)); }
void setOffsetSec(const double sec) { setOffsetUsec64(int64_t(1000000. * sec)); }
void addOffsetUsec64(const int64_t us) { setOffsetUsec64(offset + us); }
void addOffsetUsec(const double us) { setOffsetUsec64(int64_t(us)); }
void addOffsetMsec(const double ms) { addOffsetUsec64(int64_t(1000. * ms)); }
void addOffsetSec(const double sec) { addOffsetUsec64(int64_t(1000000. * sec)); }
void setDurationUsec64(const int64_t us) { duration = us; }
void setDurationUsec(const double us) { setDurationUsec64(int64_t(us)); }
void setDurationMsec(const double ms) { setDurationUsec64(int64_t(1000. * ms)); }
void setDurationSec(const double sec) { setDurationUsec64(int64_t(1000000. * sec)); }
void setTimeUsec64(const int64_t u) {
if (isStopping()) {
prev_us32 = MICROS();
prev_us64 = (int64_t)prev_us32;
origin = prev_us64 - u;
offset = 0;
} else if (isPausing()) {
int64_t diff_us = (int64_t)MICROS() - (int64_t)prev_us32;
if (diff_us >= 0) {
// overflow
if ((int64_t)UINT32_NUMERIC_LIMIT_MAX - (int64_t)prev_us32 < diff_us) {
prev_us32 += (uint32_t)diff_us; // overflow
++ovf;
} else {
prev_us32 += (uint32_t)diff_us;
}
}
// overflow
else {
diff_us = (int64_t)MICROS() + ((int64_t)UINT32_NUMERIC_LIMIT_MAX - (int64_t)prev_us32);
prev_us32 += (uint32_t)diff_us; // overflow
++ovf;
}
prev_us64 += diff_us;
origin += diff_us;
setOffsetUsec(u - elapsed());
} else { // isRunning()
setOffsetUsec(u - elapsed());
}
}
void setTimeUsec(const double u) { setTimeUsec64(int64_t(u)); }
void setTimeMsec(const double m) { setTimeUsec64(int64_t(m * 1000.)); }
void setTimeSec(const double s) { setTimeUsec64(int64_t(s * 1000000.)); }
void setLoop(const bool b) { b_loop = b; }
bool isLoop() const { return b_loop; }
bool hasOffset() const { return offset != 0; }
bool hasDuration() const { return duration != 0; }
void onStart(const std::function<void(void)>& cb) { cb_start = cb; }
void onPause(const std::function<void(void)>& cb) { cb_pause = cb; }
void onStop(const std::function<void(void)>& cb) { cb_stop = cb; }
void removeEventOnStart() { cb_start = nullptr; }
void removeEventOnPause() { cb_pause = nullptr; }
void removeEventOnStop() { cb_stop = nullptr; }
bool hasEventOnStart() const { return (bool)cb_start; }
bool hasEventOnPause() const { return (bool)cb_pause; }
bool hasEventOnStop() const { return (bool)cb_stop; }
protected:
int64_t microsec() {
if (isRunning()) {
return elapsed() + offset;
} else if (isPausing()) {
return prev_us64 - origin + offset;
} else {
return 0;
}
}
int64_t elapsed() {
uint32_t curr_us32 = MICROS();
if (curr_us32 < prev_us32) // check overflow and interrupt
{
if (MICROS() < prev_us32) // overflow
{
if (prev_us32 - MICROS() > UINT32_NUMERIC_LIMIT_HALF)
++ovf;
} else // interrupted and changed prev_us after curr_us is captured
{
curr_us32 = MICROS(); // update curr_us
}
}
prev_us32 = curr_us32;
const int64_t now = (int64_t)curr_us32 | ((int64_t)ovf << 32);
prev_us64 = now;
return now - origin;
}
};
#endif // HT_POLLINGTIMER_H