-
Notifications
You must be signed in to change notification settings - Fork 0
Lua Script Reference
- Lua General Functions general purpose functions
- Lua Model Functions model manipulation functions
- Lua Display Functions LCD display functions
All Lua scripts in OpenTX exist in same Lua environment. This means, that they share global functions and variables. Using global variable with the same name from two different scripts could lead to unpredictable results. Therefore the use of global variables and functions should be avoided!
TODO: how to share data between scripts
Even variables defined inside local functions without local keyword are global. For example if we have two scripts, script1:
local function run()
global_var = 7
local local_var = "foo"
end
and script2:
local function run()
if global_var == 7 then
print("Whoops")
end
if local_var == "foo" then
print("We don't get here, local_var is nil")
end
end
If we execute both scripts, we would see a printout "Whoops" (note: OpenTX currently does not support print function, this is just an example). This means that variable global_var is actually global even if it is first defined in some local function.
###Local variables Script can have any number of local variables, their value is preserved between each call to run function. They are defined as:
local simple_number = 4
local some_array = {1, 2, 120}
Local variables are only visible to the script that defined them. Two scripts can define a local variable with the same name. These two variables don't share anything, each script has his own instance of variable. In other words, scripts can't share local variables.
On the other hand variables local_var are separate for each script and don't share theri value (they only happen to have the same name).
Script can have any number of local functions:
local function some_function(a, b, c)
local value1 = a + b * c
return value1
end
Input are only used in model scripts. However same number format is used when using function getValue().
Inputs are analogue values from opentTX that converted to 16 bit signed integers before they reach Lua script.
Analogue values such as sticks and sliders have value multiplied by 10. Examples:
aileron stick value | input value to script |
---|---|
0 | 0 |
60.5 | 650 |
-100.0 | -1000 |
Switches (real and logical) are represented as:
switch position | input value to script |
---|---|
down | -1000 |
middle | 0 |
up | 1000 |
Telemetry values TODO
telemetry value | input value to script |
---|---|
altitude 120.5m | TODO |
A1 voltage 5.47V | TODO |
consumption 1260mAh | TODO |
There are two kinds of inputs:
- SOURCE
- VALUE
Source type provides current value of selected OpenTX variable (stick position, slider, channel). User assigns assigns actual source for this input in Custom script menu. Source can be any value OpenTX knows about (inputs, channels, telemetry values, switches, custom functions,...).
Syntax: { name, SOURCE }
Example: { "Aileron", SOURCE }
Defines SOURCE type input with name Aileron. Name length is limited to TODO.
Value type provides constant value that user sets in Custom script menu
Syntax: { name, VALUE, min, max, default }
Example: { "Ratio", VALUE, -100, 100, 0 }
Defines VALUE input with name Ratio that has limits -100 and 100 and default value of 0. Name lengths is limited to TODO.
Outputs are only used in model scripts.
Outputs are 16 bit signed integers when they leave Lua script and are then divided by 10 to produce output value. Examples:
output from script | output as seen from OpenTX |
---|---|
0 | 0.0 |
996 | 96.6 |
-1256 | -125.6 |