Toliss A321 Centre clock coding issue

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Message
Author
Macsbig
Posts: 31
Joined: Sun Jan 01, 2023 9:02 pm

Toliss A321 Centre clock coding issue

#1 Post by Macsbig »

Hi guys, first time poster here

I am learning Air Manager and LUA as I’m going along, so it’s a bit of a steep learning curve. I’ve got a bit stuck on the Clock/Chrono/Elapsed time display in the centre panel of the Toliss A321 I’m coding.
I would like to have the chrono to only display when Volts >= 10, and to disappear when I press the RST button. The clock needs to be on display all the time. I’m having a lot of trouble getting this to work, even though I have watched Tony’s YouTube tutorials and watched a 3-hour crash course on LUA! I seem to either have it on all the time, or it stays invisible, or the timer doesn’t run.

With the current code, it’s just staying on all the time.
Once that is sorted, I will want to start to code the Elapsed time Chrono.

Could someone take a look and show me the error of my ways, please?

Thanks in advance!

Code: Select all

------------------------------------------
--A321 Center Clock                     --
--Mark B 22122022                  --
--Version 1.0                           --
------------------------------------------

------------------------------------------
--  Load and display images in Z-order  --
------------------------------------------

img_add_fullscreen("Background.png")

img_gpsswch = img_add("switch.png", 393, 215, 50, 85)

img_runswch = img_add("switch.png", 393, 340, 50, 85)

img_datebtn = img_add("large_button.png", 26, 218, 80, 80)



-- variables --

local timer_state    = 0  -- 0: Stopped and reset 1:running 2:stopped
local timer_value    = 0
local timer_display  = 0

-- Fonts --

chrfont =          "font:digital-7-mono.ttf; size:60px; color: #FFFAD0; halign: center;"
clkfont_hrs_mins = "font:digital-7-mono.ttf; size:70px; color: #FFFAD0; halign: center;"
clkfont_secs =     "font:digital-7-mono.ttf; size:55px; color: #FFFAD0; halign: center;"

-- Texts --

txt_timer =          txt_add(" ", chrfont, 162, 102, 158, 60)
txt_clock_hrs_mins = txt_add(" ", clkfont_hrs_mins, 117, 230, 175, 60)
txt_clock_secs =     txt_add(" ", clkfont_secs, 288, 237, 100, 60)


-- Buttons and Switches --

function chrbutton_pressed()
         
         timer_state = timer_state + 1
         
      if timer_value == 0 and timer_state == 1 then
         timer_value = -1
         
      end 
         
      if timer_state ==2 then
         timer_state = 0        
      end              
end
    
function rstbutton_pressed()

         timer_state   = 0
         timer_value   = 0
         timer_display = 0                
end
    
    button_add("button.png", "button_press.png", 392,120,60,60,chrbutton_pressed)
    button_add("button.png", "button_press.png", 35,120,60,60,rstbutton_pressed)



-- Timer----------------------------------------------
------------------------------------------------------

function timer_callback()

      if timer_state == 1 then
         timer_value = timer_value + 1       
               
      end
    
    txt_set(txt_timer, string.format("%02.0f:%02.0f", math.floor((timer_value / 60) % 60), (timer_value % 60)) )
    
function bus_volts(volts)

        if volts[1] >= 10 then timer_display = 1
        else timer_display = 0
        
        visible(txt_timer, timer_display == 1)
        end
    xpl_dataref_subscribe ("sim/cockpit2/electrical/bus_volts", "FLOAT[6]", bus_volts) 
            
    
  
end    
    end
    
 

-- Clock ----------------------------------------------
-------------------------------------------------------

function new_xpl_data(tZulu_hours, tZulu_minutes, tZulu_seconds)
    
        txt_set(txt_clock_secs, string.format("%02.0f", (tZulu_seconds)))
        txt_set(txt_clock_hrs_mins, string.format("%02.0f:%02.0f", (tZulu_hours), math.floor(((tZulu_minutes/60)%1)*60)))
end

--  Data Refs --
                   
xpl_dataref_subscribe
                     ("sim/cockpit2/clock_timer/zulu_time_hours", "INT", 
                      "sim/cockpit2/clock_timer/zulu_time_minutes", "INT",
                      "sim/cockpit2/clock_timer/zulu_time_seconds", "INT", new_xpl_data)
 
                        
timer_start(0,1000,timer_callback)                        
Currently building my own home cockpit, general purpose one with 3 x 32” wrap around monitors and two lower monitors for instruments and PFD’s. Currently, putting together parts for the Toliss A321

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

Re: Toliss A321 Centre clock coding issue

#2 Post by SimPassion »

Macsbig wrote: Tue Jan 10, 2023 5:02 pm Hi guys, first time poster here

I am learning Air Manager and LUA as I’m going along, so it’s a bit of a steep learning curve. I’ve got a bit stuck on the Clock/Chrono/Elapsed time display in the centre panel of the Toliss A321 I’m coding.
I would like to have the chrono to only display when Volts >= 10, and to disappear when I press the RST button. The clock needs to be on display all the time. I’m having a lot of trouble getting this to work, even though I have watched Tony’s YouTube tutorials and watched a 3-hour crash course on LUA! I seem to either have it on all the time, or it stays invisible, or the timer doesn’t run.

With the current code, it’s just staying on all the time.
Once that is sorted, I will want to start to code the Elapsed time Chrono.

Could someone take a look and show me the error of my ways, please?

Thanks in advance!

Code: Select all

------------------------------------------
--A321 Center Clock                     --
--Mark B 22122022                  --
--Version 1.0                           --
------------------------------------------

------------------------------------------
--  Load and display images in Z-order  --
------------------------------------------

img_add_fullscreen("Background.png")

img_gpsswch = img_add("switch.png", 393, 215, 50, 85)

img_runswch = img_add("switch.png", 393, 340, 50, 85)

img_datebtn = img_add("large_button.png", 26, 218, 80, 80)



-- variables --

local timer_state    = 0  -- 0: Stopped and reset 1:running 2:stopped
local timer_value    = 0
local timer_display  = 0

-- Fonts --

chrfont =          "font:digital-7-mono.ttf; size:60px; color: #FFFAD0; halign: center;"
clkfont_hrs_mins = "font:digital-7-mono.ttf; size:70px; color: #FFFAD0; halign: center;"
clkfont_secs =     "font:digital-7-mono.ttf; size:55px; color: #FFFAD0; halign: center;"

-- Texts --

txt_timer =          txt_add(" ", chrfont, 162, 102, 158, 60)
txt_clock_hrs_mins = txt_add(" ", clkfont_hrs_mins, 117, 230, 175, 60)
txt_clock_secs =     txt_add(" ", clkfont_secs, 288, 237, 100, 60)


-- Buttons and Switches --

function chrbutton_pressed()
         
         timer_state = timer_state + 1
         
      if timer_value == 0 and timer_state == 1 then
         timer_value = -1
         
      end 
         
      if timer_state ==2 then
         timer_state = 0        
      end              
end
    
function rstbutton_pressed()

         timer_state   = 0
         timer_value   = 0
         timer_display = 0                
end
    
    button_add("button.png", "button_press.png", 392,120,60,60,chrbutton_pressed)
    button_add("button.png", "button_press.png", 35,120,60,60,rstbutton_pressed)



-- Timer----------------------------------------------
------------------------------------------------------

function timer_callback()

      if timer_state == 1 then
         timer_value = timer_value + 1       
               
      end
    
    txt_set(txt_timer, string.format("%02.0f:%02.0f", math.floor((timer_value / 60) % 60), (timer_value % 60)) )
    
function bus_volts(volts)

        if volts[1] >= 10 then timer_display = 1
        else timer_display = 0
        
        visible(txt_timer, timer_display == 1)
        end
    xpl_dataref_subscribe ("sim/cockpit2/electrical/bus_volts", "FLOAT[6]", bus_volts) 
            
    
  
end    
    end
    
 

-- Clock ----------------------------------------------
-------------------------------------------------------

function new_xpl_data(tZulu_hours, tZulu_minutes, tZulu_seconds)
    
        txt_set(txt_clock_secs, string.format("%02.0f", (tZulu_seconds)))
        txt_set(txt_clock_hrs_mins, string.format("%02.0f:%02.0f", (tZulu_hours), math.floor(((tZulu_minutes/60)%1)*60)))
end

--  Data Refs --
                   
xpl_dataref_subscribe
                     ("sim/cockpit2/clock_timer/zulu_time_hours", "INT", 
                      "sim/cockpit2/clock_timer/zulu_time_minutes", "INT",
                      "sim/cockpit2/clock_timer/zulu_time_seconds", "INT", new_xpl_data)
 
                        
timer_start(0,1000,timer_callback)                        
At least there's an nesting issue in your coding.
At first retry with the following and let us know how it goes, then we can go further ...

Code: Select all

------------------------------------------
--A321 Center Clock                     --
--Mark B 22122022                  --
--Version 1.0                           --
------------------------------------------

------------------------------------------
--  Load and display images in Z-order  --
------------------------------------------

img_add_fullscreen("Background.png")

img_gpsswch = img_add("switch.png", 393, 215, 50, 85)

img_runswch = img_add("switch.png", 393, 340, 50, 85)

img_datebtn = img_add("large_button.png", 26, 218, 80, 80)



-- variables --

local timer_state    = 0  -- 0: Stopped and reset 1:running 2:stopped
local timer_value    = 0
local timer_display  = 0

-- Fonts --

chrfont =          "font:digital-7-mono.ttf; size:60px; color: #FFFAD0; halign: center;"
clkfont_hrs_mins = "font:digital-7-mono.ttf; size:70px; color: #FFFAD0; halign: center;"
clkfont_secs =     "font:digital-7-mono.ttf; size:55px; color: #FFFAD0; halign: center;"

-- Texts --

txt_timer =          txt_add(" ", chrfont, 162, 102, 158, 60)
txt_clock_hrs_mins = txt_add(" ", clkfont_hrs_mins, 117, 230, 175, 60)
txt_clock_secs =     txt_add(" ", clkfont_secs, 288, 237, 100, 60)


-- Buttons and Switches --

function chrbutton_pressed()
         
         timer_state = timer_state + 1
         
      if timer_value == 0 and timer_state == 1 then
         timer_value = -1
         
      end 
         
      if timer_state ==2 then
         timer_state = 0        
      end              
end
    
function rstbutton_pressed()

         timer_state   = 0
         timer_value   = 0
         timer_display = 0                
end
    
    button_add("button.png", "button_press.png", 392,120,60,60,chrbutton_pressed)
    button_add("button.png", "button_press.png", 35,120,60,60,rstbutton_pressed)



-- Timer----------------------------------------------
------------------------------------------------------

function timer_callback()

      if timer_state == 1 then
         timer_value = timer_value + 1       
               
      end
    
    txt_set(txt_timer, string.format("%02.0f:%02.0f", math.floor((timer_value / 60) % 60), (timer_value % 60)) )
    
end
    
function bus_volts(volts)

        if volts[1] >= 10 then timer_display = 1
        else timer_display = 0
        
        visible(txt_timer, timer_display == 1)
        end
end    

xpl_dataref_subscribe ("sim/cockpit2/electrical/bus_volts", "FLOAT[6]", bus_volts) 

-- Clock ----------------------------------------------
-------------------------------------------------------

function new_xpl_data(tZulu_hours, tZulu_minutes, tZulu_seconds)
    
        txt_set(txt_clock_secs, string.format("%02.0f", (tZulu_seconds)))
        txt_set(txt_clock_hrs_mins, string.format("%02.0f:%02.0f", (tZulu_hours), math.floor(((tZulu_minutes/60)%1)*60)))
end

--  Data Refs --
                   
xpl_dataref_subscribe
                     ("sim/cockpit2/clock_timer/zulu_time_hours", "INT", 
                      "sim/cockpit2/clock_timer/zulu_time_minutes", "INT",
                      "sim/cockpit2/clock_timer/zulu_time_seconds", "INT", new_xpl_data)
 
                        
timer_start(0,1000,timer_callback)    

Macsbig
Posts: 31
Joined: Sun Jan 01, 2023 9:02 pm

Re: Toliss A321 Centre clock coding issue

#3 Post by Macsbig »

Thanks very much (I'll google what a nesting issue is!)

I will take a look and get back to you.

Thanks for the help

Mark
Currently building my own home cockpit, general purpose one with 3 x 32” wrap around monitors and two lower monitors for instruments and PFD’s. Currently, putting together parts for the Toliss A321

Macsbig
Posts: 31
Joined: Sun Jan 01, 2023 9:02 pm

Re: Toliss A321 Centre clock coding issue

#4 Post by Macsbig »

ok, so....

If I start the instrument with an "engines running" (voltage on) aircraft, the Chrono is displayed and works, but when I press the reset button, it zero's, but the "visible false" doesn't work...the chrono stays displayed. If I shut off the voltage bus, it does disappear though, which is good.

Also, if I turn off the voltage bus and then restart the voltages, I have no display, it stays in the "visible false" state.

cheers

Mark
Currently building my own home cockpit, general purpose one with 3 x 32” wrap around monitors and two lower monitors for instruments and PFD’s. Currently, putting together parts for the Toliss A321

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

Re: Toliss A321 Centre clock coding issue

#5 Post by SimPassion »

Macsbig wrote: Tue Jan 10, 2023 6:29 pm ok, so....

If I start the instrument with an "engines running" (voltage on) aircraft, the Chrono is displayed and works, but when I press the reset button, it zero's, but the "visible false" doesn't work...the chrono stays displayed. If I shut off the voltage bus, it does disappear though, which is good.

Also, if I turn off the voltage bus and then restart the voltages, I have no display, it stays in the "visible false" state.

cheers

Mark
Hi Mark

I don't own the 321, just the 319 here, I will see if datarefs are the same to check
Perhaps could you post your instrument SIFF file (export), to avoid recreating the bitmaps, here or by PM if you prefer to not make it public

[EDIT] Nevermind, the datarefs are only default "sim/...", sorry haven't checked it at first, so indeed available for the A319 too ... :roll:

Macsbig
Posts: 31
Joined: Sun Jan 01, 2023 9:02 pm

Re: Toliss A321 Centre clock coding issue

#6 Post by Macsbig »

Ah, thanks. I just can't see what's up at the moment. Practice makes perfect though.

Cheers
Currently building my own home cockpit, general purpose one with 3 x 32” wrap around monitors and two lower monitors for instruments and PFD’s. Currently, putting together parts for the Toliss A321

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

Re: Toliss A321 Centre clock coding issue

#7 Post by SimPassion »

Macsbig wrote: Tue Jan 10, 2023 7:34 pm Ah, thanks. I just can't see what's up at the moment. Practice makes perfect though.

Cheers
Working on it, this is the current AM instrument with bitmaps I've added for the check :

image.png

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

Re: Toliss A321 Centre clock coding issue

#8 Post by SimPassion »

This part wasn't correct, so should also be updated :

Code: Select all

function bus_volts(volts)

        if volts[1] >= 10 then
			timer_display = 1
		else
			timer_display = 0
        end
        
        visible(txt_timer, timer_display == 1)
end    

User avatar
Ralph
Posts: 7932
Joined: Tue Oct 27, 2015 7:02 pm
Location: De Steeg
Contact:

Re: Toliss A321 Centre clock coding issue

#9 Post by Ralph »

You can just do this

Code: Select all

visible(txt_timer, bus_volts[1] >= 10)

Macsbig
Posts: 31
Joined: Sun Jan 01, 2023 9:02 pm

Re: Toliss A321 Centre clock coding issue

#10 Post by Macsbig »

Thanks, I'll try it tomorrow.

I did try that first, but I was getting a nil value error. Got a feeling I moved the data subscribe eventually. I've got a bit confused about when I've done what...hence the help request.

Thanks guys for all the help. I'm looking forward to being educated 🙂

Mark
Currently building my own home cockpit, general purpose one with 3 x 32” wrap around monitors and two lower monitors for instruments and PFD’s. Currently, putting together parts for the Toliss A321

Post Reply