Results 1 to 8 of 8
  1. #1
    300+ Forum Addict Tom_G_2010's Avatar
    Join Date
    Mar 2011
    Location
    Central Mass
    Posts
    437
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Question Is it possible to emulate joystick buttons or send LVars with Link2FS and Arduino?

    I'm well into a Cessna Sim Pit build and have 5 Arduino cards and growing interfaced via Link2FSMulti that handle my annuniators, Wet Compass, Outside Air Temp Gauge, motorized Elevator Trim wheel, Flap Control and position indicator, and a full avionics stack. I also use several Leo Bodnar cards for conventional joystick functions.

    Up to this point I haven't run into anything I couldn't handle with this setup. However, I recently I moved my A2A C172 from the old desktop sim to the Sim Pit and I've hit an issue that really has me scratching my head. A2A has a very advanced Avionics stack with functionality well beyond what FSX has ever had and as such many of the buttons, including the Comm/Audio selector panel are handled using LVars.

    I really don't want to replace my Arduino/Link2FS based Avioncs stack as I have a great deal of money and time invested in it. But it seems the only way I could get it to play nice with the A2A aircraft is if I can get Link2FS to emulate a Joystick button output which I could then pick up in FSUIPC to launch a Lua script and send the needed LVar.

    Is something like this even possible? Am I over complicating things? Has anyone else interfaced an Arduino setup with an A2A or other aircraft that needs LVars?

    HELP!!!

    Thanks!
    Tom G.
    https://www.facebook.com/mycessnasim PC: Intel Core i7 Haswell @ 3.8GHz, 8Gb Ram, Win 7 64Bit, dual SSDs, GeFroce 780 SIM: FSX w/Aclrtn Pk, FSUIPC4, ASN, UTX, GEX, REX 4

  2. #2
    150+ Forum Groupie
    Join Date
    Nov 2013
    Location
    Evansville, Indiana
    Posts
    243
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Is it possible to emulate joystick buttons or send LVars with Link2FS and Arduino?

    Tom,
    You could actually move Link2FS out of the loop and replace it with a LUA script.
    Or you can do what I did...Use Link2FS for the displays and majority of tasks and then off-load the LVARs stuff thru a secondary Arduino that talks to a LUA script.
    As you may well know, Arduino's can only talk to one thing on a comm port.
    I took a secondary Arduino that I call a "proxy", it sends and receives to the Primary Arduino via the "Wire" protocol/library. (Its very similar to serial communication with some differences.)
    The "Proxy" also sends and receives to a LUA script via the serial comm/USB connection.
    There isn't much code to the proxy.
    What ever comes in on the "wire" it sends out the "serial".
    And what ever comes in the "serial" it sends out the "wire".
    The wire protocol uses the I2C pins on the Arduinos to communicate.

    https://www.arduino.cc/en/reference/wire

    If you know nothing of LUA then it is a daunting task.
    If you know LUA its a piece of cake.

    When I started LUA I knew nothing of it.
    LINDA Tracer Function is your friend, but since you know about LVARS you are probably aware of this.
    I use the above technique to make my MCP fully functional with the MilViz 737-200.

    ~Fess
    I will only ever be, half the Geek that I wished I was.
    TheGeekForge.Com

  3. #3
    300+ Forum Addict Tom_G_2010's Avatar
    Join Date
    Mar 2011
    Location
    Central Mass
    Posts
    437
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Is it possible to emulate joystick buttons or send LVars with Link2FS and Arduino?

    Thanks! I'll have to explore that. I've done some Lua scripting but nothing very fancy. Setting up the serial communication is a bit more of a mystery for me. What's at the other end? Serial com between the arduino and... fsuipc, or triggering a Lua script directly, or ???
    https://www.facebook.com/mycessnasim PC: Intel Core i7 Haswell @ 3.8GHz, 8Gb Ram, Win 7 64Bit, dual SSDs, GeFroce 780 SIM: FSX w/Aclrtn Pk, FSUIPC4, ASN, UTX, GEX, REX 4

  4. #4
    150+ Forum Groupie
    Join Date
    Nov 2013
    Location
    Evansville, Indiana
    Posts
    243
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Is it possible to emulate joystick buttons or send LVars with Link2FS and Arduino?

    Tom,

    Once you have an example, the serial communication is not that difficult. For ease of understanding and transition, I used some of Jim's techniques when receiving from LUA.
    On the send side of my LUA script, I am only sending the states of illuminated switches to arduino as "0" or "1".
    If you remember the EQUALS() function with the switch case; I had the LUA send the states like this, "=a1".
    Because I am running a mixed bag in my Arduino, I made a different function called EQUALS_WIRE() that gets information that LUA passes thru the proxy arduino. The EQUALS_WIRE() Function only processes information from the "Wire", where the EQUALS() Function processes only from Serial.

    Anyway here is my LUA script with very little remarks. It is my intention to remark for education purposes for the masses.

    You will notice that for inputs to LUA I used some of Jim's naming convention, like "B04". I did this for familiarity for myself. If and when I revise, I may well change this.

    This script does a few things.
    It connects to an Arduino on COM8.
    It listens for inputs on the COM.
    When and input comes, it compares it to IF statements.
    The IF statements will then write states to LVAR's or do "ipc.control"
    Some of the conditions require the reading of LVAR states.
    There is a section with a list of LVARs that are defined to variable names in the script with their states.
    Below that, the script looks for changes in states of those listed LVARs.
    If it sees a difference, then it sends the new state to Arduino.

    Finally the script gets a 50ms rest at the bottom. This is important, as the un-rested script will compete with other things in your rig for cycles.
    I do Streaming on Twitch and was having lag issues with my stream. Once I figured out the 50ms rest, everything smoothed out like glass.


    Code:
    speed = 115200
    handshake = 0
     
    dev = com.open("COM8", speed, handshake)
    if dev == 0 then
        ipc.display("Could not open device port")
        ipc.exit()
    end
    while 1 do
        red_string = ""
        char = ""
        if (com.test(dev)>0) then
            n = com.test(dev)
            while(com.test(dev)>0) do
                char = com.read(dev,1)
                red_string =  red_string..char
                ipc.display(red_string)
            end
        end
        --if (red_string == "B04") then
        --    ipc.control(65798)
        --end
        if (red_string == "A61") then
            MaxBankKnob = MaxBankKnob + 1
            if (MaxBankKnob > 3) then
                MaxBankKnob = 3
            end
            ipc.writeLvar("B737_MaxBankKnob", MaxBankKnob)
        end
        if (red_string == "A60") then
            MaxBankKnob = MaxBankKnob - 1
            if (MaxBankKnob < 0) then
                MaxBankKnob = 0
            end
            ipc.writeLvar("B737_MaxBankKnob", MaxBankKnob)
        end
        if (red_string == "B17") then
            
            ipc.control(77831)
            if (ipc.readLvar("B737_VS_SET") == 1) then
                ipc.writeLvar("L:B737_VS_SET", 0)
            else 
                ipc.writeLvar("L:B737_VS_SET", 1)
            end
        end
        if (red_string == "B07") then
            
            ipc.control(77833)
            if (ipc.readLvar("AUTOPILOT NAV1 LOCK") == 1) then
                ipc.writeLvar("L:AUTOPILOT NAV1 LOCK", 0)
            else 
                ipc.writeLvar("L:AUTOPILOT NAV1 LOCK", 1)
            end
        end
        if (red_string == "B20") then
            ipc.control(77827)
            if (ipc.readLvar("L:B737_IAS_MACH_SWITCH") == 1) then
                ipc.writeLvar("L:B737_IAS_MACH_SWITCH", 0)
            else 
                ipc.writeLvar("L:B737_IAS_MACH_SWITCH", 1)
            end
        end
        if (red_string == "B21") then
            ipc.control(77855)
            ipc.control(77856)
            if (ipc.readLvar("L:B737_LVL_CHG") == 1) then
                ipc.writeLvar("L:B737_LVL_CHG", 0)
            else 
                ipc.writeLvar("L:B737_LVL_CHG", 1)
            end
        end
        if (red_string == "B04") then
            ipc.control(77829)
            if (ipc.readLvar("L:AUTOPILOT HEADING LOCK") == 1) then
                ipc.writeLvar("L:AUTOPILOT HEADING LOCK", 0)
            else 
                ipc.writeLvar("L:AUTOPILOT HEADING LOCK", 1)
            end
        end
        if (red_string == "BL301" and ipc.readLvar("B737_flight_director_pilot") == 0) then
            ipc.control(77824)
            ipc.writeLvar("L:B737_flight_director_pilot", 1)
        end
        if (red_string == "BL300" and ipc.readLvar("B737_flight_director_pilot") == 1) then
            ipc.control(77824)
            ipc.writeLvar("L:B737_flight_director_pilot", 0)
        end
        if (red_string == "BR301" and ipc.readLvar("B737_flight_director_copilot") == 0) then
            ipc.control(77825)
            ipc.writeLvar("L:B737_flight_director_copilot", 1)
        end
        if (red_string == "BR300" and ipc.readLvar("B737_flight_director_copilot") == 1) then
            ipc.control(77825)
            ipc.writeLvar("L:B737_flight_director_copilot", 0)
        end
        if (red_string == "B10" and ipc.readLvar("B737_LNAV_SWITCH") == 0) then
            ipc.control(77840)
            ipc.writeLvar("L:B737_LNAV_SWITCH", 1)
        
        elseif (red_string == "B10" and ipc.readLvar("B737_LNAV_SWITCH") == 1) then
            ipc.control(77839)
            ipc.writeLvar("L:B737_LNAV_SWITCH", 0)
        end
        if (red_string == "A54" ) then
            ipc.control(69766)
            if (ipc.readLvar("L:B737_DriveMode") == 0) then
            
                ipc.writeLvar("L:B737_FMS_NAV_SWITCH", 1)
                ipc.writeLvar("L:B737_DriveMode", 1)
                ipc.writeLvar("L:B737_FMS_NAV_SWITCH", 0)
            else
                ipc.writeLvar("L:B737_FMS_NAV_SWITCH", 1)
                ipc.writeLvar("L:B737_DriveMode", 0)
                ipc.writeLvar("L:B737_FMS_NAV_SWITCH", 0)
            end
        end
        if (red_string == "B030" and ipc.readLvar("B737_AP_System_A_Switch") ~= 2) then
            ipc.control(77836)
            ipc.writeLvar("L:B737_AP_System_A_Switch", 2)
        end
        if (red_string == "B031" and ipc.readLvar("B737_AP_System_A_Switch") ~= 1) then
            ipc.control(77835)
            ipc.writeLvar("L:B737_AP_System_A_Switch", 1)
        end
        
        if (red_string == "B52" and ipc.readLvar("B737_AP_System_B_Switch") ~= 2) then
            ipc.control(77852)
            ipc.writeLvar("L:B737_AP_System_B_Switch", 2)
        end
        if (red_string == "B51" and ipc.readLvar("B737_AP_System_B_Switch") ~= 1) then
            ipc.control(77851)
            ipc.writeLvar("L:B737_AP_System_B_Switch", 1)
        end
        if (red_string == "B50") then
            ipc.control(77850)
            ipc.writeLvar("L:B737_AP_System_B_Switch", 0)
            ipc.control(77854)
            ipc.writeLvar("L:B737_AP_System_A_Switch", 0)
        end
            -- Get all of the data we want to display
        CMD_CWS_A_annun = ipc.readLvar("B737_AP_System_A_Switch")
        CMD_CWS_B_annun = ipc.readLvar("B737_AP_System_B_Switch")
        MaxBankKnob = ipc.readLvar("B737_MaxBankKnob")
        SPD_annun  = ipc.readLvar("AUTOPILOT AIRSPEED HOLD")
        ALT_annun  = ipc.readLvar("AUTOPILOT ALTITUDE LOCK")
        APP_annun  = ipc.readLvar("AUTOPILOT APPROACH HOLD")
        HDG_annun  = ipc.readLvar("AUTOPILOT HEADING LOCK")
        VLOC_annun = ipc.readLvar("AUTOPILOT NAV1 LOCK")
        VS_annun   = ipc.readLvar("B737_VS_SET")
        LNAV_annun = ipc.readLvar("B737_LNAV_SWITCH")
        FD_Pilot_annun = ipc.readLvar("B737_flight_director_pilot")
        FD_CoPilot_annun = ipc.readLvar("B737_flight_director_copilot")
        MACH_SW = ipc.readLvar("B737_IAS_MACH_SWITCH")
        FMS_NAV = ipc.readLvar("B737_DriveMode")
        EPR_annun = ipc.readLvar("EPR_HOLD")
        LVL_CHG_annun = ipc.readLvar("B737_LVL_CHG")
        
        if (LVL_CHG_annun ~= old_LVL_CHG_annun) then
            com.write (dev, "=f"..LVL_CHG_annun)
            old_LVL_CHG_annun = LVL_CHG_annun
        end
        if (EPR_annun ~= old_EPR_annun) then
            com.write (dev, "=e"..EPR_annun)
            old_EPR_annun = EPR_annun
        end
        if (CMD_CWS_A_annun ~= old_CMD_CWS_A_annun) then
            com.write (dev, "=a"..CMD_CWS_A_annun)
            old_CMD_CWS_A_annun = CMD_CWS_A_annun
        end
        
        if (CMD_CWS_B_annun ~= old_CMD_CWS_B_annun) then
            com.write (dev, "=b"..CMD_CWS_B_annun)
            old_CMD_CWS_B_annun = CMD_CWS_B_annun
        end
        if (FMS_NAV ~= old_FMS_NAV) then
            com.write (dev, "=l"..FMS_NAV)
            old_FMS_NAV = FMS_NAV
        end
        if (MACH_SW ~= old_MACH_SW) then
            com.write (dev, "=h"..MACH_SW)
            old_MACH_SW = MACH_SW
        end
        if (FD_Pilot_annun ~= old_FD_Pilot_annun) then
            com.write (dev, "=q"..FD_Pilot_annun)
            old_FD_Pilot_annun = FD_Pilot_annun
        end
        if (FD_CoPilot_annun ~= old_FD_CoPilot_annun) then
            com.write (dev, "=r"..FD_CoPilot_annun)
            old_FD_CoPilot_annun = FD_CoPilot_annun
        end
        if (SPD_annun ~= old_SPD_annun) then
            com.write (dev, "=s"..SPD_annun)
            old_SPD_annun = SPD_annun
        end
        if (ALT_annun ~= old_ALT_annun) then
            com.write (dev, "=k"..ALT_annun)
            old_ALT_annun = ALT_annun
            ipc.display("=k"..ALT_annun)
        end
        if (APP_annun ~= old_APP_annun) then
            com.write (dev, "=m"..APP_annun)
            old_APP_annun = APP_annun
        end
        if (HDG_annun ~= old_HDG_annun) then
            com.write (dev, "=j"..HDG_annun)
            old_HDG_annun = HDG_annun
            ipc.display("=j"..HDG_annun)
        end
        if (VLOC_annun ~= old_VLOC_annun) then
            com.write (dev, "=p"..VLOC_annun)
            old_VLOC_annun = VLOC_annun
        end
        if (VS_annun ~= old_VS_annun) then
            com.write (dev, "=v"..VS_annun)
            old_VS_annun = VS_annun
        end
        if (LNAV_annun ~= old_LNAV_annun) then
            com.write (dev, "=o"..LNAV_annun)
            old_LNAV_annun = LNAV_annun
        end
    
    
        -- Sleep for 50 mSecs so the update gets done roughly 20 times per second
        ipc.sleep(50)
    
    
    end
    I can post the Arduino Proxy code next....


    ~Fess
    I will only ever be, half the Geek that I wished I was.
    TheGeekForge.Com

  5. #5
    150+ Forum Groupie
    Join Date
    Nov 2013
    Location
    Evansville, Indiana
    Posts
    243
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Is it possible to emulate joystick buttons or send LVars with Link2FS and Arduino?

    Tom,

    Here is my Arduino Proxy code.

    There is likely better code to do this but this is what I currently have working.


    Code:
    // Created on or about June 6, 2015.
    // This Sketch was created for an Arduino to pass information, 
    // as a proxy between another Arduino and a LUA script. 
    // Arduino <-> Arduino Proxy <-> LUA Script
    // 
    // The Following code was modified, by Fess, from the original, 
    // Wire Master Writer by Nicholas Zambetti 
    
    
    
    
    #include 
    int i;
    
    
    int light;
    
    
    
    
    char Data_to_Mega;
    char Data_from_Mega;
    
    
    unsigned long time;
    unsigned long last_time;
    //************************************************************
    //************************************************************
    void setup()
    {
    	last_time = millis();
    	Wire.begin(1); // join i2c bus, our address is (1)
    	Wire.onReceive(receiveEvent); // register event
    	Serial.begin(115200);
    }
    //************************************************************
    //************************************************************
    
    
    //************************************************************
    //************************************************************
    void loop()
    {
    	time = millis();
    	if (time < 10500)
    	{
    		BLINK();	// Blinks an led on the MCP to let me know the wire is working
    	}
    	
    //********************************************************************************************************
    	// ********  FROM THE LUA TO THE MEGA  *********
    	if (Serial.available()) // Is something in the buffer from LUA Script that needs sent to the Mega?
    	{
    		Wire.beginTransmission(2);    // open the wire to send info to mega at address (2)
    		for(i = 0; i < 3; i++)		  // everything we send is only 3 characters in this instance
    		{ 
    			Data_to_Mega = getChar(); // get the character from the serial
    			Wire.write(Data_to_Mega); // send it to the Mega, and the mega will sort it out
    		}
    		Wire.endTransmission();		  // close the wire
    	}
    }
    //************************************************************
    //************************************************************
    
    
    //************************************************************
    //************************************************************
    
    
    //  ****** FROM THE MEGA TO THE LUA **********
    void receiveEvent(int howMany)  // when the wire senses something coming thru it does this
    {
    	while(0 < Wire.available()) // loop through all
    	{
    		Data_from_Mega = Wire.read(); // receive byte as a character from Mega
    		Serial.write (Data_from_Mega);// send it to LUA Script
    
    
    	}
    }
    //************************************************************
    //************************************************************
    
    
    //************************************************************
    //************************************************************
    
    
    char getChar()                  	// Get a character from the serial buffer
    {
    	while(Serial.available() == 0);	// wait for data
    	return((char)Serial.read());  	// Thanks Doug
    }  
    //************************************************************
    //************************************************************
    
    
    //************************************************************
    //************************************************************
    
    
    //  This section is not necessary, I only use it for confirmation that the arduinos are talking.
    void BLINK()		
    {
    	if (time - last_time > 500 and light == 0)
    	{
    		Wire.beginTransmission(2);
    		Wire.write("=z1");
    		Wire.endTransmission();
    		light=1;
    		last_time = time;
    	}
    	if (time - last_time >500 and light == 1)
    	{
    		Wire.beginTransmission(2);
    		Wire.write("=z0");
    		Wire.endTransmission();
    		light=0;
    		last_time = time;
    	}
    }
    //************************************************************
    //************************************************************

    ~Fess
    I will only ever be, half the Geek that I wished I was.
    TheGeekForge.Com

  6. #6
    300+ Forum Addict Tom_G_2010's Avatar
    Join Date
    Mar 2011
    Location
    Central Mass
    Posts
    437
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Is it possible to emulate joystick buttons or send LVars with Link2FS and Arduino?

    Fess, THX!!!!

    At first glance it makes sense to me. Crazy hours at work this week but, I'm hoping to have some time to go over it in depth between now and the weekend.

    I usually do a fair job of picking up on code samples like this but I'm certainly playing in a different space here. I'm looking forward to it. I'm sure I'll come up with more quesitons than answers but that'a half the fun...
    https://www.facebook.com/mycessnasim PC: Intel Core i7 Haswell @ 3.8GHz, 8Gb Ram, Win 7 64Bit, dual SSDs, GeFroce 780 SIM: FSX w/Aclrtn Pk, FSUIPC4, ASN, UTX, GEX, REX 4

  7. #7
    300+ Forum Addict Tom_G_2010's Avatar
    Join Date
    Mar 2011
    Location
    Central Mass
    Posts
    437
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Is it possible to emulate joystick buttons or send LVars with Link2FS and Arduino?

    Fess, it's been months since this post was last updated but I am finally circling back to this project and making some headway. I have to apologize to you. I lost the link to this post and could not for the life of me remeber who or where I got the idea to create a lua portal. And, of course while poking around looking for some other information I cam acorss this thread and realized it was you! THANKS YOU!!!!!

    I have made some headway and have my Com1 and Com2 Adio selector butons working on my test bench so the code is till a mess and not fully baked but, it is starting to take shape. As soon as I have something a little more firm and presetable I will be starting a post with all the details.

    Again, my apologies for having lost track of where I got the idea and my thanks for the inspiration and help!
    https://www.facebook.com/mycessnasim PC: Intel Core i7 Haswell @ 3.8GHz, 8Gb Ram, Win 7 64Bit, dual SSDs, GeFroce 780 SIM: FSX w/Aclrtn Pk, FSUIPC4, ASN, UTX, GEX, REX 4

  8. #8
    150+ Forum Groupie
    Join Date
    Nov 2013
    Location
    Evansville, Indiana
    Posts
    243
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Is it possible to emulate joystick buttons or send LVars with Link2FS and Arduino?

    Tom,
    Good to hear.
    I'm still around but sometimes I don't get back to these threads in a hurry.

    ~Fess
    I will only ever be, half the Geek that I wished I was.
    TheGeekForge.Com