home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 377b.lha / devices / timer / timemath.c < prev    next >
Encoding:
C/C++ Source or Header  |  1980-02-03  |  4.6 KB  |  104 lines

  1. /* Copyright (c) 1990 Commodore-Amiga, Inc.
  2.  *
  3.  * This example is provided in electronic form by Commodore-Amiga, Inc. for
  4.  * use with the 1.3 revisions of the Addison-Wesley Amiga reference manuals. 
  5.  * The 1.3 Addison-Wesley Amiga Reference Manual series contains additional
  6.  * information on the correct usage of the techniques and operating system
  7.  * functions presented in this example.  The source and executable code of
  8.  * this example may only be distributed in free electronic form, via bulletin
  9.  * board or as part of a fully non-commercial and freely redistributable
  10.  * diskette.  Both the source and executable code (including comments) must
  11.  * be included, without modification, in any copy.  This example may not be
  12.  * published in printed form or distributed with any commercial product.
  13.  * However, the programming techniques and support routines set forth in
  14.  * this example may be used in the development of original executable
  15.  * software products for Commodore Amiga computers.
  16.  * All other rights reserved.
  17.  * This example is provided "as-is" and is subject to change; no warranties
  18.  * are made.  All use is at your own risk.  No liability or responsibility
  19.  * is assumed.
  20.  */
  21.  
  22.  
  23. /* Lattice use -b1 - cfist -v -y */
  24. /* Link with lc.lib, amiga.lib    */
  25. #include <exec/types.h>
  26. #include <exec/memory.h>
  27. #include <devices/timer.h>
  28. #include <proto/all.h>
  29. #include <stdio.h>
  30.  
  31. #ifdef LATTICE
  32. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  33. int chkabort(void) { return(0); }  /* really */
  34. #endif
  35.  
  36. struct Library *TimerBase;  /* setup the interface variable (must be global) */
  37.  
  38. void main(int argc,char **argv)
  39. {
  40. struct timeval     *time1, *time2, *time3;
  41. struct timerequest *tr;
  42. LONG               error,result;
  43. /*------------------------------------*/
  44. /* Get some memory for our structures */
  45. /*------------------------------------*/
  46. time1=(struct timeval *)AllocMem(sizeof(struct timeval),
  47.                                MEMF_PUBLIC | MEMF_CLEAR);
  48. time2=(struct timeval *)AllocMem(sizeof(struct timeval),
  49.                                MEMF_PUBLIC | MEMF_CLEAR);
  50. time3=(struct timeval *)AllocMem(sizeof(struct timeval),
  51.                                MEMF_PUBLIC | MEMF_CLEAR);
  52. tr=(struct timerequest *)AllocMem(sizeof(struct timerequest),
  53.                                MEMF_PUBLIC | MEMF_CLEAR);
  54. /* Make sure we got the memory */
  55. if(!time1 || !time2 || !time3 || !tr) goto cleanexit;
  56. /*----------------------------------------------------*/
  57. /* Set up some values to test time arithmetic with    */
  58. /* In a real application these values might be filled */
  59. /* in via the GET_SYSTIME command of the timer device */
  60. /*----------------------------------------------------*/
  61. time1->tv_secs = 3;   time1->tv_micro = 0;           /* 3.0 seconds */
  62. time2->tv_secs = 2;   time2->tv_micro = 500000;      /* 2.5 seconds */
  63. time3->tv_secs = 1;   time3->tv_micro = 900000;      /* 1.9 seconds */
  64.  
  65. printf("Time1 is %ld.%ld\n" , time1->tv_secs,time1->tv_micro);
  66. printf("Time2 is %ld.%ld\n" , time2->tv_secs,time2->tv_micro);
  67. printf("Time3 is %ld.%ld\n\n",time3->tv_secs,time3->tv_micro);
  68. /*-------------------------------*/
  69. /* Open the MICROHZ timer device */
  70. /*-------------------------------*/
  71. error = OpenDevice(TIMERNAME,UNIT_MICROHZ,(struct IORequest *) tr, 0L);
  72. if(error) goto cleanexit;
  73.  
  74. /* Set up to use the special time arithmetic functions */
  75. TimerBase = (struct Library *)tr->tr_node.io_Device;
  76. /*---------------------------------------------------------*/
  77. /* Now that TimerBase is initialized, it is permissible    */
  78. /* to call the time-comparison or time-arithmetic routines */
  79. /* Result of this example is -1 which means the first      */
  80. /* parameter has greater time value than second parameter  */
  81. /* +1 means the second parameter is bigger; 0 means equal. */
  82. /*---------------------------------------------------------*/
  83. result = CmpTime( time1, time2 );
  84. printf("Time1 and 2 compare = %ld\n",result);
  85.  
  86. /* Add to time1 the values in time2 */
  87. AddTime( time1, time2);
  88. printf("Time1+time2 result = %ld.%ld\n",time1->tv_secs,time1->tv_micro);
  89.  
  90. /* Subtract values in time3 from the value */
  91. /* currently in time1.   Results in time1. */
  92. SubTime( time2, time3);
  93. printf("Time2-time3 result = %ld.%ld\n",time2->tv_secs,time2->tv_micro);
  94. /*------------------------------------*/
  95. /* Free system resources that we used */
  96. /*------------------------------------*/
  97. cleanexit:
  98.   if (!error)CloseDevice((struct IORequest *) tr);
  99.   if (tr)    FreeMem(tr,   sizeof(struct timerequest));
  100.   if (time3) FreeMem(time3,sizeof(struct timeval));
  101.   if (time2) FreeMem(time2,sizeof(struct timeval));
  102.   if (time1) FreeMem(time1,sizeof(struct timeval));
  103. }
  104.