Multiple subscriptions in a single function?

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Post Reply
Message
Author
User avatar
warbirdguy1
Posts: 25
Joined: Sat May 01, 2021 12:57 am
Location: United States

Multiple subscriptions in a single function?

#1 Post by warbirdguy1 »

Okay so I got a challenge my LUA buddies are even having difficulty with.

So two components

1. A "Generator 1 Out" annunciator Warning Light.
2. A "WARN LTS TEST" Warning Lights Test switch to turn on the lights manually even when the failure is not met.

We can get the "Generator 1 Out" light to illuminate when the generator is turned off or failed in the sim.

We want at the same time to have the ability to manually turn on the light when the Warning Lights Test switch is pressed.

Since the switch is not an item I can fsx_variable_subscribe from I cant seem to create a multi variable function.

Is there any way we can add a subscribe to the position of the switch and the fsx_variable_subscribe at the same time in the same function?

Code: Select all

output_WarnLtsTest = hw_led_add("0 WarnLtsTest",0)
led_is_alt1charging = hw_led_add("WL Generator 1 WARN",0)

function switch_gen1_callback(position)                               --------------------------GEN 1 SWITCH-------------------------
  print("Gen 1 SW" .. position)
  if position == 0 then
    fsx_variable_write("GENERAL ENG GENERATOR ACTIVE:1", "bool", false)
  elseif position == 1 then
    fsx_variable_write("GENERAL ENG GENERATOR ACTIVE:1", "bool", true)
  end
end


function switch_WarnLtsTest_callback(position)               ----------------------WARN LTS TEST SWITCH-------------------------
  print("WarnLts SW" .. position)
  if position == 0 then
    hw_led_set(output_WarnLtsTest, 0)
  elseif position == 1 then
    hw_led_set(output_WarnLtsTest, 1)
  end
end

function is_alt1charging(alt1charging)                              ----------------------FAULT GEN 1 LIGHT-------------------------
	if(alt1charging == false)then
		hw_led_set(led_is_alt1charging, 1)
	else
		hw_led_set(led_is_alt1charging, 0)	
	end
end

fsx_variable_subscribe("GENERAL ENG GENERATOR ACTIVE:1", "Bool", is_alt1charging)

hw_switch_add("EL - Gen1", 1, switch_gen1_callback)
hw_switch_add("EL - Warn Lts Test SW", 1, switch_WarnLtsTest_callback)
"I know just enough to break something"

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

Re: Multiple subscriptions in a single function?

#2 Post by Sling »

HI Warbirdguy1,

Are you still playing with that ALT light. :lol: What you want is quite simple really. Here is one way. There are others.

Tony

Code: Select all

output_WarnLtsTest = hw_led_add("0 WarnLtsTest",0)
led_is_alt1charging = hw_led_add("WL Generator 1 WARN",0)

function switch_gen1_callback(position)                               --------------------------GEN 1 SWITCH-------------------------
  print("Gen 1 SW" .. position)
  if position == 0 then
    fsx_variable_write("GENERAL ENG GENERATOR ACTIVE:1", "bool", false)
  elseif position == 1 then
    fsx_variable_write("GENERAL ENG GENERATOR ACTIVE:1", "bool", true)
  end
end


function switch_WarnLtsTest_callback(position)               ----------------------WARN LTS TEST SWITCH-------------------------
  print("WarnLts SW" .. position)
  if position == 0 then
    hw_led_set(output_WarnLtsTest, 0)
  elseif position == 1 then
    hw_led_set(output_WarnLtsTest, 1)
  end
  test_active = fif(position == 1, true, false)
  request_callback(is_alt1charging)
end

function is_alt1charging(alt1charging)                              ----------------------FAULT GEN 1 LIGHT-------------------------
	if(alt1charging == false or test_active) then
		hw_led_set(led_is_alt1charging, 1)
	else
		hw_led_set(led_is_alt1charging, 0)	
	end
end

fsx_variable_subscribe("GENERAL ENG GENERATOR ACTIVE:1", "Bool", is_alt1charging)

hw_switch_add("EL - Gen1", 1, switch_gen1_callback)
hw_switch_add("EL - Warn Lts Test SW", 1, switch_WarnLtsTest_callback)

JackZ
Posts: 2262
Joined: Mon Feb 22, 2016 1:02 pm

Re: Multiple subscriptions in a single function?

#3 Post by JackZ »

I would add that you could also check wether the BAT BUS is on, to take into account the MASTER is OFF, and in that case the light should not come on even when the test button is depressed.

Moreover I think that the test_active variable should be declared first outside of the function itself, so the variable scope is global to the whole code instead of being local to the function switch_WarnLtsTest_callback() only.

Having the test_active variable global will make it useful when you will want eventually to illuminate all the other lights, by using the same technique Tony demonstrated.

To add up further, since many different AM instruments are likely to be affected at the same time by the light test button, I would use the inter instruments communication feature and create a specific shared variable through them via am_variable_create() and subscribe()

https://siminnovations.com/wiki/index.p ... munication

Jacques
My YouTube Chanel on the A320 (Real SOPs by an Airline Pilot IRL):
https://www.youtube.com/playlist?list=P ... 0Q6SBASRqJ

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

Re: Multiple subscriptions in a single function?

#4 Post by Sling »

Just a small point. The test_active variable will be global regardless of where it’s located because it doesn’t use the local declaration.

User avatar
warbirdguy1
Posts: 25
Joined: Sat May 01, 2021 12:57 am
Location: United States

Re: Multiple subscriptions in a single function?

#5 Post by warbirdguy1 »

Apologies I thought I had replied to this weeks ago.

That works fantastic! Thanks a ton guys.

I am abridging my code I have a fair number more functions going. I just did not want to clutter the code I presented to you up with all sorts of unrelated matter.

My project is nearing a point where I can demonstrate it and you will certainly see some videos soon.
"I know just enough to break something"

User avatar
Keith Baxter
Posts: 4674
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Multiple subscriptions in a single function?

#6 Post by Keith Baxter »

Hi,

This is an interesting topic.
Multiple subscriptions, (Data bus <omnibus> )

When should a data bus be used? and how big can a bus be? When do they need to be throttled? and why?


Keith
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

User avatar
jph
Posts: 2846
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: Multiple subscriptions in a single function?

#7 Post by jph »

Keith Baxter wrote: Tue Aug 03, 2021 5:00 pm Hi,

This is an interesting topic.
Multiple subscriptions, (Data bus <omnibus> )

When should a data bus be used? and how big can a bus be? When do they need to be throttled? and why?


Keith
Hi,
A 'bus' in this context is abstract. It is merely conditioning and response. as for the size - it all depends on the condition / response actions - meaning as large as you need it to be.... and as for 'throttling' ??? I do not know to what you refer ?
This is, as said, a purely abstract piece of coding. There is no such thing as a 'data bus' in this respect.
Joe
Joe. CISSP, MSc.

Post Reply