Hi Nicholas, I'm happy you like ScuiLib
I've had a look at your code and I've found two problems, the first one is caused by the position in the source code where you define the two functions FncStartTimer() and FncStopTimer().
When you create the gui controls these two functions are not yet defined so actually the functions you are attaching to the button event are Nil and they have no effect.
Just move the functions definition on top, just before starting the gui commands to make them work as expected.
The second problem is caused by the approach you have in the main loop:
Code: Select all
Repeat
scui.Set("LblStopWatch", { Text = { Content = { Values = { GetTimer(1) }, Lines = 1 } } },1)
WaitEvent
Forever
When you hit the button an event is created and the label was updated, but as soon as the code execution reaches again the WaitEvent command the label is no more updated because the program go into an idle state waiting for another event.
To achieve what you want you have encapsulate the label update code into a function and call this function using an interval, this is how it works:
1. You have to define a global variable where you will store the id returned by SetInterval()
2. You have to add the code to start/stop the label update process enabling/disabling the interval updater
3. Finally you have to change a little your main loop code because there is no more need to update the label there
So your modified code should be something like this:
Code: Select all
;External
@INCLUDE "ScuiLib.hws"
;Constants
@DISPLAY {Color = $aaaaaa}
;::: Define a global variable to store the id returned later by SetInterva()
Global intervalID = -1
;Functions
Function FncStartTimer(Tag)
ResetTimer(1)
ResumeTimer(1)
;::: Check if there is an interval defined clear it before starting another one
If GetType(IntervalID) <> #NUMBER
ClearInterval(intervalID)
intervalID = -1
EndIf
;::: Start calling FncUpdateTimer every 10 ms
intervalID = SetInterval(Nil, FncUpdateTimer, 10)
EndFunction
Function FncStopTimer(Tag)
PauseTimer(1)
;::: No more need to update the label since the timer has been stopped, switch
;::: off the update
If GetType(IntervalID) <> #NUMBER
ClearInterval(intervalID)
intervalID = -1
EndIf
EndFunction
;::: This function will be called automatically by SetInterval()
Function FncUpdateTimer()
scui.Set("LblStopWatch", { Text = { Content = { Values = { GetTimer(1) }, Lines = 1 } } },1)
EndFunction
;Controls
BtnStartTimer = scui.NewObject(
#IFOCLASS_BUTTON,
"Start",
{
x = 200,
y = 0
},
{
x = 200,
y = 24
},
Nil,
{
Values = {
"Start"
},
Lines = 1
},
{
OnPushed = FncStartTimer
}
)
BtnStopTimer = scui.NewObject(
#IFOCLASS_BUTTON,
"Stop",
{
x = 400,
y = 0
},
{
x = 200,
y = 24
},
Nil,
{
Values = {
"Stop"
},
Lines = 1
},
{
OnPushed = FncStopTimer
}
)
LblStopWatch = scui.NewObject(
#IFOCLASS_LABEL,
"LblStopWatch",
{
x = 0,
y = 0
},
{
x = 200,
y = 20
},
{
Bevel = {
Colors = {
Fill1 = {
r=255,
g=255,
b=160
}
}
},
Text = {
FontSize = 12 ,
Alignment = #TEXTALIGN_HCENTER
}
}
)
LvLaps = scui.NewObject(
#IFOCLASS_LISTVIEW,
"ListView",
{
x = 0,
y = 32
},
{
x = 640,
y = 512
},
Nil,
{
FieldNames = {
"Lap",
"Time"
},
Sizes = {
40,
30
},
Entries = {
{
"1",
"00:00:00"
}
}
},
{
OnChanged = ButtonID
}
)
;Main script
StartTimer(1)
PauseTimer(1)
;::: Initialize the label with 0
scui.Set("LblStopWatch", { Text = { Content = { Values = { 0 }, Lines = 1 } } },1)
Repeat
;::: no more need to update the label here
WaitEvent
Forever
To check if the intervalID have a valid value or not I've used a trick checking in it's a number or not, if it's a #NUMBER then it's because we have assigned -1 to it, if it's not a number means that it's holding the id returned by SetInterval() which is #LIGHTUSERDATA type.
Have fun