[comp.sys.amiga] Timer Device

glamdrng@pnet51.cts.com (Rocky Lhotka) (07/12/89)

Does anyone have a nice, simple (but working) example of how to use the
timer.device to perform the function of the AmigaDOS Delay() function?
I have entered a couple example programs from books which do not work at all,
as they never return on the Wait() call...  I really would appreciate any
help anyone can give me, as this seems like such a SIMPLE thing to do, but is
apparently not so...  Thanks in advance!!

Rocky Lhotka
Glamdring, Sword of Mithrandir

UUCP: {amdahl!bungia, uunet!rosevax, chinet, killer}!orbit!pnet51!glamdrng
ARPA: crash!orbit!pnet51!glamdrng@nosc.mil
INET: glamdrng@pnet51.cts.com

papa@pollux.usc.edu (Marco Papa) (07/12/89)

In article <764@orbit.UUCP> glamdrng@pnet51.cts.com (Rocky Lhotka) writes:
>Does anyone have a nice, simple (but working) example of how to use the
>timer.device to perform the function of the AmigaDOS Delay() function?
>I have entered a couple example programs from books which do not work at all,
>as they never return on the Wait() call...  I really would appreciate any
>help anyone can give me, as this seems like such a SIMPLE thing to do, but is
>apparently not so...  Thanks in advance!!

The following code is from the XPR distribution. I don't know if Willy wrote
it himself. The nice thing is that unlike Delay() it can be used in libraries
and such.

/** timeout.c
*
*   Roll-yer-own Delay() function.
*
**/
#include <exec/exec.h>
#include <devices/timer.h>
#include <functions.h>

#define TRSIZE ((long) sizeof(struct timerequest))

TimeOut(ticks)
long ticks;
{
   long secs, micros;
   struct timerequest *Timereq = NULL;
   struct MsgPort *Timerport = 0L;

   if (ticks == 0L) return;

   Timerport = CreatePort(0L, 0L);
   if (Timerport == NULL) goto cleanup;

   Timereq = (struct timerequest *) AllocMem(TRSIZE, MEMF_PUBLIC | MEMF_CLEAR);
   if (Timereq == NULL) goto cleanup;

   if (OpenDevice(TIMERNAME, UNIT_VBLANK, Timereq, 0L) != NULL) goto cleanup;
/*
*  Set up timer request.
*/
   secs = ticks / 50L;
   micros = (ticks % 50L) * 20000L;

   Timereq->tr_node.io_Message.mn_ReplyPort = Timerport;
   Timereq->tr_node.io_Command = TR_ADDREQUEST;
   Timereq->tr_node.io_Flags = 0;
   Timereq->tr_node.io_Error = 0;
   Timereq->tr_time.tv_secs = secs;
   Timereq->tr_time.tv_micro = micros;
/*
*   Time out
*/
   SendIO(Timereq);
   Wait(1L << Timerport->mp_SigBit);
/*
*  Handle timer events.
*/
   GetMsg(Timerport);
/*
*   Clean up
*/
cleanup:
   if (Timereq) {
      if (Timereq->tr_node.io_Message.mn_ReplyPort) CloseDevice(Timereq);
      FreeMem(Timereq, TRSIZE);
   }
   if (Timerport) DeletePort(Timerport);

   return;
}
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
uucp:...!pollux!papa       BIX:papa       ARPAnet:pollux!papa@oberon.usc.edu
"There's Alpha, Beta, Gamma, Diga and Caligari!" -- Rick Unland
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=