Showing system date and time with Lua on Lintouch

From LintouchWiki

Jump to: navigation, search

Showing system date and time with Lua on Lintouch
Nelson Mambre

This tutorial explains how to use Lua in Lintouch to show the date and time of the operating system. (based on Getting start with Lua on Lintouch and Tutorial from Lintouch [1])

Start a new project (Project -> New) Change the combobox to Qt Technical Widgets, and select the ActiveText and then Analog Clock component. Leave the default properties for the moments.

Now create the file clock.lua with this content:

--get system time
while should_run() do
   
    t=os.date('*t')

    -- send to lintouch the new time
    set_real_value( variables.hrs, t.hour )
	set_real_value( variables.mins, t.min )
	set_real_value( variables.secs, t.sec )
	set_real_value( variables.yr, t.year )
	set_real_value( variables.mth, t.month )
	set_real_value( variables.dy, t.day )
   
   -- sync results back to lintouch
   sync_real_values()

   sleep( 0.01 * 1000 * 1000 )

end

Add the file clock.lua to project's resources, to do this, click right mouse button on Form then Dialogs -> Resources, click New button and locate clock.lua.

To create a Lua connection and variables to use with it, right click on Form then Dialogs -> Connections, click New and on Connection Type select Lua. You need inform the script name, just write clock.lua. To change the connection name, press F2 and change to, for example, sysclock.

Then create 6 variables: hrs, mins, secs, dy, mth, yr of type number, these variables will contain the variables of the Lua script with the system time.

To create these variables, click right mouse button on form the Dialogs -> Variables, click New, change type to number, press OK. This variable name will be Variable1, press F2 and change the name to hrs. Repeat this process for the other 5 variables.

After creating the variables, link them to the components: double click on Analog Clock component, on Inputs select sysclock as the connection and set the Hours, Minutes and Seconds with the corresponding variables. Click OK to save. Double click on Active Text component, select Apearance, and in Text put the following string "%1 / %2 / %3" (without the double quotes). Then select Inputs, change Position 1, 2 and 3 with the variables to show the date. For example, for dd/mm/yyyy format, put dy in Position 1, mth in Position 2 and yr in Position 3. Click OK to save.

Save the project.

Click on PLAY button (Go Online) or right click, Tools--> Online to test it.

You can change the Lua script to directly get a formatted date and/or time string. You can also use it to get the day of the week and of the year.

Personal tools