OpenTX 2.3 Lua Reference Guide
  • OpenTX 2.3 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()
      • accessTelemetryPush()
      • chdir(directory)
      • crossfireTelemetryPop()
      • crossfireTelemetryPush()
      • defaultChannel(stick)
      • defaultStick(channel)
      • flushAudio()
      • getAvailableMemory()
      • getDateTime()
      • getFieldInfo(name)
      • getFlightMode(mode)
      • getGeneralSettings()
      • getGlobalTimer()
      • getRAS()
      • getRSSI()
      • getRotEncSpeed()
      • getRtcTime()
      • 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, event)
      • popupInput(title, event, input, min, max)
      • popupWarning(title, event)
      • resetGlobalTimer([type])
      • serialRead([num])
      • serialWrite(str)
      • setTelemetryValue(id, subID, instance, value [, unit [, precision [, name]]])
      • setSerialBaudrate(baudrate)
      • 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(params)
      • 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)
      • 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.getColor(area)
      • lcd.getLastLeftPos()
      • lcd.getLastPos()
      • lcd.getLastRightPos()
      • lcd.refresh()
      • lcd.resetBacklightTimeout()
      • 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
  1. Part IV - Converting OpenTX 2.0 Scripts

Handling Lipo Sensor Data

With OpenTx 2.2 it is possible to have multiple Lipo sensors, each with a user-assigned name. The call to getValue() returns a table with the current voltage of each of the cells it is monitoring.

This example demonstrates getting Lipo cell voltage from a sensor with the default name of 'Cels'

Example:

local cellValue = "unknown"
local cellResult = nil
local cellID = nil

local function getTelemetryId(name)
    field = getFieldInfo(name)
    if field then
      return field.id
    else
      return -1
    end
end

local function init()
  cellId = getTelemetryId("Cels")
end

local function background()
  cellResult = getValue(cellId)
  if (type(cellResult) == "table") then
    cellValue = ""
    for i, v in ipairs(cellResult) do
      cellValue = cellValue .. i .. ": " .. v .. " "
    end
  else
    cellValue = "telemetry not available"
  end
end

local function run(e)
  background()
  lcd.clear()
  lcd.drawText(1,1,"OpenTX 2.2 cell voltage example",0)
  lcd.drawText(1,11,"Cels:", 0)
  lcd.drawText(lcd.getLastPos()+2,11,cellValue,0)
end

return{init=init,run=run,background=background}
PreviousHandling GPS Sensor dataNextPart V - Converting OpenTX 2.1 Scripts

Last updated 3 years ago