LUA Reference Guide
2.9
2.9
  • EdgeTX 2.9 Lua Reference Guide
  • Introduction
    • Acknowledgments
    • Getting Started
  • Script Type Overview
    • Custom (Mixer) Scripts
    • Telemetry Scripts
    • One-Time Scripts
    • Function Scripts
    • Widget Scripts
  • EdgeTX Lua API Programming Guide
    • Lua Basics
    • Loading Code Modules Dynamically
    • Saving Memory
    • Using Key and Touch Events
    • Drawing Flags and Colors
    • Data Exchange with the EdgeTX Model Setup
    • Included Lua Libraries
      • io Library
        • io.open()
        • io.close()
        • io.read()
        • io.write()
        • io.seek()
  • EdgeTX Lua API Reference
    • Constants
      • Key Event Constants
      • Touch Event Constants
      • Flags and Pattern Constants
      • Color Constants
      • Special Character Constants
      • Screen Constants
      • Logical Switch Function Constants
      • Special Function Constants
      • Widget Options Constants
    • General Functions
      • GREY()
      • accessTelemetryPush()
      • chdir(directory)
      • crossfireTelemetryPop()
      • crossfireTelemetryPush()
      • defaultChannel(stick)
      • defaultStick(channel)
      • flushAudio()
      • getAvailableMemory()
      • getDateTime()
      • getFieldInfo(source)
      • getFlightMode(mode)
      • getGeneralSettings()
      • getGlobalTimer()
      • getLogicalSwitchValue(id)
      • getOutputValue(outputIndex)
      • getRAS()
      • getRSSI()
      • getRotEncMode()
      • getRotEncSpeed()
      • getRtcTime()
      • getShmVar(id)
      • getSourceIndex(sourceName)
      • getSourceName(sourceIndex)
      • getSourceValue(source)
      • getSwitchIndex(positionName)
      • getSwitchName(switchIndex)
      • getSwitchValue(switchIndex)
      • getTime()
      • getTxGPS()
      • getUsage()
      • getValue(source)
      • getVersion()
      • ghostTelemetryPop()
      • ghostTelemetryPush()
      • killEvents(key)
      • loadScript(file [, mode], [,env])
      • multiBuffer(address[,value])
      • playDuration(duration [, hourFormat])
      • playFile(name)
      • playHaptic(duration, pause [, flags])
      • playNumber(value, unit [, attributes])
      • playTone(frequency, duration, pause [, flags [, freqIncr]])
      • popupConfirmation(title, message, event)
      • popupInput(title, event, input, min, max)
      • popupWarning(title, event)
      • resetGlobalTimer([type])
      • serialGetPower(port_nr)
      • serialRead([num])
      • serialSetPower(port_nr, value)
      • serialWrite(str)
      • setSerialBaudrate(baudrate)
      • setShmVar(id, value)
      • setStickySwitch(id, value)
      • setTelemetryValue(id, subID, instance, value [, unit [, precision [, name]]])
      • sources([first[, last]])
      • switches([first[, last]])
      • sportTelemetryPop()
      • sportTelemetryPush()
    • Model Functions
      • model.defaultInputs()
      • model.deleteFlightModes()
      • model.deleteInput(input, line)
      • model.deleteInputs()
      • model.deleteMix(channel, line)
      • model.deleteMixes()
      • model.getCurve(curve)
      • model.getCustomFunction(function)
      • model.getFlightMode(index)
      • model.getGlobalVariable(index, flight_mode)
      • model.getInfo()
      • model.getInput(input, line)
      • model.getInputsCount(input)
      • model.getLogicalSwitch(switch)
      • model.getMix(channel, line)
      • model.getMixesCount(channel)
      • model.getModule(index)
      • model.getOutput(index)
      • model.getSensor(sensor)
      • model.getSwashRing()
      • model.getTimer(timer)
      • model.insertInput(input, line, value)
      • model.insertMix(channel, line, value)
      • model.resetSensor(sensor)
      • model.resetTimer(timer)
      • model.setCurve(curve, params)
      • model.setCustomFunction(function, value)
      • model.setFlightMode(index, params)
      • model.setGlobalVariable(index, flight_mode, value)
      • model.setInfo(value)
      • model.setLogicalSwitch(switch, value)
      • model.setModule(index, value)
      • model.setOutput(index, value)
      • model.setSwashRing(params)
      • model.setTimer(timer, value)
    • Lcd Functions
      • Lcd Functions Overview
      • lcd.RGB(r, g, b | rgb)
      • lcd.clear([color])
      • lcd.drawAnnulus(x, y, r1, r2, start, end [, flags])
      • lcd.drawArc(x, y, r, start, end [, flags])
      • lcd.drawBitmap(bitmap, x, y [, scale])
      • lcd.drawBitmapPattern(bitmap, x, y [, flags])
      • lcd.drawBitmapPatternPie(bitmap, x, y, startAngle, endAngle [, flags])
      • lcd.drawChannel(x, y, source, flags)
      • lcd.drawCircle(x, y, r [, flags])
      • lcd.drawCombobox(x, y, w, list, idx [, flags])
      • lcd.drawFilledCircle(x, y, r [, flags])
      • lcd.drawFilledRectangle(x, y, w, h [, flags])
      • lcd.drawFilledTriangle(x1, y1, x2, y2, x3, y3 [, flags])
      • lcd.drawGauge(x, y, w, h, fill, maxfill [, flags])
      • lcd.drawHudRectangle(pitch, roll, xmin, xmax, ymin, ymax [, flags])
      • lcd.drawLine(x1, y1, x2, y2, pattern, flags)
      • lcd.drawLineWithClipping(x1, y1, x2, y2, xmin, xmax, ymin, ymax, pattern [, flags])
      • lcd.drawNumber(x, y, value [, flags [, inversColor]])
      • lcd.drawPie(x, y, r, start, end [, flags])
      • lcd.drawPixmap(x, y, name)
      • lcd.drawPoint(x, y)
      • lcd.drawRectangle(x, y, w, h [, flags [, t]])
      • lcd.drawScreenTitle(title, page, pages)
      • lcd.drawSource(x, y, source [, flags])
      • lcd.drawSwitch(x, y, switch, flags)
      • lcd.drawText(x, y, text [, flags [, inversColor]])
      • lcd.drawTextLines(x, y, w, h, text [, flags])
      • lcd.drawTimer(x, y, value [, flags [, inversColor]])
      • lcd.drawTriangle(x1, y1, x2, y2, x3, y3 [, flags])
      • lcd.exitFullScreen()
      • lcd.getColor(flags)
      • lcd.getLastLeftPos()
      • lcd.getLastPos()
      • lcd.getLastRightPos()
      • lcd.invertRect(x, y, w, h [, flags])
      • lcd.refresh()
      • lcd.resetBacklightTimeout()
      • lcd.setColor(colorIndex, color)
      • lcd.sizeText(text [, flags])
    • Bitmap Functions
      • Bitmap.getSize(name)
      • Bitmap.open(name)
      • Bitmap.resize(bitmap, width, height)
      • Bitmap.toMask(bitmap)
    • Filesystem
      • dir(directory)
      • fstat(path)
      • del(file or directory)
  • Advanced Topics
    • Lua data sharing across scripts
    • Debugging techniques
    • Speed/memory optimizaton tricks
  • Appendix
    • Fonts
    • Units
Powered by GitBook
On this page

Was this helpful?

Export as PDF
  1. EdgeTX Lua API Programming Guide

Data Exchange with the EdgeTX Model Setup

An EdgeTX model has several data components that can be exchanged with Lua scripts. This section gives an overview.

PreviousDrawing Flags and ColorsNextIncluded Lua Libraries

Last updated 2 years ago

Was this helpful?

In general terms, there are two different types of data that can be exchanged with EdgeTX:

  • Values, which generally fall on a scale between -1024 and 1024, also sometimes reported as -100% to 100%, e.g. for mixer values and channel outputs.

  • Switches, which are either true or false.

A specific data source type, e.g. Global Variables, can be indexed directly with an index that starts with 0 for the first one. This can be a little confusing, as e.g. GV1 for FM0 is read with , because GV1 is the first global variable, and FM0 is the first flight mode. But as long as you remember that all direct indices start with 0 for the first one, you should be fine!

Global Variables is a value data source that can return a value between -1024 and 1024. Examples of value sources are:

  • Global Variables

  • Sticks, sliders and knobs

  • Trim values

  • Input lines

  • Channel outputs

  • Trainer channel inputs

  • Telemetry sensors

There is a way to access a value of any such type with a meta-index. You use the function where name can be any of the valid source names listed . It returns a table with a description of the value source, including the field id, which is the meta-index.

You can use id with the function to read the current value. You can also use name directly, but this is less efficient, as EdgeTX does a linear search for the name every time it is called. Therefore, the procedure of first extracting the meta-index from getFieldInfo(name).id, and then using that, is recommended.

For switches, uses a direct index to read this specific type of switch sources, again using the index 0 for LS1 etc. But there are also several types of switch sources, such as:

  • Logical switches

  • Switch positions, testing if a physical switch is in a particular position, e.g. SA↑.

  • Trim buttons

  • Transmitter and telemetry activity

It can be very confusing that some source can be read both as a value and as a switch. As an example, elevator trim can be read as the current value of the trim:

local id = getFieldInfo("trim-ele")
local eleTrim = getValue(id)

Or it can be read as the current value of the trim button position:

local idUp = getSwitchIndex(CHAR_TRIM .. "Eu")
local idDown = getSwitchIndex(CHAR_TRIM .. "Ed")
local eleUp = getSwitchValue(idUp)
local eleDown = getSwitchValue(idDown)

These are really two different things, as the elevator trim is an internal value stored by the radio, which is changed with the trim button, and the button is a physical button.

But it can also be a more direct comparison, e.g. the 3-position switch B, which can be read as a value source with:

local idB = getFieldInfo("sb")
local swBvalue = getValue(idB) -- a value of -1024, 0 or 1024

Or the current position of switch B can be read with:

local idBup = getSwitchIndex("SB" .. CHAR_UP)
local idBdown = getSwitchIndex("SB" .. CHAR_DOWN)
local swBup = getSwitchValue(idBup)
local swBdown = getSwitchValue(idBdown)

Hopefully, these examples illustrated the differences between values and switches, and showed how these can be retrieved in a Lua script.

You can also send data the other way: from Lua to the EdgeTX model setup.

To send a value, use a global variable: model.setGlobalVariable(index, fm, value). If you use the default GV setting, where all other flight modes use the value of FM0, then you can use 0 for fm.

To send a switch, setup a STICKY type logical switch, and then use setStickySwitch(index, true/false).

The following table gives an overview of the Lua API functions that can be used to exchange data with the EdgeTX model setup.

Function
Description

getFieldInfo(name)

Gets the meta-index of a value source with name.

getValue(index)

Gets the current value of a source, where index is from the above.

getFlightMode()

Returns the current flight mode number and name. Do not confuse with model.getFlightMode, which is a completely different function!

model.getGlobalVariable( index, fm)

Read the value of a global variable between -1024 and 1024. You can use the above to get the current flioght mode for fm, or you can setup the GV to always use the value from FM0.

model.setGlobalVariable( index, fm, value)

Sets the value of a global variable.

setTelemetryValue(id, subID, instance, value, ...)

This function can send data from Lua to a telemetry sensor. If it is running, then the sensor will show up with Discover Sensors. This can be used to log values generated by Lua.

getLogicalSwitchValue( index)

Get the value of a logical switch.

setStickySwitch(id, value)

Set the value of a STICKY type logical switch.

getSwitchIndex(name)

Get the meta-index of a switch source with name.

getSwitchName(index)

Get the name of a switch source.

getSwitchValue(index)

Get the value of a switch source.

getPhysicalSwitches()

Get a list of physical switches on the radio. Can be used to make a selection list.

getShmVar(index)

Get the value of a shared memory variable. These are little "mail boxes" provided as a way to send data between Lua widgets and other Lua scripts like Mixer and Function scripts.

setShmVar(index, value)

Set the value of a shared memory variable.

model.setTimer(timer, value)

Set the timer to value.

model.getTimer(timer)

Read the timer.

You can find the meta-index of a particular switch source with , where name is exactly the name that you see in the radio menus where you can select a switch source.

You can read the current value of a switch source with as a true/false value.

model.getGlobalVariable(0, 0)
getFieldInfo(name)
here
getValue(id)
getLogicalSwitchValue(index)
getSwitchIndex(name)
getSwitchValue(index)