home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / datafiles / text / c_manual / devices / timerdevice / example3.c < prev    next >
C/C++ Source or Header  |  1995-02-27  |  4KB  |  137 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:    Example3.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. /* to get the current system time. We will then add two hours */
  24. /* and set the new system time.                               */
  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. /* The reply port: */
  36. struct MsgPort *replymp;
  37.  
  38. /* The timer request block: */
  39. struct timerequest *tr;
  40.  
  41. /* When the Timer Device is open this variable is TRUE: */
  42. BOOL not_opened = TRUE;
  43.  
  44.  
  45.  
  46. /* Declare our functions: */
  47. void clean_up();
  48. void main();
  49.  
  50.  
  51.  
  52. void main()
  53. {
  54.   /* Get a reply port: */
  55.   replymp = (struct MsgPort *)
  56.     CreatePort( NULL, 0 );
  57.   if( !replymp )
  58.     clean_up( "Could not create the reply port!" );
  59.  
  60.  
  61.   /* Get an extended request block: */
  62.   tr = (struct timerequest *)
  63.     CreateExtIO( replymp, sizeof( struct timerequest) );
  64.   if( !tr )
  65.     clean_up( "Could not create the IO!" );
  66.  
  67.  
  68.   /* Open the Timer Device: */
  69.   not_opened = OpenDevice( TIMERNAME, UNIT_VBLANK, tr ,0 );
  70.   if( not_opened )
  71.     clean_up( "Could not open the Timer Device!" );
  72.  
  73.  
  74.  
  75.   /* We want to get the current system time: */
  76.   tr->tr_node.io_Command = TR_GETSYSTIME;
  77.  
  78.   /* Do our request: */
  79.   printf( "Let's check the time...\n" );
  80.   DoIO( tr );
  81.  
  82.   /* Print the current system time: */
  83.   printf( "The current system time is:\n" );
  84.   printf( "Seconds:      %d\n", tr->tr_time.tv_secs );
  85.   printf( "Microseconds: %d\n", tr->tr_time.tv_micro );
  86.  
  87.   /* Note that while Exec has handled your request the system  */
  88.   /* time has already changed, so the current system time is   */
  89.   /* not very current (it is already some micro seconds late). */
  90.  
  91.  
  92.  
  93.   /* Add two hours: */
  94.   tr->tr_time.tv_secs += 60 * 60 * 2;
  95.  
  96.   /* We want to set a new system time: */
  97.   tr->tr_node.io_Command = TR_SETSYSTIME;
  98.  
  99.   /* Do our request: */
  100.   printf( "Adding two hours to the system time...\n" );
  101.   DoIO( tr );
  102.  
  103.   /* Note that you should not set the time backwords. The timer   */
  104.   /* device has never seen a clock going backwords, so it is best */ 
  105.   /* to not make it or some other program to excited!             */
  106.  
  107.  
  108.  
  109.   /* Clean up and quit: */
  110.   clean_up( "The End!" );
  111. }
  112.  
  113.  
  114.  
  115. void clean_up( text )
  116. STRPTR text;
  117. {
  118.   /* Close the Timer Device: */
  119.   if( !not_opened )
  120.     CloseDevice( tr );
  121.  
  122.   /* Delete the extended request block: */
  123.   if( tr )
  124.     DeleteExtIO( tr, sizeof( struct timerequest) );
  125.  
  126.   /* Remove the message port: */
  127.   if( replymp )
  128.     DeletePort( replymp);
  129.  
  130.   /* Print the last message: */
  131.   printf( "%s\n", text );
  132.  
  133.   /* Quit: */
  134.   exit( 0 );
  135. }
  136.  
  137.