Adding a condition statement

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Post Reply
Message
Author
vangyver
Posts: 127
Joined: Sat Dec 05, 2015 8:30 am

Adding a condition statement

#1 Post by vangyver »

My computer with Air Manager is connected to a pico-projector that displays the radio stack digits on the rear side of the radio stack. The radio stack with all rotary encoders and switches are connected to Leo Bodnar’s USB card. Using FSUPIC I set the parameters for L:Com1OnOff to 0 and 1 for the switch. It works perfectly. The A2A Piper Cherokee list the LVAR as L:Com1OnOff,bool so I am confused as which one to use with Air Manager.
I have strip all the code from Sim Innovations generic radio module for the buttons and switches, and modified the PNG image to suit my needs. It works perfectly but I need to add a condition to the code - if avionics >= 1 and (battery >= 1 or generator[1] >= 1) then --
For the fsx_variable_subscribe I think I can add " L:Com1OnOff ", "Bool", or maybe something else?
Any help is greatly appreciated,
Vangyver
Code I am currently using:

-- Add images in Z-order --
img_add_fullscreen("KX165A.png")
redline = img_add("redline.png",364, 10, 2, 300)

-- Add text --
txt_com1 = txt_add(" ", "-fx-font-size:55px; -fx-fill: #DCF509; -fx-text-alignment: LEFT;", 25, 90, 200, 200)
txt_com1stby = txt_add(" ", "-fx-font-size:55px; -fx-fill: #DCF509; -fx-text-alignment: LEFT;", 200, 90, 200, 200)

txt_nav1 = txt_add(" ", "-fx-font-size:55px; -fx-fill: #DCF509; -fx-text-alignment: LEFT;", 365, 90, 200, 200)
txt_nav1stby = txt_add(" ", "-fx-font-size:55px; -fx-fill: #DCF509; -fx-text-alignment: LEFT;", 520, 90, 200, 200)

-- Set default visibility --
img_visible(redline, false)

-- Functions --
function new_navcomm(avionics, nav1, nav1stby, com1, com1stby, battery, generator)

img_visible(redline, avionics >= 1 and (battery >= 1 or generator[1] >= 1))

if avionics >= 1 and (battery >= 1 or generator[1] >= 1) then
txt_set(txt_com1, string.format("%d.%.02d",com1/100, com1%100) )
txt_set(txt_com1stby, string.format("%d.%.02d",com1stby/100, com1stby%100))
txt_set(txt_nav1, string.format("%d.%.02d",nav1/100, nav1%100))
txt_set(txt_nav1stby, string.format("%d.%.02d",nav1stby/100, nav1stby%100) )
else
txt_set(txt_com1, " ")
txt_set(txt_com1stby, " ")
txt_set(txt_nav1, " ")
txt_set(txt_nav1stby, " ")
end

switch_set_state(switch_onoff, avionics > 0)

end

function new_navcomm_FSX(avionics, nav1, nav1stby, com1, com1stby, battery, generator)

if generator == true then
generator = 1
else
generator = 0
end

new_navcomm(avionics, nav1*100+0.01, nav1stby*100+0.01, com1*100+0.01, com1stby*100+0.01, battery, {generator})

end



-- Bus subscribe --

fsx_variable_subscribe("ELECTRICAL AVIONICS BUS VOLTAGE", "Volts",
"NAV ACTIVE FREQUENCY:1", "Mhz",
"NAV STANDBY FREQUENCY:1", "Mhz",
"COM ACTIVE FREQUENCY:1", "Mhz",
"COM STANDBY FREQUENCY:1", "Mhz",
"ELECTRICAL BATTERY BUS VOLTAGE", "Volts",
"GENERAL ENG GENERATOR SWITCH:1", "BOOL", new_navcomm_FSX)

User avatar
Sling
Posts: 5237
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: Adding a condition statement

#2 Post by Sling »

Hi vangyver,

First of all welcome to the forum. Its a good idea to post your Lua code into a code box by selecting it and hitting the </> button. Like this.

Code: Select all

code here.
Now onto your question. Yes you can simply use the LVAR as an additional subscribe. Additionally seeing as you are not subscribing to any xplane datarefs you don't need to first pass the subscribed data to an FSX callback function and then pass onto the xplane function like you were doing. This is just a legacy left over from the instrument you based this on which would of had xplane subscribes. You can simply do all the action directly in the FSX callback.

I've incorporated what I've just described into your code below.

I hope this helps.

Code: Select all

-- Add images in Z-order --
img_add_fullscreen("KX165A.png")
redline = img_add("redline.png", 364, 10, 2, 300)

-- Add text --
txt_com1 = txt_add(" ", "-fx-font-size:55px; -fx-fill: #DCF509; -fx-text-alignment: LEFT;", 25, 90, 200, 200)
txt_com1stby = txt_add(" ", "-fx-font-size:55px; -fx-fill: #DCF509; -fx-text-alignment: LEFT;", 200, 90, 200, 200)

txt_nav1 = txt_add(" ", "-fx-font-size:55px; -fx-fill: #DCF509; -fx-text-alignment: LEFT;", 365, 90, 200, 200)
txt_nav1stby = txt_add(" ", "-fx-font-size:55px; -fx-fill: #DCF509; -fx-text-alignment: LEFT;", 520, 90, 200, 200)

-- Set default visibility --
img_visible(redline, false)

--subscribe callback --
function new_navcomm_FSX(avionics, nav1, nav1stby, com1, com1stby, battery, generator, com1_on_off)

    --make a boolean power variable based on state of avionics, battery, generator and com1_on_off 
    local power = avionics >= 8 and (battery >= 8 or generator) and com1_on_off
    img_visible(redline, power)

    if power then
        txt_set(txt_com1, string.format("%d.%.02d",com1/100, com1%100) )
        txt_set(txt_com1stby, string.format("%d.%.02d",com1stby/100, com1stby%100))
        txt_set(txt_nav1, string.format("%d.%.02d",nav1/100, nav1%100))
        txt_set(txt_nav1stby, string.format("%d.%.02d",nav1stby/100, nav1stby%100) )
    else
        txt_set(txt_com1, " ")
        txt_set(txt_com1stby, " ")
        txt_set(txt_nav1, " ")
        txt_set(txt_nav1stby, " ")
    end

end

--subscribes --
fsx_variable_subscribe("ELECTRICAL AVIONICS BUS VOLTAGE", "Volts",
                       "NAV ACTIVE FREQUENCY:1", "Mhz",
                       "NAV STANDBY FREQUENCY:1", "Mhz",
                       "COM ACTIVE FREQUENCY:1", "Mhz",
                       "COM STANDBY FREQUENCY:1", "Mhz",
                       "ELECTRICAL BATTERY BUS VOLTAGE", "Volts",
                       "GENERAL ENG GENERATOR SWITCH:1", "Bool",
                       " L:Com1OnOff ", "Bool", new_navcomm_FSX)
                       

vangyver
Posts: 127
Joined: Sat Dec 05, 2015 8:30 am

Re: Adding a condition statement

#3 Post by vangyver »

Thank you so much for the welcome. I am new at this and getting coding into this 70 year old brain is a challenge but I have a can do attitude. The new code produces no digit displays regardless of switch positions. Maybe because I am still using Air Manger 2.11
I am running FSX and Prepar3d v3.1
I will play with the code and see if I can get it to work. I will watch for any suggestion you may have and I also realize you maybe very busy with supplying others with support for your great software "AIr Manager".
Thank you So Much,
Vangyver
PS: I also run a flight simulator group with almost 8,000 members and still growing by the day. I always highly recommend your products in my group. https://www.facebook.com/groups/628896667180635

User avatar
Sling
Posts: 5237
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: Adding a condition statement

#4 Post by Sling »

Hi,

Perhaps the new com1_on_off is inverted to what I thought. You could try a not in front the variable name to invert the logic. Additionally you can place a print statement at the head of the subscribe callback to print the state of all the vars to the console. This will make sure they are all behaving as you suspect. It’s currently set for the following behaviour.

Avionics bus volts more than 8 volts.
Bus volts more than 8 volts or generator true (on).
Com1_on_off true (com 1 on).

vangyver
Posts: 127
Joined: Sat Dec 05, 2015 8:30 am

Re: Adding a condition statement

#5 Post by vangyver »

Trying a not in front the variable name to invert the logic did not work. Was not sure where to add the not so tried it a few different places to no avail. Adding a print statement to the code is a little beyond my current knowledge.

I think I need to add a function to the code and where to place it? Similar to Sim Innovations older generic radio module as in my first post.

if L:Com1OnOff == true then
L:Com1OnOff = 1
else
L:Com1OnOff = 0
end

vangyver
Posts: 127
Joined: Sat Dec 05, 2015 8:30 am

Re: Adding a condition statement

#6 Post by vangyver »

Actually I am understanding your code better. -- "make a boolean power variable based on state of avionics, battery, generator and com1_on_off " So if com1_on_off is True it shows the digits and if False is does not show. I get it.

vangyver
Posts: 127
Joined: Sat Dec 05, 2015 8:30 am

Re: Adding a condition statement

#7 Post by vangyver »

I will have to contact A2A to see why the L:Com1OnOff ", "Bool" is not working in LUA.

User avatar
Ralph
Posts: 7878
Joined: Tue Oct 27, 2015 7:02 pm
Location: De Steeg
Contact:

Re: Adding a condition statement

#8 Post by Ralph »

Isn't that an ENUM with 0 and 1? So...
"L:Com1OnOff ", "ENUM"

User avatar
Sling
Posts: 5237
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: Adding a condition statement

#9 Post by Sling »

Hi vangyver,

Ok sorry I assumed you knew the basics like print. No worries I can expalin as we go.

So to display the status of a variable within your Lua script while it runs, a print can be used. Air manager also has a debugger but lets start with the easier print method for know. To use print you simply type

print(variable_name)

The result of this print will appear in the create/edit console (lower left pane) so for our purposes you can add the print as follows. You'll notice its a tad more complicated than the simple example i just gave and thats because we want to display the state of 4 variables. If you run the following code and report back what is printed in the console that will help figure out what piece of data is not behaving. You will have to toggle each of the things to get them to change. Note that everytime a change is seen by Air Manager the print will run so if the volts for instance are changing by really small amounts each time what you will see in the console is lots of prints. I placed an "A" at the beginning of the first part so we can easily see where the data starts.

If the new LVAR is an Enum as Ralph suggests then change the 2 lines marked with --@@@@@@@@@@@@@@@@@@@@@ for the following 2 lines.

local power = avionics >= 8 and (battery >= 8 or generator) and com1_on_off == 1
" L:Com1OnOff ", "Enum", new_navcomm_FSX)

Code: Select all

-- Add images in Z-order --
img_add_fullscreen("KX165A.png")
redline = img_add("redline.png", 364, 10, 2, 300)

-- Add text --
txt_com1 = txt_add(" ", "-fx-font-size:55px; -fx-fill: #DCF509; -fx-text-alignment: LEFT;", 25, 90, 200, 200)
txt_com1stby = txt_add(" ", "-fx-font-size:55px; -fx-fill: #DCF509; -fx-text-alignment: LEFT;", 200, 90, 200, 200)

txt_nav1 = txt_add(" ", "-fx-font-size:55px; -fx-fill: #DCF509; -fx-text-alignment: LEFT;", 365, 90, 200, 200)
txt_nav1stby = txt_add(" ", "-fx-font-size:55px; -fx-fill: #DCF509; -fx-text-alignment: LEFT;", 520, 90, 200, 200)

-- Set default visibility --
img_visible(redline, false)

--subscribe callback --
function new_navcomm_FSX(avionics, nav1, nav1stby, com1, com1stby, battery, generator, com1_on_off)

print("A"..avionics.."   "..battery, generator, com1_on_off)

    --make a boolean power variable based on state of avionics, battery, generator and com1_on_off 
    local power = avionics >= 8 and (battery >= 8 or generator) and com1_on_off   --@@@@@@@@@@@@@@@@@@@@@
    img_visible(redline, power)

    if power then
        txt_set(txt_com1, string.format("%d.%.02d",com1/100, com1%100) )
        txt_set(txt_com1stby, string.format("%d.%.02d",com1stby/100, com1stby%100))
        txt_set(txt_nav1, string.format("%d.%.02d",nav1/100, nav1%100))
        txt_set(txt_nav1stby, string.format("%d.%.02d",nav1stby/100, nav1stby%100) )
    else
        txt_set(txt_com1, " ")
        txt_set(txt_com1stby, " ")
        txt_set(txt_nav1, " ")
        txt_set(txt_nav1stby, " ")
    end

end

--subscribes --
fsx_variable_subscribe("ELECTRICAL AVIONICS BUS VOLTAGE", "Volts",
                       "NAV ACTIVE FREQUENCY:1", "Mhz",
                       "NAV STANDBY FREQUENCY:1", "Mhz",
                       "COM ACTIVE FREQUENCY:1", "Mhz",
                       "COM STANDBY FREQUENCY:1", "Mhz",
                       "ELECTRICAL BATTERY BUS VOLTAGE", "Volts",
                       "GENERAL ENG GENERATOR SWITCH:1", "Bool",     
                       " L:Com1OnOff ", "Bool", new_navcomm_FSX)   --@@@@@@@@@@@@@@@@@@@@@

User avatar
Sling
Posts: 5237
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: Adding a condition statement

#10 Post by Sling »

Ralph wrote: Thu Aug 12, 2021 10:39 am Isn't that an ENUM with 0 and 1? So...
"L:Com1OnOff ", "ENUM"
Doesn't look like it. After a little bit of digging i found this. Worth a try though.

image.png
image.png (9.23 KiB) Viewed 3033 times

Post Reply