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