-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseClock.h
219 lines (188 loc) · 4.52 KB
/
BaseClock.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
// BaseClock - Abstract base class for clocks.
#ifndef BASECLOCK_H
#define BASECLOCK_H
class BaseClock {
private:
// The current time when the clock was started.
double _started;
// The current time when the clock was stopped.
double _stopped;
// The last calculated elapsed time.
double _elapsed;
// The last calculated loop time;
double _lastloop;
protected:
/*
* diff()
* Calculate the difference between two time values.
*/
virtual double diff(double start, double end) {
return(end - start);
}
/*
* current_time()
* Get the current time from the system.
*
* The method used to get the current time will be different for the
* timers derived from this base class, but each derived class will be
* expected to return a double value that is the current time in seconds.
*
* It does not matter if the origin is a particular epoch or if it is the
* time when the program started, as long as the origin is consistent from
* call to call so that diff() can calculate the difference between two time
* values.
*/
virtual double current_time() = 0;
public:
BaseClock() = default;
virtual ~BaseClock() = default;
virtual void init() {
double ct = current_time();
_lastloop = ct;
_started = ct;
_stopped = 0.0;
_elapsed = 0.0;
}
virtual void restart() {
double ct = current_time();
double sd = diff(_stopped, ct);
_started += sd;
_lastloop += sd;
_stopped = 0.0;
}
/*
* start()
* Either start the clock
* at the current time or restarts the
* clock at 0.0.
*
* Calling start() while the clock is
* running restarts it at 0.0.
*
* Calling start() while the clock is
* stopped effectively restarts it at the
* current time by adjusting the start
* and lastlap times by the amount of time
* that the clock was stopped.
*/
virtual double start() {
if (_stopped) {
restart();
}
else {
init();
}
return elapsed();
}
/*
* stop()
* Record the time when the
* clock is stopped. When stopped,
* the recorded value is used in the elapsed
* time calculation. The clock
* remains stopped until the start()
* function is called to restart it.
*/
virtual double stop() {
if (!_stopped) {
_stopped = current_time();
}
return elapsed();
}
/*
* reset()
* Stop the clock and initialize all times to the
* current time.
* The start() function must be called to
* restart it.
*/
virtual double reset() {
_lastloop = _started = _stopped = current_time();
_elapsed = 0.0;
return _elapsed;
}
/*
* loop()
* Return the number of seconds
* since loop() was last called.
* Resets the loop timer to zero.
*/
virtual double loop() {
double dl; // difference from last loop
if (_stopped) {
dl = diff(_lastloop, _stopped);
_lastloop = _stopped;
}
else {
double ct = current_time();
dl = diff(_lastloop, ct);
_lastloop = ct;
}
return dl;
}
/*
* checkloop()
* Return the number of seconds
* since loop() was last called
* Does not reset the loop timer.
*/
virtual double check_loop() {
double dl; // difference from last loop
if (_stopped) {
dl = diff(_lastloop, _stopped);
}
else {
double ct = current_time();
dl = diff(_lastloop, ct);
}
return dl;
}
/*
* elapsed()
* Returns the elapsed time in
* seconds that the clock has been
* running.
*/
virtual double elapsed() {
if (_stopped) {
_elapsed = diff(_started, _stopped);
}
else {
_elapsed = diff(_started, current_time());
}
return _elapsed;
}
/*
* isstopped()
* Return non-zero if the
* clock is stopped, and zero if it
* is running.
*/
int isstopped() {
if (_stopped)
return 1;
else
return 0;
}
/*
* elapsedDHMS()
* Return the elapsed time in
* seconds that the clock has been
* running. In addition, it breaks out
* the elapsed time into days, hours, minutes,
* and seconds. Those are returned in the
* passed parameters.
*
* You can call this by specifying an elapsed time as the first argument, or
* not. This allows you to pass in the result of a previous call to
* elapsed() so that the days, hours, minutes, and seconds match the passed
* elapsed time.
*
* When called without the elapsed_time parameter, the elapsed time is read
* and may differ from a previous call to elapsed by a few fractions of a
* second.
*/
virtual double elapsedDHMS(double elapsed_time, double &days, double &hours, double &mins, double &secs);
virtual double elapsedDHMS(double &days, double &hours, double &mins, double &secs);
};
#endif /* BASECLOCK_H */