New bye with LUA. 3 push Buttons and 2 encoders

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

New bye with LUA. 3 push Buttons and 2 encoders

#1 Post by pepebky »

Hi to all. I am a total new bye with LUA scripts. I am modifying a Jeppesen FS200 Console with Arduino.
This console has two encoders for NAV1/2 and ADF frecuency and decimals.
And three buttons NAV1, NAV2 and ADF. Depending on which button I press, the encoders change the frequency of the respective system.
Button NAV1, the encoders change the freq of the NAV1 and so on.
I try with this code but I am at a standstill.

Code: Select all

function switch_state()

if hw_input_read(input_1)
    function encogrande_set(direction)
    if direction == 1 then
    xpl_command("sim/radios/stby_nav1_coarse_down")
    elseif direction == -1 then
    xpl_command("sim/radios/stby_nav1_coarse_up")
    end
    function encochico_set(direction)
    if direction == 1 then
    xpl_command("sim/radios/stby_nav1_fine_down")
    elseif direction == -1 then
    xpl_command("sim/radios/stby_nav1_fine_up")
    end
end
           
    elseif hw_input_read(input_2)
    function encogrande_set(direction)
    if direction == 1 then
    xpl_command("sim/radios/stby_nav2_coarse_down")
    elseif direction == -1 then
    xpl_command("sim/radios/stby_nav2_coarse_up")
    end
    function encochico_set(direction)
    if direction == 1 then
    xpl_command("sim/radios/stby_nav2_fine_down")
    elseif direction == -1 then
    xpl_command("sim/radios/stby_nav2_fine_up")
    end
end
    
    elseif hw_input_read(input_3)
    function encogrande_set(direction)
    if direction == 1 then
    xpl_command("sim/radios/stby_adf1_hundreds_thous_down")
    elseif direction == -1 then
    xpl_command("sim/radios/stby_adf1_hundreds_thous_up")
    end
    function encochico_set(direction)
    if direction == 1 then
    xpl_command("sim/radios/stby_adf1_ones_tens_down")
    elseif direction == -1 then
    xpl_command("sim/radios/stby_adf1_ones_tens_up")
    end
end           
end

input_1 = hw_input_add("Switch connection 1", function(state)
    switch_state() -- switch_state is called when the state of this pin changes
end)

input_2 = hw_input_add("Switch connection 2", function(state)
    switch_state() -- switch_state is called when the state of this pin changes
end)

input_3 = hw_input_add("Switch connection 3", function(state)
    switch_state() -- switch_state is called when the state of this pin changes
end)


-- We add a timer for redundancy
timer_start(nil, 500, function()
    switch_state()
end)

hw_dial_add("Encoder Grande", encogrande_set)
hw_dial_add("Encoder Chico", encochico_set)
hw_button_add("Sw NAV1", selnav1)
hw_button_add("Sw NAV2", selnav2)
hw_button_add("Sw ADF", seladf)
Thanks to all.

Jose

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

Re: New bye with LUA. 3 push Buttons and 2 encoders

#2 Post by Ralph »

You're doing an input read on a switch? Did you check the switch API carefully? 'hw_input_read' is only to be used with 'hw_input_add', not with a switch.
And those buttons, selnav1 etc... they don't exist. This whole script is some kind of mashup between different API functions :lol:

Having said this, does this help you any further? Get rid of those switches and input reads, use the buttons, those will bring you to your end goal.
Please use the code brackets the next time if you post a script, the </> button.

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

Re: New bye with LUA. 3 push Buttons and 2 encoders

#3 Post by pepebky »

Thanks for your help. I wil try to order and modify the script.

Jose

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

Re: New bye with LUA. 3 push Buttons and 2 encoders

#4 Post by pepebky »

Some time ago I had written this post above. After some research and reading I came up with a code that worked. I don't know if it is quite correct but it worked for me.
I did this in AM beta 4.1. Now when I changed it to a paid AM Home (V4.1.5) it does not work.
What can be happening. The project has 5 instruments and 2 hardware components that have the programming of switches and encoders. Here I copy the code. Thank you very much and sorry for the questions.

Code: Select all

function swnav1frec()
function nav1ent(direction)
    if direction == 1 then
    xpl_command("sim/radios/stby_nav1_coarse_up")
  elseif direction == -1 then
    xpl_command("sim/radios/stby_nav1_coarse_down")
  end
end

hw_dial_add("Frec Entera NAV1", nav1ent)

function nav1dec(direction)
    if direction == 1 then
    xpl_command("sim/radios/stby_nav1_fine_up")
  elseif direction == -1 then
    xpl_command("sim/radios/stby_nav1_fine_down")
  end
end

hw_dial_add("Frec Decimal NAV1", nav1dec)

end

hw_button_add("Switch Selector Frecuencia Nav1", swnav1frec)


function swnav2frec()
 function nav2ent(direction)
    if direction == 1 then
    xpl_command("sim/radios/stby_nav2_coarse_up")
  elseif direction == -1 then
    xpl_command("sim/radios/stby_nav2_coarse_down")
  end
end

hw_dial_add("Frec Entera NAV2", nav2ent)

function nav2dec(direction)
    if direction == 1 then
    xpl_command("sim/radios/stby_nav1_fine_up")
  elseif direction == -1 then
    xpl_command("sim/radios/stby_nav1_fine_down")
  end
end

hw_dial_add("Frec Decimal NAV2", nav2dec)     
end

hw_button_add("Switch Selector Frecuencia Nav2", swnav2frec)

function swadffrec()
function adfcie(direction)
    if direction == 1 then
    xpl_command("sim/radios/stby_adf1_hundreds_thous_up")
  elseif direction == -1 then
    xpl_command("sim/radios/stby_adf1_hundreds_thous_down")
  end
end

hw_dial_add("Frec Mil y Cientos ADF", adfcie)

function adfuni(direction)
    if direction == 1 then
    xpl_command("sim/radios/stby_adf1_ones_tens_up")
  elseif direction == -1 then
    xpl_command("sim/radios/stby_adf1_ones_tens_down")
  end
end

hw_dial_add("Frec Cent y Unidad ADF", adfuni)


end

hw_button_add("Switch Selector Frecuencia ADF", swadffrec)

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

Re: New bye with LUA. 3 push Buttons and 2 encoders

#5 Post by Keith Baxter »

Hi,

You cannot initiate a hardware dial outside of the initial logic lua execution.
So this will not work because the dials are within the button callback and are not initiated on first pass.

Code: Select all

function swnav1frec()
function nav1ent(direction)
    if direction == 1 then
    xpl_command("sim/radios/stby_nav1_coarse_up")
  elseif direction == -1 then
    xpl_command("sim/radios/stby_nav1_coarse_down")
  end
end

hw_dial_add("Frec Entera NAV1", nav1ent)

function nav1dec(direction)
    if direction == 1 then
    xpl_command("sim/radios/stby_nav1_fine_up")
  elseif direction == -1 then
    xpl_command("sim/radios/stby_nav1_fine_down")
  end
end

hw_dial_add("Frec Decimal NAV1", nav1dec)

end

hw_button_add("Switch Selector Frecuencia Nav1", swnav1frec)
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 

Post Reply