Skip to content

Loops and Events

Joachim Stolberg edited this page Jan 29, 2019 · 17 revisions

The SaferLua Controller distinguishes between the initialization phase (just after the controller was started) and the continuous operational phase, in which the normal code is executed.

Initialization

During the initialization phase the function init() is executed once. The init() function is typically used to initialize variables, e.g. clean the display, or reset other blocks:

-- initialize variables
counter = 1
table = Store()
player_name = "unknown"

# reset nodes
$clear_screen("0123")      -- "0123" is the number of the display
$send_cmnd("2345", "off")  -- turn off the node with the number "2345"

Functions

This tab is used to define Lua functions, cyclically called from the loop() function:

function foo(a, b)
    return a + b
end

Loops

During the continuous operational phase the loop() function is typically called every second. Code witch should be executed cyclically has to be placed here. The cycle frequency is per default once per second, but can be changed via:

$loopcycle(0)   -- no loop cyle any more
$loopcycle(1)   -- call the loop function every second
$loopcycle(10)  -- call the loop function only every 10 seconds

The provided number must be an integer value. The cycle frequency can be changed in the init() function, but also in the loop() function.

Events

You have the possibility to react directly on a received commands, the SaferLua Controller supports events. Events are usually turned off, but can be activated with the command events():

$events(true)    -- enable events
$events(false)   -- disable events

If an event occurs (a command was received from another node), the loop() is executed (in addition to the normal loop cycle). In this case the system variable event is set:

if event then
    -- event has occurred
    if $input("3456") == "on" then  -- check input from node with the number "3456"
        -- do some action...
    end
end

The first occurred event will directly processed, further events may be delayed. The SaferLua Controller allows a maximum of one event per second.