comparison osdep/timer-lx.c @ 9380:edfe34c5405d

linux->osdep
author arpi
date Sun, 09 Feb 2003 20:18:23 +0000
parents linux/timer-lx.c@f297030ef5ab
children f9755d9c479a
comparison
equal deleted inserted replaced
9379:475bb1a6ef75 9380:edfe34c5405d
1 // Precise timer routines for LINUX (C) LGB & A'rpi/ASTRAL
2
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <time.h>
6 #include <sys/time.h>
7 #include "../config.h"
8
9 int usec_sleep(int usec_delay)
10 {
11 #ifdef HAVE_NANOSLEEP
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
22 // Returns current time in microseconds
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
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
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