Simple "pause" switch (newbie level)

Questions about deployment and use of Air Manager Instruments

Moderators: russ, Ralph

Post Reply
Message
Author
chorobo@ymail.com
Posts: 2
Joined: Fri May 26, 2023 10:10 am

Simple "pause" switch (newbie level)

#1 Post by chorobo@ymail.com »

Hello friends,

Because I have very little knowledge of coding, I'm trying to adapt the Generic switch (J. Zahar based on Russ Barlow original Work) for different purposes. For example a simple pause/unpause switch.

Code: Select all

switch_display_type= 5                                         -- change the number according to the list above (1-8)

-- Xplane Function operated by the switch --
-- modify the variable according to taste --
xpl_switch_function="sim/cockpit/electrical/beacon_lights_on"   -- Xplane function/variable to perform
xpl_switch_type="INT"                                           -- Xplane function variable type returned 
-- Fsx Function operated by the switch
-- modify the variable according to taste --
fsx_switch_var="PAUSE"                                   -- FSX Event 
fsx_switch_function="PAUSE_TOGGLE"                      -- FSX action to perform
fsx_switch_type="Bool"                                          -- FSX function variable type returned 

------------------------------------------------------------
--  END OF FUNCTION DEFINITION                            --
--  Do not modifiy code beyond this point                 --
------------------------------------------------------------
I tried this but it does not work.
I tried also other variables found on this page with the same result. Sometime the sim does interact with the switch and makes it react. But I have been unable to make the switch produce any changes within MSFS2020.
What am I doing wrong please? Sorry in advance for these beginer questions, I'm so lost and I don't know where to start.

SimPassion
Posts: 5336
Joined: Thu Jul 27, 2017 12:22 am

Re: Simple "pause" switch (newbie level)

#2 Post by SimPassion »

Hi @chorobo@ymail.com, it strangely looks we don't have the whole script with missing called functions, without which process will not work as expected ??? 🤔

SimPassion
Posts: 5336
Joined: Thu Jul 27, 2017 12:22 am

Re: Simple "pause" switch (newbie level)

#3 Post by SimPassion »

Weird thing to include the "@" char in a username or nickname here on the forums ... ! 🤨

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

Re: Simple "pause" switch (newbie level)

#4 Post by JackZ »

Mmmh

I reckon that this instrument is quite old and probably needs an update

Let me a couple of days, I will come up with a new and updated version, with Configuration via user selected field and the list of Var/event/function retrieved via JSON file

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

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

Re: Simple "pause" switch (newbie level)

#5 Post by JackZ »

In the meantime you can use this code.
1-The "Switch Type" is now a user property (1 to 8).
2- A legend can be added to the switch as a user property (leave the field empty if no legend)
image.png
image.png (10.05 KiB) Viewed 1644 times
(Create a clone of the instrument, then copy/Paste (completely replace the existing code) by this one

Code: Select all

------------------------------------------------------------
--           VERTICAL GENERIC SWITCH Type 1                --
--           J. Zahar   v5.0 May 2023                              --  
-------------------------------------------------------------------

----------------- FUNCTION DEFINITION ----------------------
-- MODIFY the variables values to get the desired function    --
-------------------------------------------------------------------

-- Switch legend, leave the field empty if no Legend
switch_name_prop = user_prop_add_string("Switch Legend", "", "Switch Legend, leave empty if no legend")
switch_name=user_prop_get(switch_name_prop)
switch_name=string.sub(switch_name, 1,12 )
switch_color="white"  --The color, can be color string like "red", "blue", "cyan", etc. etc. Can also be a hex value, "#FFFFFF"     
                           
-- Xplane Function operated by the switch when ON --
-- modify the variable according to taste --
xpl_switch_function="sim/operation/pause_toggle"   -- Xplane function/variable to perform
xpl_switch_type="INT"                                           -- Xplane function variable type returned 

-- Fsx/MSFS Function operated by the switch
-- modify the Simvariable according to taste -- 
fsx_switch_var=""                                                   -- FSX/MSFS SimVar !!!! ONLY BOOLEAN TYPE ARE SUPPORTED
fsx_switch_type="Bool"                                              -- FSX function variable type returned 
                        ----   OR ---
-- modify the Event according to taste --
fsx_switch_event_ON="PAUSE_ON"                                      -- FSX/MSFS Event to perform
fsx_switch_event_OFF="PAUSE_OFF"                                    -- FSX/MSFS Event to perform


------------------------------------------------------------
--  END OF FUNCTION DEFINITION                            --
--  Do not modify the code beyond this point           --
------------------------------------------------------------
-- Type of switch to display : 
--     1: Vertical Rocker switch (color RED) with complete black bezel (standalone switch) 
--     2: Vertical Rocker switch (color RED) with left bezel only (to be assembled with another center or right switch)
--     3: Vertical Rocker switch (color RED) with right bezel only (to be assembled with another center or left switch)
--     4: vertical Rocker switch (color RED) without left/right bezel (to be assembled in between another left or right switch)
--     5: Vertical Rocker switch (color WHITE) with complete black bezel (standalone switch) 
--     6: Vertical Rocker switch (color WHITE) with left bezel only (to be assembled with another center or right switch)
--     7: Vertical Rocker switch (color WHITE) with right bezel only (to be assembled with another center or right switch)
--     8: vertical Rocker switch (color WHITE) without left/right bezel (to be assembled in between another left or right switch)

switch_type = user_prop_add_integer("Switch Type",1, 8, 1, "Type of switch (1-8)")
switch_display_type= user_prop_get(switch_type)   

-- SWITCH
function switch_click_callback(position)

    if position == 0 then
        switch_set_position(switch_id, 1)
        xpl_dataref_write(xpl_switch_function,xpl_switch_type, 1)
        fsx_event(fsx_switch_event_ON)
        fs2020_event(fsx_switch_event_ON)
        fsx_variable_write(fsx_switch_var,fsx_switch_type,true)
        fs2020_variable_write(fsx_switch_var,fsx_switch_type,true)
    elseif position == 1 then
        switch_set_position(switch_id, 0)
        xpl_dataref_write(xpl_switch_function,xpl_switch_type, 0)
        fsx_event(fsx_switch_event_OFF)
        fs2020_event(fsx_switch_event_OFF)
        fsx_variable_write(fsx_switch_var,fsx_switch_type,false)
        fs2020_variable_write(fsx_switch_var,fsx_switch_type,false)
    end

end

if switch_display_type==1 then
	switch_id = switch_add("red_ver_complete_off.png", "red_ver_complete_on.png", 0,0,70,130,switch_click_callback)
	end
if switch_display_type==2 then
	switch_id = switch_add("red_ver_left_off.png", "red_ver_left_on.png", 0,0,70,130,switch_click_callback)
	end
if switch_display_type==3 then
	switch_id = switch_add("red_ver_right_off.png", "red_ver_right_on.png", 0,0,70,130,switch_click_callback)
	end
if switch_display_type==4 then
	switch_id = switch_add("red_ver_center_off.png", "red_ver_center_on.png", 0,0,70,130,switch_click_callback)
	end
if switch_display_type==5 then
	switch_id = switch_add("white_ver_complete_off.png", "white_ver_complete_on.png", 0,0,70,130,switch_click_callback)
	end
	if switch_display_type==6 then
	switch_id = switch_add("white_ver_left_off.png", "white_ver_left_on.png", 0,0,70,130,switch_click_callback)
	end
if switch_display_type==7 then
	switch_id = switch_add("white_ver_right_off.png", "white_ver_right_on.png", 0,0,70,130,switch_click_callback)
	end
if switch_display_type==8 then
	switch_id = switch_add("white_ver_center_off.png", "white_ver_center_on.png", 0,0,70,130,switch_click_callback)
	end

legend = txt_add(switch_name, "roboto_regular.ttf; size:11; color:"..switch_color.."; halign:center;",0, 5, 70, 20)

function new_switch_pos(sw_on)

    if sw_on == 0 then
        switch_set_position(switch_id, 0)
    elseif sw_on == 1 then
        switch_set_position(switch_id, 1)
    end
    
end


function new_switch_pos_fsx(sw_on)

    sw_on = fif(sw_on, 1, 0)
    new_switch_pos(sw_on)

end

xpl_dataref_subscribe(xpl_switch_function,xpl_switch_type,new_switch_pos)
fsx_variable_subscribe(fsx_switch_var, fsx_switch_type, new_switch_pos_fsx)
fs2020_variable_subscribe(fsx_switch_var, fsx_switch_type, new_switch_pos_fsx)
My YouTube Chanel on the A320 (Real SOPs by an Airline Pilot IRL):
https://www.youtube.com/playlist?list=P ... 0Q6SBASRqJ

chorobo@ymail.com
Posts: 2
Joined: Fri May 26, 2023 10:10 am

Re: Simple "pause" switch (newbie level)

#6 Post by chorobo@ymail.com »

Thank you so much. I works fine.
The label ideal near the button is very great !

Post Reply