home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / datafiles / text / c_manual / devices / timerdevice / example1.c < prev    next >
C/C++ Source or Header  |  1995-02-27  |  3KB  |  116 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:    Example1.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 use the Timer Device. */
  23. /* The program will be put to sleep for 10 seconds.            */
  24.  
  25.  
  26.  
  27. #include <exec/types.h>    /* STRPTR         */
  28. #include <exec/ports.h>    /* struct Message */
  29. #include <exec/memory.h>   /* MEMF_PUBLIC    */
  30. #include <devices/timer.h> /* TIMERNAME */
  31.  
  32.  
  33.  
  34. /* The reply port: */
  35. struct MsgPort *replymp;
  36.  
  37. /* The timer request block: */
  38. struct timerequest *tr;
  39.  
  40. /* When the Timer Device is open this variable is TRUE: */
  41. BOOL not_opened = TRUE;
  42.  
  43.  
  44.  
  45. /* Declare our functions: */
  46. void clean_up();
  47. void main();
  48.  
  49.  
  50.  
  51. void main()
  52. {
  53.   /* 1. Get a reply port: */
  54.   replymp = (struct MsgPort *)
  55.     CreatePort( NULL, 0 );
  56.   if( !replymp )
  57.     clean_up( "Could not create the reply port!" );
  58.  
  59.  
  60.   /* 2. Get an extended request block: */
  61.   tr = (struct timerequest *)
  62.     CreateExtIO( replymp, sizeof( struct timerequest) );
  63.   if( !tr )
  64.     clean_up( "Could not create the IO!" );
  65.  
  66.  
  67.   /* 3. Open the Timer Device: */
  68.   not_opened = OpenDevice( TIMERNAME, UNIT_VBLANK, tr ,0 );
  69.   if( not_opened )
  70.     clean_up( "Could not open the Timer Device!" );
  71.  
  72.  
  73.  
  74.   /* 4. Set our request: */
  75.   tr->tr_node.io_Command = TR_ADDREQUEST; /* Add a request.   */
  76.   tr->tr_time.tv_secs = 10;               /* 10 seconds.      */
  77.   tr->tr_time.tv_micro = 0;               /* 0 micro seconds. */
  78.  
  79.  
  80.  
  81.   /* 5. Do our request: (Wait 10 seconds) */
  82.   printf( "Good night...\n" );
  83.   DoIO( tr );
  84.   printf( "Good morning!\n" );
  85.  
  86.  
  87.  
  88.   /* Clean up and quit: */
  89.   clean_up( "The End!" );
  90. }
  91.  
  92.  
  93.  
  94. void clean_up( text )
  95. STRPTR text;
  96. {
  97.   /* Close the Timer Device: */
  98.   if( !not_opened )
  99.     CloseDevice( tr );
  100.  
  101.   /* Delete the extended request block: */
  102.   if( tr )
  103.     DeleteExtIO( tr, sizeof( struct timerequest) );
  104.  
  105.   /* Remove the message port: */
  106.   if( replymp )
  107.     DeletePort( replymp);
  108.  
  109.   /* Print the last message: */
  110.   printf( "%s\n", text );
  111.  
  112.   /* Quit: */
  113.   exit( 0 );
  114. }
  115.  
  116.