OpenTX 2.2 Lua Reference Guide
  • OpenTX 2.2 Lua Reference Guide
  • Introduction
    • Acknowledgments
    • Getting Started
  • Part I - Script Type Overview
    • Mix Scripts
    • Telemetry Scripts
    • One-Time Scripts
    • Wizard Script
    • Function Scripts
    • Widget Scripts
    • Theme Scripts
  • Part II - OpenTX Lua API Programming Guide
    • Input Table Syntax
    • Output Table Syntax
    • Init Function Syntax
    • Run Function Syntax
    • Return Statement Syntax
    • Included Lua Libraries
      • io Library
        • io.open()
        • io.close()
        • io.read()
        • io.write()
        • io.seek()
  • Part III - OpenTX Lua API Reference
    • Constants
      • Key Event Constants
    • General Functions
      • GREY()
      • crossfireTelemetryPop()
      • crossfireTelemetryPush()
      • defaultChannel(stick)
      • defaultStick(channel)
      • getDateTime()
      • getFieldInfo(name)
      • getFlightMode(mode)
      • getGeneralSettings()
      • getRAS()
      • getRSSI()
      • getRtcTime()
      • getTime()
      • getTxGPS()
      • getUsage()
      • getValue(source)
      • getVersion()
      • killEvents(key)
      • loadScript(file [, mode], [,env])
      • playDuration(duration [, hourFormat])
      • playFile(name)
      • playHaptic(duration, pause [, flags])
      • playNumber(value, unit [, attributes])
      • playTone(frequency, duration, pause [, flags [, freqIncr]])
      • popupConfirmation(title, event)
      • popupInput(title, event, input, min, max)
      • popupWarning(title, event)
      • resetGlobalTimer()
      • setTelemetryValue(id, subID, instance, value [, unit [, precision [, name]]])
      • sportTelemetryPop()
      • sportTelemetryPush()
    • Model Functions
      • model.defaultInputs()
      • model.deleteInput(input, line)
      • model.deleteInputs()
      • model.deleteMix(channel, line)
      • model.deleteMixes()
      • model.getCurve(curve)
      • model.getCustomFunction(function)
      • 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.getTimer(timer)
      • model.insertInput(input, line, value)
      • model.insertMix(channel, line, value)
      • model.resetTimer(timer)
      • model.setCurve(curve, params)
      • model.setCustomFunction(function, value)
      • 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)
    • Lcd Functions
      • Lcd Functions Overview
      • lcd.RGB(r, g, b)
      • lcd.clear([color])
      • lcd.drawBitmap(bitmap, x, y [, scale])
      • lcd.drawChannel(x, y, source, flags)
      • 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.getLastLeftPos()
      • lcd.getLastPos()
      • lcd.getLastRightPos()
      • lcd.refresh()
      • lcd.setColor(area, color)
    • Bitmap Functions
      • Bitmap.getSize(name)
      • Bitmap.open(name)
  • Part IV - Converting OpenTX 2.0 Scripts
    • General Issues
    • Handling GPS Sensor data
    • Handling Lipo Sensor Data
  • Part V - Converting OpenTX 2.1 Scripts
  • Part VI - Advanced Topics
    • Lua data sharing across scripts
    • Debugging techniques
    • Speed/memory optimizaton tricks
  • Part VII - Appendix
    • Fonts
    • Units
Powered by GitBook
On this page
  • Overview
  • Typical uses
  • Limitations
  • Location
  • Lifetime
  • Script interface definition
  • Example (interface only):
  • Notes:
  • Advanced example (save as /SCRIPTS/FUNCTIONS/cntdwn.lua)

Was this helpful?

  1. Part I - Script Type Overview

Function Scripts

PreviousWizard ScriptNextWidget Scripts

Last updated 4 years ago

Was this helpful?

Overview

Function scripts are invoked via the 'Lua Script' option of Special Functions configuration page.

Typical uses

  • specialized handling in response to switch position changes

  • customized announcements

Limitations

  • should not exceed allowed run-time/ number of instructions.

  • all function scripts are stopped while one-time script is running (see Lua One-time scripts)

  • Function scripts DO NOT HAVE ACCESS TO LCD DISPLAY

Location

Place them on SD card in folder /SCRIPTS/FUNCTIONS/

Lifetime

  • script init function is called once when model is loaded

  • script run function is periodically called as long as switch condition is true

  • script is stopped and disabled if it misbehaves (too long runtime, error in code, low memory)

Script interface definition

Every script must include a return statement at the end, that defines its interface to the rest of OpenTX code. This statement defines:

Example (interface only):

local function init_func()
end

local function run_func()
end

return { run=run_func, init=init_func }

Notes:

  • local variables retain their values for as long as the model is loaded regardless of switch condition value

Advanced example (save as /SCRIPTS/FUNCTIONS/cntdwn.lua)

The script below is an example of customized countdown announcements. Note that the init function determines which version of OpenTX is running and sets the unit parameter for playNumber() accordingly.

local lstannounce
local target

local running = false

local duration = 120 -- two minute countdown
local announcements = { 120, 105, 90, 75, 60, 55, 50, 45, 40, 35, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
local annIndex

local minUnit

local function init()
  local version = getVersion()
  if version < "2.1" then
    minUnit = 16  -- unit for minutes in OpenTX 2.0
  elseif version < "2.2" then
    minUnit = 23  -- unit for minutes in OpenTX 2.1
  else
    minUnit = 25  -- unit for minutes in OpenTX 2.2
  end
end

local function run()

  local timenow = getTime() -- 10ms tick count
  local remaining
  local minutes
  local seconds

  if not running then
    running = true
    target = timenow + (duration * 100)
    annIndex = 1
  end

  remaining = math.floor(((target - timenow) / 100) + .7) --  +.7 adjust for announcement lag

  if remaining < 0 then
    running = false -- we were 'paused' and missed zero
    return
  end

  while remaining < announcements[annIndex] do
    annIndex = annIndex + 1 -- catch up in case we were paused
  end

  if remaining == announcements[annIndex] then
    minutes = math.floor(remaining / 60)
    seconds = remaining % 60
    if minutes > 0 then
      playNumber(minutes, minUnit, 0)
    end
    if seconds > 0 then
      playNumber(seconds, 0, 0)
    end
    annIndex = annIndex + 1
  end

  if remaining <= 0 then
    playNumber(0,0,0)
    running = false
  end

end

return { init=init, run=run }

script init function (optional, see )

script run function (see )

Init Function Syntax
Run Function Syntax
Companion Special Functions Window
Taranis Special Functions Display