1
|
1 // Precise timer routines for LINUX (C) LGB & A'rpi/ASTRAL
|
|
2
|
|
3 #include <unistd.h>
|
2476
|
4 #include <time.h>
|
1
|
5 #include <sys/time.h>
|
3090
|
6 #include "../config.h"
|
1
|
7
|
2273
|
8 int usec_sleep(int usec_delay)
|
|
9 {
|
3090
|
10 #ifdef HAVE_NANOSLEEP
|
2273
|
11 struct timespec ts;
|
|
12 ts.tv_sec = usec_delay / 1000000;
|
|
13 ts.tv_nsec = (usec_delay % 1000000) * 1000;
|
|
14 return nanosleep(&ts, NULL);
|
|
15 #else
|
|
16 return usleep(usec_delay);
|
|
17 #endif
|
|
18 }
|
|
19
|
|
20
|
99
|
21 // Returns current time in microseconds
|
1
|
22 unsigned int GetTimer(){
|
|
23 struct timeval tv;
|
|
24 struct timezone tz;
|
|
25 // float s;
|
|
26 gettimeofday(&tv,&tz);
|
|
27 // s=tv.tv_usec;s*=0.000001;s+=tv.tv_sec;
|
|
28 return (tv.tv_sec*1000000+tv.tv_usec);
|
|
29 }
|
|
30
|
4385
|
31 // Returns current time in milliseconds
|
|
32 unsigned int GetTimerMS(){
|
|
33 struct timeval tv;
|
|
34 struct timezone tz;
|
|
35 // float s;
|
|
36 gettimeofday(&tv,&tz);
|
|
37 // s=tv.tv_usec;s*=0.000001;s+=tv.tv_sec;
|
|
38 return (tv.tv_sec*1000+tv.tv_usec/1000);
|
|
39 }
|
|
40
|
1
|
41 static unsigned int RelativeTime=0;
|
|
42
|
|
43 // Returns time spent between now and last call in seconds
|
|
44 float GetRelativeTime(){
|
|
45 unsigned int t,r;
|
|
46 t=GetTimer();
|
|
47 // t*=16;printf("time=%ud\n",t);
|
|
48 r=t-RelativeTime;
|
|
49 RelativeTime=t;
|
|
50 return (float)r * 0.000001F;
|
|
51 }
|
|
52
|
|
53 // Initialize timer, must be called at least once at start
|
|
54 void InitTimer(){
|
|
55 GetRelativeTime();
|
|
56 }
|
|
57
|
|
58
|
|
59 #if 0
|
|
60 void main(){
|
|
61 float t=0;
|
|
62 InitTimer();
|
|
63 while(1){ t+=GetRelativeTime();printf("time= %10.6f\r",t);fflush(stdout); }
|
|
64 }
|
|
65 #endif
|
|
66
|