Global variables: share data between mix-script and widget

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
wilopaan
Posts: 155
Joined: Sun Sep 09, 2018 6:09 pm
Country: -

Global variables: share data between mix-script and widget

Post by wilopaan »

I used to use global variables to exchange data between telemetry scripts and mixer script.

Now I want to do the same with a widget and a mix-script. Looks like the global var of the widget is not seem from the mix-script.

How can one achieve this?

Thanks!

lshems
Posts: 62
Joined: Tue Sep 15, 2015 5:36 pm
Country: -

Re: RE: Global variables: share data between mix-script and widget

Post by lshems »

wilopaan wrote:I used to use global variables to exchange data between telemetry scripts and mixer script.

Now I want to do the same with a widget and a mix-script. Looks like the global var of the widget is not seem from the mix-script.

How can one achieve this?

Thanks!
You can't. Use a table to store the stuff in a file and read from the other scripts.

I gave some info on this on my blog here.
wilopaan
Posts: 155
Joined: Sun Sep 09, 2018 6:09 pm
Country: -

Re: Global variables: share data between mix-script and widget

Post by wilopaan »

Well, that is really bad!

writing / reading a file seems to be a quite of overhead.

Are you you sure, that there is not other pssibility? (in the poist above I meant global-lua-script-variables, not global vars in the sense of OpenTx).

The widget i wrote (ab)uses some trims to emulate buttons and produces values that must be sent to a channel. As a telemtry script I could fill a globale lua variable and get this in a mix-script to produce an input for a channel-mixer.
Can you imagine another way to do this?

Thanks!
lshems
Posts: 62
Joined: Tue Sep 15, 2015 5:36 pm
Country: -

Re: RE: Re: Global variables: share data between mix-script and widget

Post by lshems »

wilopaan wrote:Well, that is really bad!

writing / reading a file seems to be a quite of overhead.

Are you you sure, that there is not other pssibility? (in the poist above I meant global-lua-script-variables, not global vars in the sense of OpenTx).

The widget i wrote (ab)uses some trims to emulate buttons and produces values that must be sent to a channel. As a telemtry script I could fill a globale lua variable and get this in a mix-script to produce an input for a channel-mixer.
Can you imagine another way to do this?

Thanks!
Create a virtual sensor. Write to it from one script and read from the other.
wilopaan
Posts: 155
Joined: Sun Sep 09, 2018 6:09 pm
Country: -

Re: Global variables: share data between mix-script and widget

Post by wilopaan »

Well, I can get the value of a sensor with getValue(). How Do I set the value?

Isn't it better to use model.setGlobalVariable() / model.getGlobalVariable() ?

lshems
Posts: 62
Joined: Tue Sep 15, 2015 5:36 pm
Country: -

Re: Global variables: share data between mix-script and widget

Post by lshems »

Ok, check this script:

https://www.justfly.solutions/index.php ... ogchannels

It writes a channel value to a virtual telemetry channel.

You can just use getValue (sensorname) to read it in the other script.

Don't forget to discover while the sending is active.
lshems
Posts: 62
Joined: Tue Sep 15, 2015 5:36 pm
Country: -

Re: Global variables: share data between mix-script and widget

Post by lshems »

The global variables you referred to are the model gvars.
wilopaan
Posts: 155
Joined: Sun Sep 09, 2018 6:09 pm
Country: -

Re: Global variables: share data between mix-script and widget

Post by wilopaan »

Well, I checked your example.

Writing / reading gvars works fine, but clutters the gvars oviously.

setTelemetryValue() / getValue() dows not work for me, since getValue() allways returns 0 for the name used in setTelemetryValue(): the sensor is created, I can see it in the telemetry menu, but getValue() does not work.
lshems
Posts: 62
Joined: Tue Sep 15, 2015 5:36 pm
Country: -

Re: RE: Re: Global variables: share data between mix-script and widget

Post by lshems »

wilopaan wrote:Well, I checked your example.

Writing / reading gvars works fine, but clutters the gvars oviously.

setTelemetryValue() / getValue() dows not work for me, since getValue() allways returns 0 for the name used in setTelemetryValue(): the sensor is created, I can see it in the telemetry menu, but getValue() does not work.
Then you are doing something wrong.

Post what you have, or at least the write part. Assign the telemetry sensor you created to an input-mix-output chain and watch it on the channel monitor.

wilopaan
Posts: 155
Joined: Sun Sep 09, 2018 6:09 pm
Country: -

Re: Global variables: share data between mix-script and widget

Post by wilopaan »

The writing part (widget):

local function background()
local b = setTelemetryValue(0Xabcd,0,101, 42, 0, 0, "wm01");
if (b) then
print("add ch1");
else
print("set ch1");
end
end

The written value is visible in the telemtry screen.

The reading part (mix script):

local function run()
-- print("m: ", model.getGlobalVariable(8, getFlightMode()));
print("m: ", getValue("wm01"));
return 0;
end

The debug output of this script gives always 0.
lshems
Posts: 62
Joined: Tue Sep 15, 2015 5:36 pm
Country: -

Re: Global variables: share data between mix-script and widget

Post by lshems »

You have to activate the telemetry simuator in the sim as well. little tickbox in the left to corner of the telemetry sensor simulator screen :).

testscript (run as telemetry, discover sensors):

return
{
background =
function()
setTelemetryValue(0Xabcd,0,101, getValue('thr'), 0, 0, "wm01");
end,
run =
function()
lcd.clear()
lcd.drawText(10,10,"wm01 : " .. getValue("wm01"))
lcd.drawText(10,20,"telem1: " .. getValue("telem1"))
lcd.drawText(10,30,"telem2: " .. getValue("telem2"))
lcd.drawText(10,40,"telem3: " .. getValue("telem3"))
end,
}
wilopaan
Posts: 155
Joined: Sun Sep 09, 2018 6:09 pm
Country: -

Re: Global variables: share data between mix-script and widget

Post by wilopaan »

lshems wrote: Fri Jun 05, 2020 6:44 pm You have to activate the telemetry simuator in the sim as well. little tickbox in the left to corner of the telemetry sensor simulator screen :).

testscript (run as telemetry, discover sensors):

return
{
background =
function()
setTelemetryValue(0Xabcd,0,101, getValue('thr'), 0, 0, "wm01");
end,
run =
function()
lcd.clear()
lcd.drawText(10,10,"wm01 : " .. getValue("wm01"))
lcd.drawText(10,20,"telem1: " .. getValue("telem1"))
lcd.drawText(10,30,"telem2: " .. getValue("telem2"))
lcd.drawText(10,40,"telem3: " .. getValue("telem3"))
end,
}
Oh yeah, the telemetry simulator ...

Thanks, I will try that!

Post Reply

Return to “openTx”