Using "hw switch get position"

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Post Reply
Message
Author
pepebky
Posts: 11
Joined: Mon Mar 29, 2021 12:06 pm

Using "hw switch get position"

#1 Post by pepebky »

Hello everyone.
I am modifying an old Jeppesen FS200 console to work with Xplane and arduino.
When starting AM it should take the position of the switches. Switch ON, Xplane ON. Switch OFF, Xplane OFF.
I used the command "hw switch get position". I am programming all the switches as one hardware.
The "hw switch get position" command would seem to work for NAV2 ON, but not for NAV1 ON.
Here is part of the code I am using.

Code: Select all

---------------------------------------------NAV1----------------------------------------------------
function swnav1on(position)
local position = hw_switch_get_position(swnav1on)
  if position == 0 then
   xpl_command("sim/radios/power_nav1_off")
  elseif position == 1 then
    xpl_command("sim/radios/power_nav1_on")
  end
end
swnav1on = hw_switch_add("ARDUINO_MEGA2560_A_D9", swnav1on)
---------------------------------------------NAV2----------------------------------------------------
function swnav2on(position)
local position = hw_switch_get_position(swnav2on)
  if position == 0 then
   xpl_command("sim/radios/power_nav2_off")
  elseif position == 1 then
    xpl_command("sim/radios/power_nav2_on")
  end
end
swnav2on = hw_switch_add("ARDUINO_MEGA2560_A_D50", swnav2on)

Thank you very much.

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

Re: Using "hw switch get position"

#2 Post by Ralph »

You don't have to use hw_switch_get_position. You already get the position. Also note that the name of your switch is exactly the same as your callback name. I'm not a Lua wizard, but think that will create conflicts. The example below should work.

Code: Select all

function swnav1on_callback(position)
  if position == 0 then
    xpl_command("sim/radios/power_nav1_off")
  elseif position == 1 then
    xpl_command("sim/radios/power_nav1_on")
  end
end
sw_nav1on = hw_switch_add("ARDUINO_MEGA2560_A_D9", swnav1on_callback)

pepebky
Posts: 11
Joined: Mon Mar 29, 2021 12:06 pm

Re: Using "hw switch get position"

#3 Post by pepebky »

Thank Ralph for your quick answer. I will modify my code and test it.

Jose

pepebky
Posts: 11
Joined: Mon Mar 29, 2021 12:06 pm

Re: Using "hw switch get position"

#4 Post by pepebky »

Ralph wrote: Thu Nov 17, 2022 1:21 pm You don't have to use hw_switch_get_position. You already get the position. Also note that the name of your switch is exactly the same as your callback name. I'm not a Lua wizard, but think that will create conflicts. The example below should work. The code below. Thank you very much.

Code: Select all

function swnav1on_callback(position)
  if position == 0 then
    xpl_command("sim/radios/power_nav1_off")
  elseif position == 1 then
    xpl_command("sim/radios/power_nav1_on")
  end
end
sw_nav1on = hw_switch_add("ARDUINO_MEGA2560_A_D9", swnav1on_callback)
This works well with NAV1 and 2. I tried to use it on others that had problems and it did not work there. It is the case of the autopilot, when opening AM it connects by itself even though it is switch is off.

Code: Select all

function swapon_callback(position)
    if position == 0 then
        xpl_command("sim/autopilot/servos_off_any")
    else
       xpl_command("sim/autopilot/servos_on")
    end
end
sw_apon = hw_switch_add("ARDUINO_MEGA2560_A_D26", swapon_callback)

function swaphdgsel_callback(position)
    if position == 0 then
        xpl_command("sim/autopilot/heading")
    elseif position == 1 then
        xpl_command("sim/autopilot/heading")                            
    end
end
sw_aphdgsel = hw_switch_add("ARDUINO_MEGA2560_A_D33", swaphdgsel_callback)


function swapalt_callback(position)
    if position == 0 then
        xpl_command("sim/autopilot/altitude_hold")
    else
       xpl_command("sim/autopilot/altitude_hold")
    end
end
sw_apalt = hw_switch_add("ARDUINO_MEGA2560_A_D32", swapalt_callback)

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

Re: Using "hw switch get position"

#5 Post by Ralph »

You'll have to use prints and see what happens.

Post Reply