-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.h
79 lines (62 loc) · 1.46 KB
/
timer.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
#ifndef __INC_TIMER_H
#define __INC_TIMER_H
#include <stdint.h>
template<uint32_t (*GET_TIME)(void)> class Timer {
uint32_t last;
uint32_t time_length;
bool bLoop;
bool bRunning;
public:
Timer() : time_length(0), bRunning(false) {}
Timer(uint32_t len) : time_length(len), bRunning(false) {}
void start(uint32_t new_time, bool loop) {
time_length = new_time;
start(loop);
}
void start(bool loop = false) {
last = GET_TIME();
bLoop = loop;
bRunning = true;
}
uint32_t sinceStart() {
return GET_TIME() - last;
}
uint32_t untilDone() {
return last + time_length - GET_TIME();
}
// The percent completed, represented as a 0-255 value suitable for scale8
uint8_t perc8() {
if(bRunning) {
return (sinceStart() * 256) / time_length;
}
return 0;
}
// The percent completed, represented as a 0-65535 value suitable for scale8
uint16_t perc16() {
if(bRunning) {
return (sinceStart() * 65536) / time_length;
}
return 0;
}
void stop() {
bRunning = false;
}
// Has this timer tripped/fired since the last time we checked it?
bool fired() {
if(bRunning) {
uint32_t curtime = GET_TIME();
if((curtime - last) >= time_length) {
bRunning = bLoop;
last = curtime;
return true;
}
}
return false;
}
bool running() {
return bRunning;
}
};
typedef Timer<micros> MicrosTimer;
typedef Timer<millis> MillisTimer;
#endif