_txt dinamic with local function

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Message
Author
User avatar
Marcnamara2021
Posts: 83
Joined: Wed Dec 01, 2021 6:00 am
Location: Sao Jose dos Campos, SP

_txt dinamic with local function

#1 Post by Marcnamara2021 »

Dear AM friends

I've been developing MVP for months. There are approximately 15 screens with hundreds of functions, but I got tangled up in this item. After trying over a month, I can't think of a script that can integrate local variables, data, and the _txt inside a canvas draw callback.

Way Point, Track, F to WP, T to WP, D to WP does not work. The screen shows all functional when works with txt_add

1st screeen with _txt canvas
2nd screen with txt_add

Could you give me some guidance?

Thank You

Code: Select all


--- TELA FUEL MANAGMENT ACESSADA PELO MENU SETUP
--- ENTRADA DE TEXTO FUEL MANAGEMENT -----------


txt_Fuel_L_cyan2 =  "font:Oita W01 Cond Medium.ttf;size:32px; color:cyan; halign:right;valign:center;", 75, 80, 120, 50
txt_Fuel_R_cyan2 =  "font:Oita W01 Cond Medium.ttf;size:32px; color:cyan; halign:right;valign:center;", 75, 103, 120, 50
txt_fuel_total_cyan2 = "font:Oita W01 Cond Medium.ttf;size:32px; color:cyan; halign:right;valign:center;", 75, 173, 120, 50
txt_Fuel_F_cyan2 =  "font:Oita W01 Cond Medium.ttf;size:32px; color:cyan; halign:right;valign:center;", 75, 230, 120, 50
txt_fused_trip2 =  "font:Oita W01 Cond Medium.ttf;size:32px; color:cyan; halign:right;", 75, 255, 120, 50
txt_Fuel_U_cyan2 =  "font:Oita W01 Cond Medium.ttf;size:32px; color:cyan; halign:right;", 75, 281, 120, 50
txt_Fused_trip2 = "font:Oita W01 Cond Medium.ttf;size:32px; color:cyan; halign:right;", 75, 335, 120, 50
txt_Fuel_SF =  "font:Oita W01 Cond Medium.ttf;size:32px; color:cyan; halign:right;", 75, 310, 120, 50
txt_Fuel_rem =  "font:Oita W01 Cond Medium.ttf;size:32px; color:#ff0000; halign:right;", 75, 325, 120, 50
txt_hss2 = "font:Oita W01 Cond Medium.ttf;size:32px; color:red; halign:right;valign:center;", 38, 340, 120, 50
txt_mn2 = "font:Oita W01 Cond Medium.ttf;size:32px; color:red; halign:right;valign:center;", 75, 340, 120, 50
txt_dis_to_e2 = "font:Oita W01 Cond Medium.ttf;size:32px; color:#ff0000; halign:right;valign:center;", 75, 367, 120, 50
txt_altimeter2 = "font:Oita W01 Cond Medium.ttf;size:32px; color:cyan; halign:right;", 284, 228, 120, 50
txt_speed_knots2 = "font:Oita W01 Cond Medium.ttf;size:32px; color:cyan; halign:right;", 284, 173, 120, 50
txt_eficy2 = "font:Oita W01 Cond Medium.ttf;size:32px; color:cyan; halign:right;", 284, 197, 120, 50


--- ENTRADA DE TEXTO GPS DATA WAYPOINT CANVAS------------------------------------------------------

txt_bearingC =     "font:Oita W01 Cond Medium.ttf;size:32px; color:cyan; halign:right;", 284, 150, 120, 50
txt_speed_knotsC = "font:Oita W01 Cond Medium.ttf;size:32px; color:cyan; halign:right;", 284, 173, 120, 50
txt_altimeterC =   "font:Oita W01 Cond Medium.ttf;size:32px; color:cyan; halign:right;", 284, 228, 120, 50
txt_waypointC =    "font:Oita W01 Cond Medium.ttf;size:38px; color:cytan; halign:right;", 250, 110, 120, 50

txt_ftowpC =       "font:Oita W01 Cond Medium.ttf;size:32px; color:cyan; halign:right;", 284, 307, 120, 50

txt_ttowp_hsC =    "font:Oita W01 Cond Medium.ttf;size:32px; color:cyan; halign:right;", 244, 337, 120, 50
txt_ttowp_mnC =    "font:Oita W01 Cond Medium.ttf;size:32px; color:cyan; halign:right;", 284, 337, 120, 50
txt_dtowpC =       "font:Oita W01 Cond Medium.ttf;size:32px; color:cyan; halign:right;", 284, 367, 120, 50


lat1=0
lon1=0

lat2= 0
lon2= 0




  --- FUEL MANAGMENTCANVAS MENU -------------------------------------------------------------------------------------------------

fuelmanag_canvas     =   canvas_add(0,0,451,488)



 
                  
        function Fuelmcanvas_callback(fuelleft2,fuelright2,ftl2,
        fuelflow2,fusd,fuelcv,frem,ff2, fw2, altimeter,spd2,eficy2)
        
 
        
               
                      ---What this function is doing is creating a TABLE named <waypoints2C>
       
        
        ---Distance between two gps coordinates---
         
        


R = 6371e3
a1 = math.rad(lat1)			-- φ1
a2 = math.rad(lat2)			-- φ2
o1 = math.rad(lon1)			-- λ1
o2 = math.rad(lon2)			-- λ2
ax = math.rad(lat2-lat1)	-- Δφ
ay = math.rad(lon2-lon1)	-- Δλ

a = (math.sin(ax/2)*math.sin(ax/2)) + math.cos(a1) * math.cos(a2) * (math.sin(ay/2)*math.sin(ay/2))
c = 2 *  math.atan(math.sqrt(a)/math.sqrt(1-a))
d = R * c

print("Distance in nm   "..d/1852)
print("Distance in meters   "..d)
print("Distance in Kilometers   "..d/1000)
        
        distwayp = 0
        
        
     
        

function new_xpl_data_callback(latitude, longitude)


    lat1=latitude
    lon1=longitude
    print("Latitude: " .. lat1)
    print("Longitude: " .. lon1)
    nav_get_nearest("WAYPOINT",lat1, lon1, 1, data_callback)
    
 
	data_callback()

		

end

        ---Bearing between two coordinates---
y = math.sin(o2-o1) * math.cos(a2);
x = math.cos(a1)*math.sin(a2) - math.sin(a1)*math.cos(a2)*math.cos(o2-o1)
brng = math.atan(y, x)
deg_brng=math.deg(brng)


--------------------------------xxx

function xpl_dataref_subscribe_trottle(...)
    -- Local variables
    local args = {...}
    local user_callback = args[#args - 1]
    local delta_ms = table.remove(args, #args)
    local data = nil

    -- Replace the user callback with our own
    args[#args] = function (...)
        data = {...}
    end

    -- Do the actual subscribe
    xpl_dataref_subscribe(table.unpack(args))

    -- Start a timer that will fire the callback on requested interval
    timer_start(0, delta_ms, function()
        if data ~= nil then
            user_callback(table.unpack(data))
            data = nil
        end
xpl_dataref_subscribe_trottle("sim/flightmodel/position/latitude", "DOUBLE",
		              "sim/flightmodel/position/longitude", "DOUBLE",
		              "sim/cockpit2/engine/indicators/fuel_flow_kg_sec", "FLOAT[8]",new_xpl_data_callback, 1000)
    end)
end




-------------------------------xxx
        
        canvas_draw(fuelmanag_canvas, function()
        
         _rect(0,0,451,488)
         _fill_img("tela_fuelmanagment_short.png",0,0,451,488)
         
         
          
         

	fuelleft2 = var_cap(fuelleft2, 0, 238)
	fuelright2 = var_cap(fuelright2, 0, 238)
	ftl2 = var_cap(ftl2, 0, 474) 
	fuelflow2 = var_cap(fuelflow2[1] * 5.75462262 / 6.81 * 3600, 0, 130)
	fusd = var_cap(fusd, 0, 442)
	fuelcv = var_cap(442-fuelcv, 0, 442)
	ftrem = var_cap(frem, 0, 442)
	  
	

	_txt(string.format("%02.0f", fuelleft2),txt_Fuel_L_cyan2, 195, 96)
	_txt(string.format("%02.0f", fuelright2), txt_Fuel_R_cyan2,195, 120 )
	_txt(string.format("%02.0f", ftl2),txt_fuel_total_cyan2,195, 190 )
	_txt(string.format("%02.0f", fuelflow2),txt_Fuel_F_cyan2, 195,245 )
	_txt( string.format("%1.0f", fusd ), txt_Fused_trip2,195,254)
	_txt(string.format("%01.0f", fuelcv),txt_Fuel_SF,195,282 )
	_txt(string.format("%01.00f",frem),txt_Fuel_rem,195,310 ) 
	_txt(string.format("%01.00f",spd2),txt_speed_knots2,400,173)
	_txt(string.format("%1.01f", spd2/eficy2[1] / 1000),txt_eficy2,400,197)
        _txt(string.format("%01.00f", altimeter),txt_altimeter2, 400,228)
 
	
--- FUEL TO WAYPOINT CANVAS MENU SETUP	--- --- D to E CANVAS MENU SETUP

    local t_seconds = fif(ff2[1] > 0, fw2 / ff2[1], 0)
    
    _txt(string.format("%02.0f", math.floor(t_seconds / 3600)),txt_hss2, 155,355 )
    _txt(string.format(": %02.0f", math.floor(t_seconds / 60)%60),txt_mn2,195, 355 )
    
    local t_distance = fif(ff2[1] > 0, fw2 / ff2[1], 0)
    _txt(string.format("%02.0f", math.floor(t_distance / 39)), txt_dis_to_e2,196, 383 )
    
    
  local function data_callback(waypoints)
	if waypoints ~= nil then
	lat2= waypoints[1]["LATITUDE"]
	lon2= waypoints[1]["LONGITUDE"]
	  
    
    print("Direction/Bearing   "..deg_brng)
		
			print(" name:" .. waypoints[1]["ICAO"] .. " lon:" .. waypoints[1]["LONGITUDE"] .. " lat:" .. waypoints[1]["LATITUDE"])
			
			
			_txt(waypoints[1]["ICAO"],txt_waypointC, 380,110)
			_txt(string.format("%.2f",d/1852),txt_dtowpC,402,367)
			_txt(string.format("%.2f",deg_brng),txt_bearingC, 402,150)
			
			
			    --- COMBUSTIVEL NECESSÁRIO PARA ALCANÇAR O WAYPOINT
			_txt(string.format("%.2f", d/1852 * 0.1705, data_callback),txt_ftowpC,402,307)
			
 local ttw_seconds = 3600			
			    --- TEMPO NECESSÁRIO PARA ALCANÇAR O WAYPOINT
			_txt(string.format("%02.0f", d/1852 * math.floor( ttw_seconds / 530400, data_callback)),txt_ttowp_hsC, 365,337 )
			
			_txt(string.format(": %02.0f", d/1852 * math.floor( ttw_seconds /3600)%120,data_callback),txt_ttowp_mnC, 402,337)

end

end  
 	
        end) 
    
        end
xpl_dataref_subscribe( "sim/flightmodel/weight/m_fuel_total", "FLOAT",
                       "sim/cockpit2/engine/indicators/fuel_flow_kg_sec", "FLOAT[8]", function(fw3C,ff3hC)



end)




xpl_dataref_subscribe("sim/cockpit2/fuel/fuel_level_indicated_left", "FLOAT", 
                      "sim/cockpit2/fuel/fuel_level_indicated_right", "FLOAT",
                      "sim/flightmodel/weight/m_fuel_total", "FLOAT",
                      "sim/cockpit2/engine/indicators/fuel_flow_kg_sec", "FLOAT[8]",
                      "sim/cockpit2/fuel/fuel_totalizer_sum_kg", "FLOAT",
                       "sim/flightmodel/weight/m_fuel_total", "FLOAT",
                       "sim/flightmodel/weight/m_fuel_total", "FLOAT",
                       "sim/cockpit2/engine/indicators/fuel_flow_kg_sec", "FLOAT[8]",
                       "sim/flightmodel/weight/m_fuel_total", "FLOAT",
                       "sim/cockpit2/gauges/indicators/altitude_ft_pilot", "FLOAT",
                       "sim/cockpit/radios/gps_dme_speed_kts", "FLOAT",
                       "sim/cockpit2/engine/indicators/fuel_flow_kg_sec", "FLOAT[8]",
                       Fuelmcanvas_callback)
Attachments
image.png
image.png

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

Re: _txt dinamic with local function

#2 Post by Ralph »

I'm a bit confused. What's exactly your question?

User avatar
Marcnamara2021
Posts: 83
Joined: Wed Dec 01, 2021 6:00 am
Location: Sao Jose dos Campos, SP

Re: _txt dinamic with local function

#3 Post by Marcnamara2021 »

I can't get all _txt of this example, written for a canvas draw, to show the result, when dealing with data and calculations related to navigation.

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

Re: _txt dinamic with local function

#4 Post by Ralph »

That's still a very vague description.

User avatar
Marcnamara2021
Posts: 83
Joined: Wed Dec 01, 2021 6:00 am
Location: Sao Jose dos Campos, SP

Re: _txt dinamic with local function

#5 Post by Marcnamara2021 »

OK. Sorry, I'll try to explain better:

This is a rehearsal script for the last screen of the MVP50, here in this BLUE PAGE menu, where I work with _txt canvas texts.

From line 128 of the script to line 142 of all dynamic data in the _txt line is ok being displayed on the Blue Page.

However, from line 182 to line 194, the _txt is not being written to the screen.

Could you guide me? I've made a hundred attempts this month, without success.

Thank You
menu test sample.siff
(120.01 KiB) Downloaded 136 times

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

Re: _txt dinamic with local function

#6 Post by Keith Baxter »

Hi,


Ok I have downloaded you code and comment.

1) after downloading the .siff file I had quite a issue finding it because the siff file name and the instrument name were different. I nearly just did not bother taking a look to help. Please in future keep the names the same. A lot of us have 1000's of instruments and fishing through that lot is painful.

2) The script is very long and difficult to follow. It is better to shorten the code and only submit the code you are having issues with.

3) I see you are using a bunch of my code. That is good to see. However I get the impression you are applying it incorrectly. I will fix what I think you are trying to do. It is going to take a day or so to go through it.

$0 Next time try and tidy the code up a lot more so that it is easier to read. That would make it easier to follow and the issue can easily be addressed.
I started just tiding up the font variables. look how much nicer it looks and typing errors easily spotted. I found a few fonts that had a color typing error. Fixed those. that could have been the reason why some text was not displaying.

Look at the difference between what I have done and your code.

ice_screenshot_20220315-083419.png

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
Marcnamara2021
Posts: 83
Joined: Wed Dec 01, 2021 6:00 am
Location: Sao Jose dos Campos, SP

Re: _txt dinamic with local function

#7 Post by Marcnamara2021 »

Hi Keith.

I followed your instructions on organization and aesthetics and started a good fix at this point.

Next time I need some help, I'll do as it should be for better support effectiveness. Thanks for your patience and willingness.

Regards

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

Re: _txt dinamic with local function

#8 Post by Keith Baxter »

Marcnamara2021 wrote: Tue Mar 15, 2022 8:01 am Hi Keith.

I followed your instructions on organization and aesthetics and started a good fix at this point.

Next time I need some help, I'll do as it should be for better support effectiveness. Thanks for your patience and willingness.

Regards
Hi
No problems.
I have spent a bit of time on it this morning. There are a lot of things I am changing and making simpler to understand. I have a lot working already so you might get it back tomorrow if all gos well.


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

Re: _txt dinamic with local function

#9 Post by Keith Baxter »

Hay Marc, Not sure that is your name but from your user name i assume.

We have the first part of your blue page done. :mrgreen:

Yes the bit that you could not get to work will follow shortly. I have it working.

I changed a lot of things in the basic coding. I removed all the unnecessary stuff like the menus, unnecessary font variables etc. It just irritated me when testing and served no purpose for your issue.
Take a look at what the code looks like in notepad++

It is short and clean. easily understandable and well commented.
ice_screenshot_20220315-145432.png
Oh I changed the color of your "RED" text. It was hard to read. You can change it back should you want or find a more readable color than the red you had,something else might stand out on the blue background better.

Also the format of "TIME" and "DATE" strings is not best done with <string.format("%02.0f",.....) That function is annoying in that it does not return the correct value but a rounded value . In other words a minute value of 1.45 will return 1. A minute value of 1.55 will return 2. So your value is always rounded and the data displayed out by half a second.
But that for another discussion. I will show you how the correct values can be formatted accurately.

ice_screenshot_20220315-145808.png



I sure hope that the importance of having a neat coding style that is structured is demonstrated. 5 years down the line if changes need to be made then it is clear what the code is doing.


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
Marcnamara2021
Posts: 83
Joined: Wed Dec 01, 2021 6:00 am
Location: Sao Jose dos Campos, SP

Re: _txt dinamic with local function

#10 Post by Marcnamara2021 »

Hi Keith.
I look forward to gaining the knowledge necessary to finish this page gracefully. I believe that with this interaction I will be much more careful in setting up the script's structure.

Thanks.

Post Reply