Page 1 of 1

Lua help, trying to learn, so many questions

Posted: Mon Jul 15, 2019 5:09 am
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.

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

Posted: Fri Oct 09, 2020 2:22 pm
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?

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

Posted: Fri Oct 09, 2020 3:27 pm
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

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

Posted: Sat Oct 10, 2020 5:19 am
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-')