home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 1.2 / amidev_cd_12.iso / reference_library / devices / dev_examples / timer_arithmetic.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-20  |  3.6 KB  |  104 lines

  1. /*
  2.  * Timer_Arithmetic.c
  3.  *
  4.  * Example of timer device arithmetic functions
  5.  *
  6.  * Compile with SAS C 5.10  lc -b1 -cfistq -v -y -L
  7.  *
  8.  * Run from CLI only
  9.  *
  10.  */
  11.  
  12. #include <exec/types.h>
  13. #include <exec/io.h>
  14. #include <exec/memory.h>
  15. #include <devices/timer.h>
  16.  
  17. #include <clib/exec_protos.h>
  18. #include <clib/alib_protos.h>
  19. #include <clib/timer_protos.h>
  20.  
  21. #include <stdio.h>
  22.  
  23. #ifdef LATTICE
  24. int CXBRK(void) { return(0); }     /* Disable SAS CTRL/C handling */
  25. int chkabort(void) { return(0); }  /* really */
  26. #endif
  27.  
  28. struct Library *TimerBase;  /* setup the interface variable (must be global) */
  29.  
  30. void main(int argc,char **argv)
  31. {
  32. struct timeval     *time1, *time2, *time3;
  33. struct timerequest *tr;
  34. LONG               error,result;
  35.  
  36. /*------------------------------------*/
  37. /* Get some memory for our structures */
  38. /*------------------------------------*/
  39. time1=(struct timeval *)AllocMem(sizeof(struct timeval),
  40.                                MEMF_PUBLIC | MEMF_CLEAR);
  41. time2=(struct timeval *)AllocMem(sizeof(struct timeval),
  42.                                MEMF_PUBLIC | MEMF_CLEAR);
  43. time3=(struct timeval *)AllocMem(sizeof(struct timeval),
  44.                                MEMF_PUBLIC | MEMF_CLEAR);
  45. tr=(struct timerequest *)AllocMem(sizeof(struct timerequest),
  46.                                MEMF_PUBLIC | MEMF_CLEAR);
  47. /* Make sure we got the memory */
  48. if(!time1 | !time2 | !time3 | !tr) goto cleanexit;
  49.  
  50. /*---------------------------------------------------------------------------*/
  51. /* Set up values to test time arithmetic with.  In a real application these  */
  52. /* values might be filled in via the GET_SYSTIME command of the timer device */
  53. /*---------------------------------------------------------------------------*/
  54. time1->tv_secs = 3;   time1->tv_micro = 0;           /* 3.0 seconds */
  55. time2->tv_secs = 2;   time2->tv_micro = 500000;      /* 2.5 seconds */
  56. time3->tv_secs = 1;   time3->tv_micro = 900000;      /* 1.9 seconds */
  57.  
  58. printf("Time1 is %ld.%ld\n" , time1->tv_secs,time1->tv_micro);
  59. printf("Time2 is %ld.%ld\n" , time2->tv_secs,time2->tv_micro);
  60. printf("Time3 is %ld.%ld\n\n",time3->tv_secs,time3->tv_micro);
  61.  
  62. /*-------------------------------*/
  63. /* Open the MICROHZ timer device */
  64. /*-------------------------------*/
  65. error = OpenDevice(TIMERNAME,UNIT_MICROHZ,(struct IORequest *) tr, 0L);
  66. if(error) goto cleanexit;
  67.  
  68. /* Set up to use the special time arithmetic functions */
  69. TimerBase = (struct Library *)tr->tr_node.io_Device;
  70.  
  71. /*--------------------------------------------------------------------------*/
  72. /* Now that TimerBase is initialized, it is permissible to call the         */
  73. /* time-comparison or time-arithmetic routines.  Result of this example     */
  74. /* is -1 which means the first parameter has greater time value than second */
  75. /* parameter; +1 means the second parameter is bigger; 0 means equal.       */
  76. /*--------------------------------------------------------------------------*/
  77. result = CmpTime( time1, time2 );
  78. printf("Time1 and Time2 compare = %ld\n",result);
  79.  
  80. /* Add time2 to time1, result in time1 */
  81. AddTime( time1, time2);
  82. printf("Time1 + Time2 = %ld.%ld\n",time1->tv_secs,time1->tv_micro);
  83.  
  84. /* Subtract time3 from time2, result in time2 */
  85. SubTime( time2, time3);
  86. printf("Time2 - Time3 = %ld.%ld\n",time2->tv_secs,time2->tv_micro);
  87.  
  88. /*------------------------------------*/
  89. /* Free system resources that we used */
  90. /*------------------------------------*/
  91. cleanexit:
  92.   if (time1)
  93.       FreeMem(time1,sizeof(struct timeval));
  94.   if (time2)
  95.       FreeMem(time2,sizeof(struct timeval));
  96.   if (time3)
  97.       FreeMem(time3,sizeof(struct timeval));
  98.   if (!error)
  99.       CloseDevice((struct IORequest *) tr);
  100.   if (tr)
  101.       FreeMem(tr,sizeof(struct timerequest));
  102. }
  103.  
  104.