Squawk refresh logic

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Post Reply
Message
Author
altack
Posts: 34
Joined: Mon Nov 21, 2016 1:13 pm

Squawk refresh logic

#1 Post by altack »

Code: Select all

--************************************************************************--
--BUTTON Function SECTION--
--*************************************************************************--

--VFR
function button_VFR_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button VFR pressed")

  -- Press VFR button for A2A 172
    fsx_variable_write("L:xpdr_vfr_button", "number", 1)
    fsx_variable_write("L:xpdr_vfr", "number", 1)
end

function button_VFR_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button VFR released")

  -- Release VFR button for A2A 172
  fsx_variable_write("L:xpdr_vfr_button", "number", 0)
end

--CLR
function button_CLR_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button CLR pressed")

  -- Press CLR button for A2A 172
    fsx_variable_write("L:xpdr_clr_button", "number", 1)
    fsx_variable_write("L:xpdr_clr", "number", 1)
end

function button_CLR_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button CLR released")

  -- Release CLR button for A2A 172
  fsx_variable_write("L:xpdr_clr_button", "number", 0)
end

--7
function button_7_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 7 pressed")

  -- Press 7 button for A2A 172
    fsx_variable_write("L:xpdr_7_button", "number", 1)
    fsx_variable_write("L:xpdr_digit7", "number", 1)
end

function button_7_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 7 released")

  -- Release 7 button for A2A 172
  fsx_variable_write("L:xpdr_7_button", "number", 0)
end

--6
function button_6_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 6 pressed")

  -- Press 6 button for A2A 172
    fsx_variable_write("L:xpdr_6_button", "number", 1)
    fsx_variable_write("L:xpdr_digit6", "number", 1)
end

function button_6_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 6 released")

  -- Release 6 button for A2A 172
  fsx_variable_write("L:xpdr_6_button", "number", 0)
end

--5
function button_5_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 5 pressed")

  -- Press 5 button for A2A 172
    fsx_variable_write("L:xpdr_5_button", "number", 1)
    fsx_variable_write("L:xpdr_digit5", "number", 1)
end

function button_5_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 5 released")

  -- Release 5 button for A2A 172
  fsx_variable_write("L:xpdr_5_button", "number", 0)
end

--4
function button_4_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 4 pressed")

  -- Press 4 button for A2A 172
    fsx_variable_write("L:xpdr_4_button", "number", 1)
    fsx_variable_write("L:xpdr_digit4", "number", 1)
end

function button_4_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 4 released")

  -- Release 4 button for A2A 172
  fsx_variable_write("L:xpdr_4_button", "number", 0)
end

--3
function button_3_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 3 pressed")

  -- Press 3 button for A2A 172
    fsx_variable_write("L:xpdr_3_button", "number", 1)
    fsx_variable_write("L:xpdr_digit3", "number", 1)
end

function button_3_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 3 released")

  -- Release 3 button for A2A 172
  fsx_variable_write("L:xpdr_3_button", "number", 0)
end

--2
function button_2_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 2 pressed")

  -- Press 2 button for A2A 172
    fsx_variable_write("L:xpdr_2_button", "number", 1)
    fsx_variable_write("L:xpdr_digit2", "number", 1)
end

function button_2_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 2 released")

  -- Release 2 button for A2A 172
  fsx_variable_write("L:xpdr_2_button", "number", 0)
end

--1
function button_1_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 1 pressed")

  -- Press 1 button for A2A 172
    fsx_variable_write("L:xpdr_1_button", "number", 1)
    fsx_variable_write("L:xpdr_digit1", "number", 1)
end

function button_1_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 1 released")

  -- Release 1 button for A2A 172
  fsx_variable_write("L:xpdr_1_button", "number", 0)
end

--0
function button_0_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 0 pressed")

  -- Press 0 button for A2A 172
    fsx_variable_write("L:xpdr_0_button", "number", 1)
    fsx_variable_write("L:xpdr_digit0", "number", 1)
end

function button_0_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 0 released")

  -- Release 0 button for A2A 172
  fsx_variable_write("L:xpdr_0_button", "number", 0)
end

--IDT
function button_IDT_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button IDT pressed")

  -- Press IDT button for A2A 172
    fsx_variable_write("L:xpdr_ident_button", "number", 1)
end

function button_IDT_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button IDT released")

  -- Release IDT button for A2A 172
  fsx_variable_write("L:xpdr_ident_button", "number", 0)
end

--++++++++++++++++++++++++++++++++++++++++++++++++++++++--
-- Converting Squawk code from BCD16 format into string --
--++++++++++++++++++++++++++++++++++++++++++++++++++++++--

function new_squawk_callback(BCDStr) -- XPDR, 4 digits in BCD format.
    squawk = string.format("%04X", BCDStr) -- A squawk of 2345 is represented by 0x2345. Convert 0x2345 to a string "2345"
    print(squawk)
end

--+++++++++++++++++++++++++++++++++++++++++++++++--
--    Displaying Squawk code on MAX7219 Display  --
--+++++++++++++++++++++++++++++++++++++++++++++++--

display_chr_xpdr = hw_chr_display_add("XPDR", "MAX7219", 1, "ARDUINO_MEGA2560_D_D10", "ARDUINO_MEGA2560_D_D9", "ARDUINO_MEGA2560_D_D8")

--Rhéostat RADIO LT
-- Callback function which is called when the ADC input state changes
-- 0.0 : GND (lowest voltage)
-- 1.0 : VCC (highest voltage)

function radio_lt_adc_input_change(value)
  print("new value= " .. tostring(value) )
    hw_chr_display_set_brightness(display_chr_xpdr, 0, value)
end

-- SELECTOR
-- This function is called every time the selector switch has a new position

function switch_selector_callback(position)
  print("The selector switch got changed to position " .. position)
print(squawk)


--OFF
  if position == 0 then
    fsx_variable_write("L:xpdr_onoff_knob_pos", "number", 0)
    hw_chr_display_set_text(display_chr_xpdr, 0, 0, "        ")
--Sby    
  elseif position == 1 then
    fsx_variable_write("L:xpdr_onoff_knob_pos", "number", 1)
    hw_chr_display_set_text(display_chr_xpdr, 0, 0, "5by "..squawk)
--Test
  elseif position == 2 then
    fsx_variable_write("L:xpdr_onoff_knob_pos", "number", 2)
    hw_chr_display_set_text(display_chr_xpdr, 0, 0, "8.8.8.8.8.8.8.8.")
--On   
  elseif position == 3 then
   fsx_variable_write("L:xpdr_onoff_knob_pos", "number", 3)
   hw_chr_display_set_text(display_chr_xpdr, 0, 0, "0n  "..squawk)
--Alt      
  elseif position == 4 then
   fsx_variable_write("L:xpdr_onoff_knob_pos", "number", 4)
   hw_chr_display_set_text(display_chr_xpdr, 0, 0, "Alt "..squawk)
  end
end





--Button Hardware section
--++++++++++++++++++++++++++++--
--D52
hw_button_add("Button_VFR_D52", button_VFR_pressed, button_VFR_released)

--D50
hw_button_add("Button_CLR_D50", button_CLR_pressed, button_CLR_released)

--D48
hw_button_add("Button_7_D48", button_7_pressed, button_7_released)

--D46
hw_button_add("Button_6_D46", button_6_pressed, button_6_released)

--D44
hw_button_add("Button_5_D44", button_5_pressed, button_5_released)

--D2
hw_button_add("Button_4_D2", button_4_pressed, button_4_released)

--D3
hw_button_add("Button_3_D3", button_3_pressed, button_3_released)

--D4
hw_button_add("Button_2_D4", button_2_pressed, button_2_released)

--D5
hw_button_add("Button_1_D5", button_1_pressed, button_1_released)

--D6
hw_button_add("Button_0_D6", button_0_pressed, button_0_released)

--D7
hw_button_add("Button_IDT_D7", button_IDT_pressed, button_IDT_released)

-- Create a 5 position switch, D45 to D53 (odd numbers)
hw_switch_add("Selector", 5, switch_selector_callback)

--A1 Rhéostat for  dimming function
hw_adc_input_add("RADIO_LT_ARDUINO_MEGA2560_A_A1", radio_lt_adc_input_change)

 -- subscribe to FSX / Prepar3D variables on the databus
--++++++++++++++++++++++++++++++++++++++++++++++++++++--

fsx_variable_subscribe("TRANSPONDER CODE:1", "BCO16", new_squawk_callback)
Hi Guys,
I'm working on a Bendix-King KT76C like transponder and I experience a logic problem, your help would be more than welcome.
So far I subscrided to the P3D Transponder code using the callback argument.
I also use an hardware selector and got a refresh on any selector change.
So now, each time a have a new selection the correct squawk code is displayed on the instrument (a physical MAX7219 7 segments display) but there is no refresh of that displayed code when it is changed. The new and correct squawk code is only displayed when switching the selector again.
Both selector changes and squawk callback points to 2 functions but I don't know how to mix both refreshing ?
I would like the squawk to get refreshed without having to move the selector.
Hope it make sense...
Thank you in advance for your support,
Francois
PS: my code needs to be tidied up, I'm far from being an expert...

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

Re: Squawk refresh logic

#2 Post by SimPassion »

In the callback after AM detect a variable change, there's nothing to trigger again the new display on MAX7219 :

Code: Select all

function new_squawk_callback(BCDStr) -- XPDR, 4 digits in BCD format.
    squawk = string.format("%04X", BCDStr) -- A squawk of 2345 is represented by 0x2345. Convert 0x2345 to a string "2345"
    print(squawk)
end
should at least trigger the "hw_chr_display_set_text" statement in either way from inside the callback, as the print statement is only to confirm for us the variable change in the AM console

Gilles

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

Re: Squawk refresh logic

#3 Post by Sling »

Francois,

You have to be careful writing to the MAX7219 in several places as you have to keep track of them all. The better way to do this is only to do the write once at the most appropriate place in your code. You could create a display writing function that the other functions send there information to for display, or simply choose one of the current functions as the one that handles the display. Whichever way you choose to do it you will need to let the subscribe callback know the selector knob mode otherwise the display will get written in all modes. What you have currently is not strictly correct because new codes are sent to the sim regardless of the mode. This should only be possible when in the correct modes.

altack
Posts: 34
Joined: Mon Nov 21, 2016 1:13 pm

Re: Squawk refresh logic

#4 Post by altack »

Code: Select all

--*************************************************************************--
--BUTTON Function SECTION--
--*************************************************************************--

--VFR
function button_VFR_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button VFR pressed")

  -- Press VFR button for A2A 172
    fsx_variable_write("L:xpdr_vfr_button", "number", 1)
    fsx_variable_write("L:xpdr_vfr", "number", 1)
end

function button_VFR_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button VFR released")

  -- Release VFR button for A2A 172
  fsx_variable_write("L:xpdr_vfr_button", "number", 0)
end

--CLR
function button_CLR_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button CLR pressed")

  -- Press CLR button for A2A 172
    fsx_variable_write("L:xpdr_clr_button", "number", 1)
    fsx_variable_write("L:xpdr_clr", "number", 1)
end

function button_CLR_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button CLR released")

  -- Release CLR button for A2A 172
  fsx_variable_write("L:xpdr_clr_button", "number", 0)
end

--7
function button_7_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 7 pressed")

  -- Press 7 button for A2A 172
    fsx_variable_write("L:xpdr_7_button", "number", 1)
    fsx_variable_write("L:xpdr_digit7", "number", 1)
end

function button_7_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 7 released")

  -- Release 7 button for A2A 172
  fsx_variable_write("L:xpdr_7_button", "number", 0)
end

--6
function button_6_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 6 pressed")

  -- Press 6 button for A2A 172
    fsx_variable_write("L:xpdr_6_button", "number", 1)
    fsx_variable_write("L:xpdr_digit6", "number", 1)
end

function button_6_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 6 released")

  -- Release 6 button for A2A 172
  fsx_variable_write("L:xpdr_6_button", "number", 0)
end

--5
function button_5_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 5 pressed")

  -- Press 5 button for A2A 172
    fsx_variable_write("L:xpdr_5_button", "number", 1)
    fsx_variable_write("L:xpdr_digit5", "number", 1)
end

function button_5_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 5 released")

  -- Release 5 button for A2A 172
  fsx_variable_write("L:xpdr_5_button", "number", 0)
end

--4
function button_4_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 4 pressed")

  -- Press 4 button for A2A 172
    fsx_variable_write("L:xpdr_4_button", "number", 1)
    fsx_variable_write("L:xpdr_digit4", "number", 1)
end

function button_4_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 4 released")

  -- Release 4 button for A2A 172
  fsx_variable_write("L:xpdr_4_button", "number", 0)
end

--3
function button_3_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 3 pressed")

  -- Press 3 button for A2A 172
    fsx_variable_write("L:xpdr_3_button", "number", 1)
    fsx_variable_write("L:xpdr_digit3", "number", 1)
end

function button_3_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 3 released")

  -- Release 3 button for A2A 172
  fsx_variable_write("L:xpdr_3_button", "number", 0)
end

--2
function button_2_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 2 pressed")

  -- Press 2 button for A2A 172
    fsx_variable_write("L:xpdr_2_button", "number", 1)
    fsx_variable_write("L:xpdr_digit2", "number", 1)
end

function button_2_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 2 released")

  -- Release 2 button for A2A 172
  fsx_variable_write("L:xpdr_2_button", "number", 0)
end

--1
function button_1_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 1 pressed")

  -- Press 1 button for A2A 172
    fsx_variable_write("L:xpdr_1_button", "number", 1)
    fsx_variable_write("L:xpdr_digit1", "number", 1)
end

function button_1_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 1 released")

  -- Release 1 button for A2A 172
  fsx_variable_write("L:xpdr_1_button", "number", 0)
end

--0
function button_0_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 0 pressed")

  -- Press 0 button for A2A 172
    fsx_variable_write("L:xpdr_0_button", "number", 1)
    fsx_variable_write("L:xpdr_digit0", "number", 1)
end

function button_0_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 0 released")

  -- Release 0 button for A2A 172
  fsx_variable_write("L:xpdr_0_button", "number", 0)
end

--IDT
function button_IDT_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button IDT pressed")

  -- Press IDT button for A2A 172
    fsx_variable_write("L:xpdr_ident_button", "number", 1)
end

function button_IDT_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button IDT released")

  -- Release IDT button for A2A 172
  fsx_variable_write("L:xpdr_ident_button", "number", 0)
end



--+++++++++++++++++++++++++++++++++++++++++++++++--
--    Displaying Squawk code on MAX7219 Display  --
--+++++++++++++++++++++++++++++++++++++++++++++++--

display_chr_xpdr = hw_chr_display_add("XPDR", "MAX7219", 1, "ARDUINO_MEGA2560_D_D10", "ARDUINO_MEGA2560_D_D9", "ARDUINO_MEGA2560_D_D8")

--Rhéostat RADIO LT
-- Callback function which is called when the ADC input state changes
-- 0.0 : GND (lowest voltage)
-- 1.0 : VCC (highest voltage)

function radio_lt_adc_input_change(value)
  print("new value= " .. tostring(value) )
    hw_chr_display_set_brightness(display_chr_xpdr, 0, value)
end

--++++++++++++++++++++++++++++++++++++++++++++++++++++++--
-- Converting Squawk code from BCD16 format into string --
--++++++++++++++++++++++++++++++++++++++++++++++++++++++--
function new_squawk_callback(BCDStr) -- XPDR, 4 digits in BCD format.
    squawk = string.format("%04X", BCDStr) -- A squawk of 2345 is represented by 0x2345. Convert 0x2345 to a string "2345"
    hw_chr_display_set_text(display_chr_xpdr, 0, 0, squawk)
    print(squawk) --For Debug Purpose
end

-- SELECTOR
-- This function is called every time the selector switch has a new position

function switch_selector_callback(position)
  print("The selector switch got changed to position " .. position)
print(squawk)

--OFF
  if position == 0 then
    fsx_variable_write("L:xpdr_onoff_knob_pos", "number", 0)
    hw_chr_display_set_text(display_chr_xpdr, 0, 0, "        ")
--Sby    
  elseif position == 1 then
    fsx_variable_write("L:xpdr_onoff_knob_pos", "number", 1)
    hw_chr_display_set_text(display_chr_xpdr, 0, 0, "5by "..squawk)
--Test
  elseif position == 2 then
    fsx_variable_write("L:xpdr_onoff_knob_pos", "number", 2)
    hw_chr_display_set_text(display_chr_xpdr, 0, 0, "8.8.8.8.8.8.8.8.")
--On   
  elseif position == 3 then
   fsx_variable_write("L:xpdr_onoff_knob_pos", "number", 3)
   hw_chr_display_set_text(display_chr_xpdr, 0, 0, "0n  "..squawk)
--Alt      
  elseif position == 4 then
   fsx_variable_write("L:xpdr_onoff_knob_pos", "number", 4)
   hw_chr_display_set_text(display_chr_xpdr, 0, 0, "Al  "..squawk)
  end
end





--Button Hardware section
--++++++++++++++++++++++++++++--
--D52
hw_button_add("Button_VFR_D52", button_VFR_pressed, button_VFR_released)

--D50
hw_button_add("Button_CLR_D50", button_CLR_pressed, button_CLR_released)

--D48
hw_button_add("Button_7_D48", button_7_pressed, button_7_released)

--D46
hw_button_add("Button_6_D46", button_6_pressed, button_6_released)

--D44
hw_button_add("Button_5_D44", button_5_pressed, button_5_released)

--D2
hw_button_add("Button_4_D2", button_4_pressed, button_4_released)

--D3
hw_button_add("Button_3_D3", button_3_pressed, button_3_released)

--D4
hw_button_add("Button_2_D4", button_2_pressed, button_2_released)

--D5
hw_button_add("Button_1_D5", button_1_pressed, button_1_released)

--D6
hw_button_add("Button_0_D6", button_0_pressed, button_0_released)

--D7
hw_button_add("Button_IDT_D7", button_IDT_pressed, button_IDT_released)

-- Create a 5 position switch, D45 to D53 (odd numbers)
hw_switch_add("Selector", 5, switch_selector_callback)

--A1 Rhéostat for  dimming function
hw_adc_input_add("RADIO_LT_ARDUINO_MEGA2560_A_A1", radio_lt_adc_input_change)

 -- subscribe to FSX / Prepar3D variables on the databus
--++++++++++++++++++++++++++++++++++++++++++++++++++++--

fsx_variable_subscribe("TRANSPONDER CODE:1", "BCO16", new_squawk_callback)
Great ! Thank you Gilles for your help.
I moved down the new_squawk_callback function and added the hw_chr_display_set_text(display_chr_xpdr, 0, 0, squawk) line.
This is perfect as only the rightmost part of the display is concerned by this command line.
Now the squawk code is refreshed when needed.
I only have to pay attention not to fiddle with the keys when the selector is in OFF or TST position as I would have an awkward display with half blanck/new squawk or half "8.8.8.8."/new squawk (can't use the if/then/else as the "position" variable from selector function is declared afterward).
I will try to improve the code by displaying momentary entered partial code and dashes (like "7---" after pushing the 7 key, "77--" after pushing it twice and "770-" after entering 0)...

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

Re: Squawk refresh logic

#5 Post by Sling »

I only have to pay attention not to fiddle with the keys when the selector is in OFF or TST position as I would have an awkward display with half blanck/new squawk or half "8.8.8.8."/new squawk (can't use the if/then/else as the "position" variable from selector function is declared afterward).
Did you read my last post? This is what I described in that post. Do the writing only in the subscribe callback and create a variable to capture the mode from the switch callback and you can fix this easily.

altack
Posts: 34
Joined: Mon Nov 21, 2016 1:13 pm

Re: Squawk refresh logic

#6 Post by altack »

Did you read my last post?
Actualy no, I didn't, since I was already typing my previous answer.
You are right sling about creating variable which capture the mode but I guess I'm a pretty lousy coder: I only got error codes with "NIL" variables so far...
I'm probabely creating local variables which are useless outside their own functions ?

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

Re: Squawk refresh logic

#7 Post by Sling »

Just create the variable at the head of your code with a prefix of local, despite the local prefix this makes it available to all functions in that Lua file. I.e. it keeps it local to the level it is placed. So local to the file. If it were inside a function it would be local to the function. Omit the local prefix to make it global to everything. Rather than do this though you can pass variables to functions as an argument but you would need that third function I spoke of constructed with the arguments you needed. So for your case they would be the mode (switch position) and code (subscribed var). You can read up on the pro’s and con’s of using global variables.

Tony

altack
Posts: 34
Joined: Mon Nov 21, 2016 1:13 pm

Re: Squawk refresh logic

#8 Post by altack »

Code: Select all

local position = 0
local selector_positon_change = false
local recent_key_pressed = false
local squawk = "0000"
local squawk_change = false
local time_rc_count = 0
local time_drdc_count = 0
local random_drcd_number = 0
local displaying_reply_cue = false

--***************************************************************************--
-- SELECTOR                                                                  --
-- This function is called every time the selector switch has a new position --
--***************************************************************************--

function switch_selector_callback(new_position)
    print("The selector switch got changed to position " .. new_position) -- for debug purpose
    position = new_position 
    selector_positon_change = true  -- for debug purpose
    print(position)  -- for debug purpose
    print("selector_positon_change",selector_positon_change)  -- for debug purpose

    if position == 0 then
    fsx_variable_write("L:xpdr_onoff_knob_pos", "number", 0)
--Sby    
  elseif position == 1 then
    fsx_variable_write("L:xpdr_onoff_knob_pos", "number", 1)
--Test
  elseif position == 2 then
    fsx_variable_write("L:xpdr_onoff_knob_pos", "number", 2)
--On   
  elseif position == 3 then
   fsx_variable_write("L:xpdr_onoff_knob_pos", "number", 3)
--Alt      
  elseif position == 4 then
   fsx_variable_write("L:xpdr_onoff_knob_pos", "number", 4)
  end
  display()
end

--*************************************************************************--
--BUTTON Function SECTION--
--*************************************************************************--

--VFR
function button_VFR_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button VFR pressed")
  -- The VFR code may be changed in Standby mode by setting new VFR code and depressing both IDT and VFR keys

  -- Press VFR button for A2A 172
    if position ~= 2 then
    fsx_variable_write("L:xpdr_vfr_button", "number", 1)
    fsx_variable_write("L:xpdr_vfr", "number", 1)
    end
end

function button_VFR_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button VFR released")

  -- Release VFR button for A2A 172
    if position ~= 2 then
    fsx_variable_write("L:xpdr_vfr_button", "number", 0)
    end
end

--CLR
function button_CLR_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button CLR pressed")

  -- Press CLR button for A2A 172
    fsx_variable_write("L:xpdr_clr_button", "number", 1)
    fsx_variable_write("L:xpdr_clr", "number", 1)
end

function button_CLR_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button CLR released")

  -- Release CLR button for A2A 172
  fsx_variable_write("L:xpdr_clr_button", "number", 0)
end

--7
function button_7_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 7 pressed")

  -- Press 7 button for A2A 172
    fsx_variable_write("L:xpdr_7_button", "number", 1)
    fsx_variable_write("L:xpdr_digit7", "number", 1)
    recent_key_pressed = true
end

function button_7_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 7 released")

  -- Release 7 button for A2A 172
  fsx_variable_write("L:xpdr_7_button", "number", 0)
end

--6
function button_6_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 6 pressed")

  -- Press 6 button for A2A 172
    fsx_variable_write("L:xpdr_6_button", "number", 1)
    fsx_variable_write("L:xpdr_digit6", "number", 1)
    recent_key_pressed = true    
end

function button_6_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 6 released")

  -- Release 6 button for A2A 172
  fsx_variable_write("L:xpdr_6_button", "number", 0)
end

--5
function button_5_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 5 pressed")

  -- Press 5 button for A2A 172
    fsx_variable_write("L:xpdr_5_button", "number", 1)
    fsx_variable_write("L:xpdr_digit5", "number", 1)
    recent_key_pressed = true  
end

function button_5_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 5 released")

  -- Release 5 button for A2A 172
  fsx_variable_write("L:xpdr_5_button", "number", 0)
end

--4
function button_4_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 4 pressed")

  -- Press 4 button for A2A 172
    fsx_variable_write("L:xpdr_4_button", "number", 1)
    fsx_variable_write("L:xpdr_digit4", "number", 1)
    recent_key_pressed = true  
end

function button_4_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 4 released")

  -- Release 4 button for A2A 172
  fsx_variable_write("L:xpdr_4_button", "number", 0)
end

--3
function button_3_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 3 pressed")

  -- Press 3 button for A2A 172
    fsx_variable_write("L:xpdr_3_button", "number", 1)
    fsx_variable_write("L:xpdr_digit3", "number", 1)
    recent_key_pressed = true  
end

function button_3_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 3 released")

  -- Release 3 button for A2A 172
  fsx_variable_write("L:xpdr_3_button", "number", 0)
end

--2
function button_2_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 2 pressed")

  -- Press 2 button for A2A 172
    fsx_variable_write("L:xpdr_2_button", "number", 1)
    fsx_variable_write("L:xpdr_digit2", "number", 1)
    recent_key_pressed = true  
end

function button_2_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 2 released")

  -- Release 2 button for A2A 172
  fsx_variable_write("L:xpdr_2_button", "number", 0)
end

--1
function button_1_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 1 pressed")

  -- Press 1 button for A2A 172
    fsx_variable_write("L:xpdr_1_button", "number", 1)
    fsx_variable_write("L:xpdr_digit1", "number", 1)
    recent_key_pressed = true  
end

function button_1_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 1 released")

  -- Release 1 button for A2A 172
  fsx_variable_write("L:xpdr_1_button", "number", 0)
end

--0
function button_0_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 0 pressed")

  -- Press 0 button for A2A 172
    fsx_variable_write("L:xpdr_0_button", "number", 1)
    fsx_variable_write("L:xpdr_digit0", "number", 1)
    recent_key_pressed = true  
end

function button_0_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button 0 released")

  -- Release 0 button for A2A 172
  fsx_variable_write("L:xpdr_0_button", "number", 0)
end

--IDT
function button_IDT_pressed()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button IDT pressed")

  -- Press IDT button for A2A 172, it trigger a dummy reply cue on the MAX7219
    fsx_variable_write("L:xpdr_ident_button", "number", 1)
    displaying_reply_cue = true
    display()
    time_rc_count = 0 
    timer_rc_callback()
end

function button_IDT_released()
  -- The following print function is not necessary but can help you to verify that the button has been pressed
  print("Button IDT released")

  -- Release IDT button for A2A 172
  fsx_variable_write("L:xpdr_ident_button", "number", 0)
end

--+++++++++++++++++++++++++++++++++++++++++++++++--
-- timer for IDENT transponder reply cue display --
--+++++++++++++++++++++++++++++++++++++++++++++++--
function timer_rc_callback()
	time_rc_count = time_rc_count + 1
        print(time_rc_count, displaying_reply_cue)
       	if time_rc_count == 20 then
	displaying_reply_cue = false
        display()
        time_rc_count = 0
	end
end
						
timer_rc = timer_start(0, 1000,timer_rc_callback)

--++++++++++++++++++++++++++++++++--
-- Random dummy reply cue display --
--++++++++++++++++++++++++++++++++--
function dummy_reply_cue_display_callback()
        random_drcd_number = math.random()
        print(random_drcd_number)
--        if random_drcd_number >= 0,9 then
--        displaying_reply_cue = true
--        display()
--        time_drdc_count = 0 
--        timer_drdc_callback()
--        end
end

--+++++++++++++++++++++++++++++++++++++++++++++++--
-- timer for random transponder reply cue display --
--+++++++++++++++++++++++++++++++++++++++++++++++--
--function timer_drdc_callback()
--	time_drdc_count = time_drdc_count + 1
--       print(time_drdc_count, displaying_reply_cue)
--       	if time_drdc_count == 20 then
--	displaying_reply_cue = false
--        display()
--        time_drdc_count = 0
--	end
--end
						
--timer_drdc = timer_start(0, 100,timer_drdc_callback)

--+++++++++++++++++++++++++++++++++++++++++++++++--
--   Setting  MAX7219 Display                    --
--+++++++++++++++++++++++++++++++++++++++++++++++--

display_chr_xpdr = hw_chr_display_add("XPDR", "MAX7219", 1, "ARDUINO_MEGA2560_D_D10", "ARDUINO_MEGA2560_D_D9", "ARDUINO_MEGA2560_D_D8")

--Rhéostat RADIO LT
-- Callback function which is called when the ADC input state changes
-- 0.0 : GND (lowest voltage)
-- 1.0 : VCC (highest voltage)

function radio_lt_adc_input_change(value)
  print("new value= " .. tostring(value) )  -- for debug purpose
    hw_chr_display_set_brightness(display_chr_xpdr, 0, (0.3*value))
end

--++++++++++++++++++++++++++++++++++++++++++++++++++++++--
-- Converting Squawk code from BCD16 format into string --
--++++++++++++++++++++++++++++++++++++++++++++++++++++++--
function new_squawk_callback(BCDStr) -- XPDR, 4 digits in BCD format.
    squawk = string.format("%04X", BCDStr) -- A squawk of 2345 is represented by 0x2345. Convert 0x2345 to a string "2345"
    squawk_change = true
    print(squawk)  -- for debug purpose
    if position == 1 or position == 3 or position == 4 then
--      hw_chr_display_set_text(display_chr_xpdr, 0, 0, squawk)
    display()
      print("passing through new_squakw_callback function")  -- for debug purpose
    end
end

--+++++++++++++++++++++++++++++++++++++++++++++++--
--    Displaying Squawk code on MAX7219 Display  --
--+++++++++++++++++++++++++++++++++++++++++++++++--
function display()
    print("function display") --for debug purpose
  if position == 0 then
    hw_chr_display_set_text(display_chr_xpdr, 0, 0, "        ")
--Sby    
  elseif position == 1 then
    hw_chr_display_set_text(display_chr_xpdr, 0, 0, "5by "..squawk)
--Test
  elseif position == 2 then
    hw_chr_display_set_text(display_chr_xpdr, 0, 0, "8.8.8.8.8.8.8.8.")
--On   
  elseif position == 3 and displaying_reply_cue == true then
    hw_chr_display_set_text(display_chr_xpdr, 0, 0, "0n _"..squawk)

   elseif   position == 3 and displaying_reply_cue == false then
    hw_chr_display_set_text(display_chr_xpdr, 0, 0, "0n  "..squawk)
    
--Alt      
  elseif position == 4 and displaying_reply_cue == true then
     hw_chr_display_set_text(display_chr_xpdr, 0, 0, "Al _"..squawk)
    elseif position == 4 and displaying_reply_cue == false then
       hw_chr_display_set_text(display_chr_xpdr, 0, 0, "Al  "..squawk)
    end
end

--Button Hardware section
--++++++++++++++++++++++++++++--
--D52
hw_button_add("Button_VFR_D52", button_VFR_pressed, button_VFR_released)

--D50
hw_button_add("Button_CLR_D50", button_CLR_pressed, button_CLR_released)

--D48
hw_button_add("Button_7_D48", button_7_pressed, button_7_released)

--D46
hw_button_add("Button_6_D46", button_6_pressed, button_6_released)

--D44
hw_button_add("Button_5_D44", button_5_pressed, button_5_released)

--D2
hw_button_add("Button_4_D2", button_4_pressed, button_4_released)

--D3
hw_button_add("Button_3_D3", button_3_pressed, button_3_released)

--D4
hw_button_add("Button_2_D4", button_2_pressed, button_2_released)

--D5
hw_button_add("Button_1_D5", button_1_pressed, button_1_released)

--D6
hw_button_add("Button_0_D6", button_0_pressed, button_0_released)

--D7
hw_button_add("Button_IDT_D7", button_IDT_pressed, button_IDT_released)

-- Create a 5 position switch, D45 to D53 (odd numbers)
hw_switch_add("Selector", 5, switch_selector_callback)

--A1 Rhéostat for  dimming function
hw_adc_input_add("RADIO_LT_ARDUINO_MEGA2560_A_A1", radio_lt_adc_input_change)

 -- subscribe to FSX / Prepar3D variables on the databus
--++++++++++++++++++++++++++++++++++++++++++++++++++++--

fsx_variable_subscribe("TRANSPONDER CODE:1", "BCO16", new_squawk_callback)
Hi Guys,
I've done my homework and, thanks to your advices, I now have a more satisfactory code.
I learned enough to understand that, in my case, local variables are better and I even learned how to play with a timer !
I also learned from my blunders, like trying to test an instrument with a paused flight sim or along a panel with his own transponder in use :roll:
I got also frustrated when I had to re-enter all the hardware binds, 17 of it, after each code change...
But there is no longer logic problem and the display is in sync with the A2A 172 trainer transponder :D

Even if the letter "R" isn't available for the MAX7219, I wanted to get a visual clue of the "Reply" mode of the transponder.
For the time being this "_" clue next to the squawk code is displayed for 20 seconds when squawking "Ident" but I would like to have also a randomized display of this clue : is the math.random () function useable whithin AM 3.6 ? (later on I would like to link it to VATSIM somehow but a dummy "Reply" function will do in the meantime).
I still have to code the momentary entered partial squawk code.
I actualy enjoy very much "playing" with AM and getting results with these codes :lol:
Again, thanks for your support !

Post Reply