home *** CD-ROM | disk | FTP | other *** search
- ''
- '' $Id: SimpleTimer.bas,v 1.2 1994/03/16 14:53:29 alex Rel $
- ''
- '' A simple example of using the timer device
- ''
- '' Derived from RKM example (c) Copyright 1992 Commodore-Amiga, Inc.
- ''
-
- DEFINT A-Z
-
- 'REM $INCLUDE Exec.bh
- 'REM $INCLUDE Timer.bc
-
- REM $INCLUDE BLib/ExecSupport.bas
-
- LIBRARY OPEN "exec.library", LIBRARY_MINIMUM&
-
- DIM SHARED junk&
-
- ' manifest constants -- "never will change"
- CONST SECSPERMIN& = 60
- CONST SECSPERHOUR& = 3600 ' 60 * 60
- CONST SECSPERDAY& = 86400 ' 60 * 60 * 24
-
- SUB delete_timer(BYVAL tr&)
- STATIC tp&
-
- IF tr& <> NULL& THEN
- tp& = PEEKL(tr& + tr_node + IORequestio_Message + mn_ReplyPort)
-
- IF tp& <> 0 THEN DeletePort tp&
-
- CloseDevice tr&
- DeleteExtIO tr&
- END IF
- END SUB
-
- ' return a pointer to a timer request. if any problem, return NULL
- FUNCTION create_timer&(BYVAL unit&)
- STATIC r&, timerport&, timerIO&
-
- create_timer& = NULL&
- timerport& = CreatePort&(NULL&, 0)
- IF timerport& <> NULL& THEN
- timerIO& = CreateExtIO&(timerport&, timerequest_sizeof)
- IF timerIO& <> NULL& THEN
- r& = OpenDevice&(SADD("timer.device" + CHR$(0)), unit&, timerIO&, 0)
- IF r& = 0 THEN
- create_timer& = timerIO&
- ELSE
- delete_timer timerIO&
- END IF
- ELSE
- DeletePort timerport& ' Delete message port
- END IF
- END IF
- END FUNCTION
-
- SUB wait_for_timer(BYVAL tr&, BYVAL tv&)
- ' add a new timer request
- POKEW tr& + tr_node + IORequestio_Command, TR_ADDREQUEST&
-
- CopyMem tv&, tr& + tr_time, timeval_sizeof ' structure assignment
-
- ' post request to the timer -- will go to sleep till done
- junk& = DoIO&(tr&)
- END SUB
-
- ' more precise timer than AmigaDOS Delay
- FUNCTION time_delay(BYVAL tv&, BYVAL unit&)
- STATIC tr&
-
- time_delay = FALSE&
- ' get a pointer to an initialized timer request block
- tr& = create_timer&(unit&)
- IF tr& <> NULL& THEN
- wait_for_timer tr&, tv&
-
- ' deallocate temporary structures
- delete_timer tr&
- time_delay = TRUE&
- END IF
- END FUNCTION
-
- FUNCTION set_new_time(BYVAL secs&)
- STATIC tr&
-
- set_new_time = FALSE&
- tr& = create_timer&(UNIT_MICROHZ&)
- IF tr& <> NULL& THEN
- POKEL tr& + tr_time + tv_secs, secs&
- POKEL tr& + tr_time + tv_micro, 0&
- POKEW tr& + tr_node + IORequestio_Command, TR_SETSYSTIME&
- junk& = DoIO&(tr&)
-
- delete_timer tr&
- set_new_time = TRUE&
- END IF
- END FUNCTION
-
- FUNCTION get_sys_time(BYVAL tv&)
- STATIC tr&
-
- get_sys_time = FALSE&
- tr& = create_timer&(UNIT_MICROHZ&)
- IF tr& <> NULL& THEN
- POKEW tr& + tr_node + IORequestio_Command, TR_GETSYSTIME&
- junk& = DoIO&(tr&)
-
- CopyMem tr& + tr_time, tv&, timeval_sizeof ' structure assignment
-
- delete_timer tr&
- get_sys_time = TRUE&
- END IF
- END FUNCTION
-
- SUB show_time(BYVAL secs&)
- STATIC days&, hrs&, mins&
-
- ' Compute days, hours, etc.
- mins& = secs& \ 60
- hrs& = mins& \ 60
- days& = hrs& \ 24
- secs& = secs& MOD 60
- mins& = mins& MOD 60
- hrs& = hrs& MOD 24
-
- ' Display the time
- PRINT "* Hour Minute Second (Days since Jan.1,1978)"
- PRINT USING "*#####:#####:##### (######)"; hrs&, mins&, secs&, days&
- PRINT
- END SUB
-
- SUB main
- STATIC seconds&, junk
- STATIC tr& ' IO block for timer commands
- DIM oldtimeval(timeval_sizeof \ 2)
- DIM mytimeval(timeval_sizeof \ 2)
- DIM currentval(timeval_sizeof \ 2)
-
- PRINT "Timer test"
-
- ' sleep for two seconds
- POKEL VARPTR(currentval(0)) + tv_secs, 2
- POKEL VARPTR(currentval(0)) + tv_micro, 0
- junk = time_delay(VARPTR(currentval(0)), UNIT_VBLANK&)
- PRINT "After 2 seconds delay"
-
- ' sleep for four seconds
- POKEL VARPTR(currentval(0)) + tv_secs, 4
- POKEL VARPTR(currentval(0)) + tv_micro, 0
- junk = time_delay(VARPTR(currentval(0)), UNIT_VBLANK&)
- PRINT "After 4 seconds delay"
-
- ' sleep for 500,000 micro-seconds = 1/2 second
- POKEL VARPTR(currentval(0)) + tv_secs, 0
- POKEL VARPTR(currentval(0)) + tv_micro, 500000
- junk = time_delay(VARPTR(currentval(0)), UNIT_MICROHZ&)
- PRINT "After 1/2 second delay"
-
- ' save what system thinks is the time....we'll advance it temporarily
- junk = get_sys_time(VARPTR(oldtimeval(0)))
- PRINT "Original system time is:"
- show_time PEEKL(VARPTR(oldtimeval(0)) + tv_secs)
-
- PRINT "Setting a new system time"
-
- seconds& = 1000& * SECSPERDAY& + PEEKL(VARPTR(oldtimeval(0)) + tv_secs)
-
- junk = set_new_time(seconds&)
-
- junk = get_sys_time(VARPTR(mytimeval(0)))
- PRINT "Current system time is:"
- show_time PEEKL(VARPTR(mytimeval(0)) + tv_secs)
-
- ' Added the microseconds part to show that time keeps
- ' increasing even though you ask many times in a row
-
- PRINT "Now do three TR_GETSYSTIMEs in a row (notice how the microseconds increase)"
- PRINT
- junk = get_sys_time(VARPTR(mytimeval(0)))
- PRINT "First TR_GETSYSTIME "; PEEKL(VARPTR(mytimeval(0)) + tv_secs); "."; PEEKL(VARPTR(mytimeval(0)) + tv_micro)
- junk = get_sys_time(VARPTR(mytimeval(0)))
- PRINT "Second TR_GETSYSTIME "; PEEKL(VARPTR(mytimeval(0)) + tv_secs); "."; PEEKL(VARPTR(mytimeval(0)) + tv_micro)
- junk = get_sys_time(VARPTR(mytimeval(0)))
- PRINT "Third TR_GETSYSTIME "; PEEKL(VARPTR(mytimeval(0)) + tv_secs); "."; PEEKL(VARPTR(mytimeval(0)) + tv_micro)
- PRINT
-
- PRINT "Resetting to former time"
- junk = set_new_time(PEEKL(VARPTR(oldtimeval(0)) + tv_secs))
-
- junk = get_sys_time(VARPTR(mytimeval(0)))
- PRINT "Current system time is:"
- show_time PEEKL(VARPTR(mytimeval(0)) + tv_secs)
-
- delete_timer tr&
- END SUB
-
- main
- END
-