Jump to content
The simFlight Network Forums

Programming a three way switch with FSUIPC 4.9 in FSX .SE I need help-solved


Recommended Posts

 

Hi John I am a newbie with FSUIPC LVArs, i have been playing around with my brother's Carenado King Air B200, and the LINDA console has found a number of Lvars that are not listed as FSX controls that i can use...... With most of them, i have been assigning them in a B200.MCRO file ( placed in the modules folder), mostly with the toggle parameter but also with the SET parameter, and then assigning a key press in FSUIPC, and they work like magic. They are all working fine  However, about 4 or 5  switches in the virtual cockpit are three way switches !!!.  For example, there is a 3 position windshield anti-ice switch in the virtual cockpit, which is defined in the console ( as ASD_SWITCH_WNDSHLD_ANTIICE_PILOT.. I found the switch has three positions: OFF,  ON and TEST Its values are 0 1, and -1 . I have assigned  3 different keys on my keyboard  (QWE)  using FSUIPC  with the LVar above and different parameters( -1,0,1) and can press each key to each switch position correctly, even though it is hugely wasteful using three keys ( i just wanted to check if it works ). What i need is a FSUIPC  command? that will cycle between the three positions on the one key!!! Do i need a LUA script --can you give me an example ? or  can i do it easily in FSUIPC.. im stumped !!!! any help would be greatly appreciated           

 

Link to comment
Share on other sites

  • wade1 changed the title to Programming a three way switch with FSUIPC 4.9 in FSX .SE I need help

I see John is on leave so i went to the newest version of Chat GPT 4o.. i have a subscription, and posed the question. i was surprised that it answered such a specific question with ease .. a bit scary actually the power of AI  . However, i have not tested it yet on my PC  simulator. .I would be interested in Johns critique of the GPT CHAT 4o answer and code but big UPS to John for connecting FSUIPC to LUA wot a godsend that is

"

To create a LUA script for FSUIPC that cycles through the positions of the three-way switch (OFF, ON, and TEST) using one key, you can use the following script as a starting point. This script will increment the switch position each time the key is pressed and wrap around when it reaches the end.

Here is the script:

Create a new LUA script file and name it something like cycle_switch.lua.

Add the following code to the file:

Save the script in the Modules folder of your FSX installation.

Open FSUIPC and assign a key press to run this LUA script. To do this:

Go to the "Key Presses" tab in FSUIPC.

Press "Set" to define the key you want to use for cycling the switch.

In the action list, select "Lua <your_script_name>". In this case, it will be "Lua cycle_switch".

With this script, pressing the defined key will cycle the windshield anti-ice switch through its positions: OFF (0), ON (1), and TEST (-1). This way, you only need to use one key to control the switch.

 

-- Define the LVar for the windshield anti-ice switch

local lvar_name = "ASD_SWITCH_WDSHLD_ANTIICE_PILOT"

 

-- Define the switch positions

local switch_positions = {-1, 0, 1}

 

-- Function to cycle through switch positions

function cycleSwitch()

    -- Get the current value of the LVar

    local current_position = ipc.readLvar(lvar_name)

   

    -- Find the index of the current position

    local current_index = nil

    for i, v in ipairs(switch_positions) do

        if v == current_position then

            current_index = i

            break

        end

    end

   

    -- Determine the next position

    local next_index = current_index + 1

    if next_index > #switch_positions then

        next_index = 1

    end

    local next_position = switch_positions[next_index]

   

    -- Set the LVar to the next position

    ipc.writeLvar(lvar_name, next_position)

end

 

-- Call the function to cycle the switch position
cycleSwitch()

Link to comment
Share on other sites

Interestingly Chat GPT gave a completely different method for Prepar 3D but this probably due to the fact its an LLM and will produce a different response on repeated queries
 

-- Variable to track the state of the switch
local switch_state = 0  -- Assuming it starts at OFF

function cycle_antiice_switch()
    -- Increment the state
    switch_state = switch_state + 1

    -- If the state exceeds 2, reset to 0
    if switch_state > 2 then
        switch_state = 0
    end

    -- Map the incrementing state to LVar values
    local lvar_value = 0
    if switch_state == 0 then
        lvar_value = 0  -- OFF
    elseif switch_state == 1 then
        lvar_value = 1  -- ON
    elseif switch_state == 2 then
        lvar_value = -1  -- TEST
    end

    -- Set the LVar
    ipc.writeLvar("ASD_WNDSHLD_ANTIICE_PILOT", lvar_value)
end

-- Assign the function to a key or joystick button in FSUIPC
event.key("Q", "down", "cycle_antiice_switch")
 

Link to comment
Share on other sites

  • wade1 changed the title to Programming a three way switch with FSUIPC 4.9 in FSX .SE I need help-solved

Now i asked Chat GPT to solve another issue i have, a lua script that cycles the switch, ... great !!!! but on keyboard 1 also what i want to do is run all my lvar  switches on the B200 on a second keyboard so i can free up keyboard 1 for other stuff. The only program i could find that recognises extra keyboards is HIDmacros, which is an excellent program and conveniently  has an interface for Sim Connect, which means i can get quite a few fsx commands onto keyboard 2 but it doesn't interface directly with fsuipc ? However, the author has created some code/functions using offsets to enable Hid macros to send my lua scripts to Fsuipc and use them on keyboard 2!!!. But it all looked complicated to me John talked about another method using IPCready.lua which was a bit beyond me also , quite frankly 
Chat GPT to the rescue; here is the answer it gave me  which worked perfectly !!!
 

  1. Launch HIDMacros and ensure your second keyboard is recognized and listed under Devices.
  2. Navigate to the Macros tab, select New, and create a macro for each key you want to program. Name the macro appropriately.
  3. Use the Scan button to record the key press from the second keyboard.
  4. is command instructs FSUIPC to execute the CycleSwitch Lua script when the associated key on the keyboard is pressed. Ensure the script name matches exactly what is configured in FSUIPC.

    HIDMacros.SetFSUIPCString &H0D70, 40, "Lua CycleSwitch"
     

Step 3: Save and Compile

  1. Click Compile to compile the script within HIDMacros.
  2. Save your configuration to ensure all changes are retained.

 

  • Like 1
Link to comment
Share on other sites

On 5/14/2024 at 1:49 AM, wade1 said:

I see John is on leave so i went to the newest version of Chat GPT 4o.. i have a subscription, and posed the question. i was surprised that it answered such a specific question with ease .. a bit scary actually the power of AI  . However, i have not tested it yet on my PC  simulator. .I would be interested in Johns critique of the GPT CHAT 4o answer and code but big UPS to John for connecting FSUIPC to LUA wot a godsend that is

Wow - I am surprised that ChatGP can do that! Looks good. Note that it is generally easier to use macros to cycle through lvar values (ie. use a macro for the lvar with the Cyclic action), but this only works if the values start at 0, so you would need to use a lua script for an lvar that has values that start at any other value.

 

 

Link to comment
Share on other sites

thanx for the tip john Unfortunately, these b200 lvars always seem to start at -1 .. but I admit i was shocked at chat gpt's knowledge of fsuipc, which it must have stolen off you via the forums and every other small snippet of information about fsuipc on the web but integrating those snippets  by parallel processing it via a neural network thats the mind bend !!! ..chatgpt 4.0 has 175 billion  parameters and is seriously good, but the new chat gpt 5 which is being trained as we speak and hopefully released in aug - sep will have 5 trillion parameters111 and  will  also have vastly improved neural networks .!!!! i predict it will write lua scripts for even the most complicated panels in msfs 2020

Link to comment
Share on other sites

Well, to a certain extent  John my son and nephew are both professional AI programmers, and they both said it was tremendous for the tedious coding donkeywork, but they also told me that expert human input is still needed for the truly complex stuff. However, it would save you a lot of your precious time dealing with basic, repetitive queries 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.