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
  • Debugging your code before testing
  • A good editor is key
  • Checking if the code can be compiled
  • Ready to run the code
  • The lua debug viewer
  • Using a script loader
  • Notes
  • Script Loader

Was this helpful?

Export as PDF
  1. LUA API Programming
  2. Advanced Topics

Debugging techniques

PreviousAdvanced TopicsNextLua data sharing across scripts

Last updated 1 year ago

Was this helpful?

Debugging your code before testing

A good editor is key

It is always good practice to check your code on syntax before even testing it. There are several good LUA editors on the market, some of them for free. The ZeroBrane () suite is quite powerful, and very simple to use. In the rest of this article we will assume you use ZeroBrane, but the same techniques can be used in any powerful code editor.

You can set ZeroBrane to use the Scripts directory of your simulated transmitter SDCard image as a default directory, and it will show you all the files in a nice navigation tree.

If you open a LUA file, you will already have some markup in your screen, indicating errors or important info. In ZeroBrane for instance, a not declared variable will always get underlined, so that you are made aware you forgot to declare it, or you redeclared it by accident afterwards again.

Checking if the code can be compiled

The editor will have an "execute code" option, that will try to run the code on it's own interpreter (code processing engine). If there are any syntax errors, it will not be able to execute the code, and inform you about the errors. A common error in LUA is using a single equal sign (=) in a condition in an 'if' statement, whereas in LUA that should be a double equal sign (==). The interpreter will inform you about such an error ocurring, and mention the line where you made the error.

Since the OpenTX LUA environment has some own functions, like lcd.drawText(), the interpreter will 'complain' it cannot call an unspecified function, but it will check the entire syntax nonetheless.

Ready to run the code

In zerobrane, if you tried to run the code, it will first save it if it could be interpreted correctly. A common workflow would be:

  • do some code corrections / additions

  • try to run the code in the editor

  • if the code gets compiled, the edited LUA file gets saved automatically

  • run the code in the transmitter simulator

  • check for the desired functionality

  • restart this cycle

The lua debug viewer

In the later versions of the companion software, a LUA debug screen is available. So once you start your just syntaxically verified and saved LUA script, you can follow some of it's output and actions in the debug screen. It will tell you where and in what line an eventual crash occured.

Using a script loader

If you made some code changes, chances are that you have to do a whole sequence of key-clicks and actions on the transmitter simulator in order to retest the same script after those changes.

You can substatially reduce the effort of all this by using a script loader in your transmitter. This is nothing more then a function that will load and run your code. If you press the enter button, it will unload the current code, and ask if you want to run the code again. So, with just two clicks, you can unload the running code and reload your updated code. On the Taranis simulator, you can also reload the LUA scripts environment with just a buttonclick.

An example of such a script is found under the notes. You can adapt it for other types of scripts of course.

Notes

Script Loader

This script loader will load the file /SCRIPTS/TELEM/telem1.lua, run it, and wait for an Enter Break event. Once received, it will unload the code and wait for a next Enter Break event.

local fileToLoad="/SCRIPTS/TELEM/telem1.lua"
local active = true
 
local thisPage={}
local page={}
 
local function clearTable(t)
  if type(t)=="table" then
    for i,v in pairs(t) do
      if type(v) == "table" then
        clearTable(v)
      end
      t[i] = nil
    end
  end
  collectgarbage()
  return t 
end

thisPage.init=function(...)
  if active then
    page=dofile(fileToLoad)
    page.init(...)
  end
  return true
end
 
thisPage.background=function(...)
  if active then
    page.background(...)
  end
  return true
end
 
thisPage.run=function(...)
  if active then
    page.run(...)
    active= not (...==EVT_ENTER_BREAK)
  else
    lcd.drawText( 15, 2, fileToLoad, 0 )
    lcd.drawText( 15, 20, "disabled", 0 )
    lcd.drawText( 15, 40,"press enter-button to activate",0)
    clearTable(page)
    active= (...==EVT_ENTER_BREAK)
    thisPage.init()
  end
  return not (...==EVT_MENU_BREAK)  
end
 
return thisPage
https://studio.zerobrane.com/