home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************/
- /* */
- /* Amiga C Encyclopedia (ACE) V3.0 Amiga C Club (ACC) */
- /* ------------------------------- ------------------ */
- /* */
- /* Book: ACM Devices Amiga C Club */
- /* Chapter: Timer Device Tulevagen 22 */
- /* File: Example2.c 181 41 LIDINGO */
- /* Author: Anders Bjerin SWEDEN */
- /* Date: 92-04-27 */
- /* Version: 1.00 */
- /* */
- /* Copyright 1992, Anders Bjerin - Amiga C Club (ACC) */
- /* */
- /* Registered members may use this program freely in their */
- /* own commercial/noncommercial programs/articles. */
- /* */
- /***********************************************************/
-
-
-
- /* This example demonstrates how you can send several */
- /* requests to the Timer Device. */
-
-
-
- #include <exec/types.h> /* STRPTR */
- #include <exec/ports.h> /* struct Message */
- #include <exec/memory.h> /* MEMF_PUBLIC */
- #include <devices/timer.h> /* TIMERNAME */
-
-
-
- /* The reply port: */
- struct MsgPort *replymp;
-
- /* Pointer to the message: */
- struct Message *msg;
-
- /* The timer request blocks: */
- struct timerequest *tr1;
- struct timerequest *tr2;
- struct timerequest *tr3;
-
- /* When the Timer Device is open this variable is TRUE: */
- BOOL not1_opened = TRUE;
- BOOL not2_opened = TRUE;
- BOOL not3_opened = TRUE;
-
-
-
- /* Declare our functions: */
- void clean_up();
- void main();
-
-
-
- void main()
- {
- /* Counter: */
- int count;
-
-
- /* Get a reply port: */
- replymp = (struct MsgPort *)
- CreatePort( NULL, 0 );
- if( !replymp )
- clean_up( "Could not create the reply port!" );
-
-
- /* Get three extended request blocks: */
- /* Requestblock 1: */
- tr1 = (struct timerequest *)
- CreateExtIO( replymp, sizeof(struct timerequest) );
- if( !tr1 )
- clean_up( "Could not create requestblock 1!" );
-
-
- /* Requestblock 2: */
- tr2 = (struct timerequest *)
- CreateExtIO( replymp, sizeof(struct timerequest) );
- if( !tr2 )
- clean_up( "Could not create requestblock 2!" );
-
- /* Requestblock 3: */
- tr3 = (struct timerequest *)
- CreateExtIO( replymp, sizeof(struct timerequest) );
- if( !tr3 )
- clean_up( "Could not create requestblock 3!" );
-
-
-
- /* Since each request block must be linked together with the */
- /* timer device we have to open the timer device three times. */
- /* We could of course open the timer device once, and then */
- /* copy the first request block to the other two blocks. If */
- /* the device can be used exclusively this is then the only */
- /* solution (exclusive mode means that only one user may use */
- /* the device). For example, see chapter 27 "Serial Device". */
- /* However, since the timer device does not have any limit */
- /* of number of users, we demonstrate this and open it three */
- /* times. */
-
-
-
- /* Open the Timer Device: */
- not1_opened = OpenDevice( TIMERNAME, UNIT_VBLANK, tr1 ,0 );
- if( not1_opened )
- clean_up( "Could not open the Timer Device (1)!" );
-
- not2_opened = OpenDevice( TIMERNAME, UNIT_VBLANK, tr2 ,0 );
- if( not2_opened )
- clean_up( "Could not open the Timer Device (2)!" );
-
- not3_opened = OpenDevice( TIMERNAME, UNIT_VBLANK, tr3 ,0 );
- if( not3_opened )
- clean_up( "Could not open the Timer Device (3)!" );
-
-
-
- /* Set our request: */
- /* Requestblock 1: */
- tr1->tr_node.io_Command = TR_ADDREQUEST; /* Add a request. */
- tr1->tr_time.tv_secs = 10; /* 10 seconds. */
- tr1->tr_time.tv_micro = 0; /* 0 micro seconds. */
-
- /* Requestblock 2: */
- tr2->tr_node.io_Command = TR_ADDREQUEST; /* Add a request. */
- tr2->tr_time.tv_secs = 7; /* 7 seconds. */
- tr2->tr_time.tv_micro = 0; /* 0 micro seconds. */
-
- /* Requestblock 3: */
- tr3->tr_node.io_Command = TR_ADDREQUEST; /* Add a request. */
- tr3->tr_time.tv_secs = 5; /* 5 seconds. */
- tr3->tr_time.tv_micro = 0; /* 0 micro seconds. */
-
-
-
- /* Do our requests: (Do not wait) */
- SendIO( tr1 );
- SendIO( tr2 );
- SendIO( tr3 );
-
-
-
- count = 0;
- while( count < 3 )
- {
- /* Add counter: */
- count++;
-
- /* Wait for a message to arrive: */
- msg = (struct Message *) WaitPort( replymp );
-
- /* One of our requests has been completed: */
- printf( "Request done!\n" );
-
- /* Romove the message: */
- msg = (struct Message *) GetMsg( replymp );
- }
-
-
-
- /* Clean up and quit: */
- clean_up( "The End!" );
- }
-
-
-
- void clean_up( text )
- STRPTR text;
- {
- /* Close the Timer Device: */
- if( !not1_opened )
- CloseDevice( tr1 );
- if( !not2_opened )
- CloseDevice( tr2 );
- if( !not3_opened )
- CloseDevice( tr3 );
-
- /* Delete the extended request blocks: */
- if( tr1 ) DeleteExtIO( tr1, sizeof( struct timerequest) );
- if( tr2 ) DeleteExtIO( tr2, sizeof( struct timerequest) );
- if( tr3 ) DeleteExtIO( tr3, sizeof( struct timerequest) );
-
- /* Remove the message port: */
- if( replymp )
- DeletePort( replymp );
-
- /* Print the last message: */
- printf( "%s\n", text );
-
- /* Quit: */
- exit( 0 );
- }
-
-