Complex coding for Backlights

Questions about deployment and use of Air Manager Instruments

Moderators: Ralph, russ

Message
Author
Mikemike
Posts: 41
Joined: Thu Feb 11, 2021 11:33 pm

Re: Complex coding for Backlights

#11 Post by Mikemike » Wed Mar 10, 2021 10:20 pm

Getting closer =) I changed a few minor things like, fill color, image size, encoder properties and the initial opacity. It's set for initial transparent, but when I turn the rotary encoder, it goes straight to fully filled black and then slowly brightens up but not a huge deal. I'll keep playing with the code some.
  1.  my_canvas=canvas_add(-50,-50,575,575)
  2.  
  3.  
  4.  
  5. ---lets draw the inital black box
  6.  
  7.   canvas_draw(my_canvas, function()
  8.  
  9.      _rect(40,40,575,575)
  10.  
  11.      _fill("black",0) --Note the 0 after the "black". It is the opacity
  12.  
  13.   end)
  14.  
  15.  
  16.  
  17. ---now lets dim the black box
  18.  
  19.  
  20.  
  21. --create a var for dimming/opacity
  22.  
  23. dim_var=1
  24.  
  25. --Add a function
  26.  
  27. function dim_my_canvas()
  28.  
  29.   canvas_draw(my_canvas, function()
  30.  
  31.      _rect(40,40,575,575)
  32.  
  33.      _fill("black",dim_var) ---Note we have used the var we created vary the opacity
  34.  
  35.   end)
  36.  
  37. end
  38.  
  39.  
  40.  
  41. ---add your dial
  42.  
  43. function dial_change(dir)
  44.  
  45.    dim_var=dim_var+(dir*0.1) ---Change the opacity value
  46.  
  47.    var_cap(dim_var,0,1) ---Limit the scope of the opacity value.
  48.  
  49.    dim_my_canvas () --force a callback to the dim function
  50.  
  51. end
  52.  
  53.  
  54.  
  55. -- Create a new rotary encoder
  56.  
  57. hw_dial_add("ARDUINO_MEGA2560_A_D3", "ARDUINO_MEGA2560_A_D4","TYPE_1_DETENT_PER_PULSE", dial_change)    
  58.  
  59.  
  60.  
  61.  

JackZ
Posts: 1849
Joined: Mon Feb 22, 2016 1:02 pm

Re: Complex coding for Backlights

#12 Post by JackZ » Wed Mar 10, 2021 11:03 pm

Hmm I don’t know. Wouldn’t it be better to use opacity(), instead of redrawing the black rectangle every time?
Untested, but I guess that this function would work as well.

Code: Select all

  
function dim_my_canvas()

  opacity(my_canvas,dim_var)

end
And now that the function is stripped down to only one line, this instruction could probably better by being off directly incorporated into the calling function dial_change(dir) to save execution time and make the code easier to read.

Another remark: setting the initial value of dim_var to 1 is what makes the backlight jump to black

Code: Select all

 dim_var=1
The initial value should be 0 to reflect the initial state of your black rectangle that you set to 0 (transparent) when you created it.
So using dim_var while creating the initial rectangle is the best solution to ensure consistency.

Code: Select all

 fill("black",dim_var)
.
Of course, in that case

Code: Select all

dim_var=0
should be placed at the very beginning of your code instead of line 23, to avoid Lua throwing an error

Jacques
Last edited by JackZ on Wed Mar 10, 2021 11:20 pm, edited 1 time in total.
My YouTube Chanel on the A320 (Real SOPs by an Airline Pilot IRL):
https://www.youtube.com/playlist?list=P ... 0Q6SBASRqJ

Mikemike
Posts: 41
Joined: Thu Feb 11, 2021 11:33 pm

Re: Complex coding for Backlights

#13 Post by Mikemike » Wed Mar 10, 2021 11:20 pm

JackZ wrote:
Wed Mar 10, 2021 11:03 pm
Hmm I don’t know. Wouldn’t it be better to use opacity(), instead of redrawing the black rectangle every time?
Untested, but I guess that this function would work as well.

Code: Select all

  
function dim_my_canvas()

  opacity(my_canvas,dim_var)

end
And now that the function is stripped down to only one line, this instruction could probably better by being off directly incorporated into the calling function dial_change(dir) to save execution time and make the code easier to read.

Jacques
Probably, but I'm not truly understanding all this yet as I'm in a huge learning curve and it's a slow go. It does make sense to me that the less is better to produce the same result.

Mikemike

JackZ
Posts: 1849
Joined: Mon Feb 22, 2016 1:02 pm

Re: Complex coding for Backlights

#14 Post by JackZ » Wed Mar 10, 2021 11:21 pm

No worries, we are here to help. Everybody went through this route!
My YouTube Chanel on the A320 (Real SOPs by an Airline Pilot IRL):
https://www.youtube.com/playlist?list=P ... 0Q6SBASRqJ

Mikemike
Posts: 41
Joined: Thu Feb 11, 2021 11:33 pm

Re: Complex coding for Backlights

#15 Post by Mikemike » Thu Mar 11, 2021 1:32 pm

JackZ wrote:
Wed Mar 10, 2021 11:21 pm
No worries, we are here to help. Everybody went through this route!
Thank you.

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests