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