-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntervalCounter.h
183 lines (158 loc) · 7.12 KB
/
IntervalCounter.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
#pragma once
#ifndef HT_INTERVALCOUNTER_H
#define HT_INTERVALCOUNTER_H
#include "PollingTimer.h"
class IntervalCounter : public PollingTimer {
double interval_us {1.}; // 1us == minimum interval
double cnt {0.};
protected:
std::function<void(void)> func;
public:
explicit IntervalCounter(const double sec = 0.)
: interval_us(constrain_interval_us(sec * 1000000.)), cnt(0.) {
}
virtual ~IntervalCounter() {}
void startFromCount(const double from_count) {
startIntervalUsecFromForCount(this->interval_us, from_count, 0, false);
}
void startForCount(const double for_count, const bool loop = false) {
startIntervalUsecFromForCount(this->interval_us, 0., for_count, loop);
}
void startFromForCount(const double from_count, const double for_count, const bool loop = false) {
startIntervalUsecFromForCount(this->interval_us, from_count, for_count, loop);
}
void startIntervalSec(const double interval_sec) {
startIntervalFromForSec(interval_sec, 0., 0., false);
}
void startIntervalMsec(const double interval_ms) {
startIntervalFromForMsec(interval_ms, 0., 0., false);
}
void startIntervalUsec(const double interval_us) {
startIntervalFromForUsec(interval_us, 0., 0., false);
}
void startIntervalFromSec(const double interval_sec, const double from_sec) {
startIntervalFromForSec(interval_sec, from_sec, 0., false);
}
void startIntervalFromMsec(const double interval_ms, const double from_ms) {
startIntervalFromForMsec(interval_ms, from_ms, 0., false);
}
void startIntervalFromUsec(const double interval_us, const double from_us) {
startIntervalFromForUsec(interval_us, from_us, 0., false);
}
void startIntervalSecFromCount(const double interval_sec, const double from_count) {
startIntervalSecFromForCount(interval_sec, from_count, 0., false);
}
void startIntervalMsecFromCount(const double interval_ms, const double from_count) {
startIntervalMsecFromForCount(interval_ms, from_count, 0., false);
}
void startIntervalUsecFromCount(const double interval_us, const double from_count) {
startIntervalUsecFromForCount(interval_us, from_count, 0., false);
}
void startIntervalForSec(const double interval_sec, const double for_sec, const bool loop = false) {
startIntervalFromForSec(interval_sec, 0., for_sec, loop);
}
void startIntervalForMsec(const double interval_ms, const double for_ms, const bool loop = false) {
startIntervalFromForMsec(interval_ms, 0., for_ms, loop);
}
void startIntervalForUsec(const double interval_us, const double for_us, const bool loop = false) {
startIntervalFromForUsec(interval_us, 0., for_us, loop);
}
void startIntervalSecForCount(const double interval_sec, const double for_count, const bool loop = false) {
startIntervalSecFromForCount(interval_sec, 0., for_count, loop);
}
void startIntervalMsecForCount(const double interval_ms, const double for_count, const bool loop = false) {
startIntervalMsecFromForCount(interval_ms, 0., for_count, loop);
}
void startIntervalUsecForCount(const double interval_us, const double for_count, const bool loop = false) {
startIntervalUsecFromForCount(interval_us, 0., for_count, loop);
}
void startIntervalFromForSec(const double interval_sec, const double from_sec, const double for_sec, const bool loop = false) {
startIntervalFromForUsec(interval_sec * 1000000., from_sec * 1000000., for_sec * 1000000., loop);
}
void startIntervalFromForMsec(const double interval_ms, const double from_ms, const double for_ms, const bool loop = false) {
startIntervalFromForUsec(interval_ms * 1000., from_ms * 1000., for_ms * 1000., loop);
}
void startIntervalFromForUsec(const double interval_us, const double from_us, const double for_us, const bool loop = false) {
this->interval_us = constrain_interval_us(interval_us);
this->cnt = from_us / this->interval_us;
startFromForUsec(from_us, for_us, loop);
}
void startIntervalSecFromForCount(const double interval_sec, const double from_count, const double for_count, const bool loop = false) {
startIntervalUsecFromForCount(interval_sec * 1000000., from_count, for_count, loop);
}
void startIntervalMsecFromForCount(const double interval_ms, const double from_count, const double for_count, const bool loop = false) {
startIntervalUsecFromForCount(interval_ms * 1000., from_count, for_count, loop);
}
void startIntervalUsecFromForCount(const double interval_us, const double from_count, const double for_count, const bool loop = false) {
this->interval_us = constrain_interval_us(interval_us);
this->cnt = from_count;
startFromForUsec(from_count * this->interval_us, for_count * this->interval_us, loop);
}
void startOnce() {
startForCount(1);
}
void startOnceAfterSec(const double after_sec) {
startIntervalSecForCount(after_sec, 1);
}
void startOnceAfterMsec(const double after_ms) {
startIntervalMsecForCount(after_ms, 1);
}
void startOnceAfterUsec(const double after_us) {
startIntervalUsecForCount(after_us, 1);
}
virtual void stop() override {
cnt = 0;
PollingTimer::stop();
}
virtual void restart() override {
cnt = 0;
PollingTimer::restart();
}
virtual void clear() override {
cnt = 0;
PollingTimer::clear();
}
double count() {
if (isPausing()) IntervalCounter::update();
return (double)cnt;
}
double getIntervalUsec64() const { return int64_t(interval_us); }
double getIntervalUsec() const { return interval_us; }
double getIntervalMSec() const { return interval_us * 0.001; }
double getIntervalSec() const { return interval_us * 0.000001; }
void setIntervalUsec64(const int64_t u64) { interval_us = constrain_interval_us((double)u64); }
void setIntervalUsec(const double us) { interval_us = constrain_interval_us(us); }
void setIntervalMsec(const double ms) { interval_us = constrain_interval_us(ms * 1000.); }
void setIntervalSec(const double sec) { interval_us = constrain_interval_us(sec * 1000000.); }
bool hasInterval() const { return interval_us != 1.; }
void setOffsetCount(const double count) {
setOffsetUsec(interval_us * count);
}
void onUpdate(const std::function<void(void)>& f) {
func = f;
}
bool hasEventOnUpdate() const {
return (bool)func;
}
void removeEventOnUpdate() {
func = nullptr;
}
bool update() {
const double us = PollingTimer::update();
double prev_cnt = cnt;
cnt = us / interval_us;
bool b = (floor(cnt) > 0.) && (floor(cnt) > floor(prev_cnt));
if (b && func) func();
return b;
}
private:
double constrain_interval_us(const double interval_us) {
if (interval_us <= 1.)
return 1.;
else if (interval_us >= 1000000000.)
return 1000000000.;
else
return interval_us;
}
};
#endif // HT_INTERVALCOUNTER_H