home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 378.lha / MemEater_v1.0 / MRTimer.c < prev    next >
C/C++ Source or Header  |  1990-05-02  |  6KB  |  208 lines

  1. /* Timer device support routines.
  2.  * Filename:    Timer.c
  3.  * Author:      Mark R. Rinfret
  4.  * Date:        11/29/87
  5.  *
  6.  */
  7.  
  8. #ifndef EXEC_MEMORY_H
  9. #include <exec/memory.h>
  10. #endif
  11.  
  12. #include "MRTimer.h"
  13.  
  14. /*  FUNCTION
  15.         CreateTimer - Allocate and initialize a timer request.
  16.  
  17.     SYNOPSIS
  18.         #include <Timer.h>
  19.         struct timerequest *CreateTimer(vBlank)
  20.                             BOOL vBlank;
  21.  
  22.     DESCRIPTION
  23.         CreateTimer allocates and initializes an asynchronous timer 
  24.         request structure for later use by StartTimer and StopTimer.
  25.         If the <vBlank> argument is 1, the vertical blanking interval
  26.         timer is used.  Otherwise, the microhertz timer is used.
  27.         CreateTimer returns a pointer to the timer request structure
  28.         if successful, NULL otherwise.
  29.  
  30.     SEE ALSO
  31.         StartTimer, StopTimer, DeleteTimer
  32.  
  33.  */
  34.  
  35. struct timerequest *
  36. CreateTimer(vBlank)
  37.     BOOL vBlank;
  38. {
  39.     int status = 0;
  40.     struct MsgPort  *timerPort = NULL;
  41.     struct timerequest *timeRequest = NULL;
  42.     ULONG timerType;
  43.  
  44.     timerType = (vBlank ? UNIT_VBLANK : UNIT_MICROHZ);
  45.  
  46.     if ( timerPort = CreatePort(0L, 0L) ) {
  47.         if ( ! (timeRequest = (struct timerequest *)
  48.             AllocMem((long) sizeof(struct timerequest),
  49.                      MEMF_CLEAR | MEMF_PUBLIC) ) ) {
  50. killport:
  51.             DeletePort(timerPort);
  52.         }
  53.         else {
  54.             timeRequest->tr_node.io_Message.mn_Node.ln_Type = NT_MESSAGE;
  55.             timeRequest->tr_node.io_Message.mn_Node.ln_Pri = 0;
  56.             timeRequest->tr_node.io_Message.mn_ReplyPort = timerPort;
  57.             if ( OpenDevice(TIMERNAME, timerType, 
  58.                             (struct IORequest *) timeRequest, 1L) ) {
  59.                 DeleteTimer(timeRequest);
  60.                 timeRequest = NULL;
  61.                 goto killport;
  62.             }
  63.         }
  64.     }
  65.     return timeRequest;
  66. }
  67.  
  68. /*  FUNCTION
  69.         DeleteTimer - delete a timer request created by CreateTimer.
  70.  
  71.     SYNOPSIS
  72.         void DeleteTimer(timeRequest)
  73.              struct timerequest *timeRequest;
  74.  
  75.     DESCRIPTION
  76.         DeleteTimer is called with a pointer to a timer request
  77.         structure created by CreateTimer.  It aborts any active
  78.         timer started by StartTimer and frees all resources required 
  79.         by the timer.
  80.  */
  81.  
  82. void
  83. DeleteTimer(timeRequest)
  84.     struct timerequest *timeRequest;
  85. {
  86.     struct MsgPort *msgPort;
  87.  
  88.     msgPort = timeRequest->tr_node.io_Message.mn_ReplyPort;
  89.  
  90.     if (timeRequest->tr_node.io_Device) {
  91.         AbortIO((struct IORequest *) timeRequest);
  92.         GetMsg(msgPort);
  93.         CloseDevice((struct IORequest *) timeRequest);
  94.     }
  95.     FreeMem(timeRequest, sizeof(timeRequest));
  96.     DeletePort(msgPort);
  97. }
  98.  
  99. /*  FUNCTION
  100.         StartTimer - start an asynchronous timer request.
  101.  
  102.     SYNOPSIS
  103.         void StartTimer(timeRequest, seconds, microseconds)
  104.              struct timerequest *timeRequest;
  105.              ULONG seconds, microseconds;
  106.  
  107.     DESCRIPTION
  108.         Start an asynchronous timer request.  The user application can
  109.         detect the expiration of the timer with 
  110.             Wait(timeRequest->ReplyPort->mp_SigBit) or
  111.             WaitIO(timeRequest).
  112.  
  113.         A typical sequence might look like this:
  114.  
  115.             Start an asynchronous activity (serial read?);
  116.             StartTimer(myTimer);
  117.             bits = Wait(timer bit or other signal);
  118.             if (bits & myTimerBit)
  119.                 WaitIO(&myTimer->tr_node);
  120.  */
  121.  
  122. void
  123. StartTimer(timeRequest, seconds, microSeconds)
  124.     struct timerequest *timeRequest; ULONG seconds, microSeconds;
  125. {
  126.     timeRequest->tr_time.tv_secs = seconds;
  127.     timeRequest->tr_time.tv_micro = microSeconds;
  128.     timeRequest->tr_node.io_Command = TR_ADDREQUEST;
  129.     timeRequest->tr_node.io_Flags = 0;
  130.     timeRequest->tr_node.io_Error = 0;
  131.     SendIO((struct IORequest *) timeRequest);   /* Start the timer. */
  132. }
  133.  
  134. /*  FUNCTION
  135.         StopTimer - stop an asynchronous timer request.
  136.  
  137.     SYNOPSIS
  138.         void StopTimer(timeRequest)
  139.              struct timerequest *timeRequest;
  140.  
  141.     DESCRIPTION
  142.         StopTimer aborts a time request started by StartTimer.
  143.  
  144.  */
  145.  
  146. void
  147. StopTimer(timeRequest)
  148.     struct timerequest *timeRequest;
  149. {
  150.     AbortIO((struct IORequest *) timeRequest);
  151.     WaitIO((struct IORequest *) timeRequest);
  152. }
  153.  
  154. #ifdef DEBUG
  155. #define SHORTBIT (1L<<shortTimer->tr_node.io_Message.mn_ReplyPort->mp_SigBit)
  156. #define LONGBIT  (1L<<longTimer->tr_node.io_Message.mn_ReplyPort->mp_SigBit)
  157. main()
  158. {
  159.     unsigned intervals;
  160.     struct timerequest *shortTimer, *longTimer;
  161.     ULONG signals;
  162.  
  163.     /* This simple program example sets up two timers of different
  164.      * intervals and reports their expirations.
  165.      */
  166.  
  167.     puts("This example defines two timers.  The first has an interval of");
  168.     puts("five seconds, while the second has an interval of 10 seconds.");
  169.     puts("The expiration of each timer is reported to the screen until");
  170.     puts("10 intervals have been detected.\n");
  171.  
  172.     if (! (shortTimer = CreateTimer(0) ) ) {
  173.         puts("Failed to create short interval timer!");
  174.         exit();
  175.     }
  176.  
  177.     if (! (longTimer = CreateTimer(0) ) ) {
  178.         puts("Failed to create long interval timer!");
  179.         DeleteTimer(shortTimer);
  180.         exit();
  181.     }
  182.  
  183.     StartTimer(shortTimer, 5L, 0L);
  184.     StartTimer(longTimer, 10L, 0L);
  185.     for (intervals = 0; intervals < 10; ) {
  186.         printf("Begin wait %2d ... ", intervals);
  187.         signals = Wait(SHORTBIT | LONGBIT);
  188.         puts("end wait.");
  189.         if (signals & SHORTBIT) {
  190.             GetMsg(shortTimer->tr_node.io_Message.mn_ReplyPort);
  191.             ++intervals;
  192.             puts("  Short interval timer expired.");
  193.             StartTimer(shortTimer, 5L, 0L);
  194.         }
  195.         if (signals & LONGBIT) {
  196.             GetMsg(longTimer->tr_node.io_Message.mn_ReplyPort);
  197.             ++intervals;
  198.             puts("   Long interval timer expired.");
  199.             StartTimer(longTimer, 10L, 0L);
  200.         }
  201.     }
  202.     DeleteTimer(shortTimer);
  203.     DeleteTimer(longTimer);
  204.     puts("\nEnd of timer demo.\n");
  205. }
  206. #endif
  207.  
  208.