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>
|
|
6
|
2273
|
7 int usec_sleep(int usec_delay)
|
|
8 {
|
|
9 #if 1
|
|
10 struct timespec ts;
|
|
11 ts.tv_sec = usec_delay / 1000000;
|
|
12 ts.tv_nsec = (usec_delay % 1000000) * 1000;
|
|
13 return nanosleep(&ts, NULL);
|
|
14 #else
|
|
15 return usleep(usec_delay);
|
|
16 #endif
|
|
17 }
|
|
18
|
|
19
|
99
|
20 // Returns current time in microseconds
|
1
|
21 unsigned int GetTimer(){
|
|
22 struct timeval tv;
|
|
23 struct timezone tz;
|
|
24 // float s;
|
|
25 gettimeofday(&tv,&tz);
|
|
26 // s=tv.tv_usec;s*=0.000001;s+=tv.tv_sec;
|
|
27 return (tv.tv_sec*1000000+tv.tv_usec);
|
|
28 }
|
|
29
|
|
30 static unsigned int RelativeTime=0;
|
|
31
|
|
32 // Returns time spent between now and last call in seconds
|
|
33 float GetRelativeTime(){
|
|
34 unsigned int t,r;
|
|
35 t=GetTimer();
|
|
36 // t*=16;printf("time=%ud\n",t);
|
|
37 r=t-RelativeTime;
|
|
38 RelativeTime=t;
|
|
39 return (float)r * 0.000001F;
|
|
40 }
|
|
41
|
|
42 // Initialize timer, must be called at least once at start
|
|
43 void InitTimer(){
|
|
44 GetRelativeTime();
|
|
45 }
|
|
46
|
|
47
|
|
48 #if 0
|
|
49 void main(){
|
|
50 float t=0;
|
|
51 InitTimer();
|
|
52 while(1){ t+=GetRelativeTime();printf("time= %10.6f\r",t);fflush(stdout); }
|
|
53 }
|
|
54 #endif
|
|
55
|