-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.cpp
50 lines (38 loc) · 894 Bytes
/
timer.cpp
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
#include "timer.h"
Timer::Timer(unsigned long interval):
_origin(_millis()), _interval(interval), _enabled(true) {}
unsigned long Timer::getInterval() {
return _interval;
}
void Timer::setOrigin(unsigned long origin) {
_origin = origin;
}
void Timer::setOriginToNow() {
_origin = _millis();
}
void Timer::reset(unsigned long interval) {
_origin = _millis();
_interval = interval;
}
float Timer::progress(bool constrain) {
float progress = (_millis() - _origin) / (double) _interval;
return constrain ? progress - (long) progress : progress;
}
unsigned long Timer::elapsed() {
return _millis() - _origin;
}
void Timer::setEnabled(bool enabled) {
_enabled = enabled;
}
bool Timer::fire() {
if (!_enabled) {
return false;
}
unsigned long now = _millis();
if (now > _origin && now - _origin > _interval) {
_origin = now;
return true;
} else {
return false;
}
}