2 * Copyright (c) 1992 Commodore-Amiga, Inc.
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).
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.
20 * All other rights reserved.
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.
26 *****************************************************************************
30 * A simple example of using the timer device.
32 * Compile with SAS C 5.10: LC -b1 -cfistq -v -y -L
37 #include <exec/types.h>
39 #include <exec/memory.h>
40 #include <devices/timer.h>
42 #include <proto/exec.h>
43 #include <clib/alib_protos.h>
44 #include <proto/dos.h>
49 int CXBRK(void) { return(0); } /* Disable Lattice CTRL/C handling */
50 int chkabort(void) { return(0); } /* really */
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
);
62 struct Library
*TimerBase
; /* to get at the time comparison functions */
64 /* manifest constants -- "never will change" */
65 #define SECSPERMIN (60)
66 #define SECSPERHOUR (60*60)
67 #define SECSPERDAY (60*60*24)
69 int main(int argc
,char **argv
)
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
;
77 printf("\nTimer test\n");
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" );
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" );
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" );
97 printf( "DOS Date command shows: " );
98 (void) Execute( "date", 0, 0 );
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
);
105 printf("Setting a new system time\n");
107 seconds
= 1000 * SECSPERDAY
+ oldtimeval
.tv_secs
;
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 */
113 printf( "DOS Date command now shows: " );
114 (void) Execute( "date", 0, 0 );
116 get_sys_time( &mytimeval
);
117 printf( "Current system time is:\n");
118 show_time(mytimeval
.tv_secs
);
120 /* Added the microseconds part to show that time keeps */
121 /* increasing even though you ask many times in a row */
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
);
131 printf( "\nResetting to former time\n" );
132 set_new_time( oldtimeval
.tv_secs
);
134 get_sys_time( &mytimeval
);
135 printf( "Current system time is:\n");
136 show_time(mytimeval
.tv_secs
);
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
);
143 TimerBase
= (struct Library
*)tr
->tr_node
.io_Device
;
145 printf("Could't create timer with UNIT_MICROHZ\n");
147 /* and how to clean up afterwards */
148 TimerBase
= (struct Library
*)(-1);
154 struct timerequest
*create_timer( ULONG unit
)
156 /* return a pointer to a timer request. If any problem, return NULL */
158 struct MsgPort
*timerport
;
159 struct timerequest
*TimerIO
;
161 timerport
= CreatePort( 0, 0 );
162 if (timerport
== NULL
)
165 TimerIO
= (struct timerequest
*)
166 CreateExtIO( timerport
, sizeof( struct timerequest
) );
167 if (TimerIO
== NULL
)
169 DeletePort(timerport
); /* Delete message port */
173 error
= OpenDevice( TIMERNAME
, unit
,(struct IORequest
*) TimerIO
, 0L );
176 delete_timer( TimerIO
);
182 /* more precise timer than AmigaDOS Delay() */
183 LONG
time_delay( struct timeval
*tv
, LONG unit
)
185 struct timerequest
*tr
;
186 /* get a pointer to an initialized timer request block */
187 tr
= create_timer( unit
);
189 /* any nonzero return says timedelay routine didn't work. */
193 wait_for_timer( tr
, tv
);
195 /* deallocate temporary structures */
200 void wait_for_timer(struct timerequest
*tr
, struct timeval
*tv
)
203 tr
->tr_node
.io_Command
= TR_ADDREQUEST
; /* add a new timer request */
205 /* structure assignment */
208 /* post request to the timer -- will go to sleep till done */
209 DoIO((struct IORequest
*) tr
);
212 LONG
set_new_time(LONG secs
)
214 struct timerequest
*tr
;
215 tr
= create_timer( UNIT_MICROHZ
);
217 /* non zero return says error */
221 tr
->tr_time
.tv_secs
= secs
;
222 tr
->tr_time
.tv_micro
= 0;
223 tr
->tr_node
.io_Command
= TR_SETSYSTIME
;
224 DoIO((struct IORequest
*) tr
);
230 LONG
get_sys_time(struct timeval
*tv
)
232 struct timerequest
*tr
;
233 tr
= create_timer( UNIT_MICROHZ
);
235 /* non zero return says error */
239 tr
->tr_node
.io_Command
= TR_GETSYSTIME
;
240 DoIO((struct IORequest
*) tr
);
242 /* structure assignment */
249 void delete_timer(struct timerequest
*tr
)
255 tp
= tr
->tr_node
.io_Message
.mn_ReplyPort
;
260 CloseDevice( (struct IORequest
*) tr
);
261 DeleteExtIO( (struct IORequest
*) tr
);
265 void show_time(ULONG secs
)
269 /* Compute days, hours, etc. */
277 /* Display the time */
278 printf("* Hour Minute Second (Days since Jan.1,1978)\n");
279 printf("*%5ld:%5ld:%5ld (%6ld )\n\n",hrs
,mins
,secs
,days
);