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

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