Getting start with Lua on Lintouch

From LintouchWiki

Jump to: navigation, search

Autor: Acassis

This tutorial explains how to work with Lua on Lintouch.

We will create a Meter controlled by two button: UP and DOWN, user will use UP button to increase Meter's level and DOWN do decrease it.

Start a new project (Project -> New)
Change the combobox to Display, and select Meter component. Change the property Direction to vertical (Double click on Meter -> Appearance -> Direction).

Change the combobox to Input, select Button component. Change the property Label to UP. Place a new button on form, change its Label to DOWN.

Now create the file meter.lua with this content:

level=0

while should_run() do

    -- lock requested values
    lock_requested_values()

    -- if user pressed UP button
    if get_requested_value( variables.up ) then
        if(level < 100) then
            level=level+1
        end
    end

    -- if user pressed DOWN button
    if get_requested_value( variables.down ) then
        if(level > 0) then
            level=level-1
        end
    end

    -- send to lintouch the new level
    set_real_value( variables.level, level )

   -- clean dirty flag on requested values
   sync_requested_values()
   unlock_requested_values()

   -- sync results back to the user
   sync_real_values()

   sleep( 0.01 * 1000 * 1000 )

end

We need add this file (meter.lua) to project's resources, to it click right mouse button on Form then Dialogs -> Resources, click New button and locate your meter.lua.

All right, now we need create a Lua's connection and variables to use with it. Click right mouse on Form then Dialogs -> Connections, click New and on Connection Type select Lua. You need inform the script name, just write meter.lua.

Then we need create three variables: up of type bit, this variable will be true every time user pressed UP button, down also type bit, will be true every time user pressed DOWN button, and level of type number, this variable will be increased or decreased by our Lua script every time user pressed any button.

To create these variables, click right mouse button on form the Dialogs -> Variables, click New, keep type bit, press OK. This variable name will be Variable1, press F2 and change it name up. Repeat this process to down variable. To level variable you need change the type from bit to number.

Once variables was created, go to say to components to use it: double click on UP button, on Outputs select Connection1 and up, do the same on DOWN button, just choose 'Connection1 and down. To Meter component, on Inputs choose Connection1 and level.

Save the project.

Click on PLAY button (Go Online), test it.

Congratulations, you do your first Lua script to control Meter component.

Personal tools