home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / datafiles / text / c_manual / devices / timerdevice / example4.c < prev    next >
C/C++ Source or Header  |  1995-02-27  |  7KB  |  213 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Devices                 Amiga C Club       */
  7. /* Chapter: Timer Device                Tulevagen 22       */
  8. /* File:    Example4.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-04-27                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20.  
  21.  
  22. /* This example demonstrates how you can compare, add and  */
  23. /* subtract timevalues with help of the timer device's own */
  24. /* functions.                                              */
  25.  
  26.  
  27.  
  28. #include <exec/types.h>    /* STRPTR         */
  29. #include <exec/ports.h>    /* struct Message */
  30. #include <exec/memory.h>   /* MEMF_PUBLIC    */
  31. #include <devices/timer.h> /* TIMERNAME */
  32.  
  33.  
  34.  
  35. struct Device *TimerBase;
  36.  
  37.  
  38. /* The reply port: */
  39. struct MsgPort *replymp;
  40.  
  41.  
  42. /* The timer request blocks: */
  43. struct timerequest *tr1;
  44. struct timerequest *tr2;
  45. struct timerequest *tr3;
  46.  
  47. /* When the Timer Device is open this variable is TRUE: */
  48. BOOL not1_opened = TRUE;
  49. BOOL not2_opened = TRUE;
  50. BOOL not3_opened = TRUE;
  51.  
  52.  
  53.  
  54. /* Declare our functions: */
  55. void clean_up();
  56. void main();
  57.  
  58.  
  59.  
  60. void main()
  61. {
  62.   /* Get a reply port: */
  63.   replymp = (struct MsgPort *)
  64.     CreatePort( NULL, 0 );
  65.   if( !replymp )
  66.     clean_up( "Could not create the reply port!" );
  67.  
  68.  
  69.   /* Get three extended request blocks: */
  70.   /* Requestblock 1: */
  71.   tr1 = (struct timerequest *)
  72.     CreateExtIO( replymp, sizeof(struct timerequest) );
  73.   if( !tr1 )
  74.     clean_up( "Could not create requestblock 1!" );
  75.  
  76.  
  77.   /* Requestblock 2: */
  78.   tr2 = (struct timerequest *)
  79.     CreateExtIO( replymp, sizeof(struct timerequest) );
  80.   if( !tr2 )
  81.     clean_up( "Could not create requestblock 2!" );
  82.  
  83.   /* Requestblock 3: */
  84.   tr3 = (struct timerequest *)
  85.     CreateExtIO( replymp, sizeof(struct timerequest) );
  86.   if( !tr3 )
  87.     clean_up( "Could not create requestblock 3!" );
  88.  
  89.  
  90.  
  91.   /* Open the Timer Device: */
  92.   not1_opened = OpenDevice( TIMERNAME, UNIT_VBLANK, tr1 ,0 );
  93.   if( not1_opened )
  94.     clean_up( "Could not open the Timer Device (1)!" );
  95.  
  96.   not2_opened = OpenDevice( TIMERNAME, UNIT_VBLANK, tr2 ,0 );
  97.   if( not2_opened )
  98.     clean_up( "Could not open the Timer Device (2)!" );
  99.  
  100.   not3_opened = OpenDevice( TIMERNAME, UNIT_VBLANK, tr3 ,0 );
  101.   if( not3_opened )
  102.     clean_up( "Could not open the Timer Device (3)!" );
  103.  
  104.  
  105.  
  106.   /* Set our request: */
  107.   /* Requestblock 1: */
  108.   tr1->tr_node.io_Command = TR_ADDREQUEST; /* Add a request.   */
  109.   tr1->tr_time.tv_secs = 10;               /* 10 seconds.      */
  110.   tr1->tr_time.tv_micro = 0;               /* 0 micro seconds. */
  111.  
  112.   /* Requestblock 2: */
  113.   tr2->tr_node.io_Command = TR_ADDREQUEST; /* Add a request.   */
  114.   tr2->tr_time.tv_secs = 7;                /* 7 seconds.       */
  115.   tr2->tr_time.tv_micro = 0;               /* 0 micro seconds. */
  116.  
  117.   /* Requestblock 3: */
  118.   tr3->tr_node.io_Command = TR_ADDREQUEST; /* Add a request.   */
  119.   tr3->tr_time.tv_secs = 5;                /* 5 seconds.       */
  120.   tr3->tr_time.tv_micro = 0;               /* 0 micro seconds. */
  121.  
  122.  
  123.  
  124.   /* Get a pointer to the timer device: */
  125.   TimerBase = tr1->tr_node.io_Device;
  126.  
  127.  
  128.  
  129.   /* Compare the first two time val structures: */
  130.   /* (Since the first timeval structure is greater */
  131.   /* than the second timeval structure -1 should   */
  132.   /* be returned.                                  */
  133.   printf( "Compare time1 - time2: %d\n",
  134.     CmpTime( &(tr1->tr_time), &(tr2->tr_time) ) );
  135.  
  136.   /* Compare the same timeval structures, but put */
  137.   /* them in the opposite position: (1 should be  */
  138.   /* returned.)                                   */
  139.   printf( "Compare time2 - time1: %d\n",
  140.     CmpTime( &(tr2->tr_time), &(tr1->tr_time) ) );
  141.  
  142.  
  143.  
  144.   /* Add the time in the third timeval structure to   */
  145.   /* the second timeval structure. The second timeval */
  146.   /* structure should now contain the time 12 sec.    */
  147.   printf( "Add time3 with time2, and store the result in time2.\n" );
  148.   AddTime( &(tr2->tr_time), &(tr3->tr_time) );
  149.  
  150.   /* Compare the first time val structure with the */
  151.   /* second timeval structure again. The second    */
  152.   /* structure should now be greater, and thus 1   */
  153.   /* should be returned.                           */
  154.   printf( "Compare time1 - time2: %d\n",
  155.     CmpTime( &(tr1->tr_time), &(tr2->tr_time) ) );
  156.  
  157.  
  158.  
  159.   /* Subtract the time in the third timeval structure with      */
  160.   /* the second timeval structure. The second timeval structure */
  161.   /* should now contain the time 7 seconds again.               */
  162.   printf( "Subtract time3 with time2, and store the result in time2.\n" );
  163.   SubTime( &(tr2->tr_time), &(tr3->tr_time) );
  164.  
  165.   /* Compare the first time val structure with the */
  166.   /* second timeval structure again. The first     */
  167.   /* structure should now be greater again, and    */
  168.   /* thus 1 should be returned.                    */
  169.   printf( "Compare time1 - time2: %d\n",
  170.     CmpTime( &(tr1->tr_time), &(tr2->tr_time) ) );
  171.  
  172.  
  173.  
  174.   /* We prepare three time requestblocks as normal, but in */
  175.   /* this simple example we do not use them. We simply use */
  176.   /* the timeval structures.                               */
  177.  
  178.  
  179.  
  180.   /* Clean up and quit: */
  181.   clean_up( "The End!" );
  182. }
  183.  
  184.  
  185.  
  186. void clean_up( text )
  187. STRPTR text;
  188. {
  189.   /* Close the Timer Devices: */
  190.   if( !not1_opened )
  191.     CloseDevice( tr1 );
  192.   if( !not2_opened )
  193.     CloseDevice( tr2 );
  194.   if( !not3_opened )
  195.     CloseDevice( tr3 );
  196.  
  197.   /* Delete the extended request blocks: */
  198.   if( tr1 ) DeleteExtIO( tr1, sizeof( struct timerequest) );
  199.   if( tr2 ) DeleteExtIO( tr2, sizeof( struct timerequest) );
  200.   if( tr3 ) DeleteExtIO( tr3, sizeof( struct timerequest) );
  201.  
  202.   /* Remove the message port: */
  203.   if( replymp )
  204.     DeletePort( replymp );
  205.  
  206.   /* Print the last message: */
  207.   printf( "%s\n", text );
  208.  
  209.   /* Quit: */
  210.   exit( 0 );
  211. }
  212.  
  213.