-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtimer.h
More file actions
67 lines (53 loc) · 1.31 KB
/
Copy pathtimer.h
File metadata and controls
67 lines (53 loc) · 1.31 KB
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
#pragma once
class GlobalTimer {
public:
uint32_t now_millis;
uint32_t now_micros;
uint32_t last_micros;
uint32_t last_millis;
uint32_t delta_micros;
uint32_t delta_millis;
void setup()
{
this->last_millis = this->now_millis = millis();
this->last_micros = this->now_micros = micros();
}
void update()
{
this->last_millis = this->now_millis;
this->now_millis = millis();
this->delta_millis = this->now_millis - this->last_millis;
this->last_micros = this->now_micros;
this->now_micros = micros();
this->delta_micros = this->now_micros - this->last_micros;
}
};
GlobalTimer globalTimer;
class Timer {
public:
uint32_t markTime;
void start(uint32_t duration_ms) {
this->markTime = globalTimer.now_millis + duration_ms;
}
void stop() {
this->start(0);
}
uint32_t since_mark() {
if (globalTimer.now_millis < this->markTime)
return 0;
return globalTimer.now_millis - this->markTime;
}
void snooze(uint32_t duration_ms) {
while (this->markTime < globalTimer.now_millis)
this->markTime += duration_ms;
}
bool ended() {
return globalTimer.now_millis > this->markTime;
}
bool every(uint32_t duration_ms) {
if (!this->ended())
return 0;
this->snooze(duration_ms);
return 1;
}
};