All pages
Powered by GitBook
1 of 13

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Input Table Syntax

Overview

The input table defines what values are available as input(s) to mix scripts. There are two forms of input table entries.

  • SOURCE syntax

    { "<name>", SOURCE }

    SOURCE inputs provide the current value of a selected OpenTX variable. The source must set by the user when the mix script is configured. Source can be any value OpenTX knows about (inputs, channels, telemetry values, switches, custom functions,...). Note: typical range is -1024 thru +1024. Simply divide the input value by 10.24 to interpret as a percentage from -100% to +100%.

  • VALUE syntax

    { "<name>", VALUE, <min>, <max>, <default> }

    VALUE inputs provide a constant value that is set by the user when the mix script is configured.

    • name - maximum length of 8 characters

    • min - minimum value of -128

    • max - maximum value of 127

    • default - must be within the valid range specified

  • Maximum of 6 inputs per script (warning : was 8 in 2.1)

Example using a SOURCE and a VALUE

local input =
    {
        { "Strength", SOURCE},                -- user selects source (typically slider or knob)
        { "Interval", VALUE, 0, 100, 0 }    -- interval value, default = 0.
    }

local function run(strength, interval)
    -- variable strength will contain the current slider value
    -- variable interval is set by the user and constant through script lifetime

    -- this script has no return value but may use playFile() to alert user

    return
end

return {input=input, run=run}

io.seek()

Output Table Syntax

Overview

Outputs are only used in mix scripts. The output table defines only name(s), the actual values are determined by the script's run function.

{ "<name1>", "<name2>" }

Example:

local output { "Val1", "Val2" }

local function run()
    return 0, -1024 -- these values will be available in OpenTX as Val1 and Val2
end

return {output=output, run=run}

Notes:

  • Output name is limited to four characters.

  • A maximum of 6 outputs are supported

  • Number Format Outputs are 16 bit signed integers when they leave Lua script and are then divided by 10.24 to produce output value in percent:

Script Return Value

Mix Value in OpenTX

0

0%

996

97.2%

1024

100%

-1024

-100%

Return Statement Syntax

The return statment is the last statement in an OpenTX Lua script. It defines the input/output table values and functions used to run the script.

Parameters init, input and output are optional. If a script doesn't use them, they can be omitted from return statement.

Example without init and output:

local inputs = { { "Aileron", SOURCE }, { "Ail. ratio", VALUE, -100, 100, 0 } }

local function run_func(ail, ratio)
    -- do some stuff
    if (ail > 50) and ( ratio < 40) then
        playFile("foo.wav")    
    end
end

-- script that only uses input and run
return { run=run_func, input=inputs }

Init Function Syntax

If defined, init function is called right after the script is loaded from SD card and begins execution. Init is called only once before the run function is called for the first time.

local function <init_function_name>()
  -- code here runs only once when the model is loaded
end
  • Input Parameters:

    none

  • Return values:

    none

Included Lua Libraries

Lua Standard Libraries

Included

package

-

coroutine

-

table

-

since OpenTX 2.1.0 (with limitations)

os

-

string

since OpenTX 2.1.7

bit32

since OpenTX 2.1.0

math

since OpenTX 2.0.0

debug

-

io

io.write()

io.read()

io Library

Part II - OpenTX Lua API Programming Guide

This section provides more specifics on the OpenTX Lua implementation. Here you will find syntax rules for interface tables and functions.

io.close()

io.open()

Run Function Syntax

The run function is the function that is periodically called for the lifetime of script execution. Syntax of the run function is different between mix scripts and telemetry scripts.

Run Function for Mix Scripts

local function <run_function_name>([first input, [second input], …])

   -- if mix has no return values
   return

   -- if mix has two return values
   return value1, value2

end
  • Input parameters:

    zero or more input values, their names are arbitrary, their meaning and order is defined by the input table. (see Input Table Syntax)

  • Return values:

    • none - if output table is empty (i.e. script has no output)

      values

      • or -

    • comma separated list of output values, their order and meaning is defined by the output table. (see Output Table Syntax)

Run Function for Telemetry Scripts

local function <run_function_name>(key-event)
  return 0 -- values other than zero will halt the script
end
  • Input parameters:

    The key-event parameter indicates which transmitter button has been pressed (see Key Events)

  • Return values:

    A non-zero return value will halt the script