Basic/source/Timer.spin

136 lines
5.3 KiB
Plaintext
Raw Normal View History

2014-04-30 20:24:00 +02:00
'' ===========================================================================
'' VGA High-Res Text UI Elements Base UI Support Functions v1.2
''
'' FILE: Timer.spin
'' Author: Allen Marincak
'' Copyright (c) 2009 Allen MArincak
'' See end of file for terms of use
'' ===========================================================================
''
'' ============================================================================
'' Timer function
'' ============================================================================
''
'' Starts a new cog to provide timing functionality.
''
'' There are 8 timers available, the code running on the new cog simply
'' decrements each timer value at the rate specified in the start() call.
''
'' Only the first object to create this needs to call the START method. That
'' will launch it on a new COG, other objects can reference it in the OBJ
'' section but do not need to start it, just call the public methods.
'' ============================================================================
DAT
' stack size determined StackLengthAJM.spin
cog long 0 'cog execute is running on
stack long 0,0,0,0,0,0,0,0,0,0,0,0 'stack space 12 longs
' tmrs_used byte 0,0,0,0,0,0,0,0 'bitfield signifying timers in use
timers word 0,0,0,0,0,0,0,0 'timer values (16 bit)
PUB start( period ) : okay1
''
'' Launches TIMER TASK on new cog, returns 0 on error, else the cog that was
'' started up.
'' - period is the fractional part of a second to operate at
'' example: 10 = 1/10th of a second
'' 30 = 1/30th of a second
'' 100 = 1/100th of a second
wordfill( @timers, 0, 8 )
'bytefill( @tmrs_used, 0, 8 )
cog := cognew( execute( period, @timers ), @stack ) + 1
okay1 := cog
Pub Stop
if Cog==-1
return
cogstop(Cog)
Cog:=-1
{PUB register : new_tmr | idx
'' Register a timer. Returns the first free timer (8 timers numbered 0 to 7)
'' or returns -1 if none are available.
repeat idx from 0 to 7
if tmrs_used[idx] == 0
tmrs_used[idx] := 1
new_tmr := idx
return
new_tmr := -1
PUB unregister( tmrid )
'' Unregisters a timer, frees it up for reuse.
if tmrs_used[tmrid] == 1
timers[tmrid] := 0
tmrs_used[tmrid] := 0
}
PUB set( tmrid, val )
'' Sets a registered timer with a 16 bit value.
' if tmrs_used[tmrid] == 1
timers[tmrid] := val
PUB isClr( tmrid )': clr
'' Checks the status of the registered timer.
'' returns 1 if it has expired (zeroed)
'' 0 if is still running
' clr := 0
if timers[tmrid] == 0
return 1
else
return 0
'return clr
PUB read( tmrid )
'' returns the current count of a registered timer.
return timers[tmrid]
PRI execute( period, ptr_tmrs ) | idx, interval_cnt, time_base
'' new cog executes this, it just decrements the timers
interval_cnt := clkfreq / period
time_base := cnt
repeat
waitcnt( time_base += interval_cnt )
repeat idx from 0 to 7
if word[ptr_tmrs][idx] <> 0
word[ptr_tmrs][idx] -= 1
{{
┌────────────────────────────────────────────────────────────────────────────┐
│ TERMS OF USE: MIT License │
├────────────────────────────────────────────────────────────────────────────┤
│Permission is hereby granted, free of charge, to any person obtaining a copy│
│of this software and associated documentation files (the "Software"), to │
│deal in the Software without restriction, including without limitation the │
│rights to use, copy, modify, merge, publish, distribute, sublicense, and/or │
│sell copies of the Software, and to permit persons to whom the Software is │
│furnished to do so, subject to the following conditions: │
│ │
│The above copyright notice and this permission notice shall be included in │
│all copies or substantial portions of the Software. │
│ │
│THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR │
│IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, │
│FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE │
│AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER │
│LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING │
│FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS│
│IN THE SOFTWARE. │
└────────────────────────────────────────────────────────────────────────────┘
}}