home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / gnu / aplusplus-1.01-src.lha / GNU / src / amiga / APlusPlus-1.01 / libsource / TimerC.cxx < prev    next >
C/C++ Source or Header  |  1994-04-23  |  3KB  |  133 lines

  1. /******************************************************************************
  2.  **
  3.  **    C++ Class Library for the Amiga© system software.
  4.  **
  5.  **    Copyright (C) 1994 by Armin Vogt  **  EMail: armin@uni-paderborn.de
  6.  **    All Rights Reserved.
  7.  **
  8.  **    $Source: apphome:APlusPlus/RCS/libsource/TimerC.cxx,v $
  9.  **    $Revision: 1.3 $
  10.  **    $Date: 1994/04/23 21:02:52 $
  11.  **    $Author: Armin_Vogt $
  12.  **
  13.  ******************************************************************************/
  14.  
  15.  
  16. extern "C" {
  17. #ifdef __GNUG__
  18. #include <inline/timer.h>
  19. #endif
  20.  
  21. #ifdef __SASC
  22. #include <proto/timer.h>
  23. #endif
  24. }
  25.  
  26. #include <APlusPlus/devices/TimerC.h>
  27. #include <string.h>
  28.  
  29.  
  30. volatile static char rcs_id[] = "$Id: TimerC.cxx,v 1.3 1994/04/23 21:02:52 Armin_Vogt Exp Armin_Vogt $";
  31.  
  32.  
  33. TimerC::TimerC(UWORD unit,struct MsgPort *replyPort)
  34. {
  35.    set(0,0);
  36.    create(unit,replyPort);
  37. }
  38.  
  39. TimerC::~TimerC()
  40.     dispose(); 
  41. }
  42.  
  43. BOOL TimerC::create(UWORD unit,struct MsgPort *replyPort)
  44.    /* create a timer object to a msgport.
  45.    */
  46. {
  47.    memset((APTR)&timerIO,0,sizeof(timerequest));
  48.    timerIO.tr_node.io_Message.mn_ReplyPort = replyPort;
  49.  
  50.       if ( !(OpenDevice((UBYTE*)TIMERNAME,(ULONG)unit,(struct IORequest*)&timerIO,(ULONG)0L)) )
  51.       {
  52.          timerIO.tr_node.io_Command = TR_ADDREQUEST;
  53.          timerIO.tr_time = time;   // copy time values to Timer IORequest.
  54.          sent = FALSE;
  55.          setID(TIMER_CLASS);
  56.  
  57.          return TRUE;
  58.       }
  59.       else _ierror(TIMER_OPENDEVICE_FAILED);
  60.  
  61.    return FALSE;
  62. }
  63.  
  64. void TimerC::dispose()
  65.    /* Dispose of the timerequest, no matter if it is sent or in the replyport's message queue.
  66.    */
  67. {
  68.    if (Ok())
  69.    {
  70.       abort();  // make sure the Timer is no longer in use.
  71.       CloseDevice((struct IORequest*)&timerIO);
  72.       setID(APPOBJECT_INVALID);
  73.    }
  74. }
  75.  
  76. BOOL TimerC::start(ULONG secs,ULONG micros)
  77. {
  78.    if (Ok())
  79.       if (sent==FALSE)
  80.       {
  81.          if (secs || micros)  set(secs,micros);
  82.  
  83.          timerIO.tr_time = time;    // copy time values to Timer IORequest.
  84.          SendIO((struct IORequest*)&timerIO);
  85.             sent = TRUE;
  86.          return TRUE;
  87.       }
  88.  
  89.    return FALSE;
  90. }
  91.  
  92. void TimerC::abort()
  93.    /* Abort a started Timer. If the Timer has not been started yet, nothing will happen.
  94.       After execution the Timer is ready to be started again.
  95.    */
  96. {
  97.    if (Ok())
  98.       if (sent==TRUE)
  99.       {
  100.          AbortIO((struct IORequest*)&timerIO);  // tell timer device to send back request if not done.
  101.          WaitIO((struct IORequest*)&timerIO);   // wait for arrival, then remove from message queue.
  102.          sent = FALSE;
  103.       }
  104. }
  105.  
  106. BOOL TimerC::reuse()
  107.    /* Make a Timer that has returned from the Timer.device useable for subsequent start().
  108.         Usually call reuse() after having received and identified a timer reply message.
  109.       An already sent Timer that has not been replied yet will fail being reused and return FALSE.
  110.       To reuse an already sent Timer without waiting for it any longer use abort().
  111.    */
  112. {
  113.    if (Ok())
  114.       if (sent == FALSE)   // timer is not in use.
  115.          return TRUE;
  116.       else
  117.          if (CheckIO((struct IORequest*)&timerIO))  // request already replied ?
  118.          {
  119.                 // the replied request is assumed be removed from the port queue via getMsg().
  120.             sent = FALSE;
  121.             return TRUE;
  122.          }
  123.  
  124.    return FALSE;
  125. }
  126.  
  127. void TimerC::changeReplyPort(UWORD unit,struct MsgPort *newReplyPort)
  128. {
  129.    dispose();
  130.    create(unit,newReplyPort);
  131. }
  132.