Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Search instead for 
Did you mean: 

Timer in BrightScript

Hi , I am aware that brightscript has two components, roTimeSpan and roDateTime which can be used for date/time purposes, but was wondering if there a way to implement the digital timer on roku as a small sub-component on a particular screen with a precision uptil second level, generally, in other languages it can be implemented using threads, but in brightscript, I think it should not be a problem to look for it in the while loop where in we can check for msgs received :

For eg :
while true
msg=wait(100,port) ' I assume roku is fast enough, since the wait loop can wait for events at milliseconds precision
if x <> an_ifTimeSpan_Object.TotalSeconds()
'x is a previously marked time
x=an_ifTimeSpan_Object.TotalSeconds()
'write code to replace the z-layer of roImageCanvas that has the timer
end if
end while


I did a POC and it worked well(though I fumbled at displaying it, the ClearLayer() function does not seem to clear a layer),it does not seem to lag since I could see the values printed in the debug console.

Is this approach really scalable? I mean in an app wherein many other things are being handled in the event checking loop, will this not lag ? is there any other approach to this?
0 Kudos
2 Replies
belltown
Level 7

Re: Timer in BrightScript

I don't see a problem with this as long as your program doesn't block for more than your timer resolution, and even if it does, your timer won't "lag" cumulatively; it just may miss some updates.

What I would do in a case like this is create my own timer object to keep track of the time with a mechanism to indicate whether the time has changed since the last time the display was updated. That will prevent you from updating your display component every 10 or 100 ms or whatever you use in your Wait loops. Your code will work, but with an object-oriented approach you can store the timer object globally or pass it around to any other functions that need to know the current timer value, or instantiate multiple timer objects in different parts of your code, with only one place in your code that you define the implementation details (e.g. if you want to change to 0.1 sec timer resolution, the changes would be confined to one function).

Here's a simple solution that should do what you want if you're looking for one-second timer resolution:


Function new_TimerClass () As Object
this = {}
this.ts = CreateObject ("roTimespan")
this.prevSeconds = -1

this.start = Function () As Void
m.ts.Mark ()
End Function

this.ticked = Function () As Boolean
ticked = False
seconds = m.ts.TotalSeconds ()
If seconds <> m.prevSeconds
ticked = True
m.prevSeconds = seconds
EndIf
Return ticked
End Function

this.elapsed = Function () As Integer
Return m.ts.TotalSeconds ()
End Function

Return this
End Function

Sub Main ()
timerObj = new_TimerClass ()
timerObj.start ()

port = CreateObject ("roMessagePort")
While True
msg = Wait (100, port)
If timerObj.ticked ()
elapsed = timerObj.elapsed ()
' Update screen component with new elapsed value

Print "New timer tick: "; elapsed
EndIf

End While
End Sub
https://github.com/belltown/
0 Kudos

Re: Timer in BrightScript

Smiley Very Happy thank you beltown , thanx a ton
0 Kudos