Function scripts are invoked via EdgeTX 'Lua Script' of Special Functions configuration page.
Typical uses of Function scripts are:
specialized handling in response to switch position changes
customized announcements
Function script is loded when model is selected. Script executes until
it misbehaves (e.g. run-time error or low memory)
One-time Script is running. When One-time script finishes execution, Wigdet Script resumes execution.
Function scripts DO NOT HAVE ACCESS TO LCD DISPLAY
Function scripts are located on the SD card in the folder /SCRIPTS/FUNCTIONS/
File name length (without extension) must be 6 characters or less
Every Function script must include a return
statement at the end, defining its interface to EdgeTX.
This statement returns a table with the following fields:
run
function
This function is called periodicaly when switch assiociated the Special Function is ON.
Parameters none
Return values none
init
function
This function is called once when Function script is loaded and executed for the first time
Parameters none
Return Values none
background
function
This function is called periodicaly when switch assiociated the Special Function is OFF
Parameters none
Return Values none\
This section introduces the types of Lua scripts supported by EdgeTX and how they may be used.
One-time scripts are used mainly to perform tasks that are not connected with flying. In example ELRS configuration LUA script. They do their task and are then terminated (by user or function) and unloaded.
Script is executed when user selects "Execute" on a script file from SD card browser screen, or opens a Lua Tool, or creates a new model with a Wizard script.
The script executes until:
it returns value different from 0 (except returning string that contains new script name to be executed)
is forcefully closed by user by long press of EXIT key
is forcefully closed by system if it misbehaves (e.g. run-time error or low
memory)
Running a One-Time script will suspend execution of all other currently loaded Lua scripts (Custom, Telemetry, and Functions). They are automatically restarted once the One-Time script is finished. This is done to provide enough system resources to execute the One-Time script.
One-Time Scripts can be placed anywhere on SD card, however, the folder /SCRIPTS/ is recommended.
If One-Time Script is placed in special folder /SCRIPTS/TOOLS it will be visible in EdgeTX RADIO>TOOLS tab
To give this One-Time Script unique name place at the beginning of lua script line:
-- toolName = "TNS|ScriptName|TNE
Otherwise script's filename will be used to display in RADIO>TOOLS list.
Wizard scripts must be stored in the same subfolder of /TEMPLATES/ with the same "first name" as the template file using it. Some Wizard scripts are just small scripts that load one of the common scripts located in /SCRIPTS/WIZARD/.
Every script must include a return
statement at the end, defining its interface to EdgeTX. This statement returns a table with the following fields:
run
function
Function is called periodicaly when script is running.
Parameters
event number
touchState table
Return values
exit multi type
if exit
value is 0 (zero) script will continue to run
if exit
value is non-zero script will be halted.
If exit
value is a text string with the file path to a new Lua script, then the new script will be loaded and run.
init
function
This function is called once when script is executed
Parameters none
Return Values none
Simplest one-time LUA script
Because 0 is returned all the time this script will continue running until user long press EXIT (RTN) key.
One-Time LUA script with initialization and exit feature if user short press and release EXIT (RTN) key
Widget scripts are avaiable on radios equiped with color LCD. They are designed to run constantly in the background performinfg various task. Widget scripts are mostly used to extend EdgeTX functionality via Widgets that are places by user on Main Views. They are equivalent of Telemetry Scripts on radios equiped with B&W LCD.
Most of the time, widget scripts show some info in a Widget's zone in one of the user defined Main Views. They cannot receive direct input from the user via key events with exeption of being displayed in so called Full Screen mode. Full screen mode can be entered by selecting the widget, pressing ENTER and selecting Full screen from the widget's contextual menu, or by double tapping the widget on radios with a touch screen. Full screen mode can be exited by long pressing the EXIT (RTN) button, or by calling the Lua function lcd.exitFullScreen()
.
Each model can have up to nine Main Views, with up to 8 widgets per screen, depending on their size and layout. Each instance of a widget has his own options table.
Widget scripts are only available on radios with color LCD screens, such as e.g. FrSky X10 or X12, Radiomaster TX16S, Jumper T16 or T18, Flysky NV14., etc. Read more about radios.
All widget scripts on the SD card are loaded into memory when the model is selected, even widgets that are not used. This has the side effect that any global functions defined in a widget script will always be available to other widget scripts. It also means that any Widget Script placed in proper location on the SD card will consume part of the radio's memory - even if it is not being used.
It is important to either keep Widget Scripts small, or to use Lua's loadScript() function to load code dynamically
Script executes until:
it misbehaves (e.g. too long runtime, run-time error, or low memory)
One-Time script is running. When One-time script finishes execution, Wigdet Script resumes execution.
Widget scripts are located on the SD card, each one in their specific folder: /WIDGETS/<folder name>/
Widget script folder name length must be 8 characters or less
Widget script name is constant and has to be named main.lua
Example of proper Widget script placement to be registered by EdgeTX as valid Widget script available to user in Widgets selection menu: /WIDGETS/MYWGT/main.lua
Try to use unique folder name. In case of naming clash, previously installed widget will be overwritten.
Every Widget Script must include a return
statement at the end, defining its interface to EdgeTX. This statement returns a table with the following fields:
name
string
This variable holds a name that is displayed to user as Widget scripts name in available Widgets list.
The name
length must be 10 characters or less.
options
table
options
table is passed to create
function when invoked and then stored in Lua. Changing options table values while Widget script is running has no effect. This table is designed to be changed with EdgeTX system menus.
If options
is changed by the user in the Widget Settings menu, then update
will be called with a new options
table, unaffected by any changes made by Lua code to the old options
table.
create
function
function is called once when the widget instance is registered (started).
Parameters
zone table
This parameter will hold visible dimensions of Widget (height & width)
options table
Initial options table as described above
Return values
widget table
Create function will return table that has to be later passed to update
, background
& refresh
functions allowing to access widget's unique variables
The size of the widget's zone area is as follows:
Full screen mode: LCD_W
by LCD_H
Not full screen mode: zone.w
by zone.h
(updated if screen options are changed)
If local variables are declared outside functions in the widget script, then they are shared between all instances of the widget. Therefore, local variables that are private for each instance should be added to the widget
table in the create
function before returning the widget
table to EdgeTX.
update
function
This function is called when Widget's Settings are changed by the user. It is mostly used to modify Widget Script variables or behaviour basing on options values entered by user.
Parameters
widget table
Widget's table returned by create
function, decribed above.
options table
Initial options table as described above
Return values none
background
function
This function is called periodically when the widget instance is NOT VISIBLE.
Parameters
widget table
Widget's table returned by create
function, decribed above.
Return values none
refresh
function
This function is called periodically when the widget instance IS VISIBLE.
Parameters
widget table
Widget's table returned by create
function, decribed above.
event number
When the widget is not in full screen mode, then event
is nil
touchState table
This parameter is only present when radio is equiped with touch interface and event
is a touch event.
When the widget is not in full screen mode then touchState
is nil
Return values none
if you want background
function to run when the widget is visible, then call it from refresh function.
Although they are named "Telemetry scripts" in fact they can be used to perform constant task while running in background. Telemetry scripts are mostly used for building customized screens that are avalilable to user directly from main screen using key shortcut. Each model can have up to three active scripts as configured on the model's Telemetry configuration page. The same script can be assigned to multiple models.
Telemetry scripts are only available on radios with B&W LCD screens, such as e.g. FrSky Taranis models (including Xlite), Radiomaster TX12, Zorro, Boxer or Jumper T12. Read more about <radios>.
Telemetry script is loaded and executed when model is selected. Script executes until:
it misbehaves (e.g. run-time error or low memory)
is running. When One-time script finishes execution, Wigdet Script resumes execution.
Telemetry scripts are located on the SD card in the folder /SCRIPTS/TELEMETRY/.
Telemetry script file name length (without extension) must be 6 characters or less
Every Telemetry script must include a return
statement at the end, defining its interface to EdgeTX.
This statement returns a table with the following fields:
Parameters
Return values none
Parameters none
Return Values none
Parameters none
Return Values none
Used to indicates which radio key has been pressed (see )
This parameter is only present when radio is equiped with touch interface and event
is a touch event (see ).
Options table is to store Widget's options available to EdgeTX user via Widget's Settings menu. To see valid options read .
When Widget Script is in full screen mode, then event
is either 0, a , or a .
See .
If event
is a , then touchState
is a table. Otherwise, it is nil
.
See .
event number
Used to indicates which radio key has been pressed (see Key Events)
init
function
This function is called once when script is executed
background
function
This function is called periodically, both when the telemetry screen is visible and when it is not
run
function
Function is called periodicaly when when telemetry screen is visible.
Mixes Scripts take one or more values as inputs, do some processing in Lua code, and output one or more values. Each model can have several Mixes Scripts associated with it, and these scripts are run periodically. They behave similarly to standard EdgeTX mixers, but at the same time they provide a much more flexible and powerful tool.
Typical use cases:
replacement for complex mixes that are not critical to model function
complex processing of inputs and reaction to their current state and/or their history
filtering of telemetry values
Mixes script is loded when model is selected. Script executes until
it misbehaves (e.g. run-time error or low memory)
is running. When One-time script finishes execution, Wigdet Script resumes execution.
If the script output is used as a mixer source
, and the script is killed for whatever reason, then the whole mixer line is disabled!
Mixes Scripts should be as short as possible, to avoid delays. It is also important to keep in mind that other loaded Telemetry and Function scripts can add to the response time, or worse: hang the system!
To enable Mixes Scripts, firmware must be compiled with the option LUA_MIXER=Y
.
Mixes scripts are located on SD card in folder /SCRIPTS/MIXES/
File name length (without extension) must be 6 characters or less
Every Mixes Script must include a return
statement at the end, defining its interface to EdgeTX. This statement returns a table with the following fields:
This function is called periodicaly when script is running.
Parameters
Variables matching those used in input
table
Return values
Variables matching those used in output
table\
This function is called once when Mixes Script is loaded and executed for the first time
Parameters none Return values none\
The input table defines what values are available as input(s) to custom scripts.
There are two forms of input table entries
SOURCE
SOURCE inputs provide the current value of a selected OpenTX variable. The source must be selected by the user when the script is configured. Source can be any value that EdgeTX knows about (inputs, channels, telemetry values, switches, custom functions etc.). Note: the typical input range is -1024 thru +1024. Simply divide the input value by 10.24 to convert to a percentage from -100% to +100%.
VALUE
VALUE inputs provide a constant value that is set by the user when the 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
The output table defines only name(s), as the actual values are returned by the script's run function.
Above names are only visible as source values on the radio screen when the script is running. If the model is edited in Companion, then the values show as LUA1a
and LUA1b
etc.
Example of <decribe what it does>.
Example where Lua mix script is used to drive ailerons in some clever way, but control falls back to the standard aileron mix if script is killed. Second mixer line replaces the first one when the script is alive:
run
function
yes
init
function
no
input
table
yes
output
table
yes