Beta 35 with Arduino hangs when connecting to FS2020

Discuss suspected bugs with other users and Sim Innovations Staff

Moderators: russ, Ralph

Message
Author
User avatar
bartroozendaal
Posts: 30
Joined: Wed Jan 06, 2021 8:01 pm
Contact:

Beta 35 with Arduino hangs when connecting to FS2020

#1 Post by bartroozendaal »

Note: the script 'hangs'. I can still use AM itself...

This happens 9 out of 10 times. I have an Arduino script, connecting a couple of buttons and 5 rotary encoders. I also use a LCD display. With the latest beta 35 it looks like the panel code 'hangs' after first subscribing to a fs2020-variable, or getting new values for them (can't really figure out when *exactly* it happens. I have reflashed the Arduino, I also restart the fsbridge. I close and re-open the panel and even restarting AM or the whole machine. 9 out of 10 times it doesn't start. 1 out of 10 times it does, and then the script works fine. For reference I'll post the script below (it is work in progress and will be cleaned up a bit later on). I also created a test script with just the com1-code; that gives the same effect.

Also, AM reports a new version of the fsbridge all the time. Doesn't matter if I install the linked setup or not; it keeps reporting that. I've also tried a clean install of AM beta 35; to no avail.

I no longer have an older beta version. Can that be made available? Can't find it that quick on the site.

This happened before but also after today's update of fs2020. So, it looks like this is a AM issue

Any hints on how to debug this particular issue?

The script:

Code: Select all

-- /*
-- +----------------------------------------------------------+
-- | |
-- | +-------------------+ (53) (52) |
-- | | | |
-- | | | (38) (40) |
-- | | | [28/29] [24/25] |
-- | +-------------------+ |
-- | |
-- | (48) (51) (50) (39) (41) (43) |
-- | [30/31] [26/27] [22/23] |
-- | |
-- | (32) (46) (47) (44) (37) (42) |
-- | |
-- +----------------------------------------------------------+
-- */

CLOCKWISE = 1
COUNTERCLOCKWISE = -1

btntogglecom1highlow = 38
btntogglecom1 = 53
knobcom1a = 28
knobcom1b = 29

btntogglecom2 = 52
btntogglecom2highlow = 40
knobcom2a = 24
knobcom2b = 25

btnonoffpower = 48
btnonoffavionics = 51
btnonoffpitotheat = 50

knobvor1a = 26
knobvor1b = 27
knobvor2a = 22
knobvor2b = 23

btnheadingtoggle = 39
knobheadinga = 30
knobheadingb = 31

btnonofffuelpump = 32
btnonoffbeaconlights = 46
btnonofflandinglights = 47
btnonofftaxilights = 44
btnonoffnavigationlights = 37
btnonoffstrobelights = 42

display = hw_chr_display_add("HD44780", 2, 16, "ARDUINO_MEGA2560_A_D6", "ARDUINO_MEGA2560_A_D7", "ARDUINO_MEGA2560_A_D5", "ARDUINO_MEGA2560_A_D4", "ARDUINO_MEGA2560_A_D3", "ARDUINO_MEGA2560_A_D2")
function debugprint(what)
-- hw_chr_display_set_text(display, 0, what)
print(what)
end

-- ************* COM1 **********
-- Switch com1 mode (inc/dec high/low)
com1high = true
function btntogglecom1highlowon()
end
function btntogglecom1highlowoff()
com1high = not (com1high)
end
hw_button_add("ARDUINO_MEGA2560_A_D" .. btntogglecom1highlow, btntogglecom1highlowon, btntogglecom1highlowoff)

-- Switch frequencies COM1
function btntogglecom1on()
end
function btntogglecom1off()
fs2020_event("COM_STBY_RADIO_SWAP")
end
hw_button_add("ARDUINO_MEGA2560_A_D" .. btntogglecom1, btntogglecom1on, btntogglecom1off)
-- Radio COM1 frequency
function knobcom1callback(direction)
if (direction == CLOCKWISE) then
if (com1high) then
fs2020_event("COM_RADIO_WHOLE_INC")
else
fs2020_event("COM_RADIO_FRACT_INC")
end
else
if (com1high) then
fs2020_event("COM_RADIO_WHOLE_DEC")
else
fs2020_event("COM_RADIO_FRACT_DEC")
end
end
end

function newCom1Frequencies(com1, com1stby)
hw_chr_display_set_text(display, 0, string.format("%03.03f", com1) .. " " .. string.format("%03.03f", com1stby))
end

hw_dial_add("ARDUINO_MEGA2560_A_D" .. knobcom1a, "ARDUINO_MEGA2560_A_D" .. knobcom1b, "TYPE_1_DETENT_PER_PULSE",
knobcom1callback)
fs2020_variable_subscribe("COM ACTIVE FREQUENCY:1", "Mhz", "COM STANDBY FREQUENCY:1", "MHz", newCom1Frequencies)

-- ************* ENDOF COM1 **********


-- ************* COM2 ****************
-- Switch frequencies COM2
function btntogglecom2on()
end
function btntogglecom2off()
fs2020_event("COM2_RADIO_SWAP")
end
hw_button_add("ARDUINO_MEGA2560_A_D" .. btntogglecom2, btntogglecom2on, btntogglecom2off)

-- Switch com2 mode (inc/dec high/low)
com2high = true
function btntogglecom2highlowon()
end
function btntogglecom2highlowoff()
com2high = not (com2high)
end
hw_button_add("ARDUINO_MEGA2560_A_D" .. btntogglecom2highlow, btntogglecom2highlowon, btntogglecom2highlowoff)

-- Radio COM2 frequency
function knobcom2callback(direction)
if (direction == CLOCKWISE) then
if (com2high) then
fs2020_event("COM2_RADIO_WHOLE_INC")
else
fs2020_event("COM2_RADIO_FRACT_INC")
end
else
if (com2high) then
fs2020_event("COM2_RADIO_WHOLE_DEC")
else
fs2020_event("COM2_RADIO_FRACT_DEC")
end
end

end
hw_dial_add("ARDUINO_MEGA2560_A_D" .. knobcom2a, "ARDUINO_MEGA2560_A_D" .. knobcom2b, "TYPE_1_DETENT_PER_PULSE",
knobcom2callback)

function newCom2Frequencies(com2, com2stby)
hw_chr_display_set_text(display, 1, string.format("%03.03f", com2) .. " " .. string.format("%03.03f", com2stby))
end
fs2020_variable_subscribe("COM ACTIVE FREQUENCY:2", "Mhz", "COM STANDBY FREQUENCY:2", "Mhz", newCom2Frequencies)

-- ********** ENDCOM2 ******************

-- Turn power on/off
masteralternator = false
function btnonoffpoweron()
triggerOptionally("TOGGLE_MASTER_ALTERNATOR", masteralternator, true)
end
function btnonoffpoweroff()
triggerOptionally("TOGGLE_MASTER_ALTERNATOR", masteralternator, false)
end
hw_button_add("ARDUINO_MEGA2560_A_D" .. btnonoffpower, btnonoffpoweron, btnonoffpoweroff)

-- Turn avionics on/off
function btnonoffavionicson()
fs2020_event("AVIONICS_MASTER_SET", 1)
end
function btnonoffavionicsoff()
fs2020_event("AVIONICS_MASTER_SET", 0)
end
hw_button_add("ARDUINO_MEGA2560_A_D" .. btnonoffavionics, btnonoffavionicson, btnonoffavionicsoff)

-- Turn pitot head on/off
function btnonoffpitotheaton()
fs2020_event("PITOT_HEAT_SET", 1)
end
function btnonoffpitotheatoff()
fs2020_event("PITOT_HEAT_SET", 0)
end
hw_button_add("ARDUINO_MEGA2560_A_D" .. btnonoffpitotheat, btnonoffpitotheaton, btnonoffpitotheatoff)

-- Toggle heading / ADF
headingmode = false
function btnheadingtoggleon()
end
function btnheadingtoggleoff()
headingmode = not (headingmode)
end
hw_button_add("ARDUINO_MEGA2560_A_D" .. btnheadingtoggle, btnheadingtoggleon, btnheadingtoggleoff)

-- Turn fuel pump on/off
fuelpump = false
function btnonofffuelpumpon()
triggerOptionally("FUEL_PUMP", fuelpump, true)
end
function btnonofffuelpumpoff()
triggerOptionally("FUEL_PUMP", fuelpump, false)
end
hw_button_add("ARDUINO_MEGA2560_A_D" .. btnonofffuelpump, btnonofffuelpumpon, btnonofffuelpumpoff)

-- Turn beacon lights on/off
beaconlights = false
function btnonoffbeaconlightson()
triggerOptionally("TOGGLE_BEACON_LIGHTS", beaconlights, true)
end
function btnonoffbeaconlightsoff()
triggerOptionally("TOGGLE_BEACON_LIGHTS", beaconlights, false)
end
hw_button_add("ARDUINO_MEGA2560_A_D" .. btnonoffbeaconlights, btnonoffbeaconlightson, btnonoffbeaconlightsoff)

-- Turn landing lights on/off
function btnonofflandinglightson()
fs2020_event("LANDING_LIGHTS_SET", 1)
end
function btnonofflandinglightsoff()
fs2020_event("LANDING_LIGHTS_SET", 0)
end
hw_button_add("ARDUINO_MEGA2560_A_D" .. btnonofflandinglights, btnonofflandinglightson, btnonofflandinglightsoff)

-- Turn taxi lights on/off
taxilights = false
function btnonofftaxilightson()
triggerOptionally("TOGGLE_TAXI_LIGHTS", taxilights, true)
end
function btnonofftaxilightsoff()
triggerOptionally("TOGGLE_TAXI_LIGHTS", taxilights, false)
end
hw_button_add("ARDUINO_MEGA2560_A_D" .. btnonofftaxilights, btnonofftaxilightson, btnonofftaxilightsoff)

-- Turn taxi lights on/off
navlights = false
function btnonoffnavigationlightson()
triggerOptionally("TOGGLE_NAV_LIGHTS", navlights, true)
end
function btnonoffnavigationlightsoff()
triggerOptionally("TOGGLE_NAV_LIGHTS", navlights, false)
end
hw_button_add("ARDUINO_MEGA2560_A_D" .. btnonoffnavigationlights, btnonoffnavigationlightson,
btnonoffnavigationlightsoff)

-- Turn taxi lights on/off
function btnonoffstrobelightson()
fs2020_event("STROBES_SET", 1)
end
function btnonoffstrobelightsoff()
fs2020_event("STROBES_SET", 0)
end
hw_button_add("ARDUINO_MEGA2560_A_D" .. btnonoffstrobelights, btnonoffstrobelightson, btnonoffstrobelightsoff)

-- Change heading
function knobheadingcallback(direction)
if (direction == CLOCKWISE) then
if (headingmode) then
fs2020_event("HEADING_BUG_INC")
else
fs2020_event("ADF_CARD_INC")
end
else
if (headingmode) then
fs2020_event("HEADING_BUG_DEC")
else
fs2020_event("ADF_CARD_DEC")
end
end
end
hw_dial_add("ARDUINO_MEGA2560_A_D" .. knobheadinga, "ARDUINO_MEGA2560_A_D" .. knobheadingb, "TYPE_1_DETENT_PER_PULSE",
knobheadingcallback)

-- Change VOR1
function knobvor1callback(direction)
if (direction == CLOCKWISE) then
fs2020_event("VOR1_OBI_INC")
else
fs2020_event("VOR1_OBI_DEC")
end
end
hw_dial_add("ARDUINO_MEGA2560_A_D" .. knobvor1a, "ARDUINO_MEGA2560_A_D" .. knobvor1b, "TYPE_1_DETENT_PER_PULSE",
knobvor1callback)

-- Change VOR2
function knobvor2callback(direction)
if (direction == CLOCKWISE) then
fs2020_event("VOR2_OBI_INC")
else
fs2020_event("VOR2_OBI_DEC")
end
end
hw_dial_add("ARDUINO_MEGA2560_A_D" .. knobvor2a, "ARDUINO_MEGA2560_A_D" .. knobvor2b, "TYPE_1_DETENT_PER_PULSE",
knobvor2callback)

function updateVariables(newMasterAlternator, newBeaconLights, newTaxiLights, newNavLights, newFuelPump)
masteralternator = newMasterAlternator
beaconlights = newBeaconLights
taxilights = newTaxiLights
navlights = newNavLights
fuelpump = newFuelPump
end

function triggerOptionally(event, oldvalue, newvalue)
if (not (oldvalue == newvalue)) then
fs2020_event(event)
end
end

fs2020_variable_subscribe("GENERAL ENG MASTER ALTERNATOR", "Bool", "LIGHT BEACON", "Bool", "LIGHT TAXI", "Bool",
"LIGHT NAV", "Bool", "GENERAL ENG FUEL PUMP SWITCH:1", "Bool", updateVariables)

debugprint("Ready...")
Last edited by bartroozendaal on Wed Apr 14, 2021 11:38 am, edited 2 times in total.
Bart Roozendaal
https://fsarduino.com

User avatar
Sling
Posts: 5237
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: Beta 35 with Arduino hangs when connecting to FS2020

#2 Post by Sling »

To start you should run in Create/edit and see if there are an errors reported. You can use the debugging tools or simply use some prints placed in your code for testing purposes.

I must say that the code you posted is quite long and a bit messy so to expect someone to be able to help from that is a long shot. It would be better to post a smaller script that is not working or export the whole instrument to a .siff file and post here.

It’s probably a script issue but as I said that might take a while to establish.

User avatar
bartroozendaal
Posts: 30
Joined: Wed Jan 06, 2021 8:01 pm
Contact:

Re: Beta 35 with Arduino hangs when connecting to FS2020

#3 Post by bartroozendaal »

logic.lua
(7.57 KiB) Downloaded 191 times
There definitely is a bug in AM; there are no errors in the debugger when running it from Edit script.

I am thinking it is in either the functions for the display, or in the subscription to variables from fs2020 (the last line in the script below). I also think that whatever engine is running the script inside AM is leaking, keeping an instance alive, maybe locking some memory location. That might introduce a deadlock to the memory in the new instance of that engine whenever a new panel or running script is started, making that instance freeze. It doesn't look there is a time limit on that lock.

The effects I am seeing kind of give those hints. It is not consistent, but pretty frequent. I carefully went through my code, putting parts of it in, taking parts of it out. It happens intermittently, without any clear indication what leads to the issue. The above analysis is just a hunch based om quite a bit of software development experience. I'm not a Java expert though, and don't know how to best debug AM. I'm willing to help out though as much as I can.

Below you find a bit of the script I to debug it. For reference, I'll also attach the complete (cleaned up a bit...) code. I also attached the siff file.

** The last bit of code I used for debugging purposes **

Code: Select all

CLOCKWISE = 1
COUNTERCLOCKWISE = -1

btntogglecom1highlow = 38
btntogglecom1 = 53
knobcom1a = 28
knobcom1b = 29

btntogglecom2 = 52
btntogglecom2highlow = 40
knobcom2a = 24
knobcom2b = 25

btnonoffpower = 48
btnonoffavionics = 51
btnonoffpitotheat = 50

knobvor1a = 26
knobvor1b = 27
knobvor2a = 22
knobvor2b = 23

btnheadingtoggle = 39
knobheadinga = 30
knobheadingb = 31

btnonofffuelpump = 32
btnonoffbeaconlights = 46
btnonofflandinglights = 47
btnonofftaxilights = 44
btnonoffnavigationlights = 37
btnonoffstrobelights = 42

display = hw_chr_display_add("HD44780", 2, 16, "ARDUINO_MEGA2560_A_D6", "ARDUINO_MEGA2560_A_D7",
              "ARDUINO_MEGA2560_A_D5", "ARDUINO_MEGA2560_A_D4", "ARDUINO_MEGA2560_A_D3", "ARDUINO_MEGA2560_A_D2")

function triggerOptionally(event, oldvalue, newvalue)
    if (not (oldvalue == newvalue)) then
        fs2020_event(event)
    end
end

-- ************* COM1 **********
-- Switch com1 mode (inc/dec high/low)
whatcom = "FRACT"
function btntogglecom1highlowon()
end
function btntogglecom1highlowoff()
    whatcom = fif(whatcom == "FRACT", "WHOLE", "FRACT")
end
hw_button_add("ARDUINO_MEGA2560_A_D" .. btntogglecom1highlow, btntogglecom1highlowon, btntogglecom1highlowoff)

-- Switch frequencies COM1
function btntogglecom1on()
end
function btntogglecom1off()
    fs2020_event("COM_STBY_RADIO_SWAP")
end
hw_button_add("ARDUINO_MEGA2560_A_D" .. btntogglecom1, btntogglecom1on, btntogglecom1off)

-- Radio COM1 frequency
function knobcom1callback(direction)
    fs2020_event("COM_RADIO_" .. whatcom .. fif(direction == CLOCKWISE, "_INC", "_DEC"))
end

function newCom1Frequencies(com1, com1stby)
    hw_chr_display_set_text(display, 0, string.format("%03.03f", com1) .. "  " .. string.format("%03.03f", com1stby))
end

hw_dial_add("ARDUINO_MEGA2560_A_D" .. knobcom1a, "ARDUINO_MEGA2560_A_D" .. knobcom1b, "TYPE_1_DETENT_PER_PULSE",
    knobcom1callback)
fs2020_variable_subscribe("COM ACTIVE FREQUENCY:1", "Mhz", "COM STANDBY FREQUENCY:1", "MHz", newCom1Frequencies)

-- ************* ENDOF COM1 **********

-- Turn power on/off
masteralternator = false
function btnonoffpoweron()
    triggerOptionally("TOGGLE_MASTER_ALTERNATOR", masteralternator, true)
end
function btnonoffpoweroff()
    triggerOptionally("TOGGLE_MASTER_ALTERNATOR", masteralternator, false)
end
hw_button_add("ARDUINO_MEGA2560_A_D" .. btnonoffpower, btnonoffpoweron, btnonoffpoweroff)

-- *** Read variables from FS -----
function updateVariables(newMasterAlternator, newBeaconLights, newTaxiLights, newNavLights, newFuelPump)
    masteralternator = newMasterAlternator
    beaconlights = newBeaconLights
    taxilights = newTaxiLights
    navlights = newNavLights
    fuelpump = newFuelPump
end

fs2020_variable_subscribe("GENERAL ENG MASTER ALTERNATOR", "Bool", "LIGHT BEACON", "Bool", "LIGHT TAXI", "Bool",
    "LIGHT NAV", "Bool", "GENERAL ENG FUEL PUMP SWITCH:1", "Bool", updateVariables)
Bart - .siff
(9.16 KiB) Downloaded 184 times
Last edited by bartroozendaal on Wed Apr 14, 2021 8:51 am, edited 1 time in total.
Bart Roozendaal
https://fsarduino.com

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

Re: Beta 35 with Arduino hangs when connecting to FS2020

#4 Post by Keith Baxter »

Hi,

Just a small heads up.
It is easier for us to read code and log text it it is placed within the code boxes.

You will see an Icon </> next to the 66 you used. That is the better option.

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 

User avatar
bartroozendaal
Posts: 30
Joined: Wed Jan 06, 2021 8:01 pm
Contact:

Re: Beta 35 with Arduino hangs when connecting to FS2020

#5 Post by bartroozendaal »

Done ;-) I was looking at all variations in the 'Select code' drop down box, but that seems to always add superfluous returns. Anyways, this works better; see the original post
Bart Roozendaal
https://fsarduino.com

User avatar
bartroozendaal
Posts: 30
Joined: Wed Jan 06, 2021 8:01 pm
Contact:

Re: Beta 35 with Arduino hangs when connecting to FS2020

#6 Post by bartroozendaal »

I have now a procedure to consistently reproduce (and reset) the issue. I have added this instrument to the free C172 panel. Btw: the effect is also there when running the script from the editor.

1) Have FS2020 up and running.
2) Start AM, wait for the Arduino to be connected
3) Show the C172 panel
4) My instrument works.
5) Change some settings, just to make sure it works and the C172 panel or radios gets updated
6) Right click on the panel and close it
7) Show the C172 panel.
9) Change some settings, just to make sure it works and the C172 panel or radios gets updated
9) If my instrument works, go to step 6 and repeat until it doesn't work anymore. Takes between 1 and 5 attempts typically
10) Shut down AM
11) Restart the FSBridge service
12) Unplug the Arduino (reset alone is not enough)
13) Start AM, wait for the Arduino to be connected
14) Show the C172 panel

And my instrument works again

This is the panel I am using, with the button / rotary encoder assignment with it

https://fsarduino.com/Buttonbox-software-done

Image

Code: Select all

-- /*
-- +----------------------------------------------------------+
-- |                                                          |
-- |   +-------------------+             (53)     (52)        |
-- |   |                   |                                  |
-- |   |                   |             (38)     (40)        |
-- |   |                   |             [28/29]  [24/25]     |
-- |   +-------------------+                                  |
-- |                                                          |
-- |     (48)    (51)    (50)    (39)    (41)     (43)        |
-- |                             [30/31] [26/27]  [22/23]     |
-- |                                                          |
-- |     (32)    (46)    (47)    (44)    (37)     (42)        |
-- |                                                          |
-- +----------------------------------------------------------+
-- */
Bart Roozendaal
https://fsarduino.com

User avatar
Sling
Posts: 5237
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: Beta 35 with Arduino hangs when connecting to FS2020

#7 Post by Sling »

bartroozendaal wrote: Wed Apr 14, 2021 8:11 am There definitely is a bug in AM; there are no errors in the debugger when running it from Edit script.
That's a very strong claim with not much evidence yet.

I've had a quick look at your code because any longer would be a lot longer. You have an unusual way of writing your code but i guess if it makes sense to you that is the main thing. One thing i did happen to notice was your use of buttons for things that are actually switches. I wonder if this is the best because you effectively have several button functions all waiting to complete the release. Not sure if this will cause any issues or not but its certainly not normal to do it this way when there is a switch function specifically for switches.

I read your original post again and perhaps you have a network or hardware connection issue. It will take a little time after AM boots for the hardware to be detected so if you are starting your panel very soon after AM starts that can cause problems. You can detect when the hardware is available for use using the hw_connected() function.

On the network side i wouldn't assume its not the issue just because you believe nothing may have changed with that. I've had issues before when apparently nothing had changed. Try running through the connection issues troubleshooting steps listed on the SI wiki to see if that helps.

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

Re: Beta 35 with Arduino hangs when connecting to FS2020

#8 Post by Keith Baxter »

Very nice pit.

The code presentation is perfect.
Unfortunately I do not have FS2020 so cannot help much. The SI guys are your best chance of help.


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 

User avatar
bartroozendaal
Posts: 30
Joined: Wed Jan 06, 2021 8:01 pm
Contact:

Re: Beta 35 with Arduino hangs when connecting to FS2020

#9 Post by bartroozendaal »

Sling wrote: Wed Apr 14, 2021 9:27 am
bartroozendaal wrote: Wed Apr 14, 2021 8:11 am There definitely is a bug in AM; there are no errors in the debugger when running it from Edit script.
That's a very strong claim with not much evidence yet.
Well, I can now repeat the issue at will, see the follow up sequence I found out. This happens without changing anything to the script, so...
I've had a quick look at your code because any longer would be a lot longer. You have an unusual way of writing your code but i guess if it makes sense to you that is the main thing. One thing i did happen to notice was your use of buttons for things that are actually switches. I wonder if this is the best because you effectively have several button functions all waiting to complete the release. Not sure if this will cause any issues or not but its certainly not normal to do it this way when there is
a switch function specifically for switches.
I'll see if changing to switches helps. Thanks for the pointer
I read your original post again and perhaps you have a network or hardware connection issue. It will take a little time after AM boots for the hardware to be detected so if you are starting your panel very soon after AM starts that can cause problems. You can detect when the hardware is available for use using the hw_connected() function.

On the network side i wouldn't assume its not the issue just because you believe nothing may have changed with that. I've had issues before when apparently nothing had changed. Try running through the connection issues troubleshooting steps listed on the SI wiki to see if that helps.
I'll see if I can introduce some safety in the code using hw_connected(). Thanks for that pointer too.

The issue happens in running AM on the same machine as FS2020, as well as running it from a different machine in the network. It doesn't make a difference. I've also used earlier versions of the script in beta33, and never had the issue. All the above leads me to believe there is something not right in AM. Not saying that the issue is not elsewhere (in the network, any hardware I wired, faulty switches, display, or anything else), but AM should not simply 'hang' the script. That is a bug in my book, no matter what the cause.
Bart Roozendaal
https://fsarduino.com

User avatar
bartroozendaal
Posts: 30
Joined: Wed Jan 06, 2021 8:01 pm
Contact:

Re: Beta 35 with Arduino hangs when connecting to FS2020

#10 Post by bartroozendaal »

Based on the feedback from Sling, I updated my code to use switches where I can. Also, I'm printing an error in case hardware is not connected.

Code is cleaner, but alas, non of these changes have a positive effect. The problem is still there. Still thinking I'm hitting a AM-bug here.

Is beta-33 still available somewhere?

Code: Select all

-- /*
-- +----------------------------------------------------------+
-- |                                                          |
-- |   +-------------------+             (53)     (52)        |
-- |   |                   |                                  |
-- |   |                   |             (38)     (40)        |
-- |   |                   |             [28/29]  [24/25]     |
-- |   +-------------------+                                  |
-- |                                                          |
-- |     (48)    (51)    (50)    (39)    (41)     (43)        |
-- |                             [30/31] [26/27]  [22/23]     |
-- |                                                          |
-- |     (32)    (46)    (47)    (44)    (37)     (42)        |
-- |                                                          |
-- +----------------------------------------------------------+
-- */
local CLOCKWISE = 1
local COUNTERCLOCKWISE = -1

local btntogglecom1highlow = 38
local btntogglecom1 = 53
local knobcom1a = 28
local knobcom1b = 29

local btntogglecom2 = 52
local btntogglecom2highlow = 40
local knobcom2a = 24
local knobcom2b = 25

local btnonoffpower = 48
local btnonoffavionics = 51
local btnonoffpitotheat = 50

local knobvor1a = 26
local knobvor1b = 27
local knobvor2a = 22
local knobvor2b = 23

local btnheadingtoggle = 39
local knobheadinga = 30
local knobheadingb = 31

local btnonofffuelpump = 32
local btnonoffbeaconlights = 46
local btnonofflandinglights = 47
local btnonofftaxilights = 44
local btnonoffnavigationlights = 37
local btnonoffstrobelights = 42

local display

local whatcom = "FRACT"
local whatcom2 = "FRACT"
local masteralternator = false
local headingmode = "HEADING_BUG"
local fuelpump = false
local beaconlights = false
local taxilights = false
local navlights = false

function addHardware()
    display = hw_chr_display_add("HD44780", 2, 16, "ARDUINO_MEGA2560_A_D6", "ARDUINO_MEGA2560_A_D7",
                  "ARDUINO_MEGA2560_A_D5", "ARDUINO_MEGA2560_A_D4", "ARDUINO_MEGA2560_A_D3", "ARDUINO_MEGA2560_A_D2")
    if (not (hw_connected(display))) then
        return false
    end
    if (not (hw_connected(hw_button_add("ARDUINO_MEGA2560_A_D" .. btntogglecom1highlow, noop, btntogglecom1highlowoff)))) then
        return false
    end
    if (not (hw_connected(hw_button_add("ARDUINO_MEGA2560_A_D" .. btntogglecom1, noop, btntogglecom1off)))) then
        return false
    end
    if (not (hw_connected(hw_dial_add("ARDUINO_MEGA2560_A_D" .. knobcom1a, "ARDUINO_MEGA2560_A_D" .. knobcom1b,
                              "TYPE_1_DETENT_PER_PULSE", knobcom1callback)))) then
        return false
    end
    if (not (hw_connected(hw_button_add("ARDUINO_MEGA2560_A_D" .. btntogglecom2highlow, noop, btntogglecom2highlowoff)))) then
        return false
    end
    if (not (hw_connected(hw_button_add("ARDUINO_MEGA2560_A_D" .. btntogglecom2, noop, btntogglecom2off)))) then
        return false
    end
    if (not (hw_connected(hw_dial_add("ARDUINO_MEGA2560_A_D" .. knobcom2a, "ARDUINO_MEGA2560_A_D" .. knobcom2b,
                              "TYPE_1_DETENT_PER_PULSE", knobcom2callback)))) then
        return false
    end
    if (not (hw_connected(hw_switch_add("ARDUINO_MEGA2560_A_D" .. btnonoffpower, btnonoffpowerchanged)))) then
        return false
    end
    if (not (hw_connected(hw_switch_add("ARDUINO_MEGA2560_A_D" .. btnonoffavionics, btnonoffavionicschanged)))) then
        return false
    end
    if (not (hw_connected(hw_button_add("ARDUINO_MEGA2560_A_D" .. btnheadingtoggle, noop, btnheadingtoggleoff)))) then
        return false
    end
    if (not (hw_connected(hw_switch_add("ARDUINO_MEGA2560_A_D" .. btnonoffpitotheat, btnonoffpitotheatchanged)))) then
        return false
    end
    if (not (hw_connected(hw_switch_add("ARDUINO_MEGA2560_A_D" .. btnonofffuelpump, btnonofffuelpumpchanged)))) then
        return false
    end
    if (not (hw_connected(hw_switch_add("ARDUINO_MEGA2560_A_D" .. btnonoffbeaconlights, btnonoffbeaconlightschanged)))) then
        return false
    end
    if (not (hw_connected(hw_switch_add("ARDUINO_MEGA2560_A_D" .. btnonofflandinglights, btnonofflandinglightschanged)))) then
        return false
    end
    if (not (hw_connected(hw_switch_add("ARDUINO_MEGA2560_A_D" .. btnonofftaxilights, btnonofftaxilightschanged)))) then
        return false
    end
    if (not (hw_connected(hw_switch_add("ARDUINO_MEGA2560_A_D" .. btnonoffnavigationlights,
                              btnonoffnavigationlightschanged)))) then
        return false
    end
    if (not (hw_connected(hw_switch_add("ARDUINO_MEGA2560_A_D" .. btnonoffstrobelights, btnonoffstrobelightschanged)))) then
        return false
    end
    if (not (hw_connected(hw_dial_add("ARDUINO_MEGA2560_A_D" .. knobheadinga, "ARDUINO_MEGA2560_A_D" .. knobheadingb,
                              "TYPE_1_DETENT_PER_PULSE", knobheadingcallback)))) then
        return false
    end
    if (not (hw_connected(hw_dial_add("ARDUINO_MEGA2560_A_D" .. knobvor1a, "ARDUINO_MEGA2560_A_D" .. knobvor1b,
                              "TYPE_1_DETENT_PER_PULSE", knobvor1callback)))) then
        return false
    end
    if (not (hw_connected(hw_dial_add("ARDUINO_MEGA2560_A_D" .. knobvor2a, "ARDUINO_MEGA2560_A_D" .. knobvor2b,
                              "TYPE_1_DETENT_PER_PULSE", knobvor2callback)))) then
        return false
    end
    return true
end

function triggerOptionally(event, oldvalue, newvalue)
    if (not (oldvalue == newvalue)) then
        fs2020_event(event)
    end
end

function noop()
end

-- ************* COM1 **********
-- Switch com1 mode (inc/dec high/low)
function btntogglecom1highlowoff()
    whatcom = fif(whatcom == "FRACT", "WHOLE", "FRACT")
end

-- Switch frequencies COM1
function btntogglecom1off()
    fs2020_event("COM_STBY_RADIO_SWAP")
end

-- Radio COM1 frequency
function knobcom1callback(direction)
    fs2020_event("COM_RADIO_" .. whatcom .. fif(direction == CLOCKWISE, "_INC", "_DEC"))
end

function newCom1Frequencies(com1, com1stby)
    print(com1)
    hw_chr_display_set_text(display, 0, string.format("%03.03f", com1) .. "  " .. string.format("%03.03f", com1stby))
end
-- ************* ENDOF COM1 **********

-- ************* COM2 **********
-- Switch com1 mode (inc/dec high/low)
function btntogglecom2highlowoff()
    whatcom2 = fif(whatcom2 == "FRACT", "WHOLE", "FRACT")
end

-- Switch frequencies COM2
function btntogglecom2off()
    fs2020_event("COM2_RADIO_SWAP")
end

-- Radio COM2 frequency
function knobcom2callback(direction)
    fs2020_event("COM2_RADIO_" .. whatcom2 .. fif(direction == CLOCKWISE, "_INC", "_DEC"))
end

function newCom2Frequencies(com2, com2stby)
    print(com2)
    hw_chr_display_set_text(display, 1, string.format("%03.03f", com2) .. "  " .. string.format("%03.03f", com2stby))
end

-- ************* ENDOF COM2 **********

-- Turn power on/off
function btnonoffpowerchanged(position)
    triggerOptionally("TOGGLE_MASTER_ALTERNATOR", masteralternator, (position == 1))
end

-- Turn avionics on/off
function btnonoffavionicschanged(position)
    fs2020_event("AVIONICS_MASTER_SET", position)
end

-- Toggle heading / ADF
function btnheadingtoggleoff()
    headingmode = fif(headingmode == "HEADING_BUG", "ADF_CARD", "HEADING_BUG")
end

-- Turn pitot head on/off
function btnonoffpitotheatchanged(position)
    fs2020_event("PITOT_HEAT_SET", position)
end

-- Turn fuel pump on/off
function btnonofffuelpumpchanged(position)
    triggerOptionally("FUEL_PUMP", fuelpump, (position == 1))
end

-- Turn beacon lights on/off
function btnonoffbeaconlightschanged(position)
    triggerOptionally("TOGGLE_BEACON_LIGHTS", beaconlights, (position == 1))
end

-- Turn landing lights on/off
function btnonofflandinglightschanged(position)
    fs2020_event("LANDING_LIGHTS_SET", position)
end

-- Turn taxi lights on/off
function btnonofftaxilightschanged(position)
    triggerOptionally("TOGGLE_TAXI_LIGHTS", taxilights, (position == 1))
end

-- Turn navigation lights on/off
function btnonoffnavigationlightschanged(position)
    triggerOptionally("TOGGLE_NAV_LIGHTS", navlights, (position == 1))
end

-- Turn strobes on/off
function btnonoffstrobelightschanged(position)
    fs2020_event("STROBES_SET", position)
end

-- Change heading
function knobheadingcallback(direction)
    fs2020_event(headingmode .. fif(direction == CLOCKWISE, "_INC", "_DEC"))
end

-- Change VOR1
function knobvor1callback(direction)
    fs2020_event("VOR1_OBI" .. fif(direction == CLOCKWISE, "_INC", "_DEC"))
end

-- Change VOR2
function knobvor2callback(direction)
    fs2020_event("VOR2_OBI" .. fif(direction == CLOCKWISE, "_INC", "_DEC"))
end

-- *** Read variables from FS -----
function updateVariables(newMasterAlternator, newBeaconLights, newTaxiLights, newNavLights, newFuelPump)
    masteralternator = newMasterAlternator
    beaconlights = newBeaconLights
    taxilights = newTaxiLights
    navlights = newNavLights
    fuelpump = newFuelPump
end

function subscribeVariables()
    fs2020_variable_subscribe("COM ACTIVE FREQUENCY:1", "Mhz", "COM STANDBY FREQUENCY:1", "MHz", newCom1Frequencies)
    fs2020_variable_subscribe("COM ACTIVE FREQUENCY:2", "Mhz", "COM STANDBY FREQUENCY:2", "MHz", newCom2Frequencies)
    fs2020_variable_subscribe("GENERAL ENG MASTER ALTERNATOR", "Bool", "LIGHT BEACON", "Bool", "LIGHT TAXI", "Bool",
        "LIGHT NAV", "Bool", "GENERAL ENG FUEL PUMP SWITCH:1", "Bool", updateVariables)
end

if (not (addHardware())) then
    print "Hardware is not ready..."
end
subscribeVariables()
-- /*
-- +----------------------------------------------------------+
-- |                                                          |
-- |   +-------------------+             (53)     (52)        |
-- |   |                   |                                  |
-- |   |                   |             (38)     (40)        |
-- |   |                   |             [28/29]  [24/25]     |
-- |   +-------------------+                                  |
-- |                                                          |
-- |     (48)    (51)    (50)    (39)    (41)     (43)        |
-- |                             [30/31] [26/27]  [22/23]     |
-- |                                                          |
-- |     (32)    (46)    (47)    (44)    (37)     (42)        |
-- |                                                          |
-- +----------------------------------------------------------+
-- */
local CLOCKWISE = 1
local COUNTERCLOCKWISE = -1

local btntogglecom1highlow = 38
local btntogglecom1 = 53
local knobcom1a = 28
local knobcom1b = 29

local btntogglecom2 = 52
local btntogglecom2highlow = 40
local knobcom2a = 24
local knobcom2b = 25

local btnonoffpower = 48
local btnonoffavionics = 51
local btnonoffpitotheat = 50

local knobvor1a = 26
local knobvor1b = 27
local knobvor2a = 22
local knobvor2b = 23

local btnheadingtoggle = 39
local knobheadinga = 30
local knobheadingb = 31

local btnonofffuelpump = 32
local btnonoffbeaconlights = 46
local btnonofflandinglights = 47
local btnonofftaxilights = 44
local btnonoffnavigationlights = 37
local btnonoffstrobelights = 42

local display

local whatcom = "FRACT"
local whatcom2 = "FRACT"
local masteralternator = false
local headingmode = "HEADING_BUG"
local fuelpump = false
local beaconlights = false
local taxilights = false
local navlights = false

function addHardware()
    display = hw_chr_display_add("HD44780", 2, 16, "ARDUINO_MEGA2560_A_D6", "ARDUINO_MEGA2560_A_D7",
                  "ARDUINO_MEGA2560_A_D5", "ARDUINO_MEGA2560_A_D4", "ARDUINO_MEGA2560_A_D3", "ARDUINO_MEGA2560_A_D2")
    if (not (hw_connected(display))) then
        return false
    end
    if (not (hw_connected(hw_button_add("ARDUINO_MEGA2560_A_D" .. btntogglecom1highlow, noop, btntogglecom1highlowoff)))) then
        return false
    end
    if (not (hw_connected(hw_button_add("ARDUINO_MEGA2560_A_D" .. btntogglecom1, noop, btntogglecom1off)))) then
        return false
    end
    if (not (hw_connected(hw_dial_add("ARDUINO_MEGA2560_A_D" .. knobcom1a, "ARDUINO_MEGA2560_A_D" .. knobcom1b,
                              "TYPE_1_DETENT_PER_PULSE", knobcom1callback)))) then
        return false
    end
    if (not (hw_connected(hw_button_add("ARDUINO_MEGA2560_A_D" .. btntogglecom2highlow, noop, btntogglecom2highlowoff)))) then
        return false
    end
    if (not (hw_connected(hw_button_add("ARDUINO_MEGA2560_A_D" .. btntogglecom2, noop, btntogglecom2off)))) then
        return false
    end
    if (not (hw_connected(hw_dial_add("ARDUINO_MEGA2560_A_D" .. knobcom2a, "ARDUINO_MEGA2560_A_D" .. knobcom2b,
                              "TYPE_1_DETENT_PER_PULSE", knobcom2callback)))) then
        return false
    end
    if (not (hw_connected(hw_switch_add("ARDUINO_MEGA2560_A_D" .. btnonoffpower, btnonoffpowerchanged)))) then
        return false
    end
    if (not (hw_connected(hw_switch_add("ARDUINO_MEGA2560_A_D" .. btnonoffavionics, btnonoffavionicschanged)))) then
        return false
    end
    if (not (hw_connected(hw_button_add("ARDUINO_MEGA2560_A_D" .. btnheadingtoggle, noop, btnheadingtoggleoff)))) then
        return false
    end
    if (not (hw_connected(hw_switch_add("ARDUINO_MEGA2560_A_D" .. btnonoffpitotheat, btnonoffpitotheatchanged)))) then
        return false
    end
    if (not (hw_connected(hw_switch_add("ARDUINO_MEGA2560_A_D" .. btnonofffuelpump, btnonofffuelpumpchanged)))) then
        return false
    end
    if (not (hw_connected(hw_switch_add("ARDUINO_MEGA2560_A_D" .. btnonoffbeaconlights, btnonoffbeaconlightschanged)))) then
        return false
    end
    if (not (hw_connected(hw_switch_add("ARDUINO_MEGA2560_A_D" .. btnonofflandinglights, btnonofflandinglightschanged)))) then
        return false
    end
    if (not (hw_connected(hw_switch_add("ARDUINO_MEGA2560_A_D" .. btnonofftaxilights, btnonofftaxilightschanged)))) then
        return false
    end
    if (not (hw_connected(hw_switch_add("ARDUINO_MEGA2560_A_D" .. btnonoffnavigationlights,
                              btnonoffnavigationlightschanged)))) then
        return false
    end
    if (not (hw_connected(hw_switch_add("ARDUINO_MEGA2560_A_D" .. btnonoffstrobelights, btnonoffstrobelightschanged)))) then
        return false
    end
    if (not (hw_connected(hw_dial_add("ARDUINO_MEGA2560_A_D" .. knobheadinga, "ARDUINO_MEGA2560_A_D" .. knobheadingb,
                              "TYPE_1_DETENT_PER_PULSE", knobheadingcallback)))) then
        return false
    end
    if (not (hw_connected(hw_dial_add("ARDUINO_MEGA2560_A_D" .. knobvor1a, "ARDUINO_MEGA2560_A_D" .. knobvor1b,
                              "TYPE_1_DETENT_PER_PULSE", knobvor1callback)))) then
        return false
    end
    if (not (hw_connected(hw_dial_add("ARDUINO_MEGA2560_A_D" .. knobvor2a, "ARDUINO_MEGA2560_A_D" .. knobvor2b,
                              "TYPE_1_DETENT_PER_PULSE", knobvor2callback)))) then
        return false
    end
    return true
end

function triggerOptionally(event, oldvalue, newvalue)
    if (not (oldvalue == newvalue)) then
        fs2020_event(event)
    end
end

function noop()
end

-- ************* COM1 **********
-- Switch com1 mode (inc/dec high/low)
function btntogglecom1highlowoff()
    whatcom = fif(whatcom == "FRACT", "WHOLE", "FRACT")
end

-- Switch frequencies COM1
function btntogglecom1off()
    fs2020_event("COM_STBY_RADIO_SWAP")
end

-- Radio COM1 frequency
function knobcom1callback(direction)
    fs2020_event("COM_RADIO_" .. whatcom .. fif(direction == CLOCKWISE, "_INC", "_DEC"))
end

function newCom1Frequencies(com1, com1stby)
    print(com1)
    hw_chr_display_set_text(display, 0, string.format("%03.03f", com1) .. "  " .. string.format("%03.03f", com1stby))
end
-- ************* ENDOF COM1 **********

-- ************* COM2 **********
-- Switch com1 mode (inc/dec high/low)
function btntogglecom2highlowoff()
    whatcom2 = fif(whatcom2 == "FRACT", "WHOLE", "FRACT")
end

-- Switch frequencies COM2
function btntogglecom2off()
    fs2020_event("COM2_RADIO_SWAP")
end

-- Radio COM2 frequency
function knobcom2callback(direction)
    fs2020_event("COM2_RADIO_" .. whatcom2 .. fif(direction == CLOCKWISE, "_INC", "_DEC"))
end

function newCom2Frequencies(com2, com2stby)
    print(com2)
    hw_chr_display_set_text(display, 1, string.format("%03.03f", com2) .. "  " .. string.format("%03.03f", com2stby))
end

-- ************* ENDOF COM2 **********

-- Turn power on/off
function btnonoffpowerchanged(position)
    triggerOptionally("TOGGLE_MASTER_ALTERNATOR", masteralternator, (position == 1))
end

-- Turn avionics on/off
function btnonoffavionicschanged(position)
    fs2020_event("AVIONICS_MASTER_SET", position)
end

-- Toggle heading / ADF
function btnheadingtoggleoff()
    headingmode = fif(headingmode == "HEADING_BUG", "ADF_CARD", "HEADING_BUG")
end

-- Turn pitot head on/off
function btnonoffpitotheatchanged(position)
    fs2020_event("PITOT_HEAT_SET", position)
end

-- Turn fuel pump on/off
function btnonofffuelpumpchanged(position)
    triggerOptionally("FUEL_PUMP", fuelpump, (position == 1))
end

-- Turn beacon lights on/off
function btnonoffbeaconlightschanged(position)
    triggerOptionally("TOGGLE_BEACON_LIGHTS", beaconlights, (position == 1))
end

-- Turn landing lights on/off
function btnonofflandinglightschanged(position)
    fs2020_event("LANDING_LIGHTS_SET", position)
end

-- Turn taxi lights on/off
function btnonofftaxilightschanged(position)
    triggerOptionally("TOGGLE_TAXI_LIGHTS", taxilights, (position == 1))
end

-- Turn navigation lights on/off
function btnonoffnavigationlightschanged(position)
    triggerOptionally("TOGGLE_NAV_LIGHTS", navlights, (position == 1))
end

-- Turn strobes on/off
function btnonoffstrobelightschanged(position)
    fs2020_event("STROBES_SET", position)
end

-- Change heading
function knobheadingcallback(direction)
    fs2020_event(headingmode .. fif(direction == CLOCKWISE, "_INC", "_DEC"))
end

-- Change VOR1
function knobvor1callback(direction)
    fs2020_event("VOR1_OBI" .. fif(direction == CLOCKWISE, "_INC", "_DEC"))
end

-- Change VOR2
function knobvor2callback(direction)
    fs2020_event("VOR2_OBI" .. fif(direction == CLOCKWISE, "_INC", "_DEC"))
end

-- *** Read variables from FS -----
function updateVariables(newMasterAlternator, newBeaconLights, newTaxiLights, newNavLights, newFuelPump)
    masteralternator = newMasterAlternator
    beaconlights = newBeaconLights
    taxilights = newTaxiLights
    navlights = newNavLights
    fuelpump = newFuelPump
end

function subscribeVariables()
    fs2020_variable_subscribe("COM ACTIVE FREQUENCY:1", "Mhz", "COM STANDBY FREQUENCY:1", "MHz", newCom1Frequencies)
    fs2020_variable_subscribe("COM ACTIVE FREQUENCY:2", "Mhz", "COM STANDBY FREQUENCY:2", "MHz", newCom2Frequencies)
    fs2020_variable_subscribe("GENERAL ENG MASTER ALTERNATOR", "Bool", "LIGHT BEACON", "Bool", "LIGHT TAXI", "Bool",
        "LIGHT NAV", "Bool", "GENERAL ENG FUEL PUMP SWITCH:1", "Bool", updateVariables)
end

if (not (addHardware())) then
    print "Hardware is not ready..."
end
subscribeVariables()
-- /*
-- +----------------------------------------------------------+
-- |                                                          |
-- |   +-------------------+             (53)     (52)        |
-- |   |                   |                                  |
-- |   |                   |             (38)     (40)        |
-- |   |                   |             [28/29]  [24/25]     |
-- |   +-------------------+                                  |
-- |                                                          |
-- |     (48)    (51)    (50)    (39)    (41)     (43)        |
-- |                             [30/31] [26/27]  [22/23]     |
-- |                                                          |
-- |     (32)    (46)    (47)    (44)    (37)     (42)        |
-- |                                                          |
-- +----------------------------------------------------------+
-- */
local CLOCKWISE = 1
local COUNTERCLOCKWISE = -1

local btntogglecom1highlow = 38
local btntogglecom1 = 53
local knobcom1a = 28
local knobcom1b = 29

local btntogglecom2 = 52
local btntogglecom2highlow = 40
local knobcom2a = 24
local knobcom2b = 25

local btnonoffpower = 48
local btnonoffavionics = 51
local btnonoffpitotheat = 50

local knobvor1a = 26
local knobvor1b = 27
local knobvor2a = 22
local knobvor2b = 23

local btnheadingtoggle = 39
local knobheadinga = 30
local knobheadingb = 31

local btnonofffuelpump = 32
local btnonoffbeaconlights = 46
local btnonofflandinglights = 47
local btnonofftaxilights = 44
local btnonoffnavigationlights = 37
local btnonoffstrobelights = 42

local display

local whatcom = "FRACT"
local whatcom2 = "FRACT"
local masteralternator = false
local headingmode = "HEADING_BUG"
local fuelpump = false
local beaconlights = false
local taxilights = false
local navlights = false

function addHardware()
    display = hw_chr_display_add("HD44780", 2, 16, "ARDUINO_MEGA2560_A_D6", "ARDUINO_MEGA2560_A_D7",
                  "ARDUINO_MEGA2560_A_D5", "ARDUINO_MEGA2560_A_D4", "ARDUINO_MEGA2560_A_D3", "ARDUINO_MEGA2560_A_D2")
    if (not (hw_connected(display))) then
        return false
    end
    if (not (hw_connected(hw_button_add("ARDUINO_MEGA2560_A_D" .. btntogglecom1highlow, noop, btntogglecom1highlowoff)))) then
        return false
    end
    if (not (hw_connected(hw_button_add("ARDUINO_MEGA2560_A_D" .. btntogglecom1, noop, btntogglecom1off)))) then
        return false
    end
    if (not (hw_connected(hw_dial_add("ARDUINO_MEGA2560_A_D" .. knobcom1a, "ARDUINO_MEGA2560_A_D" .. knobcom1b,
                              "TYPE_1_DETENT_PER_PULSE", knobcom1callback)))) then
        return false
    end
    if (not (hw_connected(hw_button_add("ARDUINO_MEGA2560_A_D" .. btntogglecom2highlow, noop, btntogglecom2highlowoff)))) then
        return false
    end
    if (not (hw_connected(hw_button_add("ARDUINO_MEGA2560_A_D" .. btntogglecom2, noop, btntogglecom2off)))) then
        return false
    end
    if (not (hw_connected(hw_dial_add("ARDUINO_MEGA2560_A_D" .. knobcom2a, "ARDUINO_MEGA2560_A_D" .. knobcom2b,
                              "TYPE_1_DETENT_PER_PULSE", knobcom2callback)))) then
        return false
    end
    if (not (hw_connected(hw_switch_add("ARDUINO_MEGA2560_A_D" .. btnonoffpower, btnonoffpowerchanged)))) then
        return false
    end
    if (not (hw_connected(hw_switch_add("ARDUINO_MEGA2560_A_D" .. btnonoffavionics, btnonoffavionicschanged)))) then
        return false
    end
    if (not (hw_connected(hw_button_add("ARDUINO_MEGA2560_A_D" .. btnheadingtoggle, noop, btnheadingtoggleoff)))) then
        return false
    end
    if (not (hw_connected(hw_switch_add("ARDUINO_MEGA2560_A_D" .. btnonoffpitotheat, btnonoffpitotheatchanged)))) then
        return false
    end
    if (not (hw_connected(hw_switch_add("ARDUINO_MEGA2560_A_D" .. btnonofffuelpump, btnonofffuelpumpchanged)))) then
        return false
    end
    if (not (hw_connected(hw_switch_add("ARDUINO_MEGA2560_A_D" .. btnonoffbeaconlights, btnonoffbeaconlightschanged)))) then
        return false
    end
    if (not (hw_connected(hw_switch_add("ARDUINO_MEGA2560_A_D" .. btnonofflandinglights, btnonofflandinglightschanged)))) then
        return false
    end
    if (not (hw_connected(hw_switch_add("ARDUINO_MEGA2560_A_D" .. btnonofftaxilights, btnonofftaxilightschanged)))) then
        return false
    end
    if (not (hw_connected(hw_switch_add("ARDUINO_MEGA2560_A_D" .. btnonoffnavigationlights,
                              btnonoffnavigationlightschanged)))) then
        return false
    end
    if (not (hw_connected(hw_switch_add("ARDUINO_MEGA2560_A_D" .. btnonoffstrobelights, btnonoffstrobelightschanged)))) then
        return false
    end
    if (not (hw_connected(hw_dial_add("ARDUINO_MEGA2560_A_D" .. knobheadinga, "ARDUINO_MEGA2560_A_D" .. knobheadingb,
                              "TYPE_1_DETENT_PER_PULSE", knobheadingcallback)))) then
        return false
    end
    if (not (hw_connected(hw_dial_add("ARDUINO_MEGA2560_A_D" .. knobvor1a, "ARDUINO_MEGA2560_A_D" .. knobvor1b,
                              "TYPE_1_DETENT_PER_PULSE", knobvor1callback)))) then
        return false
    end
    if (not (hw_connected(hw_dial_add("ARDUINO_MEGA2560_A_D" .. knobvor2a, "ARDUINO_MEGA2560_A_D" .. knobvor2b,
                              "TYPE_1_DETENT_PER_PULSE", knobvor2callback)))) then
        return false
    end
    return true
end

function triggerOptionally(event, oldvalue, newvalue)
    if (not (oldvalue == newvalue)) then
        fs2020_event(event)
    end
end

function noop()
end

-- ************* COM1 **********
-- Switch com1 mode (inc/dec high/low)
function btntogglecom1highlowoff()
    whatcom = fif(whatcom == "FRACT", "WHOLE", "FRACT")
end

-- Switch frequencies COM1
function btntogglecom1off()
    fs2020_event("COM_STBY_RADIO_SWAP")
end

-- Radio COM1 frequency
function knobcom1callback(direction)
    fs2020_event("COM_RADIO_" .. whatcom .. fif(direction == CLOCKWISE, "_INC", "_DEC"))
end

function newCom1Frequencies(com1, com1stby)
    print(com1)
    hw_chr_display_set_text(display, 0, string.format("%03.03f", com1) .. "  " .. string.format("%03.03f", com1stby))
end
-- ************* ENDOF COM1 **********

-- ************* COM2 **********
-- Switch com1 mode (inc/dec high/low)
function btntogglecom2highlowoff()
    whatcom2 = fif(whatcom2 == "FRACT", "WHOLE", "FRACT")
end

-- Switch frequencies COM2
function btntogglecom2off()
    fs2020_event("COM2_RADIO_SWAP")
end

-- Radio COM2 frequency
function knobcom2callback(direction)
    fs2020_event("COM2_RADIO_" .. whatcom2 .. fif(direction == CLOCKWISE, "_INC", "_DEC"))
end

function newCom2Frequencies(com2, com2stby)
    print(com2)
    hw_chr_display_set_text(display, 1, string.format("%03.03f", com2) .. "  " .. string.format("%03.03f", com2stby))
end

-- ************* ENDOF COM2 **********

-- Turn power on/off
function btnonoffpowerchanged(position)
    triggerOptionally("TOGGLE_MASTER_ALTERNATOR", masteralternator, (position == 1))
end

-- Turn avionics on/off
function btnonoffavionicschanged(position)
    fs2020_event("AVIONICS_MASTER_SET", position)
end

-- Toggle heading / ADF
function btnheadingtoggleoff()
    headingmode = fif(headingmode == "HEADING_BUG", "ADF_CARD", "HEADING_BUG")
end

-- Turn pitot head on/off
function btnonoffpitotheatchanged(position)
    fs2020_event("PITOT_HEAT_SET", position)
end

-- Turn fuel pump on/off
function btnonofffuelpumpchanged(position)
    triggerOptionally("FUEL_PUMP", fuelpump, (position == 1))
end

-- Turn beacon lights on/off
function btnonoffbeaconlightschanged(position)
    triggerOptionally("TOGGLE_BEACON_LIGHTS", beaconlights, (position == 1))
end

-- Turn landing lights on/off
function btnonofflandinglightschanged(position)
    fs2020_event("LANDING_LIGHTS_SET", position)
end

-- Turn taxi lights on/off
function btnonofftaxilightschanged(position)
    triggerOptionally("TOGGLE_TAXI_LIGHTS", taxilights, (position == 1))
end

-- Turn navigation lights on/off
function btnonoffnavigationlightschanged(position)
    triggerOptionally("TOGGLE_NAV_LIGHTS", navlights, (position == 1))
end

-- Turn strobes on/off
function btnonoffstrobelightschanged(position)
    fs2020_event("STROBES_SET", position)
end

-- Change heading
function knobheadingcallback(direction)
    fs2020_event(headingmode .. fif(direction == CLOCKWISE, "_INC", "_DEC"))
end

-- Change VOR1
function knobvor1callback(direction)
    fs2020_event("VOR1_OBI" .. fif(direction == CLOCKWISE, "_INC", "_DEC"))
end

-- Change VOR2
function knobvor2callback(direction)
    fs2020_event("VOR2_OBI" .. fif(direction == CLOCKWISE, "_INC", "_DEC"))
end

-- *** Read variables from FS -----
function updateVariables(newMasterAlternator, newBeaconLights, newTaxiLights, newNavLights, newFuelPump)
    masteralternator = newMasterAlternator
    beaconlights = newBeaconLights
    taxilights = newTaxiLights
    navlights = newNavLights
    fuelpump = newFuelPump
end

function subscribeVariables()
    fs2020_variable_subscribe("COM ACTIVE FREQUENCY:1", "Mhz", "COM STANDBY FREQUENCY:1", "MHz", newCom1Frequencies)
    fs2020_variable_subscribe("COM ACTIVE FREQUENCY:2", "Mhz", "COM STANDBY FREQUENCY:2", "MHz", newCom2Frequencies)
    fs2020_variable_subscribe("GENERAL ENG MASTER ALTERNATOR", "Bool", "LIGHT BEACON", "Bool", "LIGHT TAXI", "Bool",
        "LIGHT NAV", "Bool", "GENERAL ENG FUEL PUMP SWITCH:1", "Bool", updateVariables)
end

if (not (addHardware())) then
    print "Hardware is not ready..."
end
subscribeVariables()
Bart Roozendaal
https://fsarduino.com

Post Reply