I've built a button box with 2 rotary encoders and several buttons to 'tell' the encoders what to do and LEDs so that I know what it's doing. Here is the code:
Code: Select all
--Button Box Logic
--When a button is pressed, the corresponding LED will illuminate
--and the function will be active on the rotary
local BP = 0
-------------- Buttons --------------------------
--Button 3
function BP3()
if BP == 3 or BP == 7 then
BP = 0
hw_led_set(led7, 0.0)
hw_led_set(led3, 0.0)
else
BP = 3
illumination(led3)
rot1()
end
end
--Button 4
function BP4()
if BP == 4 then
BP = 0
hw_led_set(led4, 0.0) else
BP = 4
illumination(led4)
rot1()
end
end
--Button 6
function BP6()
if BP == 6 or BP == 8 then
BP = 0
hw_led_set(led6, 0.0)
hw_led_set(led8, 0.0)
else
BP = 6
illumination(led6)
rot1()
end
end
--Top Rotary Button
function tpress()
if BP == 0 then
si_command("si/g5/1/knob_button", "BEGIN")
end
if BP == 3 then
BP = 7
illumination(led7)
elseif
BP == 7 then
BP = 3
illumination(led3)
rot1()
end
if BP == 6 then
BP = 8
illumination(led8)
elseif
BP == 8 then
BP = 6
illumination(led6)
rot1()
end
end
function trelease()
if BP == 0 then
si_command("si/g5/1/knob_button", "END")
end
end
--Bottom Rotary Button
function bpress()
if BP == 0 then
si_command("si/g5/2/knob_button", "BEGIN")
end
if BP == 3 or BP == 7 then
fsx_event("NAV2_RADIO_SWAP")
xpl_command("sim/radios/nav2_standy_flip")
end
if BP == 6 or BP == 8 then
fsx_event("COM2_RADIO_SWAP")
xpl_command("sim/radios/com2_standy_flip")
end
end
function brelease()
if BP == 0 then
si_command("si/g5/2/knob_button", "END")
end
end
-------------- LED illumination ------------------
function illumination(light)
hw_led_set(led3, 0.0)
hw_led_set(led4, 0.0)
hw_led_set(led6, 0.0)
hw_led_set(led7, 0.0)
hw_led_set(led8, 0.0)
hw_led_set(light, 0.5)
end
-------------- Rotary functions ------------------
function rot1(direction)
--Default behavior-control G5PFD
if BP == 0 then
if direction == 1 then
si_command("si/g5/1/knob_cw")
elseif direction == -1 then
si_command("si/g5/1/knob_ccw")
end
end
--Button 3, NAV2
if BP == 3 then
if direction == -1 then
fsx_event("NAV2_RADIO_WHOLE_DEC")
xpl_command("sim/radios/stby_nav2_coarse_down")
elseif direction == 1 then
fsx_event("NAV2_RADIO_WHOLE_INC")
xpl_command("sim/radios/stby_nav2_coarse_up")
end
end
if BP == 7 then
if direction == -1 then
fsx_event("NAV2_RADIO_FRACT_DEC")
xpl_command("sim/radios/stby_nav2_fine_down")
elseif direction == 1 then
fsx_event("NAV2_RADIO_FRACT_INC")
xpl_command("sim/radios/stby_nav2_fine_up")
end
end
--Button 4, OBS2
if BP == 4 then
if direction == 1 then
xpl_command("sim/radios/obs2_down")
fsx_event("VOR2_OBI_DEC")
elseif direction == -1 then
xpl_command("sim/radios/obs2_up")
fsx_event("VOR2_OBI_INC")
end
end
--Button 6, COM2
if BP == 6 then
if direction == -1 then
fsx_event("COM2_RADIO_WHOLE_DEC")
xpl_command("sim/radios/stby_com2_coarse_down")
elseif direction == 1 then
fsx_event("COM2_RADIO_WHOLE_INC")
xpl_command("sim/radios/stby_com2_coarse_up")
end
end
if BP == 8 then
if direction == -1 then
fsx_event("COM2_RADIO_FRACT_DEC")
xpl_command("sim/radios/stby_com2_fine_down")
elseif direction == 1 then
fsx_event("COM2_RADIO_FRACT_INC")
xpl_command("sim/radios/stby_com2_fine_up")
end
end
end
---------------
function rot2(direction)
--Default behavior-control G5HSI
if BP == 0 then
if direction == 1 then
si_command("si/g5/2/knob_cw")
elseif direction == -1 then
si_command("si/g5/2/knob_ccw")
end
end
end
-------------- Create hardware assignments -------
hw_button_add("Button3", BP3)
hw_button_add("Button4", BP4)
hw_button_add("Button6", BP6)
led3=hw_led_add("LED3", 0.0)
led4=hw_led_add("LED4", 0.0)
led6=hw_led_add("LED6", 0.0)
led7=hw_led_add("LED7", 0.0)
led8=hw_led_add("LED8", 0.0)
hw_dial_add("TopRotary", "TYPE_1_DETENT_PER_PULSE", rot1)
hw_dial_add("BottomRotary", "TYPE_1_DETENT_PER_PULSE", rot2)
hw_button_add("TopButton", tpress, trelease)
hw_button_add("BottomButton", bpress, brelease)
Thank you for looking.
Steve