How to call function in another insrument

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Post Reply
Message
Author
AzPangesti
Posts: 6
Joined: Mon Apr 26, 2021 9:03 pm

How to call function in another insrument

#1 Post by AzPangesti »

Hi All...
I am building a phyical button (panel) to put on top of a panel layout (covering an LCD screen that shows multiple instruments)
Most of the buttons are arranged in a matrix array (hw_button_array_add ...) with some encoders here and there (hw_dial_add ...)

I managed to create an instrument which basically just call up (or trigger) a bunch of xpl_command for the buttons.
Works well for most of the functions but.. there are some instruments that are a bit more complicated.
Example: KAP140 Autopilot up-down and baro which has some ifs-elseif of its own variable in its function.

Question:
Is there any way for my instrument to call functions from another instrument?
Like an #include xxx in arduino IDE. That way I don't have to modify the original instrument and just trigger its function.

Limitation/Complication:
If only I have physical 1-to-1 pin from button to arduino, I could probably just add a line below the original instrument code to call the same function/callback
button_add("up_up.png", "up_dn.png", 510,70,46,30, up_callback) // original
hw_button_add("ARDUINO_MEGA2560_A_DXX, up_callback) // I could add this
UNFORTUNATELY .. my buttons are setup in a matrix.
I imagine calling up hw_button_array_add from several instruments would have mess things up in the background.

Any ideas/workaround that is doable for a dummy (like me) will be appreciated.

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

Re: How to call function in another insrument

#2 Post by SimPassion »

Hi @AzPangesti,

here some snippets of how it can be performed using functions array :

Code: Select all

------------------------------
-- SWITCH (SELECT CASE like)
------------------------------

-- sample 1

	local a = 2
	local switch = {
		[1] = function()print "Case 1."end,
		[2] = function()print "Case 2."end,
		[3] = function()print "Case 3."end
	}

	local f = switch[a]
	if(f) then
		f()
	else
		print "Case default."
	end

-- other sample for the function array only

local chann_on = {
	["A"] = function() txt_style(str_channelA,txt_style5) end,
	["B"] = function() txt_style(str_channelB,txt_style5) end,
	["C"] = function() txt_style(str_channelC,txt_style5) end,
	["D"] = function() txt_style(str_channelD,txt_style5) end,
	["E"] = function() txt_style(str_channelE,txt_style5) end,
	["F"] = function() txt_style(str_channelF,txt_style5) end,
	["G"] = function() txt_style(str_channelG,txt_style5) end,
	["H"] = function() txt_style(str_channelH,txt_style5) end,
	["I"] = function() txt_style(str_channelI,txt_style5) end,
	["J"] = function() txt_style(str_channelJ,txt_style5) end,
	["K"] = function() txt_style(str_channelK,txt_style5) end,
	["L"] = function() txt_style(str_channelL,txt_style5) end,
	["M"] = function() txt_style(str_channelM,txt_style5) end,
	["N"] = function() txt_style(str_channelN,txt_style5) end,
	["O"] = function() txt_style(str_channelO,txt_style5) end,
	["P"] = function() txt_style(str_channelP,txt_style5) end
}

-- sample 3

local btn_pr = {
	[1] = function()  end,
	[2] = function()  end,
	[3] = function()  end,
	[4] = function()  end,
	[5] = function()  end,
	[6] = function()  end,
	[7] = function()  end,
	[8] = function()  end,
	[9] = function()  end,
	[10] = function()  end,
	[11] = function()  end,
	[12] = function()  end,
	[13] = function()  end,
	[14] = function()  end,
	[15] = function()  end,
	[16] = function()  end
}

function pr_arr_press_capt(row, col)
	-- print("CAPT button pressed at row " .. row .. ", col " .. col)

	btn_nbr = (col+1)+(row*4)

	local f_exec_btn_pr = btn_pr[btn_nbr]
	if (f_exec_btn_pr) then
		f_exec_btn_pr()
	else
	end
	
	print("CAPT PRESS Multiply : "..(col+1)+(row*4))
end

-- sample 4

local btn_pr2 = {
	 [1] = function() press_EXEC() end,
	 [2] = function() press_INIT() end,
	 [3] = function() press_RTE() end,
	 [4] = function() press_CLB() end,
	 [5] = function() press_MENU() end,
	 [6] = function() press_LEGS() end,
	 [7] = function() press_DEPARR() end,
	 [8] = function() press_N1() end,
	 [9] = function() press_FIX() end,
	[10] = function() press_PREV() end,
	[11] = function() press_NEXT() end,
	[12] = function() press_ONE() end,
	[13] = function() press_TWO() end,
	[14] = function() press_THREE() end,
	[15] = function() press_FOUR() end,
	[16] = function() press_FIVE() end
}

local btn_rl2 = {
	 [1] = function() end,
	 [2] = function() end,
	 [3] = function() end,
	 [4] = function() end,
	 [5] = function() release_MENU() end,
	 [6] = function() end,
	 [7] = function() end,
	 [8] = function() end,
	 [9] = function() end,
	[10] = function() end,
	[11] = function() end,
	[12] = function() end,
	[13] = function() end,
	[14] = function() end,
	[15] = function() end,
	[16] = function() end
}

function pr_arr2_press(row, col)
	btn_nbr = (col+1)+(row*4)
	local f_exec_btn_pr = btn_pr2[btn_nbr]
	if (f_exec_btn_pr) then
		f_exec_btn_pr()
	else
	end
end

function pr_arr2_release(row, col)
	btn_nbr = (col+1)+(row*4)
	local f_exec_btn_pr = btn_rl2[btn_nbr]
	if (f_exec_btn_pr) then
		f_exec_btn_pr()
	else
	end
end


Post Reply