Lua help, trying to learn, so many questions

openTx has introduced a range of new features, ideas and bling. It is fast becoming the firmware of choice for many users. openTx will run on ALL current hardware platforms, including the gruvin9x and sky9x boards. Work has already started to support the new FrSky X9D radio!
Post Reply
Ivanstein
Posts: 2
Joined: Sat Jun 29, 2019 12:47 am
Country: -

Lua help, trying to learn, so many questions

Post by Ivanstein »

I am trying to learn Lua for Open TX and I am faltering. I have some questions, which are probably obvious to the seasoned programmer.

I would like to reference the BattCheck widget script that comes as part of OpenTX with the Horus.

The fist line is a local variable call:
local shadowed = 0

So, why is this a local variable? It is outside any other functions, so is it necessary to call it local? Does it make it local to the widget script and if not set local becomes obtrusive to the rest of the OS?

Next is a variable set as a table.
local options = {
{ "Sensor", SOURCE, 1 },
{ "Color", COLOR, WHITE },
{ "Shadow", BOOL, 0 }
}

The OpenTX Lua scripting manual (the book) says these are only used in widget creation. Does this mean they are available to all other functions as the script executes, meaning create, update, background and refresh can all pull values from that table?

Also, the table has the key "Sensor", type SOURCE, value (I think??? it doesn't coincide with the book at input table syntax) 1. So, that sets up the table spot for the expected data. Now, here is where I get totally lost....I can't figure out what tells the script to look for the FMVSS sensor and not, for instance, Galt or RSSI.

Ultimately, I would like to figure out a way to read a telemetry value, say RPM, and set a user maximum which is adjustable with a trim switch, calculate the percentage and display it in a widget. I think I can use a global variable for the maximum and a special function to adjust it. However, it would be nice to make that happen in the script because that's what scripts are for (aren't they?)

Thanks for any help. I am sure I will ask more questions as time progresses.

pancakeMSTR
Posts: 3
Joined: Fri Oct 09, 2020 2:20 pm
Country: -

Re: Lua help, trying to learn, so many questions

Post by pancakeMSTR »

I'm confused by the same thing regarding the options table syntax vs the input table syntax cited elsewhere in the manual. Did you ever make any progress on this?
lshems
Posts: 63
Joined: Tue Sep 15, 2015 5:36 pm
Country: -

Re: Lua help, trying to learn, so many questions

Post by lshems »

Try and read the lua manual on variable scoping first.

Then, widgets are so not easy to understand, bad point to start.

But, check my blog on rcgroups, it answers a lot f these questions.

Guido
offers
Posts: 81
Joined: Tue Jan 22, 2013 4:14 pm
Country: -

Re: Lua help, trying to learn, so many questions

Post by offers »

Hi
I will try to put some light on this:
any parameter in the LUA should be "local", this why it is isolated from another instance of the same script,
especially on widget where you can more than one instance of the widget(code) at a time

the parameter "Sensor" is the one that choose on what sensor the widget will work, it expected to be set by user to "Cels" "Bat" or something similar that represent the FLVS sensor.
you can put there RSSI, but then the widget will not work of course
so I put also a default:
https://github.com/opentx/opentx/blob/2 ... in.lua#L96

Code: Select all

 -- use default if user did not set, So widget is operational on "select widget"
  if wgt.options.Sensor == 0 then
    wgt.options.Sensor = "Cels"
  end
to get a value of sensor, you need it's name, and that can change from transmitter to transmitter

Code: Select all

 local currentRpmValue = getValue('RPM') 
 local minRpmValue = getValue('RPM-') 
 

Post Reply

Return to “openTx”