Need help with Switch-Sim-Status Syncronisation

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Message
Author
Witzman
Posts: 26
Joined: Sun Sep 12, 2021 9:42 am

Need help with Switch-Sim-Status Syncronisation

#1 Post by Witzman »

Hi,

i am making my first steps with lua and need some help when it comes to hardware switches.

For testing i use the C172 switch panel, i am using FSX.
Here is the code from the switchpanel:

Code: Select all

-- FUEL PUMP SWITCH
function fuel_pump_click_callback(position)

    if position == 0 then
        xpl_dataref_write("sim/cockpit/engine/fuel_pump_on", "INT[8]", {1})
        fsx_event("TOGGLE_ELECT_FUEL_PUMP1")
        fs2020_event("TOGGLE_ELECT_FUEL_PUMP")
    elseif position == 1 then
        xpl_dataref_write("sim/cockpit/engine/fuel_pump_on", "INT[8]", {0})
        fsx_event("TOGGLE_ELECT_FUEL_PUMP1")
        fs2020_event("TOGGLE_ELECT_FUEL_PUMP")
    end

end

fuel_pump_switch_id = switch_add("toggle_off.png", "toggle_on.png", 160,202,63,63,fuel_pump_click_callback)

function new_fuel_pump_switch_pos(sw_on)

    if sw_on[1] == 0 then
        switch_set_position(fuel_pump_switch_id, 0)
    elseif sw_on[1] == 1 then
        switch_set_position(fuel_pump_switch_id, 1)
    end
    
end    

function new_fuel_pump_switch_pos_fsx(sw_on)

    sw_on = fif(sw_on, 1, 0)
    new_fuel_pump_switch_pos({sw_on})

end

xpl_dataref_subscribe("sim/cockpit/engine/fuel_pump_on", "INT[8]",new_fuel_pump_switch_pos)
fsx_variable_subscribe("GENERAL ENG FUEL PUMP SWITCH:1", "Bool", new_fuel_pump_switch_pos_fsx)
fs2020_variable_subscribe("GENERAL ENG FUEL PUMP SWITCH:1", "Bool", new_fuel_pump_switch_pos_fsx)
-- END FUEL PUMP SWITCH
at the bottom of the switch panel i added following code:

Code: Select all

hw_switch_add("ARDUINO_UNO_A_D9", fuel_pump_click_callback)
hw_switch_add("ARDUINO_UNO_A_D8", beacon_click_callback)
hw_switch_add("ARDUINO_UNO_A_D10", landing_click_callback)
hw_switch_add("ARDUINO_UNO_A_D11", taxi_click_callback)
hw_switch_add("ARDUINO_UNO_A_D12", nav_click_callback)
hw_switch_add("ARDUINO_UNO_A_D13", strobe_click_callback)
I got my 6 Switches to work in general but as i can see in the fuelpump code, it "toggles" the FSX setting.
I need to sync the panel switch with my current button setting, so when i start everything and my hardware switch is on 1, while the panel software switch is 0, its out of sync.

The general ideal should be, read the hardware switch -> set the softwareswitch.
Found the same Problem described and solved here: https://siminnovations.com/forums/viewt ... 416#p30416
I think i have to combine https://siminnovations.com/wiki/index.p ... t_position & https://siminnovations.com/wiki/index.p ... t_position

Maybe anyone can help me with this basic concept?

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

Re: Need help with Switch-Sim-Status Syncronisation

#2 Post by Sling »

Hi,

You certainly don’t need the set position function because that’s for soft switches only. Unless your intention is to retain both soft and hard switches.

The process as follows.

Subscribe to the FSX variable that reports the switch position or perhaps the status of the device the switch operates. Let’s say fuel pump as per your example. So fsx_variable_subscribe("GENERAL ENG FUEL PUMP SWITCH:1", "Bool", new_fuel_pump_switch_pos_fsx). This is already done in the code you are using.

Inside the corresponding callback function use the hw_switch_get_position() function to get the current switch position. So local fp_pos = hw_switch_get_position(id). Where the id is the id of your hardware switch. You haven’t allocated an id yet to your switches but it’s very important you do that. Save the position in a variable. I’ve used fp_pos.

Now you have both the sim and hardware switch positions you compare them and if they don’t match then you issue the toggle event.
if (sw_on and fp_pos == 0) or (not sw_on and fp_pos == 1) then
fsx_event("TOGGLE_ELECT_FUEL_PUMP1")
end
The comparison logic will depend on how you have your switch wired/oriented. If direction is swapped just swap the fp_pos == 0 and fp_pos == 1.

I hope that helps.

Witzman
Posts: 26
Joined: Sun Sep 12, 2021 9:42 am

Re: Need help with Switch-Sim-Status Syncronisation

#3 Post by Witzman »

Thanks, helped a lot. Only part - which i found myself missing, but i wasnt able toe figure it out was this:
You haven’t allocated an id yet to your switches but it’s very important you do that. Save the position in a variable. I’ve used fp_pos.

Can you maybe give me some help here as well. i think, then i will get it.
Doku was confusing me here

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

how can i add a name/id to my generated hardware switches and define specific pins?

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

Re: Need help with Switch-Sim-Status Syncronisation

#4 Post by Sling »

The id for any of the so called AM nodes is simply allocated at the beginning of the line with id =. The id has to be unique so call it something meaningful and then you can use it later as a reference in other functions that require an id.

Witzman
Posts: 26
Joined: Sun Sep 12, 2021 9:42 am

Re: Need help with Switch-Sim-Status Syncronisation

#5 Post by Witzman »

One step further but i didnt made it yet:

Current code:

Code: Select all


-- FUEL PUMP SWITCH
function fuel_pump_click_callback(position)
    local fp_pos = hw_switch_get_position(hw_switch_FuelPump)
    if position == 0 then
        fsx_event("TOGGLE_ELECT_FUEL_PUMP1")
    elseif position == 1 then
        fsx_event("TOGGLE_ELECT_FUEL_PUMP1")
    end
end

fuel_pump_switch_id = switch_add("toggle_off.png", "toggle_on.png", 160,202,63,63,fuel_pump_click_callback)

function new_fuel_pump_switch_pos(sw_on)
    if sw_on[1] == 0 then
        switch_set_position(fuel_pump_switch_id, 0)
    elseif sw_on[1] == 1 then
        switch_set_position(fuel_pump_switch_id, 1)
    end
end    

function new_fuel_pump_switch_pos_fsx(sw_on)
    sw_on = fif(sw_on, 1, 0)
    new_fuel_pump_switch_pos({sw_on})
end


fsx_variable_subscribe("GENERAL ENG FUEL PUMP SWITCH:1", "Bool", new_fuel_pump_switch_pos_fsx)

-- Panel Hardware Switches
hw_switch_FuelPump = hw_switch_add("ARDUINO_UNO_A_D9", fuel_pump_click_callback)

Last Problem is your code snippet, changed to my variables:

Code: Select all

if (sw_on and fp_pos == 0) or (not sw_on and fp_pos == 1) then
fsx_event("TOGGLE_ELECT_FUEL_PUMP1")
end
Where this needs to go? Inside the "function fuel_pump_click_callback"?
I dont get the concept, where "sw_on" comes from in this function

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

Re: Need help with Switch-Sim-Status Syncronisation

#6 Post by Sling »

It goes in either the FSX subscribe callback or the function it calls. While you are at it you did not interpret what I wrote correctly with regards to getting the position of the hardware switch. This also needs to go in the above location just ahead of the comparison. You have it in the switch callback which is wrong.

Witzman
Posts: 26
Joined: Sun Sep 12, 2021 9:42 am

Re: Need help with Switch-Sim-Status Syncronisation

#7 Post by Witzman »

Maybe this is not the proposed solution, but this is working for me - thanks for helping me out

Code: Select all

-- FUEL PUMP SWITCH
function fuel_pump_click_callback(position)
    if position == 0 then
        fsx_event("TOGGLE_ELECT_FUEL_PUMP1")
    elseif position == 1 then
        fsx_event("TOGGLE_ELECT_FUEL_PUMP1")
    end
fphw_pos = hw_switch_get_position(hw_switch_FuelPump)
fpsw_pos = switch_get_position(fuel_pump_switch_id)


if (fphw_pos == fpsw_pos) then 
else
fsx_event("TOGGLE_ELECT_FUEL_PUMP1")
end
end

fuel_pump_switch_id = switch_add("toggle_off.png", "toggle_on.png", 160,202,63,63,fuel_pump_click_callback)

function new_fuel_pump_switch_pos(sw_on)

    if sw_on[1] == 0 then
        switch_set_position(fuel_pump_switch_id, 0)
    elseif sw_on[1] == 1 then
        switch_set_position(fuel_pump_switch_id, 1)
    end
    
end    

function new_fuel_pump_switch_pos_fsx(sw_on)
    sw_on = fif(sw_on, 1, 0)
    new_fuel_pump_switch_pos({sw_on})

end


fsx_variable_subscribe("GENERAL ENG FUEL PUMP SWITCH:1", "Bool", new_fuel_pump_switch_pos_fsx)


hw_switch_FuelPump = hw_switch_add("ARDUINO_MEGA2560_B_A0", fuel_pump_click_callback)
Did this for all 7 switches and its working fine.

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

Re: Need help with Switch-Sim-Status Syncronisation

#8 Post by Sling »

That is indeed wrong. It is such a simple fix to move the get position and comparison to the subscribe callback rather than where you have it in the switch callback. Your solution needs the switch to be operated for them to be in sync. Doing it the correct way means they are always in sync even before the switch is touched on startup or if any commands are dropped.

Witzman
Posts: 26
Joined: Sun Sep 12, 2021 9:42 am

Re: Need help with Switch-Sim-Status Syncronisation

#9 Post by Witzman »

Played arround and now i got concept of named hardware.

Also edited the code like you mentioned and came up with this working solution.
Thanks for not posting code and inteadof this explaining the concepts.


Here my solution

Code: Select all

function fuel_pump_click_callback(position)
    if position == 0 then
        fsx_event("TOGGLE_ELECT_FUEL_PUMP1")
    elseif position == 1 then
        fsx_event("TOGGLE_ELECT_FUEL_PUMP1")
    end
end

fuel_pump_switch_id = switch_add("toggle_off.png", "toggle_on.png", 160,202,63,63,fuel_pump_click_callback)

function new_fuel_pump_switch_pos(sw_on)
    if sw_on[1] == 0 then
        switch_set_position(fuel_pump_switch_id, 0)
    elseif sw_on[1] == 1 then
        switch_set_position(fuel_pump_switch_id, 1)
    end
end    

function new_fuel_pump_switch_pos_fsx(sw_on)
fphw_pos = hw_switch_get_position(hw_switch_FuelPump)
fpsw_pos = switch_get_position(fuel_pump_switch_id)
    sw_on = fif(sw_on, 1, 0)
    new_fuel_pump_switch_pos({sw_on})
if (fphw_pos == fpsw_pos) then 
else
fsx_event("TOGGLE_ELECT_FUEL_PUMP1")
end
end


fsx_variable_subscribe("GENERAL ENG FUEL PUMP SWITCH:1", "Bool", new_fuel_pump_switch_pos_fsx)

Tetrachromat
Posts: 236
Joined: Sun Feb 14, 2021 6:55 pm

Re: Need help with Switch-Sim-Status Syncronisation

#10 Post by Tetrachromat »

HI @Witzman

The confusion stems from having 3 switches, that you want to have in sync:
- The cockpit switch in the sim (GENERAL ENG FUEL PUMP SWITCH:1)
- the swicth on the panel (fuel_pump_switch_id)
. the hardware switch (hw_switch_FuelPump)

You should question wheter you really need that switch on your panel. As you have a hardware switch, you don't really need a switch on the panel.

It would be sufficient to make the state of the hardware switch visible on the panel. For that you just need the two images and switch the 'visible' state accordingly.

Another issue is that you cannot put the FSX switch in a defined state. Your option is limited to toggling, through event 'TOGGLE_ELECT_FUEL_PUMP1'.
I would assume it is OFF on startup. Otherwise you need to sync the in sim switch (toggle the cockpit switch) to the state of your hardware switch manually once.

Also your code is highly bloated. I always wonder how much code one can write with such simple logic. But it may be due to limited knowledge of LUA.

Here the code I would use (untested):

Code: Select all

fuel_pump_switch_off = img_add( "toggle_off.png", 160, 202, 63,63 )
fuel_pump_switch_on  = img_add( "toggle_on.png", 160, 202, 63, 63 )
visible( fuel_pump_switch_on, false ) -- initially off

function fuel_pump_click_callback(_)
  fsx_event( "TOGGLE_ELECT_FUEL_PUMP1" )
end

hw_switch_FuelPump = hw_switch_add("ARDUINO_MEGA2560_B_A0", fuel_pump_click_callback)

function new_fuel_pump_switch_pos_fsx( sw_on )
  visible( fuel_pump_switch_on, sw_on )
end

fsx_variable_subscribe("GENERAL ENG FUEL PUMP SWITCH:1", "Bool", new_fuel_pump_switch_pos_fsx)
Cheers
Paul
Last edited by Tetrachromat on Mon Oct 04, 2021 12:48 pm, edited 5 times in total.

Post Reply