-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.h
49 lines (40 loc) · 922 Bytes
/
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
#ifndef TIMER_T
#define TIMER_T
#include <time.h>
#include <stdbool.h>
struct timer_t
{
double start_time, set_time;
};
void set_timer(struct timer_t *tmr, unsigned long miliseconds)
{
tmr->set_time = miliseconds/1000.0;
}
double timespec2sec(struct timespec tp)
{
return (double)tp.tv_sec + tp.tv_nsec/(1000.0*1000.0*1000.0);
}
void start_timer(struct timer_t *tmr)
{
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
tmr->start_time = timespec2sec(now);
}
/*
void diff_timespec(struct timespec *l, struct timespec *r, struct timespec *out)
{
out->tv_sec = (l->tv_sec - r->tv_sec);
out->tv_nsec = (l->tv_nsec - r->tv_nsec);
}
*/
bool timer_elapsed(struct timer_t *tmr)
{
struct timespec now; clock_gettime(CLOCK_REALTIME, &now);
double diff = (timespec2sec(now) - tmr->start_time);
if(diff > tmr->set_time)
{
return 1;
}
return 0;
}
#endif