home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 1.2 / amidev_cd_12.iso / reference_library / devices / dev_examples / multiple_timers.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-20  |  4.1 KB  |  125 lines

  1. /*  Multiple_Timers.c
  2.  *
  3.  *  This program is designed to do multiple (3) time requests using one
  4.  *  OpenDevice.  It creates a message port - TimerMP, creates an
  5.  *  extended I/O structure of type timerequest named TimerIO[0] and
  6.  *  then uses that to open the device.  The other two time request
  7.  *  structures - TimerIO[1] and TimerIO[2] - are created using AllocMem
  8.  *  and then copying TimerIO[0] into them.  The tv_secs field of each
  9.  *  structure is set and then three SendIOs are done with the requests.
  10.  *  The program then goes into a while loop until all messages are received.
  11.  *
  12.  * Compile with SAS C 5.10  lc -b1 -cfistq -v -y -L
  13.  *
  14.  * Run from CLI only
  15.  */
  16.  
  17. #include <exec/types.h>
  18. #include <exec/memory.h>
  19. #include <devices/timer.h>
  20.  
  21. #include <clib/exec_protos.h>
  22. #include <clib/alib_protos.h>
  23.  
  24. #include <stdio.h>
  25.  
  26. #ifdef LATTICE
  27. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  28. int chkabort(void) { return(0); }  /* really */
  29. #endif
  30.  
  31. VOID main(VOID);
  32.  
  33. void main(void)
  34. {
  35. struct timerequest *TimerIO[3];
  36. struct MsgPort *TimerMP;
  37. struct Message *TimerMSG;
  38.  
  39. ULONG error,x,seconds[3]={4,1,2}, microseconds[3]={0,0,0};
  40.  
  41. int allin = 3;
  42. char *position[]={"last","second","first"};
  43.  
  44. if (TimerMP = CreatePort(0,0))
  45.     {
  46.     if (TimerIO[0] = (struct timerequest *)
  47.                       CreateExtIO(TimerMP,sizeof(struct timerequest)) )
  48.         {
  49.             /* Open the device once */
  50.         if (!(error=OpenDevice( TIMERNAME, UNIT_VBLANK,(struct IORequest *) TimerIO[0], 0L)))
  51.             {
  52.             /* Set command to TR_ADDREQUEST */
  53.             TimerIO[0]->tr_node.io_Command = TR_ADDREQUEST;
  54.  
  55.             if (TimerIO[1]=(struct timerequest *)
  56.                     AllocMem(sizeof(struct timerequest),MEMF_PUBLIC | MEMF_CLEAR))
  57.                 {
  58.                 if (TimerIO[2]=(struct timerequest *)
  59.                        AllocMem(sizeof(struct timerequest),MEMF_PUBLIC | MEMF_CLEAR))
  60.                     {
  61.                     /* Copy fields from the request used to open the timer device */
  62.                     *TimerIO[1] = *TimerIO[0];
  63.                     *TimerIO[2] = *TimerIO[0];
  64.  
  65.                     /* Initialize other fields */
  66.                     for (x=0;x<3;x++)
  67.                         {
  68.                         TimerIO[x]->tr_time.tv_secs   = seconds[x];
  69.                         TimerIO[x]->tr_time.tv_micro  = microseconds[x];
  70.                         printf("\nInitializing TimerIO[%d]",x);
  71.                         }
  72.  
  73.                     printf("\n\nSending multiple requests\n\n");
  74.  
  75.                     /* Send multiple requests asynchronously */
  76.                     /* Do not got to sleep yet...            */
  77.                     SendIO((struct IORequest *)TimerIO[0]);
  78.                     SendIO((struct IORequest *)TimerIO[1]);
  79.                     SendIO((struct IORequest *)TimerIO[2]);
  80.  
  81.                     /* There might be other processing done here */
  82.  
  83.                     /* Now go to sleep with WaitPort() waiting for the requests */
  84.                     while (allin)
  85.                           {
  86.                           WaitPort(TimerMP);
  87.                           /* Get the reply message */
  88.                           TimerMSG=GetMsg(TimerMP);
  89.                           for (x=0;x<3;x++)
  90.                               if (TimerMSG==(struct Message *)TimerIO[x])
  91.                                   printf("Request %ld finished %s\n",x,position[--allin]);
  92.                           }
  93.  
  94.                     FreeMem(TimerIO[2],sizeof(struct timerequest));
  95.                     }
  96.  
  97.                 else
  98.                     printf("Error: could not allocate TimerIO[2] memory\n");
  99.  
  100.                 FreeMem(TimerIO[1],sizeof(struct timerequest));
  101.                 }
  102.  
  103.             else
  104.                 printf("Error could not allocate TimerIO[1] memory\n");
  105.  
  106.             CloseDevice((struct IORequest *) TimerIO[0]);
  107.             }
  108.  
  109.         else
  110.             printf("\nError: Could not OpenDevice\n");
  111.  
  112.         DeleteExtIO((struct IORequest *) TimerIO[0]);
  113.         }
  114.  
  115.     else
  116.         printf("Error: could not create IORequest\n");
  117.  
  118.     DeletePort(TimerMP);
  119.     }
  120.  
  121. else
  122.     printf("\nError: Could not CreatePort\n");
  123. }
  124.  
  125.