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