-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.c
46 lines (36 loc) · 838 Bytes
/
timer.c
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
/**
* timer.c
* Created on Aug, 23th 2023
* Author: Tiago Barros
* Based on "From C to C++ course - 2002"
*/
#include "timer.h"
#include <stdio.h>
#include <sys/time.h>
static struct timeval timer, now;
static int delay = -1;
void timerInit(int valueMilliSec) {
delay = valueMilliSec;
gettimeofday(&timer, NULL);
}
void timerDestroy() { delay = -1; }
void timerUpdateTimer(int valueMilliSec) {
delay = valueMilliSec;
gettimeofday(&timer, NULL);
}
int getTimeDiff() {
gettimeofday(&now, NULL);
long diff =
(((now.tv_sec - timer.tv_sec) * 1000000) + now.tv_usec - timer.tv_usec) /
1000;
return (int)diff;
}
int timerTimeOver() {
int ret = 0;
if (getTimeDiff() > delay) {
ret = 1;
gettimeofday(&timer, NULL);
}
return ret;
}
void timerPrint() { printf("Timer: %d", getTimeDiff()); }