Keith's LUA Help thread.

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Message
Author
SimPassion
Posts: 5336
Joined: Thu Jul 27, 2017 12:22 am

Re: Keith's LUA Help thread.

#231 Post by SimPassion »

Keith Baxter wrote: Mon Aug 19, 2019 10:57 am
SimPassion wrote: Mon Aug 19, 2019 10:49 am Please Keith, take time to try with other approach that are proposed to you by other,
it will save a lot of time, stay confident with this

Regards
Gilles
Gilles,

I am trying to understand what is happening Gilles. I am confused and need to get to the bottom of it. That is why I am asking so many questions because it does not make sense to me at this stage. It has always confused me. Once it sinks in I will be away.


Keith
I understand it could be frustrating to not find an easy way to get over coding language behavior
here, however we get LUA interpreter embedded in AM vs C++ coding rules and so we don't get the same level of freedom and ability that we already know in C++ structure

Just to help each other going in the logic that already simplify our coding tasks
Though I understand troubleshooting is something like a challenge that give us often a good feedback and fluent scripting on final, when all things are working as expected

Gilles

User avatar
Keith Baxter
Posts: 4674
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Keith's LUA Help thread.

#232 Post by Keith Baxter »

Thank you to all for the help and assistance. I think I now know what is happening and why I have had inconsistencies in the past. It was that I did not grasp the fact that the code had to run one pass first before the variables were changed. It makes sense now.

My head was spinning and I am now going to take a day or so off just to relax before I stuck in to completing those panels that were giving me issues.

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
Keith Baxter
Posts: 4674
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Keith's LUA Help thread.

#233 Post by Keith Baxter »

So i woke up in the middle of the night with an idea. Test the short code in the debugger to see what is actually happening. Now I am even more confused.

Please explain.



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

Re: Keith's LUA Help thread.

#234 Post by Sling »

The result is different because when you do it slow the timer expires before the subscribe callback is called, hence the var is not updated. If you make the timer long enough to cover stepping through the code it’s likely you will get the same result.

User avatar
Keith Baxter
Posts: 4674
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Keith's LUA Help thread.

#235 Post by Keith Baxter »

Sling wrote: Tue Aug 20, 2019 1:34 pm The result is different because when you do it slow the timer expires before the subscribe callback is called, hence the var is not updated. If you make the timer long enough to cover stepping through the code it’s likely you will get the same result.
Thank you Tony

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
Keith Baxter
Posts: 4674
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Keith's LUA Help thread.

#236 Post by Keith Baxter »

Hi @Sling Tony,

Is there a more elegant way of coding this button array? I thing you had a method of doing away with all The "esleif"
I cannot test without physically connecting 80 buttons . @Corjan

Code: Select all

-- Callback function which is called when a button is pressed
function button_pressed(row, column)

	if row == 1 and column == 1 then
		xpl_command("laminar/B738/button/fmc1_A")
	elseif row == 1 and column == 2 then
		xpl_command("laminar/B738/button/fmc1_B")
	elseif row == 1 and column == 3 then
		xpl_command("laminar/B738/button/fmc1_C")
	elseif row == 1 and column == 4 then
		xpl_command("laminar/B738/button/fmc1_D")
	elseif row == 1 and column == 5 then
		xpl_command("laminar/B738/button/fmc1_E")
	end
	  print("button pressed at " .. row .. ", " .. column)
end

function button_released()
end


-- Create button array with name "my keypad" with 2 rows and 2 columns
hw_button_array_add("my keypad", 10, 8, button_pressed, button_released)
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
Sling
Posts: 5237
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: Keith's LUA Help thread.

#237 Post by Sling »

Keith,

I don't remember coming up with a smart way for this one but after a quick look I've come up with a nice method that saves having to use any if..else's at all.
Some parts of the add function have bugs so the code is the way it is because of these. It can be tidied a little further once the bugs are fixed.

Its only for a 3x2 array but it will work for 10x8 by simply filling in the whole array with 80 elements and changing the n_rows and n_cols variable's to 10 & 8. The rest should be self explanatory.

@Ralph and @Corjan These are the issues.

--Note: named hardware for hw_button_array_add() function is broke in AM 3.7 beta 12 so do it the old way for now
--Note: the button_released argument is not required normally but a bug in AM 3.7 beta 12 exists that forces you to add it or you get an error
--Note: if you fail to declare the button_released function before the add it does not create an error, but it should

Code: Select all

local fmc_command = "laminar/B738/button/fmc1_"
--array format = r1c1, r1c2, r2c1, r2c2, r3c1, r3c2
local fmc_command_array = {"A", "B", "C", "D", "E", "F"}
local n_rows, n_cols = 3, 2

-- row function which is called after the column is established
function button_pressed(row, col)

    local i = row * n_cols + (col + 1)
    xpl_command(fmc_command .. fmc_command_array[i])

      print("button pressed at " .. row .. ", " .. col .. ", " .. fmc_command_array[i])

end

function button_released(row, col)
end

-- Create button array with name "my keypad" with 10 rows and 8 columns
--hw_button_array_add("my keypad", 3, 2, button_pressed, button_released)

hw_button_array_add(n_rows, n_cols, "ARDUINO_UNO_A_D2", "ARDUINO_UNO_A_D3", "ARDUINO_UNO_A_D4", "ARDUINO_UNO_A_D8", "ARDUINO_UNO_A_D9", button_pressed, button_released)

User avatar
Keith Baxter
Posts: 4674
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Keith's LUA Help thread.

#238 Post by Keith Baxter »

Sling wrote: Fri Feb 07, 2020 6:11 am Keith,

I don't remember coming up with a smart way for this one but after a quick look I've come up with a nice method that saves having to use any if..else's at all.
Some parts of the add function have bugs so the code is the way it is because of these. It can be tidied a little further once the bugs are fixed.

Its only for a 3x2 array but it will work for 10x8 by simply filling in the whole array with 80 elements and changing the n_rows and n_cols variable's to 10 & 8. The rest should be self explanatory.

@Ralph and @Corjan These are the issues.

--Note: named hardware for hw_button_array_add() function is broke in AM 3.7 beta 12 so do it the old way for now
--Note: the button_released argument is not required normally but a bug in AM 3.7 beta 12 exists that forces you to add it or you get an error
--Note: if you fail to declare the button_released function before the add it does not create an error, but it should

Code: Select all

local fmc_command = "laminar/B738/button/fmc1_"
--array format = r1c1, r1c2, r2c1, r2c2, r3c1, r3c2
local fmc_command_array = {"A", "B", "C", "D", "E", "F"}
local n_rows, n_cols = 3, 2

-- row function which is called after the column is established
function button_pressed(row, col)

    local i = row * n_cols + (col + 1)
    xpl_command(fmc_command .. fmc_command_array[i])

      print("button pressed at " .. row .. ", " .. col .. ", " .. fmc_command_array[i])

end

function button_released(row, col)
end

-- Create button array with name "my keypad" with 10 rows and 8 columns
--hw_button_array_add("my keypad", 3, 2, button_pressed, button_released)

hw_button_array_add(n_rows, n_cols, "ARDUINO_UNO_A_D2", "ARDUINO_UNO_A_D3", "ARDUINO_UNO_A_D4", "ARDUINO_UNO_A_D8", "ARDUINO_UNO_A_D9", button_pressed, button_released)
@Sling
Thank you Tony.

Yes I found those broken button array issues as well.

One of the reasons I was asking Corjan about support for the RPi zero is a remember having a chat to Ralph about 2 years ago.
With using the hw_button_array_add we cut down the number of pins used on the FMC from 69 to 18. That allows us to use a RPI for the 5" HDMI display and the button_array. Which is sweet.

I would want to test this first before I go to the effort of getting a PC board made and soldering 69 mini push buttons on it.

I did this before you posted your smart code. But it should work now. It is the Captains side FMC button hardware code.
@SimPassion

Code: Select all

-- Callback function which is called when a button is pressed
function button_pressed(row, column)
--row1
	if row == 1 and column == 1 then
		xpl_command("laminar/B738/button/fmc1_1L")
	elseif row == 1 and column == 2 then
		xpl_command("laminar/B738/button/fmc1_2L")
	elseif row == 1 and column == 3 then
		xpl_command("laminar/B738/button/fmc1_3L")
	elseif row == 1 and column == 4 then
		xpl_command("laminar/B738/button/fmc1_4L")
	elseif row == 1 and column == 5 then
		xpl_command("laminar/B738/button/fmc1_5L")
	elseif row == 1 and column == 6 then
		xpl_command("laminar/B738/button/fmc1_6L")
--row 2
	elseif row == 2 and column == 1 then
		xpl_command("laminar/B738/button/fmc1_1R")
	elseif row == 2 and column == 2 then
		xpl_command("laminar/B738/button/fmc1_2R")
	elseif row == 2 and column == 3 then
		xpl_command("laminar/B738/button/fmc1_3R")		
	elseif row == 2 and column == 4 then
		xpl_command("laminar/B738/button/fmc1_4R")		
	elseif row == 2 and column == 5 then
		xpl_command("laminar/B738/button/fmc1_5R")
	elseif row == 2 and column == 6 then
		xpl_command("laminar/B738/button/fmc1_6R")
---row 3		
	elseif row == 3 and column == 1 then
		xpl_command("laminar/B738/button/fmc1_init_ref")
	elseif row == 3 and column == 2 then
		xpl_command("laminar/B738/button/fmc1_rte")
	elseif row == 3 and column == 3 then
		xpl_command("laminar/B738/button/fmc1_clb")
	elseif row == 3 and column == 4 then
		xpl_command("laminar/B738/button/fmc1_crz")
	elseif row == 3 and column == 5 then
		xpl_command("laminar/B738/button/fmc1_des")
---row 4		
		
	elseif row == 4 and column == 1 then
		xpl_command("laminar/B738/button/fmc1_menu")		
	elseif row == 4 and column == 2 then
		xpl_command("laminar/B738/button/fmc1_legs")		
	elseif row == 4 and column == 3 then
		xpl_command("laminar/B738/button/fmc1_dep_app")
	elseif row == 4 and column == 4 then
		xpl_command("laminar/B738/button/fmc1_hold")
	elseif row == 4 and column == 5 then
		xpl_command("laminar/B738/button/fmc1_prog")
	elseif row == 4 and column == 6 then
		xpl_command("laminar/B738/button/fmc1_exec")
--row 5				
	elseif row == 5 and column == 1 then
		xpl_command("laminar/B738/button/fmc1_n1_lim")
	elseif row == 5 and column == 2 then
		xpl_command("laminar/B738/button/fmc1_fix")
	elseif row == 5 and column == 3 then
		xpl_command("laminar/B738/button/fmc1_A")
	elseif row == 5 and column == 4 then
		xpl_command("laminar/B738/button/fmc1_B")
	elseif row == 5 and column == 5 then
		xpl_command("laminar/B738/button/fmc1_C")
	elseif row == 5 and column == 6 then
		xpl_command("laminar/B738/button/fmc1_D")
	elseif row == 5 and column == 7 then
		xpl_command("laminar/B738/button/fmc1_E")
--row 6				
	elseif row == 6 and column == 1 then
		xpl_command("laminar/B738/button/fmc1_prev_page")
	elseif row == 6 and column == 2 then
		xpl_command("laminar/B738/button/fmc1_next_page")
	elseif row == 6 and column == 3 then
		xpl_command("laminar/B738/button/fmc1_F")
	elseif row == 6 and column == 4 then
		xpl_command("laminar/B738/button/fmc1_G")
	elseif row == 6 and column == 5 then
		xpl_command("laminar/B738/button/fmc1_H")
	elseif row == 6 and column == 6 then
		xpl_command("laminar/B738/button/fmc1_I")
	elseif row == 6 and column == 7 then
		xpl_command("laminar/B738/button/fmc1_J")		
--row 7				
	elseif row == 7 and column == 1 then
		xpl_command("laminar/B738/button/fmc1_1")
	elseif row == 7 and column == 2 then
		xpl_command("laminar/B738/button/fmc1_2")
	elseif row == 7 and column == 3 then
		xpl_command("laminar/B738/button/fmc1_3")
	elseif row == 7 and column == 4 then
		xpl_command("laminar/B738/button/fmc1_K")
	elseif row == 7 and column == 5 then
		xpl_command("laminar/B738/button/fmc1_L")
	elseif row == 7 and column == 6 then
		xpl_command("laminar/B738/button/fmc1_M")
	elseif row == 7 and column == 7 then
		xpl_command("laminar/B738/button/fmc1_N")		
	elseif row == 7 and column == 8 then
		xpl_command("laminar/B738/button/fmc1_O")		
--row 8			
	elseif row == 8 and column == 1 then
		xpl_command("laminar/B738/button/fmc1_4")
	elseif row == 8 and column == 2 then
		xpl_command("laminar/B738/button/fmc1_5")
	elseif row == 8 and column == 3 then
		xpl_command("laminar/B738/button/fmc1_6")
	elseif row == 8 and column == 4 then
		xpl_command("laminar/B738/button/fmc1_P")
	elseif row == 8 and column == 5 then
		xpl_command("laminar/B738/button/fmc1_Q")
	elseif row == 8 and column == 6 then
		xpl_command("laminar/B738/button/fmc1_R")
	elseif row == 8 and column == 7 then
		xpl_command("laminar/B738/button/fmc1_S")		
	elseif row == 8 and column == 8 then
		xpl_command("laminar/B738/button/fmc1_T")		
--row 9			
	elseif row == 9 and column == 1 then
		xpl_command("laminar/B738/button/fmc1_7")
	elseif row == 9 and column == 2 then
		xpl_command("laminar/B738/button/fmc1_8")
	elseif row == 9 and column == 3 then
		xpl_command("laminar/B738/button/fmc1_9")
	elseif row == 9 and column == 4 then
		xpl_command("laminar/B738/button/fmc1_U")
	elseif row == 9 and column == 5 then
		xpl_command("laminar/B738/button/fmc1_V")
	elseif row == 9 and column == 6 then
		xpl_command("laminar/B738/button/fmc1_W")
	elseif row == 9 and column == 7 then
		xpl_command("laminar/B738/button/fmc1_X")		
	elseif row == 9 and column == 8 then
		xpl_command("laminar/B738/button/fmc1_Y")		
--row 10			
	elseif row == 10 and column == 1 then
		xpl_command("laminar/B738/button/fmc1_period")
	elseif row == 10 and column == 2 then
		xpl_command("laminar/B738/button/fmc1_0")
	elseif row == 10 and column == 3 then
		xpl_command("laminar/B738/button/fmc1_minus")
	elseif row == 10 and column == 4 then
		xpl_command("laminar/B738/button/fmc1_Z")
	elseif row == 10 and column == 5 then
		xpl_command("laminar/B738/button/fmc1_SP")
	elseif row == 10 and column == 6 then
		xpl_command("laminar/B738/button/fmc1_del")
	elseif row == 10 and column == 7 then
		xpl_command("laminar/B738/button/fmc1_slash")		
	elseif row == 10 and column == 8 then
		xpl_command("laminar/B738/button/fmc1_clr")		
		
	end
		
	  print("button pressed at " .. row .. ", " .. column)
end

function button_released()
end


-- Create button array with name "my keypad" with 2 rows and 2 columns
hw_button_array_add("my keypad", 10, 8, button_pressed, button_released)
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
Sling
Posts: 5237
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: Keith's LUA Help thread.

#239 Post by Sling »

Keith Baxter wrote: Fri Feb 07, 2020 6:30 am I did this before you posted your smart code. But it should work now. It is the Captains side FMC button hardware code.
Yes that is a lot of if..else's to cycle through each time. The code i posted avoids all that. Thanks for stimulating me to come up with these solutions. I do enjoy it despite it taking my time away from further dev on my sim. :D

Tony

User avatar
Keith Baxter
Posts: 4674
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: Keith's LUA Help thread.

#240 Post by Keith Baxter »

Sling wrote: Fri Feb 07, 2020 6:39 am
Keith Baxter wrote: Fri Feb 07, 2020 6:30 am I did this before you posted your smart code. But it should work now. It is the Captains side FMC button hardware code.
Yes that is a lot of if..else's to cycle through each time. The code i posted avoids all that. Thanks for stimulating me to come up with these solutions. I do enjoy it despite it taking my time away from further dev on my sim. :D

Tony
Yes, Thank you Tony.

Now to hope foe a 10 x 8 key pad in the "HARDWARE" tab to test. :lol:

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 

Post Reply