home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 377b.lha / devices / timer / timertest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1980-02-03  |  7.7 KB  |  268 lines

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