-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTime.cpp
108 lines (86 loc) · 2.89 KB
/
Time.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
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
#include "Time.h"
using namespace myRIO;
/** Constructor
* Create a time object referencing the given time
*/
Time::Time(unsigned long sec, unsigned long nsec) : startTimeSec(sec), startTimeNsec(nsec) {}
/** Get the current time
* @return creates a time object with the current time
*/
Time Time::stopwatch() {
struct timespec currentTime;
clock_gettime(CLOCK_MONOTONIC, ¤tTime);
return Time(currentTime.tv_sec, currentTime.tv_nsec);
}
/** Reset timer to the current time
*/
void Time::reset() {
struct timespec currentTime;
clock_gettime(CLOCK_MONOTONIC, ¤tTime);
startTimeSec = currentTime.tv_sec;
startTimeNsec = currentTime.tv_nsec;
}
/** Elapsed time nanoseconds
* If you get elapsed_ns() = 1000, 1000ns are elapsed, or 1000e-9 seconds
* WARNING : cannot measure more than 2 seconds (long limit = 2e9; 1 seconds in ns = 1e9)
* @return the elapsed time in nanoseconds
*/
long Time::elapsed_ns() {
struct timespec currentTime;
clock_gettime(CLOCK_MONOTONIC, ¤tTime);
unsigned long d_sec = currentTime.tv_sec - startTimeSec;
long d_nsec = startTimeNsec - currentTime.tv_nsec;
unsigned long finalTime = d_sec * 1e9L - d_nsec;
return finalTime;
}
/** Wait function in seconds
* @param s the seconds to wait
*/
void Time::wait_s(long int s) {
time_t currentTime, finalTime;
time(¤tTime);
finalTime = currentTime + s;
while (currentTime < finalTime)
time(¤tTime);
}
/** Wait function in milliseconds
* @param ms the milliseconds to wait
*/
void Time::wait_ms(long int ms) {
struct timeval currentTime, finalTime;
gettimeofday(¤tTime, NULL);
// gettimeofday return µs%1e6
if(currentTime.tv_usec + ms*1000L >= 1e6) { // if we overflowed
time_t inRange = 1e6 - currentTime.tv_usec; // we counted $inRange µs
time_t left = 1000L*ms - inRange; // so there is $left left from 0
while(currentTime.tv_usec > left) // wait to overflow
gettimeofday(¤tTime, NULL);
while(currentTime.tv_usec < left) // then count until $left
gettimeofday(¤tTime, NULL);
} else {
finalTime.tv_usec = currentTime.tv_usec + ms*1000L;
while(currentTime.tv_usec < finalTime.tv_usec)
gettimeofday(¤tTime, NULL);
}
}
/** Wait function in microseconds
* @param us the microseconds to wait
*/
void Time::wait_us(long int us) {
struct timeval currentTime, finalTime;
gettimeofday(¤tTime, NULL);
if(currentTime.tv_usec + us > 1e6) { // if we overflowed
time_t inRange = 1e6 - currentTime.tv_usec; // we counted $inRange µs
time_t left = us - inRange; // so there is $left left from 0
while(currentTime.tv_usec > left) // wait to overflow
gettimeofday(¤tTime, NULL);
while(currentTime.tv_usec < left) // then count until $left
gettimeofday(¤tTime, NULL);
} else {
finalTime.tv_usec = currentTime.tv_usec + us;
while (currentTime.tv_usec < finalTime.tv_usec)
gettimeofday(¤tTime, NULL);
}
}
Time::~Time() {
}