WORKING.. Scripting Code Automate Button, POT and ROTs functions

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Post Reply
Message
Author
Paulsharp360
Posts: 8
Joined: Wed Aug 01, 2018 7:56 am
Location: New Zealand
Contact:

WORKING.. Scripting Code Automate Button, POT and ROTs functions

#1 Post by Paulsharp360 »

Hi Everybody

I have developed some code with a friend that provides routines/loops for Buttons, LEDs, POTs, and ROT.

The structure is basically;
(1) Define ARRAYS
(2) Write the arrays to memory in loops
(3) Run the FUNCTIONS


I managed to reduce the code from X hundred lines to 10's of lines :)
Feel free use.

I am sorry but I dont have a lot of time to support... however will add tweaks and changes over time.

All the best.
Paul

Here is my project https://plus.google.com/u/0/?tab=mX

Code: Select all

-- Pauls Challenger 300 Hardware Device Generic Code
-- Version 5.3 03/01/2019


-- ##### THIS IS FOR LEFT PILOT DCP #####

hw_name = "ARDUINO_MEGA2560_A_D" -- The A BOARD
LNDG_VALUE = -1
Map_Range = 1
--Tune1=0
--Tune2=0

-- Default LED brightness levels
on_brightness = 0.25
off_brightness = 0.0
-- Variables for LED test
led_number = 0
led_test_state = 0


-- ##################################################################
--
-- ARRAYS
--
-- ##################################################################

LEDS = {
-- {"Name", LED pin}
-- {"LED_TEST", 4},

}

SPECIAL_BUTTONS = {
-- {"Name", LED pin, button pin}
-- {"PANEL_LED_TEST", 3, 5},
}

MFD_BUTTONS = { -- Assigned to 1st Row on Arduino Board --
-- {"Name", LED pin, button pin , "C"/"D", "dataref", "datatype", Value, LEDtime}
-- {"MFD control panel move l/r switch to left", 3, 8, "C", "cl300/mfd_lr_left"},


}

PUSH_BUTTONS = { -- Assigned to 1nd Row on Arduino Board --
-- {"Name", LED pin, button pin , "C"/"D", "dataref", "datatype", Value, LEDtime}

{"NAVSRC", 14, 15, "C", "cl300/DCP/navsrc_toggle", nil, nil, nil, nil },
{"FRMT", 16, 17, "C", "xhsi/nd_pilot/mode_cycle", nil, nil, nil, nil },
{"TRWX", 18, 19, "C", "cl300/tr_wx", nil, nil, nil, nil },
{"BRG SRC", 20, 21, "C", "cl300/DCP/brgsrc_toggle", nil, nil, nil, nil },
{"REFS", 22, 23, "C", "cl300/DCP/dcp_refs_button", nil, nil, nil, nil },

{"TUNE_Button", 3, 42, "C", "xap/DCP/dcp_tune_stb", nil, nil, nil, nil },

-- new board {"TUNEROTButton", 3, 24, "C", "xap/DCP/dcp_tune_stb", nil, nil, nil, nil },
{"DATAROTButton", 3, 11, "C", "xap/DCP/dcp_data_sel", nil, nil, nil, nil },



-- PANEL RIGHT
-- new board -- button{"CRSROTButton", 3, 22, "C", "xap/DCP/dcp_tune_stb", nil, nil, nil, nil },

-- new board {"SpeedROTButton", 3, 22, "C", "xap/DCP/dcp_tune_stb", nil, nil, nil, nil },


}


LATCHING_BUTTONS = {
-- "Name", LED pin, button pin, "C"/"D", "dataref_on", "dataref_off", "datatype", value_on, value_off

{"HALF", 4, 5, "D", "cl300/dcp_1_2", "cl300/dcp_1_2", "INT", 1, 0 },
{"DME", 6, 7, "D", "cl300/dcp_dmeh", "cl300/dcp_dmeh", "INT", 1, 0 },

{"TFC", 8, 9, "D", "sim/cockpit2/EFIS/EFIS_tcas_on", "sim/cockpit2/EFIS/EFIS_tcas_on", "INT", 0, 1 },
-- new board -- not sure of ref{"RADIO", ??, ??, "D", "sim/flightmodel/controls/flaprqst", nil, "FLOAT", 1, nil },
{"RADAR", 12, 13, "D", "cl300/dcp_radar", "cl300/dcp_radar", "INT", 1, 0 },

{"RADIO", 52, 53, "D", "cl300/mfd_sel_state_h", "cl300/mfd_sel_state_h", "INT", 1, 0, nil },

{"HDGROTButton", 3, 35, "C", "cl300/fgp_s_hdg", "cl300/fgp_s_hdg", "INT", 1, 0 },

-- PANEL RIGHT
-- {"FlightDirector", 38, 39, "C", "sim/autopilot/fdir_toggle", nil, nil, nil, nil },
-- {"NAV", 40, 41, "C", "sim/autopilot/NAV", nil, nil, nil, nil },
-- {"HDG", 42, 43, "C", "sim/autopilot/heading", nil, nil, nil, nil },
-- {"APPR", 44, 45, "C", "sim/autopilot/approach", nil, nil, nil, nil },
-- {"FLC_PITCH", 46, 47, "D", "sim/cockpit2/autopilot/pitch_status", "sim/cockpit2/autopilot/pitch_status", "INT", 2, 0 },
-- {"VS", 48, 49, "C", "sim/autopilot/vertical_speed", nil, nil, nil, nil },
-- {"HalfBank", 50, 51, "D", "cl300/half_bank_h", "cl300/half_bank_h", "INT", 1, 0 },
--{"back_course", 52, 53, "C", "sim/autopilot/back_course", nil, nil, nil, nil },
-- new board {"VNAV", 1, 2, "D", "sim/cockpit2/autopilot/fms_vnav", "sim/cockpit2/autopilot/fms_vnav, "INT", 1, 0 },
}


COUNT_PUSH_BUTTONS = {
-- {"Name", LED pin, button pin , "C"/"D", "dataref", "datatype", Value, LEDtime, Value1, Value2, Value3, ValueCount}
--Not implemented yet


}





-- ******************************************************************
-- ******************************************************************
-- R O T A R Y E N C O D E R array
-- Quickly create several ROT variables. Add an entry to this array containing the ROT name and the Arduino pin. A variable called rotary_id will be created for each one
ROTENC = {
-- {"Name", ClockW_pin, AntiClock, "TYPE C/D/N", datarefCW,datarefAC}

{"TUNE_OUTTER", 48, 49 ,"C", "xap/DCP/dcp_tune_right_coarse","xap/DCP/dcp_tune_left_coarse" },
{"TUNE_INNER", 47,46, "C", "xap/DCP/dcp_tune_right_fine","xap/DCP/dcp_tune_left_fine" },

{"DATA_OUTTER", 31, 30,"C", "xap/DCP/dcp_data_right","xap/DCP/dcp_data_left"},
{"MENU_INNER", 32,33,"C", "xap/DCP/dcp_menu_right", "xap/DCP/dcp_menu_left"},

{"RANGE", 34, 35, "C", "xhsi/nd_pilot/range_cycle", "xhsi/nd_pilot/range_cycle"}, -- 0 to 6????,"xap/DCP/dcp_data_left" },
-- {"TILT", 36,37, "N","cl300/dcp_tilt",-99,-99}, -- int maybe reset to 0" xap/DCP/dcp_menu_left"},



-- PANEL RIGHT
-- new board {"CRS","D", 28, 29, "sim/radios/obs_HSI_down","sim/radios/obs_HSI_up" },
-- {"Heading", 10, 11, "C", "sim/autopilot/heading_down","sim/autopilot/heading_up" },
-- new board {"SPEED","D", 30,31, "xap/DCP/dcp_menu_right", "xap/DCP/dcp_menu_left"},

-- new board {"VS_ROT","D", 30,31, "sim/autopilot/vertical_speed_down", "sim/autopilot/vertical_speed_up"},



}







-- ###################################################################
--
-- LED ROUNTINE
--
-- ###################################################################
-- Create LEDs from LED array, all initially off
for i, j in ipairs(LEDS) do
_G["led_"..j[1]]=hw_led_add(hw_name..j[2], off_brightness)
end
--Test LEDs
function next_LED()
led_number = led_number + 1
-- fixme: Incorporate other LEDs here too
LED = LATCHING_BUTTONS[led_number]---- this needs to trap serveral arrays
if LED ~= nil then
if led_test_state == 0 then
hw_led_set(_G["led_"..LED[1]], on_brightness)
print("LED "..LED[1].." on")
elseif led_test_state == 1 then
hw_led_set(_G["led_"..LED[1]], off_brightness)
print("LED "..LED[1].." off")
end
else
if led_test_state == 0 then
led_number = 0
led_test_state = 1
else
timer_stop(led_test_timer)
print("Timer stopped")
end
end
end

--timer_start(1000, 200, next_LED)
-- fixme: we should stop this timer when all LEDs have been done








-- ###################################################################
--
-- SPECIAL CUSTOM FUNCTIONS plus Adhoc
--
-- ###################################################################
-- Put custom functions for BUTTONS array here.
-- ** Panel Test Function **
function PANEL_LED_TEST_pressed()
-- fixme: don't restart the timer if it's already running
print("PANEL_TEST pressed")
led_number = 0
led_test_state = 0
led_test_timer = timer_start(500, 200, next_LED) -- this line doubleus up as a tester
end
function PANEL_LED_TEST_released()
print("PANEL_TEST released")
end
-- ** LED Brightness Function **
-- Pot for LED brightness this needs to reflect the brightness on XP so there is a common value for all instruments OR keep as individual
function adc_input_change(BrightnessValue)
print("new value= " .. tostring(BrightnessValue) )
if BrightnessValue > 0 then
on_brightness = BrightnessValue
else
on_brightness = 0.01
end
-- fixme: propagate new brightness to all LEDs that are currently on
end


function LNDG_ALT_CHANGE(direction)
if direction == 1 then
LNDG_VALUE = LNDG_VALUE +.01
xpl_dataref_write("cl300/pressure_lndg_alt_2","FLOAT",LNDG_VALUE)

print("LNDG_ALT_ increased")
end

if direction == -1 then
LNDG_VALUE = LNDG_VALUE -.01
xpl_dataref_write("cl300/pressure_lndg_alt_2","FLOAT",LNDG_VALUE)

print("LNDG_ALT_ decreased")
end
end



-- ###################################################################
--
-- ARRAY Automated FUNCTIONS
--
-- ###################################################################
-- Now create function calls for BUTTONS array

function ROT_CHANGE(rotary_id,datatype,datarefAC,datarefCW,direction)
if direction == -1 then
print (rotary_id.." anti clock")
xpl_command(datarefAC)



else
print (rotary_id.." clock")
xpl_command(datarefCW)

end
-- fixme: Command to set dataref=1 here
end


-- Support for LATCH buttons
function Latch_pressed(button_id, cmd_dat, dataref_on, dataref_off, datatype, datavalue_on, datavalue_off)
print(button_id.." button pressed")

if cmd_dat == "C" then
if dataref_on ~= nil then
xpl_command(dataref_on)
else
print("No action specified")
end
else
if dataref_on ~= nil then
xpl_dataref_write(dataref_on, datatype, datavalue_on)
print(dataref_on)
else
print("No action specified")
end
end

hw_led_set(_G["led_"..button_id], on_brightness)

end

function Latch_released(button_id, cmd_dat, dataref_on, dataref_off, datatype, datavalue_on, datavalue_off)
print(button_id.." button released")

if cmd_dat == "C" then
if dataref_off ~= nil then
xpl_command(dataref_off)
else
print("No action specified")
end
else
if dataref_off ~= nil then
xpl_dataref_write(dataref_off, datatype, datavalue_off)
else
print("No action specified")
end
end

hw_led_set(_G["led_"..button_id], off_brightness)

end


-- Support for PUSH buttons
function Push_pressed(button_id, cmd_dat, dataref , datatype, datavalue, LEDtime)
print(button_id.." button pressed")

if cmd_dat == "C" then
if dataref ~= nil then
xpl_command(dataref)
else
print("No action specified")
end
else
if dataref ~= nil then
xpl_dataref_write(dataref, datatype, datavalue)
else
print("No action specified")
end
end
hw_led_set(_G["led_"..button_id], on_brightness)

end

function Push_released(button_id, cmd_dat, dataref_, datatype, datavalue, LEDtime)
-- print(button_id.." button released")

-- timer_stop(LEDtime) need a timer here so lights stays on for X time
hw_led_set(_G["led_"..button_id], off_brightness)

end

-- Support for MFD_PUSH buttons
function MFD_Push_pressed(button_id, cmd_dat, dataref , datatype, datavalue, LEDtime)
print(button_id.." button pressed")

if cmd_dat == "C" then
if dataref ~= nil then
xpl_command(dataref)
else
print("No action specified")
end
else
if dataref ~= nil then
xpl_dataref_write(dataref, datatype, datavalue)
else
print("No action specified")
end
end
hw_led_set(_G["led_"..button_id], on_brightness)

end

function MFD_Push_released(button_id, cmd_dat, dataref_, datatype, datavalue, LEDtime)
print(button_id.." button released")

-- timer_stop(LEDtime) need a timer here so lights stays on for X time
hw_led_set(_G["led_"..button_id], off_brightness)

end



-- Support for COUNT PUSH buttons
function Count_Button_pressed(button_id, cmd_dat, dataref , datatype, datavalue, LEDtime,V1,V2,v3,VC)
print(button_id.." button pressed")

if cmd_dat == "C" then
if dataref_on ~= nil then
xpl_command(dataref_on)
else
print("No action specified")
end
else
if dataref_on ~= nil then
xpl_dataref_write(dataref_on, datatype, datavalue_on)
else
print("No action specified")
end
end

hw_led_set(_G["led_"..button_id], on_brightness)

end

function Count_Button_released(button_id, cmd_dat, dataref_, datatype, datavalue, LEDtime,V1,V2,v3,VC)
print(button_id.." button released")

-- if cmd_dat == "C" then
-- if dataref_off ~= nil then
-- xpl_command(dataref_off)
-- else
-- print("No action specified")
-- end
-- else
-- if dataref_off ~= nil then
-- xpl_dataref_write(dataref_off, datatype, datavalue_off)
-- else
-- print("No action specified")
-- end
-- end

hw_led_set(_G["led_"..button_id], off_brightness)

end


-- ###################################################################
--
-- BINDING of STANDARD ARRAY COMPONENTS
--
-- ###################################################################

for i, j in ipairs(LATCHING_BUTTONS) do
_G["led_"..j[1]] = hw_led_add(hw_name..j[2], off_brightness)
hw_button_add(hw_name..j[3], function() Latch_pressed(j[1], j[4], j[5], j[6], j[7], j[8]) end, function() Latch_released(j[1], j[4], j[5], j[6], j[7], j[8],j[9]) end)
end

for i, j in ipairs(COUNT_PUSH_BUTTONS) do
_G["led_"..j[1]] = hw_led_add(hw_name..j[2], off_brightness)
hw_button_add(hw_name..j[3], function() Count_Button_pressed(j[1], j[4], j[5], j[6], j[7], j[8], j[9], j[10], j[11]) end, function() Count_Button_released(j[1], j[4], j[5], j[6], j[7], j[8], j[9], j[10], j[11], j[12]) end)
end

for i, j in ipairs(PUSH_BUTTONS) do
_G["led_"..j[1]] = hw_led_add(hw_name..j[2], off_brightness)
hw_button_add(hw_name..j[3], function() Push_pressed(j[1], j[4], j[5], j[6], j[7], j[8]) end, function() Push_released(j[1], j[4], j[5], j[6], j[7], j[8]) end)
end

for i, j in ipairs(MFD_BUTTONS) do
_G["led_"..j[1]] = hw_led_add(hw_name..j[2], off_brightness)
hw_button_add(hw_name..j[3], function() MFD_Push_pressed(j[1], j[4], j[5], j[6], j[7], j[8]) end, function() MFD_Push_released(j[1], j[4], j[5], j[6], j[7], j[8]) end)
end

for i, j in ipairs(SPECIAL_BUTTONS) do
hw_button_add(hw_name..j[2], _G[j[1].."_pressed"], _G[j[1].."_released"])
end

-- function calls for ROTENC array
for i, j in ipairs(ROTENC) do
_G["ROT_"..j[1]]=hw_dial_add(hw_name..j[2], hw_name..j[3],function(direction) ROT_CHANGE(j[1], j[4],j[5], j[6], direction) end)
end


-----------------------------------------------------------------------------------------



-- ###################################################################
--
-- BINDING of NON STANDARD COMPONENTS
--
-- ###################################################################


-- Brightness Bind to the pin A13
--hw_adc_input_add("ARDUINO_MEGA2560_G_A13", adc_input_change)
-- fixme: use hw_name here somehow



Paul Sharp
Oxford
New Zealand

Building Challenger 300. Mainly with custom 3d printed parts and AM3 scripts using 8 Arduino Boards

Post Reply