Minimal code examples – LUA & Python

<< Click to Display Table of Contents >>

Navigation:  Components > Control, logic & time > Script IDE (LUA / Python) >

Minimal code examples – LUA & Python

These examples show correct usage of the Script IDE including debug output via #DEBUG# and processing of channel arrays.
 
Channel Editor assumption:
- Inputs: Input[0] … Input[7]
- Outputs: Output[0] … Output[3]

Example logic

The first four inputs are mapped to the four outputs. Additionally, the sum of Input[0]…Input[3] is calculated and written to Output[0]. Debug messages are printed via #DEBUG#.

LUA – ScriptFunc()

Notes:
- EisBaer channel arrays are zero-based.
- Input_count / Output_count are provided automatically.
- Debug output via eisbaerComponent:SetOutputSpFromLua("#DEBUG#", text).

function ScriptFunc()
 
   -- Debug: start
   eisbaerComponent:SetOutputSpFromLua("#DEBUG#", "LUA: Script start")
 
   -- Sanity check (at least 4 inputs & 4 outputs)
   if Input_count < 4 or Output_count < 4 then
       ScriptFehler = 1
       Diagnose = "Error: Input_count/Output_count too small"
       eisbaerComponent:SetOutputSpFromLua("#DEBUG#", "LUA: ERROR - count too small")
       return
   end
 
   ScriptFehler = 0
   Diagnose = ""
 
   sum = 0
   for i = 0, 3 do
       -- current value (optional explicit read)
       currentValue = eisbaerComponent:ReadInputSpFromLua("Input", i)
 
       if currentValue == nil then
           ScriptFehler = 1
           Diagnose = "Error: Input[" .. tostring(i) .. "] is nil"
           eisbaerComponent:SetOutputSpFromLua("#DEBUG#", "LUA: ERROR - Input[" .. tostring(i) .. "] nil")
           return
       end
 
       sum = sum + currentValue
       Output[i] = currentValue  -- 1:1 mapping
   end
 
   Output[0] = sum
   Diagnose = "OK: Sum(Input0..3)=" .. tostring(sum)
   eisbaerComponent:SetOutputSpFromLua("#DEBUG#", "LUA: OK - sum=" .. tostring(sum))
 
end
 

Python – ScriptFunc()

Notes:
- EisBaer channel arrays are zero-based.
- Debug output via Eisbaer_SetOutputSp("#DEBUG#", text, True).

def ScriptFunc():
   global Input, Output, Input_count, Output_count, ScriptFehler, Diagnose
 
   # Debug: start
   Eisbaer_SetOutputSp("#DEBUG#", "PY: Script start", True)
 
   if Input_count < 4 or Output_count < 4:
       ScriptFehler = 1
       Diagnose = "Error: Input_count/Output_count too small"
       Eisbaer_SetOutputSp("#DEBUG#", "PY: ERROR - count too small", True)
       return
 
   ScriptFehler = 0
   Diagnose = ""
 
   s = 0
   for i in range(4):
       # current value (optional explicit read)
       currentValue = Eisbaer_ReadInputSp("Input", i)
 
       if currentValue is None:
           ScriptFehler = 1
           Diagnose = f"Error: Input[{i}] is None"
           Eisbaer_SetOutputSp("#DEBUG#", f"PY: ERROR - Input[{i}] None", True)
           return
 
       s += currentValue
       Output[i] = currentValue  # 1:1 mapping
 
   Output[0] = s
   Diagnose = f"OK: Sum(Input0..3)={s}"
   Eisbaer_SetOutputSp("#DEBUG#", f"PY: OK - sum={s}", True)
 

Short notes

- Use #DEBUG_VERBOSE# for permanent verbose debugging.
- Use diagnostics sparingly (performance).
- With many triggers: enable processing delay + trigger buffering.