Main Page | Data Structures | File List | Data Fields | Globals

timer.c

Go to the documentation of this file.
00001 /* timer.c --- 
00002  * 
00003  * Filename: timer.c
00004  * Description: 
00005  * Author: Bryce Himebaugh
00006  * Maintainer: 
00007  * Created: Tue Oct  3 08:32:40 2006
00008  * Version: 
00009  * Last-Updated: 
00010  *           By: 
00011  *     Update #: 0
00012  * Keywords: 
00013  * Compatibility: 
00014  * 
00015  */
00016 
00017 /* Commentary: 
00018  * 
00019  * 
00020  * 
00021  */
00022 
00023 /* Change log:
00024  * 
00025  * 
00026  */
00027 
00028 /* This program is free software; you can redistribute it and/or modify
00029  * it under the terms of the GNU General Public License as published by
00030  * the Free Software Foundation; either version 2, or (at your option)
00031  * any later version.
00032  * 
00033  * This program is distributed in the hope that it will be useful,
00034  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00035  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00036  * GNU General Public License for more details.
00037  * 
00038  * You should have received a copy of the GNU General Public License
00039  * along with this program; see the file COPYING.  If not, write to the
00040  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00041  * Boston, MA 02111-1307, USA.
00042  */
00043 
00044 /* Code: */
00045 #include <stdio.h>
00046 #include <sys/types.h> 
00047 #include <sys/socket.h>
00048 #include <netinet/in.h>
00049 #include <strings.h>
00050 #include <string.h>
00051 #include <arpa/inet.h>
00052 #include <stdlib.h>
00053 #include <unistd.h>
00054 #include <signal.h>
00055 #include <errno.h>
00056 #include <pthread.h>
00057 #include <time.h>
00058 #include <sys/time.h>
00059 #include "timer.h"
00060 
00061 #define JITTER_MAX 1000
00062 
00063 long jitter(long period);
00064 
00065 int delay_uS(int uS) {
00066   struct timeval to;
00067   long time_error;
00068   to.tv_sec = 0;
00069   to.tv_usec = uS;    
00070   select(0,NULL,NULL,NULL,&to);
00071   time_error=jitter((long)uS);
00072   if ((time_error>JITTER_MAX)||(time_error<-JITTER_MAX)) {
00073     printf("%ld\n",time_error);
00074   }
00075   return (0);
00076 }
00077 
00078 void *periodic_thread(void *arg) {
00079   int status;
00080   timer_struct_t *tp = (timer_struct_t *) arg;
00081 
00082   while (1) {
00083     delay_uS(tp->timer_period);
00084     tp->timeout=1;
00085   }
00086   status = pthread_detach(pthread_self());
00087   return NULL;
00088 }
00089 
00090 long jitter(long period) {
00091   static struct timeval tv_last;
00092   static int init=1;
00093   struct timeval tv;
00094   long delta;
00095   long error;
00096   gettimeofday(&tv,NULL);
00097   if (!init) {
00098     if (tv.tv_usec<tv_last.tv_usec) {
00099       delta=(1000000-tv_last.tv_usec)+tv.tv_usec;
00100     }
00101     else {
00102       delta=tv.tv_usec-tv_last.tv_usec;
00103     }
00104     tv_last.tv_usec=tv.tv_usec;
00105     error=delta-period;
00106   }
00107   else {
00108     tv_last.tv_usec=tv.tv_usec;
00109     error=0;
00110     init=0;
00111   }
00112   return(error);
00113 }
00114 
00115 /* timer.c ends here */

Generated on Fri Nov 3 15:24:51 2006 for ERT1 Communication Server by  doxygen 1.3.9.1