Beta 35 with Arduino hangs when connecting to FS2020

Discuss suspected bugs with other users and Sim Innovations Staff

Moderators: Ralph, russ

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 » Tue Apr 13, 2021 9:24 pm

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:
  1. -- /*
  2.  
  3. -- +----------------------------------------------------------+
  4.  
  5. -- |                                                          |
  6.  
  7. -- |   +-------------------+             (53)     (52)        |
  8.  
  9. -- |   |                   |                                  |
  10.  
  11. -- |   |                   |             (38)     (40)        |
  12.  
  13. -- |   |                   |             [28/29]  [24/25]     |
  14.  
  15. -- |   +-------------------+                                  |
  16.  
  17. -- |                                                          |
  18.  
  19. -- |     (48)    (51)    (50)    (39)    (41)     (43)        |
  20.  
  21. -- |                             [30/31] [26/27]  [22/23]     |
  22.  
  23. -- |                                                          |
  24.  
  25. -- |     (32)    (46)    (47)    (44)    (37)     (42)        |
  26.  
  27. -- |                                                          |
  28.  
  29. -- +----------------------------------------------------------+
  30.  
  31. -- */
  32.  
  33.  
  34.  
  35. CLOCKWISE = 1
  36.  
  37. COUNTERCLOCKWISE = -1
  38.  
  39.  
  40.  
  41. btntogglecom1highlow = 38
  42.  
  43. btntogglecom1 = 53
  44.  
  45. knobcom1a = 28
  46.  
  47. knobcom1b = 29
  48.  
  49.  
  50.  
  51. btntogglecom2 = 52
  52.  
  53. btntogglecom2highlow = 40
  54.  
  55. knobcom2a = 24
  56.  
  57. knobcom2b = 25
  58.  
  59.  
  60.  
  61. btnonoffpower = 48
  62.  
  63. btnonoffavionics = 51
  64.  
  65. btnonoffpitotheat = 50
  66.  
  67.  
  68.  
  69. knobvor1a = 26
  70.  
  71. knobvor1b = 27
  72.  
  73. knobvor2a = 22
  74.  
  75. knobvor2b = 23
  76.  
  77.  
  78.  
  79. btnheadingtoggle = 39
  80.  
  81. knobheadinga = 30
  82.  
  83. knobheadingb = 31
  84.  
  85.  
  86.  
  87. btnonofffuelpump = 32
  88.  
  89. btnonoffbeaconlights = 46
  90.  
  91. btnonofflandinglights = 47
  92.  
  93. btnonofftaxilights = 44
  94.  
  95. btnonoffnavigationlights = 37
  96.  
  97. btnonoffstrobelights = 42
  98.  
  99.  
  100.  
  101. 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")
  102.  
  103. function debugprint(what)
  104.  
  105.     -- hw_chr_display_set_text(display, 0, what)
  106.  
  107.     print(what)
  108.  
  109. end
  110.  
  111.  
  112.  
  113. -- ************* COM1 **********
  114.  
  115. -- Switch com1 mode (inc/dec high/low)
  116.  
  117. com1high = true
  118.  
  119. function btntogglecom1highlowon()
  120.  
  121. end
  122.  
  123. function btntogglecom1highlowoff()
  124.  
  125.     com1high = not (com1high)
  126.  
  127. end
  128.  
  129. hw_button_add("ARDUINO_MEGA2560_A_D" .. btntogglecom1highlow, btntogglecom1highlowon, btntogglecom1highlowoff)
  130.  
  131.  
  132.  
  133. -- Switch frequencies COM1
  134.  
  135. function btntogglecom1on()
  136.  
  137. end
  138.  
  139. function btntogglecom1off()
  140.  
  141.     fs2020_event("COM_STBY_RADIO_SWAP")
  142.  
  143. end
  144.  
  145. hw_button_add("ARDUINO_MEGA2560_A_D" .. btntogglecom1, btntogglecom1on, btntogglecom1off)
  146.  
  147. -- Radio COM1 frequency
  148.  
  149. function knobcom1callback(direction)
  150.  
  151.     if (direction == CLOCKWISE) then
  152.  
  153.         if (com1high) then
  154.  
  155.             fs2020_event("COM_RADIO_WHOLE_INC")
  156.  
  157.         else
  158.  
  159.             fs2020_event("COM_RADIO_FRACT_INC")
  160.  
  161.         end
  162.  
  163.     else
  164.  
  165.         if (com1high) then
  166.  
  167.             fs2020_event("COM_RADIO_WHOLE_DEC")
  168.  
  169.         else
  170.  
  171.             fs2020_event("COM_RADIO_FRACT_DEC")
  172.  
  173.         end
  174.  
  175.     end
  176.  
  177. end
  178.  
  179.  
  180.  
  181. function newCom1Frequencies(com1, com1stby)
  182.  
  183.     hw_chr_display_set_text(display, 0, string.format("%03.03f", com1) .. "  " .. string.format("%03.03f", com1stby))
  184.  
  185. end
  186.  
  187.  
  188.  
  189. hw_dial_add("ARDUINO_MEGA2560_A_D" .. knobcom1a, "ARDUINO_MEGA2560_A_D" .. knobcom1b, "TYPE_1_DETENT_PER_PULSE",
  190.  
  191.     knobcom1callback)
  192.  
  193. fs2020_variable_subscribe("COM ACTIVE FREQUENCY:1", "Mhz", "COM STANDBY FREQUENCY:1", "MHz", newCom1Frequencies)
  194.  
  195.  
  196.  
  197. -- ************* ENDOF COM1 **********
  198.  
  199.  
  200.  
  201.  
  202.  
  203. -- ************* COM2 ****************
  204.  
  205. -- Switch frequencies COM2
  206.  
  207. function btntogglecom2on()
  208.  
  209. end
  210.  
  211. function btntogglecom2off()
  212.  
  213.     fs2020_event("COM2_RADIO_SWAP")
  214.  
  215. end
  216.  
  217. hw_button_add("ARDUINO_MEGA2560_A_D" .. btntogglecom2, btntogglecom2on, btntogglecom2off)
  218.  
  219.  
  220.  
  221. -- Switch com2 mode (inc/dec high/low)
  222.  
  223. com2high = true
  224.  
  225. function btntogglecom2highlowon()
  226.  
  227. end
  228.  
  229. function btntogglecom2highlowoff()
  230.  
  231.     com2high = not (com2high)
  232.  
  233. end
  234.  
  235. hw_button_add("ARDUINO_MEGA2560_A_D" .. btntogglecom2highlow, btntogglecom2highlowon, btntogglecom2highlowoff)
  236.  
  237.  
  238.  
  239. -- Radio COM2 frequency
  240.  
  241. function knobcom2callback(direction)
  242.  
  243.     if (direction == CLOCKWISE) then
  244.  
  245.         if (com2high) then
  246.  
  247.             fs2020_event("COM2_RADIO_WHOLE_INC")
  248.  
  249.         else
  250.  
  251.             fs2020_event("COM2_RADIO_FRACT_INC")
  252.  
  253.         end
  254.  
  255.     else
  256.  
  257.         if (com2high) then
  258.  
  259.             fs2020_event("COM2_RADIO_WHOLE_DEC")
  260.  
  261.         else
  262.  
  263.             fs2020_event("COM2_RADIO_FRACT_DEC")
  264.  
  265.         end
  266.  
  267.     end
  268.  
  269.  
  270.  
  271. end
  272.  
  273. hw_dial_add("ARDUINO_MEGA2560_A_D" .. knobcom2a, "ARDUINO_MEGA2560_A_D" .. knobcom2b, "TYPE_1_DETENT_PER_PULSE",
  274.  
  275.     knobcom2callback)
  276.  
  277.    
  278.  
  279. function newCom2Frequencies(com2, com2stby)
  280.  
  281.     hw_chr_display_set_text(display, 1, string.format("%03.03f", com2) .. "  " .. string.format("%03.03f", com2stby))
  282.  
  283. end
  284.  
  285. fs2020_variable_subscribe("COM ACTIVE FREQUENCY:2", "Mhz", "COM STANDBY FREQUENCY:2", "Mhz", newCom2Frequencies)
  286.  
  287.    
  288.  
  289. -- ********** ENDCOM2 ******************
  290.  
  291.  
  292.  
  293. -- Turn power on/off
  294.  
  295. masteralternator = false
  296.  
  297. function btnonoffpoweron()
  298.  
  299.     triggerOptionally("TOGGLE_MASTER_ALTERNATOR", masteralternator, true)
  300.  
  301. end
  302.  
  303. function btnonoffpoweroff()
  304.  
  305.     triggerOptionally("TOGGLE_MASTER_ALTERNATOR", masteralternator, false)
  306.  
  307. end
  308.  
  309. hw_button_add("ARDUINO_MEGA2560_A_D" .. btnonoffpower, btnonoffpoweron, btnonoffpoweroff)
  310.  
  311.  
  312.  
  313. -- Turn avionics on/off
  314.  
  315. function btnonoffavionicson()
  316.  
  317.     fs2020_event("AVIONICS_MASTER_SET", 1)
  318.  
  319. end
  320.  
  321. function btnonoffavionicsoff()
  322.  
  323.     fs2020_event("AVIONICS_MASTER_SET", 0)
  324.  
  325. end
  326.  
  327. hw_button_add("ARDUINO_MEGA2560_A_D" .. btnonoffavionics, btnonoffavionicson, btnonoffavionicsoff)
  328.  
  329.  
  330.  
  331. -- Turn pitot head on/off
  332.  
  333. function btnonoffpitotheaton()
  334.  
  335.     fs2020_event("PITOT_HEAT_SET", 1)
  336.  
  337. end
  338.  
  339. function btnonoffpitotheatoff()
  340.  
  341.     fs2020_event("PITOT_HEAT_SET", 0)
  342.  
  343. end
  344.  
  345. hw_button_add("ARDUINO_MEGA2560_A_D" .. btnonoffpitotheat, btnonoffpitotheaton, btnonoffpitotheatoff)
  346.  
  347.  
  348.  
  349. -- Toggle heading / ADF
  350.  
  351. headingmode = false
  352.  
  353. function btnheadingtoggleon()
  354.  
  355. end
  356.  
  357. function btnheadingtoggleoff()
  358.  
  359.     headingmode = not (headingmode)
  360.  
  361. end
  362.  
  363. hw_button_add("ARDUINO_MEGA2560_A_D" .. btnheadingtoggle, btnheadingtoggleon, btnheadingtoggleoff)
  364.  
  365.  
  366.  
  367. -- Turn fuel pump on/off
  368.  
  369. fuelpump = false
  370.  
  371. function btnonofffuelpumpon()
  372.  
  373.     triggerOptionally("FUEL_PUMP", fuelpump, true)
  374.  
  375. end
  376.  
  377. function btnonofffuelpumpoff()
  378.  
  379.     triggerOptionally("FUEL_PUMP", fuelpump, false)
  380.  
  381. end
  382.  
  383. hw_button_add("ARDUINO_MEGA2560_A_D" .. btnonofffuelpump, btnonofffuelpumpon, btnonofffuelpumpoff)
  384.  
  385.  
  386.  
  387. -- Turn beacon lights on/off
  388.  
  389. beaconlights = false
  390.  
  391. function btnonoffbeaconlightson()
  392.  
  393.     triggerOptionally("TOGGLE_BEACON_LIGHTS", beaconlights, true)
  394.  
  395. end
  396.  
  397. function btnonoffbeaconlightsoff()
  398.  
  399.     triggerOptionally("TOGGLE_BEACON_LIGHTS", beaconlights, false)
  400.  
  401. end
  402.  
  403. hw_button_add("ARDUINO_MEGA2560_A_D" .. btnonoffbeaconlights, btnonoffbeaconlightson, btnonoffbeaconlightsoff)
  404.  
  405.  
  406.  
  407. -- Turn landing lights on/off
  408.  
  409. function btnonofflandinglightson()
  410.  
  411.     fs2020_event("LANDING_LIGHTS_SET", 1)
  412.  
  413. end
  414.  
  415. function btnonofflandinglightsoff()
  416.  
  417.     fs2020_event("LANDING_LIGHTS_SET", 0)
  418.  
  419. end
  420.  
  421. hw_button_add("ARDUINO_MEGA2560_A_D" .. btnonofflandinglights, btnonofflandinglightson, btnonofflandinglightsoff)
  422.  
  423.  
  424.  
  425. -- Turn taxi lights on/off
  426.  
  427. taxilights = false
  428.  
  429. function btnonofftaxilightson()
  430.  
  431.     triggerOptionally("TOGGLE_TAXI_LIGHTS", taxilights, true)
  432.  
  433. end
  434.  
  435. function btnonofftaxilightsoff()
  436.  
  437.     triggerOptionally("TOGGLE_TAXI_LIGHTS", taxilights, false)
  438.  
  439. end
  440.  
  441. hw_button_add("ARDUINO_MEGA2560_A_D" .. btnonofftaxilights, btnonofftaxilightson, btnonofftaxilightsoff)
  442.  
  443.  
  444.  
  445. -- Turn taxi lights on/off
  446.  
  447. navlights = false
  448.  
  449. function btnonoffnavigationlightson()
  450.  
  451.     triggerOptionally("TOGGLE_NAV_LIGHTS", navlights, true)
  452.  
  453. end
  454.  
  455. function btnonoffnavigationlightsoff()
  456.  
  457.     triggerOptionally("TOGGLE_NAV_LIGHTS", navlights, false)
  458.  
  459. end
  460.  
  461. hw_button_add("ARDUINO_MEGA2560_A_D" .. btnonoffnavigationlights, btnonoffnavigationlightson,
  462.  
  463.     btnonoffnavigationlightsoff)
  464.  
  465.  
  466.  
  467. -- Turn taxi lights on/off
  468.  
  469. function btnonoffstrobelightson()
  470.  
  471.     fs2020_event("STROBES_SET", 1)
  472.  
  473. end
  474.  
  475. function btnonoffstrobelightsoff()
  476.  
  477.     fs2020_event("STROBES_SET", 0)
  478.  
  479. end
  480.  
  481. hw_button_add("ARDUINO_MEGA2560_A_D" .. btnonoffstrobelights, btnonoffstrobelightson, btnonoffstrobelightsoff)
  482.  
  483.  
  484.  
  485. -- Change heading
  486.  
  487. function knobheadingcallback(direction)
  488.  
  489.     if (direction == CLOCKWISE) then
  490.  
  491.         if (headingmode) then
  492.  
  493.             fs2020_event("HEADING_BUG_INC")
  494.  
  495.         else
  496.  
  497.             fs2020_event("ADF_CARD_INC")
  498.  
  499.         end
  500.  
  501.     else
  502.  
  503.         if (headingmode) then
  504.  
  505.             fs2020_event("HEADING_BUG_DEC")
  506.  
  507.         else
  508.  
  509.             fs2020_event("ADF_CARD_DEC")
  510.  
  511.         end
  512.  
  513.     end
  514.  
  515. end
  516.  
  517. hw_dial_add("ARDUINO_MEGA2560_A_D" .. knobheadinga, "ARDUINO_MEGA2560_A_D" .. knobheadingb, "TYPE_1_DETENT_PER_PULSE",
  518.  
  519.     knobheadingcallback)
  520.  
  521.  
  522.  
  523. -- Change VOR1
  524.  
  525. function knobvor1callback(direction)
  526.  
  527.     if (direction == CLOCKWISE) then
  528.  
  529.         fs2020_event("VOR1_OBI_INC")
  530.  
  531.     else
  532.  
  533.         fs2020_event("VOR1_OBI_DEC")
  534.  
  535.     end
  536.  
  537. end
  538.  
  539. hw_dial_add("ARDUINO_MEGA2560_A_D" .. knobvor1a, "ARDUINO_MEGA2560_A_D" .. knobvor1b, "TYPE_1_DETENT_PER_PULSE",
  540.  
  541.     knobvor1callback)
  542.  
  543.  
  544.  
  545. -- Change VOR2
  546.  
  547. function knobvor2callback(direction)
  548.  
  549.     if (direction == CLOCKWISE) then
  550.  
  551.         fs2020_event("VOR2_OBI_INC")
  552.  
  553.     else
  554.  
  555.         fs2020_event("VOR2_OBI_DEC")
  556.  
  557.     end
  558.  
  559. end
  560.  
  561. hw_dial_add("ARDUINO_MEGA2560_A_D" .. knobvor2a, "ARDUINO_MEGA2560_A_D" .. knobvor2b, "TYPE_1_DETENT_PER_PULSE",
  562.  
  563.     knobvor2callback)
  564.  
  565.  
  566.  
  567. function updateVariables(newMasterAlternator, newBeaconLights, newTaxiLights, newNavLights, newFuelPump)
  568.  
  569.     masteralternator = newMasterAlternator
  570.  
  571.     beaconlights = newBeaconLights
  572.  
  573.     taxilights = newTaxiLights
  574.  
  575.     navlights = newNavLights
  576.  
  577.     fuelpump = newFuelPump
  578.  
  579. end
  580.  
  581.  
  582.  
  583. function triggerOptionally(event, oldvalue, newvalue)
  584.  
  585.     if (not (oldvalue == newvalue)) then
  586.  
  587.         fs2020_event(event)
  588.  
  589.     end
  590.  
  591. end
  592.  
  593.  
  594.  
  595. fs2020_variable_subscribe("GENERAL ENG MASTER ALTERNATOR", "Bool", "LIGHT BEACON", "Bool", "LIGHT TAXI", "Bool",
  596.  
  597.     "LIGHT NAV", "Bool", "GENERAL ENG FUEL PUMP SWITCH:1", "Bool", updateVariables)
  598.  
  599.  
  600.  
  601. 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: 4426
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: Beta 35 with Arduino hangs when connecting to FS2020

#2 Post by Sling » Wed Apr 14, 2021 3:11 am

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 » Wed Apr 14, 2021 8:11 am

logic.lua
(7.57 KiB) Downloaded 32 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 29 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: 2805
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 » Wed Apr 14, 2021 8:18 am

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 » Wed Apr 14, 2021 8:52 am

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 » Wed Apr 14, 2021 9:10 am

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: 4426
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: Beta 35 with Arduino hangs when connecting to FS2020

#7 Post by Sling » 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.

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: 2805
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 » Wed Apr 14, 2021 9:29 am

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 » Wed Apr 14, 2021 9:36 am

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 » Wed Apr 14, 2021 10:39 am

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

Who is online

Users browsing this forum: No registered users and 4 guests