Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Lua is a small but powerful language. This section will explain a few of the most important concepts.
Lua was chosen for EdgeTX, because it is a small language designed to be highly extensible by libraries written in C and C++, so it can be integrated into other systems like EdgeTX. It is also relatively efficient, both in terms of memory and CPU usage, and hence well suited for the radios.
In addition to the provided libraries, Lua has a very elegant mechanism for loading new Lua code modules during run-time. A lot of the elegance comes from the way that the loading mechanism meshes with another concept supported by Lua: first class functions with closures.
Computer science pioneer Christopher Strachey introduced the concept of functions as first-class objects in his paper Fundamental Concepts in Programming Languages from 1967. What it means is that functions can be treated as other variables: as arguments passed in function calls, as results returned from function calls, and a function identifier can be re-assigned to another chunk of function code, just like a variable ca be assigned to a new value.
In Lua, a function declaration is really "syntactic sugar" for assigning a variable to the chunk of code that is called when the function is invoked, i.e.
local function f(x) return x + 1 end
is the same as
local f = function(x) return x + 1 end
You can even add functions to Lua tables, i.e.
t = { f = f }
will add the above function to the table t
, as a field that is also called f
. Does that look familiar to the return statement required at the end of a Lua script?
Yes indeed, because a script is really an anonymous function that returns a list of functions to the system. The function declarations assign variables to chunks of function code, these variables are added to the list returned at the end of the script, and the system then calls the functions periodically to run the script. So the script itself is run one time initially, and subsequently the functions returned by the last statement are called periodically.
Another important concept that goes with first-class functions, is closures. This is the environment of the function with the variable names that the function can see. Please consider the following function counter
that returns another function:
The function is returned directly without being assigned to a variable name. The closure of the function returned is the body of the function counter
, containing both the arguments start
and step
, and the local variable x
. So if c1 = counter(1, 1)
then c1()
will return 1, 2, 3, ... when called repeatedly, and if c2 = counter(2, 3)
then c2()
will return 2, 5, 8, ...
Likewise, the local variables that you declare outside the functions of your script can be used by all of the functions in your script, and they persist between function calls, but they are not visible to other scripts.
The widget scripts are a little trickier, as you can register multiple instances of the same widget script, and all of these instances run within the same Lua closure. Therefore, local variables declared outside any functions in a widget script are shared among all of the instances of that script. But each call to the create(...)
function returns a new widget
list to the system. And since this list is unique to each instance, you can add private instance variables to it.
Please consider this function:
It takes an argument x
and a vararg list ...
A vararg list is just like a list of arguments separated by commas, and you can call the function with any number of arguments greater than 1. Inside the function, you can also treat ...
like a comma separated list of variables, e.g. in the above function, ...
is converted to a table by {...}
just like you could construct a table by e.g. {a, b, c}
. The table is iterated with the ipairs
function to look for an element matching the first argument x
. So e.g. the following statement
if event == EVT_VIRTUAL_ENTER or event == EVT_VIRTUAL_EXIT then
can be replaced by
if match(event, EVT_VIRTUAL_ENTER, EVT_VIRTUAL_EXIT) then
You can also use ...
directly as a comma separated list of values, e.g. local a, b, c = ...
will assign the three variables to the three first arguments following x
, or nil
if none are given.
This section covers various topics of LUA implementation in EdgeTX operating system system
Topic | Description |
---|---|
LUA Basics
Brief introduction to LUA language
LUA Version and included libraries
Dut to memory limits not all standard LUA libraries are included in EdgeTX embedded version of LUA interpreter & compiler.
Script Types
EdgeTX allows to use various LUA scripts for different purposes. Here you can check which one suits your needs.
This section introduces the types of Lua scripts supported by EdgeTX and how they may be used.
EdgeTX 2.10 uses LUA interpreter and compiler version 5.2 For detailed reference read Lua 5.2 Reference Manual
Lua Standard Libraries | Comment |
---|---|
included
included
included
included only on color LCD Radios
included partialy
package
not included
coroutine
not included
os
not included
debug
not included
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:
Parameters none
Return values none
Parameters none
Return Values none
Parameters none
Return Values none\
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)
One-time Script 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:
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:
The name
length must be 10 characters or less.
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.
Parameters
Return values
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.
Parameters
Return values none
Parameters
Return values none
Parameters
Return values none
if you want background
function to run when the widget is visible, then call it from refresh function.
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:
Parameters
Return values
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
EdgeTX internal Source List contains every source available on particular radio model.
sourceListName | Type | Description |
---|
Particular Source List ID is a integer number from 1 to whatever last source entry is used by EdgeTX developers during firmware implementation. Different Radios have different number of physical inputs that affects this list. Therefore for the same source type (ie LS04 - Logical switch number 4) sourceListID can have different value on particular radio model
To avoid confusion wherever there is reference to Source List ID short-name sourceListID is used.
To avoid confusion wherever there is reference to Source List Name short-name sourceListName is used.
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:
Field | Type | Required | Desctiption |
---|
Parameters
Return values none
Parameters none
Return Values none
Parameters none
Return Values none
Field | Type | Required | Desctiption |
---|---|---|---|
Field | Type | Required | Desctiption |
---|---|---|---|
Field | Type | Required | Desctiption |
---|---|---|---|
table item | type | required |
---|---|---|
table item | type | required |
---|---|---|
table item | type | required |
---|---|---|
table item | type | required |
---|---|---|
Field | Type | Required | Desctiption |
---|---|---|---|
Field | Type | Required | Desctiption |
---|---|---|---|
Field | Type | Required | Desctiption |
---|---|---|---|
Field | Type | Required | Desctiption |
---|---|---|---|
Field | Type | Required | Desctiption |
---|---|---|---|
Field | Type | Required | Desctiption |
---|---|---|---|
Field | Type | Required | Desctiption |
---|
Field | Type | Required | Desctiption |
---|
Manufacturer | Model | introduced in EdgeTX |
---|---|---|
Manufacturer | Model | introduced in EdgeTX |
---|---|---|
Source List name can also change due to firmware changes during development or user configuration (ie. assigning custom name to input). In current version of EdgeTX firmware Source List Name also may contain special visual symbols to indicate type source (see ).
Field | Type | Required | Desctiption |
---|
Field | Type | Required | Desctiption |
---|
RadioMaster
TX12
2.4
RadioMaster
T8 Pro
2.4
RadioMaster
Zorro
2.6
RadioMaster
TX12 Mark II
2.8
RadioMaster
Boxer
2.8.1
RadioMaster
MT12
2.10
RadioMaster
2.10
Jumper
T-Lite / T-Lite v2
2.4
Jumper
T-12 / T12 Pro / T12 Pro Hall
2.4
Jumper
T-Pro
2.6
Jumper
T-14
2.10
Jumper
T-20
2.10
FrSky
QX7 / QX7S / QX7 ACCESS / QX7S ACCESS
2.4
FrSky
X9 Lite / X9 Lite S
2.4
FrSky
X9E / X9E Hall
2.4
FrSky
X-Lite / X-Lite S / X-Lite Pro
2.4
iFlight
Commando8
2.8
Betaflight
LireRadio3
2.8
FrSky
X9D / X9D+ / X9D SE
2.4
FrSky
X9D+2019 / X9D+2019 SE
2.4
Flysky
NV14
2.5
Flysky
EL18
2.8
RadioMaster
TX16S / TX16S MAX / TX16S Mark II
2.4
Jumper
T16 / T16 Plus / T16 Pro Hall
2.4
Jumper
T18 / T18 Lite / T18 Pro
2.4
Eachine
TX16S
2.4
FrSky
X10 / X10S / X10S Express / X10S Express
2.4
FrSky
X12S / X12S IRSM
2.4
run
function
This function is called periodicaly when switch assiociated the Special Function is ON.
init
function
This function is called once when Function script is loaded and executed for the first time
background
function
This function is called periodicaly when switch assiociated the Special Function is OFF
run
function
yes
init
function
no
input
table
yes
output
table
yes
Name
TX16S
MCU
STM32F7
LCD type
color TFT or IPS
LCD size
480x320
LCD color depth
16 bit - RGB565
Internal module
Multi-module, ELRS
External Module Bay
Yes (JR micro)
LCD_W
480
LCD_H
320
memory for LUA
1MB
name
string
This variable holds a name that is displayed to user as Widget scripts name in available Widgets list.
options
table
Options table is to store Widget's options available to EdgeTX user via Widget's Settings menu. To see valid options read Widget Options Constants.
create
function
function is called once when the widget instance is registered (started).
zone table
This parameter will hold visible dimensions of Widget (height & width)
options table
Initial options table as described above
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
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.
widget table
Widget's table returned by create
function, decribed above.
options table
Initial options table as described above
background
function
This function is called periodically when the widget instance is NOT VISIBLE.
widget table
Widget's table returned by create
function, decribed above.
refresh
function
This function is called periodically when the widget instance IS VISIBLE.
widget table
Widget's table returned by create
function, decribed above.
event number
When Widget Script is in full screen mode, then event
is either 0, a key event value, or a touch event value.
When the widget is not in full screen mode, then event
is nil
See Key Events.
touchState table
This parameter is only present when radio is equiped with touch interface and event
is a touch event.
If event
is a touch event value, then touchState
is a table. Otherwise, it is nil
.
When the widget is not in full screen mode then touchState
is nil
See Touch State Events.
I[01-32] | Inputs | Input value |
LUA[1-7][a-f] | Mixes Scripts | Mixes (custom) scripts value |
Rud,Ele,Thr,Ail | Sticks | Stick position value |
S[1-x] | Potentiometers | Potentiometer value |
LS,RS | Sliders | Left or right slider value |
CYC[1-3] | Cyclic | Cyclic (heli) value |
T[1-x] | Trims | Trim switch value |
S[A-J] | Switches | Physical switch value |
L[01-64] | Logical Switches | Logical switch value |
TR[1-16] | Trainer input | Trainer input value |
CH[01-32] | Mixer channel | Mixer channel value |
GV[1-9] | Global variables | Global variable value |
Batt | Radio data | Radio battery level |
Time | Radio data | Radio time |
GPS | Radio data | Radio internal GPS data |
Tmr[1-3] | Timers | Value of timer |
Tele[1-64] | Telemetry | Current value of telemetry sensor |
Tele[1-64]+ | Telemetry | Maximum value of telemetry sensor |
Tele[1-64]- | Telemetry | Minimum value of telemetry sensor |
run | function | Function is called periodicaly when script is running. |
exit multi type |
|
init | function | This function is called once when script is executed |
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. |
event number |
touchState table |
event number |
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 ).
Used to indicates which radio key has been pressed (see )