Stopwatch demo

Find quick help here to get you started with Hollywood
Post Reply
User avatar
nicholas.jj.taylor
Posts: 9
Joined: Thu Oct 03, 2013 11:28 am
Location: Harworth and Bircotes, Doncaster, Nottinghamshire, UK

Stopwatch demo

Post by nicholas.jj.taylor »

I'm trying to find my feet with Hollywood and scuilib.

I have a request that would be easy for a Hollywood expert.

  • I want a label, a start button and a stop button.

    I want a timer in the background.

    I want the label to update with the value of the timer every time it ticks.

    the start and stop buttons should start and stop the timer.

    I want it to be no frills as it's for learning purposes.

Being new to Hollywood and Lua, how should I go about this?

Any help is appreciated. Thanks
A1200 + Blizzard MC030@50Mhz 128mb + MC68882 FPU (SCSi kit and +128Mb to come!)
User avatar
nicholas.jj.taylor
Posts: 9
Joined: Thu Oct 03, 2013 11:28 am
Location: Harworth and Bircotes, Doncaster, Nottinghamshire, UK

Re: Stopwatch demo

Post by nicholas.jj.taylor »

I think I may have just about cracked it, but I don't seem to be able to edit the text on a label in scuilib.

The way I'm trying to do this is:

scui.Set("LblStopWatch", { Values = { GetTimer(1) } }, true )

The code compiles fine and the program runs, but the label's text won't change.
A1200 + Blizzard MC030@50Mhz 128mb + MC68882 FPU (SCSi kit and +128Mb to come!)
User avatar
nicholas.jj.taylor
Posts: 9
Joined: Thu Oct 03, 2013 11:28 am
Location: Harworth and Bircotes, Doncaster, Nottinghamshire, UK

Re: Stopwatch demo

Post by nicholas.jj.taylor »

Whoopee! I found the answer!

scui.Set("LblStopWatch", { Text = { Content = { Values = { "A Test " }, Lines = 1 } } },1)

I won't be home tonight, because I have to go to the airport tomorrow, but as soon as I get back I will post my code up here.

What I'm trying to do is to code the Hollywood way early on, to help me later on.

I'm mainly used to coding in .Net and SQL so I don't want to be writing code that goes against the grain of the community, when I hope to contribute many things to the project.

Particularly in the area of nets and comms, as at work I write those automated telephone operator things for making payments on a wide range of services.
A1200 + Blizzard MC030@50Mhz 128mb + MC68882 FPU (SCSi kit and +128Mb to come!)
User avatar
Allanon
Posts: 732
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: Stopwatch demo

Post by Allanon »

Hi nicholas.jj.taylor!
Happy to see you are using ScuiLib and you have found the solution, I'm the one who developed it :)
If you need further help just ask, I'm working on a new version using a more OO approach and soon will be available for download
Here is a screenshot of my current work:
Image
User avatar
nicholas.jj.taylor
Posts: 9
Joined: Thu Oct 03, 2013 11:28 am
Location: Harworth and Bircotes, Doncaster, Nottinghamshire, UK

Re: Stopwatch demo

Post by nicholas.jj.taylor »

Sounds great Allanon,and I'm loving your Scuilib. The controls look really nice.

I'm almost there I think, and I'll post the code I have in a moment.

My current problem is updating the label control once the timer begins.

I currently have a WaitEvent() pause, which updates the label text when I roll the mouse over the controls as expected.

I have also tried Wait(1), but then the buttons stop animating.

What should I be doing instead?

Here is the code. It looks more lengthy than it is:

Code: Select all

;External

@INCLUDE "ScuiLib.hws"



;Constants

@DISPLAY {Color = $aaaaaa}



;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 
	
	} 

)



;Functions

Function FncStartTimer(Tag)
	ResetTimer(1)
	
	ResumeTimer(1)

EndFunction


Function FncStopTimer(Tag)
	PauseTimer(1)

EndFunction



;Main script

StartTimer(1)

PauseTimer(1)

Repeat
	scui.Set("LblStopWatch", { Text = { Content = { Values = { GetTimer(1) }, Lines = 1 } } },1) 	
	
	WaitEvent
	
Forever
Any advice will be greatly appreciated.
A1200 + Blizzard MC030@50Mhz 128mb + MC68882 FPU (SCSi kit and +128Mb to come!)
User avatar
Allanon
Posts: 732
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: Stopwatch demo

Post by Allanon »

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 :)
User avatar
nicholas.jj.taylor
Posts: 9
Joined: Thu Oct 03, 2013 11:28 am
Location: Harworth and Bircotes, Doncaster, Nottinghamshire, UK

Re: Stopwatch demo

Post by nicholas.jj.taylor »

Incredible. That's absolutely perfect.

Thank you so much!
A1200 + Blizzard MC030@50Mhz 128mb + MC68882 FPU (SCSi kit and +128Mb to come!)
Post Reply