home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / useful / dev / c / rkrm / timer / simple_timer.c < prev    next >
C/C++ Source or Header  |  1992-09-03  |  7KB  |  276 lines

  1. /*
  2.  * Copyright (c) 1992 Commodore-Amiga, Inc.
  3.  * 
  4.  * This example is provided in electronic form by Commodore-Amiga, Inc. for 
  5.  * use with the "Amiga ROM Kernel Reference Manual: Devices", 3rd Edition, 
  6.  * published by Addison-Wesley (ISBN 0-201-56775-X).
  7.  * 
  8.  * The "Amiga ROM Kernel Reference Manual: Devices" contains additional 
  9.  * information on the correct usage of the techniques and operating system 
  10.  * functions presented in these examples.  The source and executable code 
  11.  * of these examples may only be distributed in free electronic form, via 
  12.  * bulletin board or as part of a fully non-commercial and freely 
  13.  * redistributable diskette.  Both the source and executable code (including 
  14.  * comments) must be included, without modification, in any copy.  This 
  15.  * example may not be published in printed form or distributed with any
  16.  * commercial product.  However, the programming techniques and support
  17.  * routines set forth in these examples may be used in the development
  18.  * of original executable software products for Commodore Amiga computers.
  19.  * 
  20.  * All other rights reserved.
  21.  * 
  22.  * This example is provided "as-is" and is subject to change; no
  23.  * warranties are made.  All use is at your own risk. No liability or
  24.  * responsibility is assumed.
  25.  *
  26.  *****************************************************************************
  27.  *
  28.  * Simple_Timer.c
  29.  *
  30.  * A simple example of using the timer device.
  31.  *
  32.  * Compile with SAS C 5.10: LC -b1 -cfistq -v -y -L
  33.  *
  34.  * Run from CLI only
  35.  */
  36.  
  37. #include <exec/types.h>
  38. #include <exec/io.h>
  39. #include <exec/memory.h>
  40. #include <devices/timer.h>
  41.  
  42. #include <clib/exec_protos.h>
  43. #include <clib/alib_protos.h>
  44. #include <clib/dos_protos.h>
  45.  
  46. #include <stdio.h>
  47.  
  48. #ifdef LATTICE
  49. int CXBRK(void) { return(0); }     /* Disable Lattice CTRL/C handling */
  50. int chkabort(void) { return(0); }  /* really */
  51. #endif
  52.  
  53. /* Our timer sub-routines */
  54. void delete_timer  (struct timerequest *);
  55. LONG get_sys_time  (struct timeval *);
  56. LONG set_new_time  (LONG);
  57. void wait_for_timer(struct timerequest *, struct timeval *);
  58. LONG time_delay    ( struct timeval *, LONG );
  59. struct timerequest *create_timer( ULONG );
  60. void show_time     (ULONG);
  61.  
  62. struct Library *TimerBase;      /* to get at the time comparison functions */
  63.  
  64. /* manifest constants -- "never will change" */
  65. #define   SECSPERMIN   (60)
  66. #define   SECSPERHOUR  (60*60)
  67. #define   SECSPERDAY   (60*60*24)
  68.  
  69. void main(int argc,char **argv)
  70. {
  71. LONG seconds;
  72. struct timerequest *tr;      /* IO block for timer commands */
  73. struct timeval oldtimeval;   /* timevals to store times     */
  74. struct timeval mytimeval;
  75. struct timeval currentval;
  76.  
  77. printf("\nTimer test\n");
  78.  
  79. /* sleep for two seconds */
  80. currentval.tv_secs = 2;
  81. currentval.tv_micro = 0;
  82. time_delay( ¤tval, UNIT_VBLANK );
  83. printf( "After 2 seconds delay\n" );
  84.  
  85. /* sleep for four seconds */
  86. currentval.tv_secs = 4;
  87. currentval.tv_micro = 0;
  88. time_delay( ¤tval, UNIT_VBLANK );
  89. printf( "After 4 seconds delay\n" );
  90.  
  91. /* sleep for 500,000 micro-seconds = 1/2 second */
  92. currentval.tv_secs = 0;
  93. currentval.tv_micro = 500000;
  94. time_delay( ¤tval, UNIT_MICROHZ );
  95. printf( "After 1/2 second delay\n" );
  96.  
  97. printf( "DOS Date command shows: " );
  98. (void) Execute( "date", 0, 0 );
  99.  
  100. /* save what system thinks is the time....we'll advance it temporarily */
  101. get_sys_time( &oldtimeval );
  102. printf("Original system time is:\n");
  103. show_time(oldtimeval.tv_secs );
  104.  
  105. printf("Setting a new system time\n");
  106.  
  107. seconds = 1000 * SECSPERDAY + oldtimeval.tv_secs;
  108.  
  109. set_new_time( seconds );
  110. /* (if user executes the AmigaDOS DATE command now, he will*/
  111. /* see that the time has advanced something over 1000 days */
  112.  
  113. printf( "DOS Date command now shows: " );
  114. (void) Execute( "date", 0, 0 );
  115.  
  116. get_sys_time( &mytimeval );
  117. printf( "Current system time is:\n");
  118. show_time(mytimeval.tv_secs);
  119.  
  120. /* Added the microseconds part to show that time keeps */
  121. /* increasing even though you ask many times in a row  */
  122.  
  123. printf("Now do three TR_GETSYSTIMEs in a row (notice how the microseconds increase)\n\n");
  124. get_sys_time( &mytimeval );
  125. printf("First TR_GETSYSTIME \t%ld.%ld\n",mytimeval.tv_secs, mytimeval.tv_micro);
  126. get_sys_time( &mytimeval );
  127. printf("Second TR_GETSYSTIME \t%ld.%ld\n",mytimeval.tv_secs, mytimeval.tv_micro);
  128. get_sys_time( &mytimeval );
  129. printf("Third TR_GETSYSTIME \t%ld.%ld\n",mytimeval.tv_secs, mytimeval.tv_micro);
  130.  
  131. printf( "\nResetting to former time\n" );
  132. set_new_time( oldtimeval.tv_secs );
  133.  
  134. get_sys_time( &mytimeval );
  135. printf( "Current system time is:\n");
  136. show_time(mytimeval.tv_secs);
  137.  
  138. /* just shows how to set up for using the timer functions, does not */
  139. /* demonstrate the functions themselves.  (TimerBase must have a    */
  140. /* legal value before AddTime, SubTime or CmpTime are performed.    */
  141. tr = create_timer( UNIT_MICROHZ );
  142. TimerBase = (struct Library *)tr->tr_node.io_Device;
  143.  
  144. /* and how to clean up afterwards */
  145. TimerBase = (struct Library *)(-1);
  146. delete_timer( tr );
  147. }
  148.  
  149. struct timerequest *create_timer( ULONG unit )
  150. {
  151. /* return a pointer to a timer request.  If any problem, return NULL */
  152. LONG error;
  153. struct MsgPort *timerport;
  154. struct timerequest *TimerIO;
  155.  
  156. timerport = CreatePort( 0, 0 );
  157. if (timerport == NULL )
  158.     return( NULL );
  159.  
  160. TimerIO = (struct timerequest *)
  161.     CreateExtIO( timerport, sizeof( struct timerequest ) );
  162. if (TimerIO == NULL )
  163.     {
  164.     DeletePort(timerport);   /* Delete message port */
  165.     return( NULL );
  166.     }
  167.  
  168. error = OpenDevice( TIMERNAME, unit,(struct IORequest *) TimerIO, 0L );
  169. if (error != 0 )
  170.     {
  171.     delete_timer( TimerIO );
  172.     return( NULL );
  173.     }
  174. return( TimerIO );
  175. }
  176.  
  177. /* more precise timer than AmigaDOS Delay() */
  178. LONG time_delay( struct timeval *tv, LONG unit )
  179. {
  180. struct timerequest *tr;
  181. /* get a pointer to an initialized timer request block */
  182. tr = create_timer( unit );
  183.  
  184. /* any nonzero return says timedelay routine didn't work. */
  185. if (tr == NULL )
  186.     return( -1L );
  187.  
  188. wait_for_timer( tr, tv );
  189.  
  190. /* deallocate temporary structures */
  191. delete_timer( tr );
  192. return( 0L );
  193. }
  194.  
  195. void wait_for_timer(struct timerequest *tr, struct timeval *tv )
  196. {
  197.  
  198. tr->tr_node.io_Command = TR_ADDREQUEST; /* add a new timer request */
  199.  
  200. /* structure assignment */
  201. tr->tr_time = *tv;
  202.  
  203. /* post request to the timer -- will go to sleep till done */
  204. DoIO((struct IORequest *) tr );
  205. }
  206.  
  207. LONG set_new_time(LONG secs)
  208. {
  209. struct timerequest *tr;
  210. tr = create_timer( UNIT_MICROHZ );
  211.  
  212. /* non zero return says error */
  213. if (tr == 0 )
  214.     return( -1 );
  215.  
  216. tr->tr_time.tv_secs = secs;
  217. tr->tr_time.tv_micro = 0;
  218. tr->tr_node.io_Command = TR_SETSYSTIME;
  219. DoIO((struct IORequest *) tr );
  220.  
  221. delete_timer(tr);
  222. return(0);
  223. }
  224.  
  225. LONG get_sys_time(struct timeval *tv)
  226. {
  227. struct timerequest *tr;
  228. tr = create_timer( UNIT_MICROHZ );
  229.  
  230. /* non zero return says error */
  231. if (tr == 0 )
  232.     return( -1 );
  233.  
  234. tr->tr_node.io_Command = TR_GETSYSTIME;
  235. DoIO((struct IORequest *) tr );
  236.  
  237. /* structure assignment */
  238. *tv = tr->tr_time;
  239.  
  240. delete_timer( tr );
  241. return( 0 );
  242. }
  243.  
  244. void delete_timer(struct timerequest *tr )
  245. {
  246. struct MsgPort *tp;
  247.  
  248. if (tr != 0 )
  249.     {
  250.     tp = tr->tr_node.io_Message.mn_ReplyPort;
  251.  
  252.     if (tp != 0)
  253.         DeletePort(tp);
  254.  
  255.     CloseDevice( (struct IORequest *) tr );
  256.     DeleteExtIO( (struct IORequest *) tr );
  257.     }
  258. }
  259.  
  260. void show_time(ULONG secs)
  261. {
  262. ULONG days,hrs,mins;
  263.  
  264. /* Compute days, hours, etc. */
  265. mins=secs/60;
  266. hrs=mins/60;
  267. days=hrs/24;
  268. secs=secs%60;
  269. mins=mins%60;
  270. hrs=hrs%24;
  271.  
  272. /* Display the time */
  273. printf("*   Hour Minute Second  (Days since Jan.1,1978)\n");
  274. printf("*%5ld:%5ld:%5ld      (%6ld )\n\n",hrs,mins,secs,days);
  275. }      /* end of main */
  276.