annotate linux/timer-lx.c @ 4910:e99d47acfce6

strike period over, commiting stuff. next commit will be rewrite. (?) - Gabu
author jonas
date Fri, 01 Mar 2002 22:27:01 +0000
parents 7bb8f7905000
children f297030ef5ab
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
1 // Precise timer routines for LINUX (C) LGB & A'rpi/ASTRAL
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
2
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
3 #include <unistd.h>
2476
a6c5a537f30a a few warning fixes (missing #include's)
pl
parents: 2273
diff changeset
4 #include <time.h>
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
5 #include <sys/time.h>
3090
57abb24ac58b HAVE_NANOSLEEP
alex
parents: 2476
diff changeset
6 #include "../config.h"
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
7
2273
e407fc4562f1 sleep stuff moved to linux/timer
arpi
parents: 99
diff changeset
8 int usec_sleep(int usec_delay)
e407fc4562f1 sleep stuff moved to linux/timer
arpi
parents: 99
diff changeset
9 {
3090
57abb24ac58b HAVE_NANOSLEEP
alex
parents: 2476
diff changeset
10 #ifdef HAVE_NANOSLEEP
2273
e407fc4562f1 sleep stuff moved to linux/timer
arpi
parents: 99
diff changeset
11 struct timespec ts;
e407fc4562f1 sleep stuff moved to linux/timer
arpi
parents: 99
diff changeset
12 ts.tv_sec = usec_delay / 1000000;
e407fc4562f1 sleep stuff moved to linux/timer
arpi
parents: 99
diff changeset
13 ts.tv_nsec = (usec_delay % 1000000) * 1000;
e407fc4562f1 sleep stuff moved to linux/timer
arpi
parents: 99
diff changeset
14 return nanosleep(&ts, NULL);
e407fc4562f1 sleep stuff moved to linux/timer
arpi
parents: 99
diff changeset
15 #else
e407fc4562f1 sleep stuff moved to linux/timer
arpi
parents: 99
diff changeset
16 return usleep(usec_delay);
e407fc4562f1 sleep stuff moved to linux/timer
arpi
parents: 99
diff changeset
17 #endif
e407fc4562f1 sleep stuff moved to linux/timer
arpi
parents: 99
diff changeset
18 }
e407fc4562f1 sleep stuff moved to linux/timer
arpi
parents: 99
diff changeset
19
e407fc4562f1 sleep stuff moved to linux/timer
arpi
parents: 99
diff changeset
20
99
fb1fc94eaff0 removed redundancy...
arpi_esp
parents: 1
diff changeset
21 // Returns current time in microseconds
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
22 unsigned int GetTimer(){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
23 struct timeval tv;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
24 struct timezone tz;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
25 // float s;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
26 gettimeofday(&tv,&tz);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
27 // s=tv.tv_usec;s*=0.000001;s+=tv.tv_sec;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
28 return (tv.tv_sec*1000000+tv.tv_usec);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
29 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
30
4385
7bb8f7905000 GetTimerMS added - get timer in millisec
arpi
parents: 3090
diff changeset
31 // Returns current time in milliseconds
7bb8f7905000 GetTimerMS added - get timer in millisec
arpi
parents: 3090
diff changeset
32 unsigned int GetTimerMS(){
7bb8f7905000 GetTimerMS added - get timer in millisec
arpi
parents: 3090
diff changeset
33 struct timeval tv;
7bb8f7905000 GetTimerMS added - get timer in millisec
arpi
parents: 3090
diff changeset
34 struct timezone tz;
7bb8f7905000 GetTimerMS added - get timer in millisec
arpi
parents: 3090
diff changeset
35 // float s;
7bb8f7905000 GetTimerMS added - get timer in millisec
arpi
parents: 3090
diff changeset
36 gettimeofday(&tv,&tz);
7bb8f7905000 GetTimerMS added - get timer in millisec
arpi
parents: 3090
diff changeset
37 // s=tv.tv_usec;s*=0.000001;s+=tv.tv_sec;
7bb8f7905000 GetTimerMS added - get timer in millisec
arpi
parents: 3090
diff changeset
38 return (tv.tv_sec*1000+tv.tv_usec/1000);
7bb8f7905000 GetTimerMS added - get timer in millisec
arpi
parents: 3090
diff changeset
39 }
7bb8f7905000 GetTimerMS added - get timer in millisec
arpi
parents: 3090
diff changeset
40
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
41 static unsigned int RelativeTime=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
42
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
43 // Returns time spent between now and last call in seconds
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
44 float GetRelativeTime(){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
45 unsigned int t,r;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
46 t=GetTimer();
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
47 // t*=16;printf("time=%ud\n",t);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
48 r=t-RelativeTime;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
49 RelativeTime=t;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
50 return (float)r * 0.000001F;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
51 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
52
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
53 // Initialize timer, must be called at least once at start
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
54 void InitTimer(){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
55 GetRelativeTime();
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
56 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
57
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
58
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
59 #if 0
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
60 void main(){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
61 float t=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
62 InitTimer();
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
63 while(1){ t+=GetRelativeTime();printf("time= %10.6f\r",t);fflush(stdout); }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
64 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
65 #endif
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
66