Complex coding for Backlights

Questions about deployment and use of Air Manager Instruments

Moderators: russ, Ralph

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

Re: Complex coding for Backlights

#11 Post by Mikemike »

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.

Code: Select all

my_canvas=canvas_add(-50,-50,575,575)

---lets draw the inital black box
canvas_draw(my_canvas, function()
_rect(40,40,575,575)
_fill("black",0) --Note the 0 after the "black". It is the opacity
end)

---now lets dim the black box

--create a var for dimming/opacity
dim_var=1
--Add a function
function dim_my_canvas()
canvas_draw(my_canvas, function()
_rect(40,40,575,575)
_fill("black",dim_var) ---Note we have used the var we created vary the opacity
end)
end

---add your dial
function dial_change(dir)
dim_var=dim_var+(dir*0.1) ---Change the opacity value
var_cap(dim_var,0,1) ---Limit the scope of the opacity value.
dim_my_canvas () --force a callback to the dim function
end

-- Create a new rotary encoder
hw_dial_add("ARDUINO_MEGA2560_A_D3", "ARDUINO_MEGA2560_A_D4","TYPE_1_DETENT_PER_PULSE", dial_change)


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

Re: Complex coding for Backlights

#12 Post by JackZ »

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 »

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: 2266
Joined: Mon Feb 22, 2016 1:02 pm

Re: Complex coding for Backlights

#14 Post by JackZ »

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 »

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