gosub TM_ResetTimers
gosub TM_RegisterTimer Timer1 auto 10 second Callback1
gosub TM_RegisterTimer Timer2 5 1 second
set %timer1 0
set %timer2 0
repeat
gosub TM_ProcessTimers
if Timer1 in #RESULT
{
set %timer1 %timer1 + 1
if %timer1 % 2 = 1
gosub TM_RestartTimer Timer2
}
if Timer2 in #RESULT
set %timer2 %timer2 + 1
until #FALSE
stop
; --------------------------------------------------------
sub Callback1
display ok Callback 1 called!
return
; --------------------------------------------------------
sub TM_ResetTimers
namespace push
namespace global TM_TIMER
namespace clear
namespace pop
return
; --------------------------------------------------------
sub TM_RegisterTimer
namespace push
namespace global TM_TIMER
if !num = N/A
set !num -1
if %0 < 4
{
display ok 4 arguments required for TM_RegisterTimer - stopping
stop
}
set !num !num + 1
set !var %1
set !function %2 ; auto, number
set !time_length %3
set !time_unit %4 ; millisecond, second, minute, hour
if %0 = 5
set !callback %5
else
set !callback N/A
set ! . timer , !num !var ; save the name to a list for automatic time updating
set ! . !var , function !function
set ! . !var , trigger_max !function
set ! . !var , time_length !time_length
set ! . !var , time_unit !time_unit
set ! . !var , callback !callback
set ! . !var , enabled #TRUE
if !time_unit notin _millisecond_second_minute_hour_
{
display ok time unit not equal to millisecond, second, minute, hour - stopping
stop
}
if !time_unit = millisecond
set ! . !var , time_unit !time_length
if !time_unit = second
set ! . !var , time_unit ( !time_length * 20 )
if !time_unit = minute
set ! . !var , time_unit ( !time_length * 20 * 60 )
if !time_unit = hour
set ! . !var , time_unit ( !time_length * 20 * 60 * 60 )
set !time_unit !var , time_unit
set ! . !var , timer ( #SCNT2 + ( ! . !time_unit ) ) ; set the actual end time
namespace pop
return
; --------------------------------------------------------
sub TM_ProcessTimers
namespace push
namespace global TM_TIMER
set !lpc #LPC
set #LPC 200
set !RESULT
for !i 0 !num
{
set !current_name ! . timer . !i
set !enabled !current_name , enabled
set !timer !current_name , timer
set !time_unit !current_name , time_unit
set !function !current_name , function
set !callback !current_name , callback
set !test1 #SCNT2 , _ , ! . !timer
if ( ( ! . !enabled = #TRUE ) && ( #SCNT2 > ( ! . !timer ) ) )
{
set !function !current_name , function
if ( ( ( ! . !function ) = auto ) || ( ! . !function <> 0 ) )
{
set !RESULT !RESULT , _ , !current_name ; add expired timer name to present list
set ! . !timer ( #SCNT2 + ( ! . !time_unit ) ) ; set the actual end time, resetting timer
if ! . !function <> auto
set ! . !function ( ( ! . !function ) - 1 )
if ! . !callback <> N/A
gosub ! . !callback
}
}
}
set #RESULT !RESULT
set #LPC !lpc
namespace pop
return #RESULT
; --------------------------------------------------------
sub TM_RestartTimer
namespace push
namespace global TM_TIMER
set !name %1
set !function !name , function
set !trigger_max !name , trigger_max
set !enabled !name , enabled
set !timer !name , timer
set !time_unit !name , time_unit
set ! . !function ! . !trigger_max
set ! . !enable #TRUE
set ! . !timer ( #SCNT2 + ( ! . !time_unit ) ) ; set the actual end time
namespace pop
return
; --------------------------------------------------------
sub TM_StopTimer
namespace push
namespace global TM_TIMER
set !name %1
set !enabled !name , enabled
set ! . !enabled #FALSE
namespace pop
return