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
|
|
31 static unsigned int RelativeTime=0;
|
|
32
|
|
33 // Returns time spent between now and last call in seconds
|
|
34 float GetRelativeTime(){
|
|
35 unsigned int t,r;
|
|
36 t=GetTimer();
|
|
37 // t*=16;printf("time=%ud\n",t);
|
|
38 r=t-RelativeTime;
|
|
39 RelativeTime=t;
|
|
40 return (float)r * 0.000001F;
|
|
41 }
|
|
42
|
|
43 // Initialize timer, must be called at least once at start
|
|
44 void InitTimer(){
|
|
45 GetRelativeTime();
|
|
46 }
|
|
47
|
|
48
|
|
49 #if 0
|
|
50 void main(){
|
|
51 float t=0;
|
|
52 InitTimer();
|
|
53 while(1){ t+=GetRelativeTime();printf("time= %10.6f\r",t);fflush(stdout); }
|
|
54 }
|
|
55 #endif
|
|
56
|