Synchronize switches with xplane not working

Support for Arduino in combination with Air Manager and Air Player

Moderators: russ, Ralph

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

Re: Synchronize switches with xplane not working

#41 Post by Sling »

xplanearg wrote: Fri Apr 16, 2021 11:30 am ok, i think i wont be able to find help here.
If you listened to the advice being given you would of had the solution already and moved onto other things. Your loss.

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

Re: Synchronize switches with xplane not working

#42 Post by Keith Baxter »

Joe,

@xplanearg Is anxious to get his setup working as he wants. We should understand that.

Patients and understanding is needed. I will not walk away and give up when I know that there is a solution no matter how indifferent views are.

At the end of all this. The OP will have a solution if he is prepared to listen and accept advice.

More we cannot do. The ball is in his court.


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 

xplanearg
Posts: 44
Joined: Mon May 04, 2020 10:37 am

Re: Synchronize switches with xplane not working

#43 Post by xplanearg »

Hi, ive finally found a solution that works for most situations. For anyone else with this issue, here it is:

PROBLEM
Using example code to add a switch from airmanager's documentation is not enough for them to work properly.

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

Code: Select all

-- This function is called every time the battery switch has a new position
function switch_battery_callback(position)
  print("The battery switch got changed to position " .. position)

  if position == 0 then
    fsx_event("BATTERY_BUS_CONTACT_SET", 0)
    xpl_command("sim/electrical/battery_1_off")
  elseif position == 1 then
    fsx_event("BATTERY_BUS_CONTACT_SET", 1)
    xpl_command("sim/electrical/battery_1_on")
  end
end

-- Here we create the switch. In case of a two-way switch use 2, in case of a one-way switch use 1. In this example we use 2 (two-way).
hw_switch_add("Battery", 2, switch_battery_callback)
If you start first Airmanager and second x-plane, Airmanager ignores the switches position and they are not synchronized with x-plane. You have to flip every single switch in your cockpit for them to synchronize which is unpractical in my opinion

SOLUTION:
Add a switch as follows:

Code: Select all

function switch_36_callback(position)
  local position = hw_switch_get_position(switch_36_id)
  print("The battery switch got changed to position " .. position)
  if position == 0 then
    xpl_command("sim/electrical/battery_1_off")
  elseif position == 1 then
    xpl_command("sim/electrical/battery_1_on")
  end
end
switch_36_id = hw_switch_add("ARDUINO_MEGA2560_A_D36", switch_36_callback)
xpl_dataref_subscribe("sim/cockpit/electrical/battery_on", "INT", switch_36_callback)
Im purposely ignoring the hw_switch_add argument that gets passed to the switch callback. im reading for the switch state inside the switch callback function. Also im registering this switch callback to a dataref, this should trigger the callback when ever the given dataref changes

The second part is to add a sync routine like so:

Code: Select all


function sync()
  switch_36_callback(0)
end

function event_callback(event)
  if event == "FLIGHT_SIM_CHANGED" or event == "STARTED" then
    sync()
    if xpl_connected() then
      print(" ------------- connected to X-plane ----------- ")
      sync()
    end
  end
end

event_subscribe(event_callback)
it does not matter what argument you pass to the switch callback in the sync routine as it will get ignored. Inside the callback the actual switch position will be read
So far this works well in most situations.
hope that helps someone else

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

Re: Synchronize switches with xplane not working

#44 Post by Sling »

Each to their own.

However I suggest anyone reading this please don’t use the code posted by xplanearg in the post before this because you will get yourself in a mess with it. The conventional way to do this and in line with how the API is supposed to be used is based around the code I posted earlier in this thread. It can be enhanced for different scenarios as was suggested to xplanearg but obviously they have chosen a different path.

Just my recommendation.

Post Reply