OPEN TX LUA SCRIPT

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
erwabo
Posts: 7
Joined: Wed Dec 09, 2015 10:40 pm
Country: -

OPEN TX LUA SCRIPT

Post by erwabo »

I CANNOT figure this out. I want this tiny piece of tx-voltage script work in my LUA script. It works fine by itself but it will NOT work when I place it in the script below the dashed line. Please help! Im on version 2.1.9

local function run()
local v = getValue("tx-voltage")
print ("tx-voltage: "..v)
lcd.clear()
lcd.drawText(155,55, "tx-voltage: "..v)
return 0
end
------------------------------------------------------------------------------------------
--[[ GLow Fuel - Fuel Guage
by Eric Bojonell
Version 1.0 - 20 January 2017

]]



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

local function getValueOrDefault(value)
local tmp = getValue(value)

if tmp == nil then
return 0
end

return tmp
end



local function init()
ver = getVersion()
if ver>="2.1.0" then
A2 = getTelemetryId("A2")
A1 = getTelemetryId("A1")

else
A2 = getFieldInfo(A2).id
A1 = getFieldInfo(A1).id

end



end




local function run(event)

local v = getValueOrDefault("tx-voltage")
print ("tx-voltage: "..v)
local r = getValueOrDefault("rssi")
print ("rssi: "..r)
lcd.drawText(145,55, "tx: "..v.." rssi: "..r)



----------------------------------------
-- GAUGE VARIBLES;
----------------------------------------


local x1 = 37
local y1 = 40
local angle
local x2
local y2
lcd.clear()

----------------------------------------
-- LINES;
----------------------------------------


lcd.drawLine(67,8,67,58, SOLID, 0)
lcd.drawLine(130,8,130,58, SOLID, 0)

----------------------------------------
-- TIMER;
----------------------------------------

local timer = model.getTimer(0)
lcd.drawTimer(95, 5, timer.value, MIDSIZE)
lcd.drawPixmap(75, 6, "/SCRIPTS/TELEMETRY/IMAGES/timer.bmp")

----------------------------------------
-- CLOCK;
----------------------------------------


lcd.drawTimer(95, 23, getValueOrDefault(190), LEFT+MIDSIZE)
lcd.drawPixmap(75, 23, "/SCRIPTS/TELEMETRY/IMAGES/clock.bmp")

----------------------------------------
-- RATE MODE;
----------------------------------------



lcd.drawPixmap(75, 44, "/SCRIPTS/TELEMETRY/IMAGES/fm.bmp")
if getValueOrDefault(94) > 0 and getValueOrDefault(97) < 0 then
lcd.drawText(97, 48, "LOW", SMLSIZE)
else
if getValueOrDefault(97) == 0 and getValueOrDefault(97) < 0 then
lcd.drawText(97, 48, "MED", SMLSIZE)
else
if getValueOrDefault(94) < 0 and getValueOrDefault(97) < 0 then
lcd.drawText(97, 48, "HIGH", SMLSIZE)
else
lcd.drawText(97, 48, "MAN", SMLSIZE)
end
end
end
----------------------------------------
-- FUEL;
----------------------------------------
--lcd.drawText(165, 55, "FUEL", SMLSIZE)
lcd.drawPixmap(135,2, "/SCRIPTS/TELEMETRY/IMAGES/fuel-gauge.bmp")


x1=172
for i=0, 10 do

angle = math.rad(((17*i)-180))
x2 = 25 * math.cos(angle) + x1
y2 = 25 * math.sin(angle) + y1

end

local FuelVoltage = getValueOrDefault("A2")--get sensor voltage

-- Draw indicator

angle = math.rad((FuelVoltage*17)-180)
x2 = 27 * math.cos(angle) + x1
y2 = 27 * math.sin(angle) + y1
lcd.drawLine(x1, y1, x2, y2, SOLID, 0)--GREY_DEFAULT)


----------------------------------------
-- RSSI;
----------------------------------------
local percent = 0

local CurrentRSSI = getValueOrDefault("RSSI")


if CurrentRSSI > 90 then
lcd.drawPixmap(35, 1, "/SCRIPTS/TELEMETRY/IMAGES/RSSIh10.bmp")
elseif CurrentRSSI > 80 then
lcd.drawPixmap( 35, 1, "/SCRIPTS/TELEMETRY/IMAGES/RSSIh09.bmp")
elseif CurrentRSSI > 70 then
lcd.drawPixmap( 35, 1, "/SCRIPTS/TELEMETRY/IMAGES/RSSIh08.bmp")
elseif CurrentRSSI > 60 then
lcd.drawPixmap( 35, 1, "/SCRIPTS/TELEMETRY/IMAGES/RSSIh07.bmp")
elseif CurrentRSSI > 50 then
lcd.drawPixmap( 35, 1, "/SCRIPTS/TELEMETRY/IMAGES/RSSIh06.bmp")
elseif CurrentRSSI > 40 then
lcd.drawPixmap( 35, 1, "/SCRIPTS/TELEMETRY/IMAGES/RSSIh05.bmp")
elseif CurrentRSSI > 30 then
lcd.drawPixmap( 35, 1, "/SCRIPTS/TELEMETRY/IMAGES/RSSIh04.bmp")
elseif CurrentRSSI > 20 then
lcd.drawPixmap( 35, 1, "/SCRIPTS/TELEMETRY/IMAGES/RSSIh03.bmp")
elseif CurrentRSSI > 10 then
lcd.drawPixmap( 35, 1, "/SCRIPTS/TELEMETRY/IMAGES/RSSIh02.bmp")
elseif CurrentRSSI > 0 then
lcd.drawPixmap(35, 1, "/SCRIPTS/TELEMETRY/IMAGES/RSSIh01.bmp")
else
lcd.drawPixmap( 35, 1, "/SCRIPTS/TELEMETRY/IMAGES/RSSIh07.bmp")

end


lcd.drawChannel( 46, 55, "RSSI", SMLSIZE)


----------------------------------------
-- Battery;
----------------------------------------

local myCurrent = getValueOrDefault("A1")
local myMaxV = 5.2
local myMinV = 4.0
local myRangeV = myMaxV - myMinV
local myAvailV = myCurrent - myMinV
local myPercent = math.floor(myAvailV / myRangeV * 100)

if myAvailV < 0 then myAvailV = 0 end
if myPercent < 0 then myPercent = 0 end
if myPercent > 100 then myPercent = 100 end

local myPxHeight = math.floor(myPercent * 0.37)
local myPxY = 11 + 37 - myPxHeight

lcd.drawPixmap(10, 1, "/SCRIPTS/TELEMETRY/IMAGES/batteryh.bmp")

if myPercent > 0 then
lcd.drawFilledRectangle(13, myPxY, 09, myPxHeight, FILL_WHITE )
end


local i = 36
while (i > 0) do
lcd.drawLine( 14, 10 + i, 20, 10 + i, SOLID, GREY_DEFAULT)
i= i-2
end

if (myCurrent > myMaxV) or (myCurrent < myMinV) then
lcd.drawChannel( 10, 55, "A1" ,PREC1 + LEFT + SMLSIZE)
else
lcd.drawChannel( 10,55, "A1" ,PREC1 + LEFT + SMLSIZE)
end


end
return { run=run, init=init}

User avatar
rcdiy
Posts: 190
Joined: Fri Jul 01, 2016 6:13 am
Country: Canada
Contact:

Re: OPEN TX LUA SCRIPT

Post by rcdiy »

local v = getValue("tx-voltage")
local v = getValueOrDefault("tx-voltage")
RCdiy.ca
Stay Safe & Have Fun
OpenTX Manual http://rcdiy.ca
Paying it forward by sharing my Lua scripts, documentation and videos.
erwabo
Posts: 7
Joined: Wed Dec 09, 2015 10:40 pm
Country: -

Re: OPEN TX LUA SCRIPT

Post by erwabo »

OMG...wow, missed that one. THANKS!! I was messing with that for a couple hours getting really ticked!
User avatar
rcdiy
Posts: 190
Joined: Fri Jul 01, 2016 6:13 am
Country: Canada
Contact:

Re: OPEN TX LUA SCRIPT

Post by rcdiy »

erwabo wrote:OMG...wow, missed that one. THANKS!! I was messing with that for a couple hours getting really ticked!
I was wondering. Even tried executing the code:)
RCdiy.ca
Stay Safe & Have Fun
OpenTX Manual http://rcdiy.ca
Paying it forward by sharing my Lua scripts, documentation and videos.
erwabo
Posts: 7
Joined: Wed Dec 09, 2015 10:40 pm
Country: -

Re: OPEN TX LUA SCRIPT

Post by erwabo »

Yes, but I was using one or the other with the two variables you posted. I knew it was something to do with that but I never thought to put both of them in the script together. As soon as I put them both in, it worked, but I don't understand 100% why it works since I'm still very new to LUA

User avatar
rcdiy
Posts: 190
Joined: Fri Jul 01, 2016 6:13 am
Country: Canada
Contact:

Re: OPEN TX LUA SCRIPT

Post by rcdiy »

oops.... no... I made a mistake.... have to look again...
RCdiy.ca
Stay Safe & Have Fun
OpenTX Manual http://rcdiy.ca
Paying it forward by sharing my Lua scripts, documentation and videos.
User avatar
rcdiy
Posts: 190
Joined: Fri Jul 01, 2016 6:13 am
Country: Canada
Contact:

Re: OPEN TX LUA SCRIPT

Post by rcdiy »

Okay. this is what I have.

local v = getValueOrDefault("RxBt")
print ("tx-voltage: "..v)
local r = getValueOrDefault("RSSI")
print ("rssi: "..r)
lcd.drawText(145,55, "tx: "..v.." rssi: "..r)

tx-voltage: 5.1999998092651
rssi: 75
tx-voltage: 5.1999998092651
rssi: 75
tx-voltage: 5.1999998092651
rssi: 75
tx-voltage: 5.1999998092651
rssi: 75
tx-voltage: 5.1999998092651
rssi: 75

So your code AS IS works... all I did was change the sensor names to match something I have and then changed values in the simulator....
So your code is correct I think...

I spent forever trying to simulate using a new blank model and nothing worked, even my own scripts.... then remembered and used an existing model that I had bound to an Rx....
Untitled.jpeg
RCdiy.ca
Stay Safe & Have Fun
OpenTX Manual http://rcdiy.ca
Paying it forward by sharing my Lua scripts, documentation and videos.
erwabo
Posts: 7
Joined: Wed Dec 09, 2015 10:40 pm
Country: -

Re: OPEN TX LUA SCRIPT

Post by erwabo »

Well this piece always worked by itself :

local v = getValueOrDefault("tx-voltage")
print ("tx-voltage: "..v)
lcd.clear()
lcd.drawText(155,55, "tx-voltage: "..v)

As soo as I added it to my script, it would NOT work...but then I added BOTH "local v" as you posted and it worked, I had to set it up like this:

local v = getValueOrDefault("tx-voltage")
local v = getValue("tx-voltage")
print ("tx-voltage: "..v)
lcd.clear()
lcd.drawText(155,55, "tx-voltage: "..v)
User avatar
rcdiy
Posts: 190
Joined: Fri Jul 01, 2016 6:13 am
Country: Canada
Contact:

Re: OPEN TX LUA SCRIPT

Post by rcdiy »

the second v replaces the first in which case your nill check won't work...

just for kicks try
local function getValueOrDefault(value)
return = getValue(value)
end


Your code as is works for me.... something else may be going on... I tested using the companion 1.9 simulator.
Attachments
123.lua
(4.98 KiB) Downloaded 327 times
RCdiy.ca
Stay Safe & Have Fun
OpenTX Manual http://rcdiy.ca
Paying it forward by sharing my Lua scripts, documentation and videos.
erwabo
Posts: 7
Joined: Wed Dec 09, 2015 10:40 pm
Country: -

Re: OPEN TX LUA SCRIPT

Post by erwabo »

Yeah it works now that I have it like this :

----------------------------------------
-- GAUGE VARIBLES;
----------------------------------------


local x1 = 37
local y1 = 35
local angle
local x2
local y2
lcd.clear()
local v = getValueOrDefault("tx-voltage")


I tried this before and it wasn't working, not sure why its working now LOL...so if you have 2 "local v" variables in there, the 2nd one would override the first one? Is that correct?
User avatar
rcdiy
Posts: 190
Joined: Fri Jul 01, 2016 6:13 am
Country: Canada
Contact:

Re: OPEN TX LUA SCRIPT

Post by rcdiy »

erwabo wrote: I tried this before and it wasn't working, not sure why its working now LOL...so if you have 2 "local v" variables in there, the 2nd one would override the first one? Is that correct?
Yup
local v =10
local v =2
print(v)
RCdiy.ca
Stay Safe & Have Fun
OpenTX Manual http://rcdiy.ca
Paying it forward by sharing my Lua scripts, documentation and videos.
erwabo
Posts: 7
Joined: Wed Dec 09, 2015 10:40 pm
Country: -

Re: OPEN TX LUA SCRIPT

Post by erwabo »

I think that was my problem then...I had them both in there and I had the wrong one second so it over wrote the correct one I have set for my nil. Man, such an easy fix to a frustrating issue. Always that way with code. Thanks for the help

Post Reply

Return to “openTx”