LUA Reference Guide
2.11
2.11
  • EdgeTX LUA Reference Guide
  • Overview
    • Lua Basics
    • LUA version and included libraries
      • io Library
    • Script Types
      • One-Time Scripts
      • Telemetry Scripts
      • Widget Scripts
      • Function Scripts
      • Mixes Scripts
    • Radios
      • BetaFPV
      • FlySky
      • FrSky
      • Jumper
      • Radiomaster
        • TX16S
    • Internal indexes
      • Source List
      • Switch Positions List
  • LUA API Reference
    • Constants
      • Color Constants
      • Flags and Pattern Constants
      • Key Event Constants
      • Logical Switch Function Constants
      • Screen Constants
      • Special Character Constants
      • Special Function Constants
      • Touch Event Constants
      • Widget Options Constants
      • Units
    • Display LCD
      • Lcd Functions Overview
      • lcd.clear
      • lcd.drawBitmap
      • lcd.drawChannel
      • lcd.drawCombobox(x, y, w, list, idx [, flags])
      • lcd.drawFilledRectangle(x, y, w, h [, flags])
      • lcd.drawGauge(x, y, w, h, fill, maxfill [, flags])
      • lcd.drawLine(x1, y1, x2, y2, pattern, flags)
      • lcd.drawNumber(x, y, value [, 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])
      • lcd.drawTimer(x, y, value [, flags])
      • lcd.drawAnnulus(x, y, r1, r2, start, end [, flags])
      • lcd.drawArc(x, y, r, start, end [, flags])
      • lcd.drawCircle(x, y, r [, flags])
      • lcd.drawFilledCircle(x, y, r [, flags])
      • lcd.drawFilledTriangle(x1, y1, x2, y2, x3, y3 [, flags])
      • lcd.drawHudRectangle(pitch, roll, xmin, xmax, ymin, ymax [, flags])
      • lcd.drawLineWithClipping(x1, y1, x2, y2, xmin, xmax, ymin, ymax, pattern [, flags])
      • lcd.drawPie(x, y, r, start, end [, flags])
      • lcd.drawTriangle(x1, y1, x2, y2, x3, y3 [, flags])
      • lcd.getColor(area)
      • lcd.getLastLeftPos()
      • lcd.getLastPos()
      • lcd.getLastRightPos()
      • lcd.drawBitmapPattern(bitmap, x, y [, flags])
      • lcd.drawBitmapPatternPie(bitmap, x, y, startAngle, endAngle [, flags])
      • lcd.drawTextLines(x, y, w, h, text [, flags])
      • lcd.exitFullScreen()
      • lcd.invertRect(x, y, w, h [, flags])
      • lcd.refresh()
      • lcd.RGB
      • lcd.resetBacklightTimeout()
      • lcd.setColor(area, color)
      • lcd.sizeText(text [, flags])
      • popupConfirmation(title, message, event)
      • popupInput(title, event, input, min, max)
      • popupWarning(title, event)
      • GREY()
    • LVGL for Lua
      • Overview
      • Examples
      • Constants
      • API
      • lvgl.align
      • lvgl.arc
      • lvgl.box
      • lvgl.build
      • lvgl.button
      • lvgl.choice
      • lvgl.file
      • lvgl.circle
      • lvgl.clear
      • lvgl.color
      • lvgl.confirm
      • lvgl.dialog
      • lvgl.isAppMode
      • lvgl.isFullScreen
      • lvgl.exitFullScreen
      • lvgl.font
      • lvgl.getContext
      • lvgl.message
      • lvgl.momentaryButton
      • lvgl.hide
      • lvgl.hline
      • lvgl.image
      • lvgl.label
      • lvgl.line
      • lvgl.menu
      • lvgl.numberEdit
      • lvgl.page
      • lvgl.qrcode
      • lvgl.rectangle
      • lvgl.set
      • lvgl.setting
      • lvgl.show
      • lvgl.slider
      • lvgl.verticalSlider
      • lvgl.source
      • lvgl.switch
      • lvgl.textEdit
      • lvgl.timer
      • lvgl.toggle
      • lvgl.triangle
      • lvgl.vline
    • Model Functions
      • model.defaultInputs
      • model.deleteFlightModes
      • model.deleteInput
      • model.deleteInputs
      • model.deleteMix
      • model.deleteMixes
      • model.getCurve
      • 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.getTimer(timer)
      • model.insertInput(input, line, value)
      • model.insertMix(channel, line, value)
      • model.getSwashRing()
      • model.setSwashRing(params)
      • 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.setTimer(timer, value)
    • Bitmap Functions
      • Bitmap.resize(bitmap, width, height)
      • Bitmap.toMask(bitmap)
      • Bitmap.getSize(name)
      • Bitmap.open(name)
    • Filesystem
      • chdir(directory)
      • del(file or directory)
      • dir(directory)
      • fstat(path)
    • Serial Ports
      • serialGetPower(port_nr)
      • serialRead([num])
      • serialSetPower(port_nr, value)
      • serialWrite(str)
      • setSerialBaudrate(baudrate)
    • System
      • defaultChannel(stick)
      • defaultStick(channel)
      • getGeneralSettings()
      • getVersion()
      • screenshot()
    • Key Inputs
      • getRotEncMode()
      • getRotEncSpeed()
      • killEvents(key)
    • LUA Scripts
      • getAvailableMemory()
      • getUsage()
      • loadScript(file [, mode], [,env])
    • Time
      • getDateTime()
      • getGlobalTimer()
      • getRtcTime()
      • getTime()
      • resetGlobalTimer([type])
    • Audio-Sound
      • playDuration(duration [, hourFormat])
      • playFile(name)
      • playHaptic(duration, pause [, flags])
      • playNumber(value, unit [, attributes])
      • playTone(frequency, duration, pause [, flags [, freqIncr]])
      • flushAudio()
    • Telemetry
      • getRSSI()
    • RF module
      • accessTelemetryPush()
      • crossfireTelemetryPop()
      • crossfireTelemetryPush()
      • ghostTelemetryPop()
      • ghostTelemetryPush()
      • sportTelemetryPop()
      • sportTelemetryPush()
      • getRAS()
      • multiBuffer(address[,value])
    • GPS
      • getTxGPS()
    • Variables
      • getFieldInfo(source)
      • getFlightMode(mode)
      • getLogicalSwitchValue(id)
      • getOutputValue(outputIndex)
      • getShmVar(id)
      • getSourceIndex(sourceName)
      • getSourceName(sourceIndex)
      • getSourceValue(source)
      • getSwitchIndex(positionName)
      • getSwitchName(switchIndex)
      • getSwitchValue(switchIndex)
      • getValue(source)
      • setShmVar(id, value)
      • setStickySwitch(id, value)
      • sources([first[, last]])
      • switches([first[, last]])
  • LUA API Programming
    • Data Exchange with the EdgeTX Model Setup
    • Drawing Flags and Colors
    • Init Function Syntax
    • Input Table Syntax
    • Loading Code Modules Dynamically
    • Output Table Syntax
    • Return Statement Syntax
    • Run Function Syntax
    • Saving Memory
    • Using Key and Touch Events
    • Advanced Topics
      • Debugging techniques
      • Lua data sharing across scripts
      • Speed/memory optimizaton tricks
  • Appendixes
    • Fonts
Powered by GitBook
On this page
  • Telemetry Script Radios
  • Widget Script Radios

Was this helpful?

Export as PDF
  1. LUA API Programming

Saving Memory

This chapter will show you some ways that large script projects can be fitted into the limited memory of our radios.

Regarding memory, the situation is a bit different for the radios with black/white or grey scale screens and telemetry scripts, and the radios with color screens and widget scripts. The telemetry script radios only have 128-192 KB RAM memory - that is very small! The widget script radios have 8 MB RAM memory. But the way that widgets are designed means that all widget scripts present on the SD card will be loaded into memory, whether or not they are actually used. Therefore, different strategies should be applied to save memory for the two different types of radios and scripts.

Telemetry Script Radios

Radios with black/white or grey scale screens and telemetry scripts such as e.g. FrSky Taranis, Xlite, Jumper T12 and Radiomaster TX12 have extremely small RAM memories, and therefore it may be necessary to divide up your script into smaller loadable modules.

The following simple example demonstrates how different screens can be loaded on demand, and how shared data can be stored in a table.

-- Main telemetry script

local shared = { }
shared.screens = {
  "/SCRIPTS/Test/menu1.lua",
  "/SCRIPTS/Test/menu2.lua",
  "/SCRIPTS/Test/menu3.lua"
}

function shared.changeScreen(delta)
  shared.current = shared.current + delta
  if shared.current > #shared.screens then
    shared.current = 1
  elseif shared.current < 1 then
    shared.current = #shared.screens
  end
  local chunk = loadScript(shared.screens[shared.current])
  chunk(shared)
end

local function init()
  shared.current = 1
  shared.changeScreen(0)
end

local function run(event)
  shared.run(event)
end

return { run = run, init = init }
-- /SCRIPTS/Test/menu1.lua

local shared = ...

function shared.run(event)
  lcd.clear()
  lcd.drawText(20, 10, "Screen 1", MIDSIZE)
  
  if event == EVT_VIRTUAL_NEXT then
    shared.changeScreen(1)
  elseif event == EVT_VIRTUAL_PREV then
    shared.changeScreen(-1)
  end
end
-- /SCRIPTS/Test/menu2.lua

local shared = ...

function shared.run(event)
  lcd.clear()
  lcd.drawText(20, 10, "Screen 2", MIDSIZE)
  
  if event == EVT_VIRTUAL_NEXT then
    shared.changeScreen(1)
  elseif event == EVT_VIRTUAL_PREV then
    shared.changeScreen(-1)
  end
end
-- /SCRIPTS/Test/menu3.lua

local shared = ...

function shared.run(event)
  lcd.clear()
  lcd.drawText(20, 10, "Screen 3", MIDSIZE)
  
  if event == EVT_VIRTUAL_NEXT then
    shared.changeScreen(1)
  elseif event == EVT_VIRTUAL_PREV then
    shared.changeScreen(-1)
  end
end

The table shared contains data that is shared between the main telemetry script and the loadable screens. Notice that the functions shared.changeScreen and shared.run are also shared this way.

Code is loaded by shared.changeScreen with the loadScript function, which returns the loadable script as a chunk of code. The code is executed with shared as the argument, and the loadable script adds a new run function to the shared table. shared.run is called by run in the main script.

Widget Script Radios

Radios with color screens and widget scripts such as e.g. FrSky Horus, Jumper T16 and Radiomaster TX16S have fairly large RAM memories, but since all widget scripts present on the SD card are always loaded into memory, they could run out of memory if many big widget scripts are present on the card - even if they are not being used by the selected model. Therefore, large widget scripts should be divided into a small main script and a large loadable part. One way to accomplish this is the following.

-- main.lua
local name = "<widget name>"

local function create(zone, options)
  -- Loadable code is called immediately and returns a widget table
  return loadScript("/WIDGETS/" .. name .. "/loadable.lua")(zone, options)
end

local function refresh(widget, event, touchState)
  widget.refresh(event, touchState)
end

local options = {
  -- default options here
}

local function update(widget, options)
  widget.update(options)
end

local function background(widget)
  widget.background()
end

return {
  name = name, 
  create = create, 
  refresh = refresh, 
  options = options, 
  update = update, 
  background = background
}
-- loadable.lua
local zone, options = ...

-- The widget table will be returned to the main script.
local widget = { }

function widget.refresh(event, touchState)
  -- refresh code here
end

function widget.update(opt)
  options = opt
  -- update code here
end

function widget.background()
  -- background code here
end

-- Return to the create(...) function in the main script
return widget

Obviously, the bulk of the widget's code goes in loadable.lua, and is only loaded if the widgets is in fact being used. Therefore, if the widget is not used, only the small amount of code in main.lua is loaded into the radio's memory.

For an example of a widget that uses the above design pattern, please have a look at the EventDemo widget that is included on the SD card with EdgeTX for color screen radios.

PreviousRun Function SyntaxNextUsing Key and Touch Events

Last updated 1 year ago

Was this helpful?

The create function loads the file loadable.lua in the folder /WIDGETS/<widget name>/, and calls it immediately as described in . It passes zone and options as arguments to loadable.lua. This scripts adds the functions refresh, update and (optionally) background to the widget table:

zone and options are stored in the of loadable.lua, therefore they do not need to be added to the widget table, as is commonly done.

the previous section
closure