home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / dirs / timer_429.lzh / Timer / timer.c < prev    next >
C/C++ Source or Header  |  1991-01-10  |  3KB  |  145 lines

  1. /**************************************
  2. *  TIMER.C  08/04/90
  3. *  Written by Timm Martin
  4. *  This source code is public domain.
  5. ***************************************/
  6.  
  7. #include <devices/timer.h>
  8. #include <exec/ports.h>
  9. #include <exec/types.h>
  10. #include <functions.h>
  11. #include "timer.h"
  12.  
  13. /********************
  14. *  SHARED VARIABLES
  15. *********************/
  16.  
  17. long                timer_error = 1;
  18. struct timerequest  timer_req;
  19. struct MsgPort *    timer_port = NULL;  /* GLOBAL */
  20.  
  21. /***************
  22. *  TIMER ABORT
  23. ****************/
  24.  
  25. /*
  26. This function cancels an existing time request.
  27. */
  28.  
  29. void timer_abort( void )
  30. {
  31.   AbortIO( &timer_req.tr_node );
  32.   Wait( 1L<<timer_port->mp_SigBit );
  33.   GetMsg( timer_port );
  34. }
  35.  
  36. /***************
  37. *  TIMER CLOSE
  38. ****************/
  39.  
  40. /*
  41. This function closes the timer device and deletes the timer port.
  42. */
  43.  
  44. void timer_close( void )
  45. {
  46.   if (!timer_error)
  47.   {
  48.     CloseDevice( (struct IORequest *)&timer_req );
  49.     timer_error = NULL;
  50.   }
  51.   if (timer_port)
  52.   {
  53.     DeletePort( timer_port );
  54.     timer_port = NULL;
  55.   }
  56. }
  57.  
  58. /**************
  59. *  TIMER OPEN
  60. ***************/
  61.  
  62. /*
  63. This function opens the timer device and initializes it.
  64. */
  65.  
  66. BOOL timer_open( void )
  67. {
  68.   if (!(timer_port = CreatePort( NULL, 0L )) ||
  69.       (timer_error = OpenDevice( TIMERNAME, UNIT_VBLANK,
  70.           (struct IORequest *)&timer_req, NULL )))
  71.     return (0);
  72.  
  73.   timer_req.tr_node.io_Message.mn_ReplyPort = timer_port;
  74.   timer_req.tr_node.io_Command = TR_ADDREQUEST;
  75.   timer_req.tr_node.io_Flags = 0;
  76.   return (1);
  77. }
  78.  
  79. /***************
  80. *  TIMER START
  81. ****************/
  82.  
  83. /*
  84. This function issues a request to the timer device to notify the program
  85. in the specified number of microseconds.  This function does not wait for
  86. a reply from the timer device.
  87. */
  88.  
  89. void timer_start( long micros )
  90. {
  91.   long secs;
  92.  
  93.   /* a bug in Kickstart v1.3 requires this check */
  94.   if (micros < 2) return;
  95.  
  96.   secs = micros / MICROS_PER_SEC;
  97.   micros %= MICROS_PER_SEC;
  98.  
  99.   timer_req.tr_time.tv_secs  = secs;
  100.   timer_req.tr_time.tv_micro = micros;
  101.   SendIO( &timer_req.tr_node );
  102. }
  103.  
  104. /**************
  105. *  TIMER TEST
  106. ***************/
  107.  
  108. /*
  109. This function returns whether the last timer request has been satisfied.
  110. */
  111.  
  112. BOOL timer_test( void )
  113. {
  114.   return (GetMsg( timer_port ) != NULL);
  115. }
  116.  
  117. /**************
  118. *  TIMER WAIT
  119. ***************/
  120.  
  121. /*
  122. This function waits for the specified number of microseconds.
  123. */
  124.  
  125. #define MICROS_PER_SEC 1000000L
  126.  
  127. void timer_wait( long micros )
  128. {
  129.   long secs;
  130.  
  131.   /* a bug in Kickstart v1.3 requires this check */
  132.   if (micros < 2) return;
  133.  
  134.   secs = micros / MICROS_PER_SEC;
  135.   micros %= MICROS_PER_SEC;
  136.  
  137.   timer_req.tr_time.tv_secs  = secs;
  138.   timer_req.tr_time.tv_micro = micros;
  139.   SendIO( &timer_req.tr_node );
  140.  
  141.   /* wait until time is up */
  142.   Wait( 1L<<timer_port->mp_SigBit );
  143.   GetMsg( timer_port );
  144. }
  145.