Is there a more efficient way of doing this code?
iQuit
Posts: 78
Here is a method I created so that I could time stuff, but also be able to reset the timer mid timing if needed. It runs in another cog, and stops that cog when finished.
It also has a time out flag that goes to 1 when the timer is done. This can be monitored within the main object that started the cog.
I'm new to spin, so this took me two hours (should I admit that?). It works, but it seems a little kludgy to me. Any ideas as to how it could be done more efficiently?
clock settings and other stuff were left out for brevity.
Dan := thanks
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"She may not be very pretty now, but she was somebody's baby once." Bugs Bunny
Post Edited (iQuit) : 4/27/2010 5:35:38 AM GMT
It also has a time out flag that goes to 1 when the timer is done. This can be monitored within the main object that started the cog.
I'm new to spin, so this took me two hours (should I admit that?). It works, but it seems a little kludgy to me. Any ideas as to how it could be done more efficiently?
clock settings and other stuff were left out for brevity.
VAR long TimerStack[noparse][[/noparse]30] long reset_secs, TimeOutFlag PUB Main cognew(Timer(10), @TimerStack) 'call to Timer method with input_secs of 10 . check TimeOutFlag 'see if timer has run out . reset_secs := 20 'reset Timer at any time by giving reset_secs a new value, 20 here . . PUB Timer(input_secs)|t, start_secs TimeOutFlag := 0 reset_secs := input_secs start_secs := input_secs t := cnt repeat while input_secs > 0 waitcnt(t += clkfreq) if reset_secs <> start_secs input_secs := reset_secs start_secs := reset_secs input_secs -= 1 TimeOutFlag := 1 cogstop(cogID)
Dan := thanks
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"She may not be very pretty now, but she was somebody's baby once." Bugs Bunny
Post Edited (iQuit) : 4/27/2010 5:35:38 AM GMT
Comments
You start the function with Timer(10). Now you do some stuff for a while and then say: reset_secs := 10 . In this case the timer would not be resetted!
Wanna think about it by yourself? The solution will also lead to a version which needs one variable less.
As far as I know cogstop is not needed. If the function called by cognew ends, the COG is stopped automatically.
But maybe a clue first?
Dan := thanks
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"She may not be very pretty now, but she was somebody's baby once." Bugs Bunny
Post Edited (iQuit) : 4/27/2010 3:31:56 PM GMT
Enough for a hint ;o)
one up later.
I haven't tried this yet, but it looks as though it'll work. I check for zero and negative values coming in. I think that this will work! I'll try this as soon as I have time.
Made some changes; added a run status variable.
Using:
OBJ
Timer : "TimerObject"
Timer.Start(60)
Timer.pause
Timer.Reset(45)
etc. etc.
All criticism welcomed.
Dan == Thanks
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"She may not be very pretty now, but she was somebody's baby once." Bugs Bunny
Post Edited (iQuit) : 4/28/2010 3:55:52 AM GMT
TimerID:=cognew(...) + 1
Then you know, if TimerID==0 no timer has been started before. If you want to stop the COG you'd say:
cogstop( TimerID-1 )
Variable reset is not needed ... Your loop looks the same as before, so reset_time == 0 is not valid. So, why not check if reset_time<>0 for takeover of the new time?