Jump to content
Sign in to follow this  
Guest VAPilot

Altimeter String

Recommended Posts

Guest VAPilot

I am doing a gauge which only uses the basics while reading about the complex stuff, and all was going well untill I got to the altitude string. The real gauge uses two big numbers for the Tens of Thousands and Thousands, and three small number for the Hundreds, Tens, and the Units, so it has to be done using two string. I have the two big numbers working correctly, but for the life of me I can not get the three little number to only display the hundred, tens and the units above 1000 feet. The String I have shows the hundreds, tens and units when below 1000 feet, but as soon as you are above that, the string then shows thousands as well. After many hours of trying to get this to work, I am hoping there is someone who can point out what I am doing wrong, as it is realy doing my head in.The codeFLOAT64 altitude_feet;FLOAT64 FSAPI altitudestringhundred_cb (PELEMENT_STRING pelement ){ altitude_feet = current_altitude.var_value.n; altitude_feet = altitude_feet / 100; sprintf (pelement->string,"%3.0f", altitude_feet); return altitude_feet ;}//10 Altitude Hundreds StringMAKE_STRING( altitudehundredstring, NULL, d10_fail, IMAGE_USE_ERASE | IMAGE_USE_TRANSPARENCY | BIT2, 0, 420,81, 76,39, 3, // what is the point of this f@#!ing line, if it displays as many numbers as it likes. ALT_FROM_BAROMETRIC_PRESSURE, MODULE_VAR_NONE, MODULE_VAR_NONE, RGB (13,13,13), RGB (0,0,0), RGB (13,13,13), GLASS, NORMAL, GAUGE_CHARSET, 0, DT_CENTER | DT_VCENTER, NULL, altitudestringhundred_cb )

Share this post


Link to post
Share on other sites

Although the code below is for a different variable, the technique is the same. Essentially you need to calculate the "whole thousands part" and then subtract it from the full variable to obtain the "whole hundreds part" of the number...int altsel_thou = 0;int altsel_hund = 0;lookup_var(&AUTOPILOT_ALTITUDE_LOCK_VARvar);selected_altitude = AUTOPILOT_ALTITUDE_LOCK_VARvar.var_value.n;altsel_thou = selected_altitude / 1000 ;altsel_hund = selected_altitude - ( altsel_thou * 1000 ) ;BTW, when 'printing' the hundreds, if the value is 000, then you will need to substitute as in the following, otherwise you'll end up with "..0" instead of "000" (where the periods indicate blank space) :)if ( altsel_hund != 0 ) { sprintf(pelement->string,"%3.0f",altsel_hund); } else { wsprintf(pelement->string,"%3s","000"); }As for your semi-profane question regarding the NUMCHARS entry, I refer you to the definition in the SDK:"Specifies the number of characters to use in the string. The optimal font size is calculated using the information passed to the macro."This implies that NUMCHARS does not limit the number of characters to be displayed, but simply provides a baseline for auto-calculating the scaling for font size.In this regard, it behaves no differently than the XML equivalent entry... :)


Fr. Bill    

AOPA Member: 07141481 AARP Member: 3209010556


     Avsim Board of Directors | Avsim Forums Moderator

Share this post


Link to post
Share on other sites

There's a better way to do this:if ( altsel_hund != 0 ){ sprintf(pelement->string,"%3.0f",altsel_hund); }else{ wsprintf(pelement->string,"%3s","000"); }It is:sprintf(pelement->string,"%03.0f,altsel_hund); It will always left-fill with zeros.


Ed Wilson

Mindstar Aviation
My Playland - I69

Share this post


Link to post
Share on other sites

Dang! I always forget that 'leading zero' syntax...


Fr. Bill    

AOPA Member: 07141481 AARP Member: 3209010556


     Avsim Board of Directors | Avsim Forums Moderator

Share this post


Link to post
Share on other sites
Guest VAPilot

Thanks Bill and Ed, will give it a try a.s.a.pregarding the semi-profane question, it was something I put there, when it failed to work for the 75th time, I just copied the code, and forgot to edit that out :-doh Thanks:-beerchug

Share this post


Link to post
Share on other sites
Guest VAPilot

Tried it and all I get is 000.In the Header:MODULE_VAR current_altitude = {ALT_FROM_BAROMETRIC_PRESSURE};In the PANEL_SERVICE_PRE_UPDATElookup_var(&current_altitude);altitude_feet = (current_altitude.var_value.n);Then add this to the main gauge code:int altitude_thousands = 0;int altitude_hundreds =0;FLOAT64 FSAPI altitudestringhundred_cb (PELEMENT_STRING pelement ){ altitude_feet = current_altitude.var_value.n; altitude_thousands = altitude_feet / 1000 ; altitude_hundreds = altitude_feet - ( altitude_thousands * 1000 ) ; sprintf (pelement->string,"%03.0f", altitude_hundreds); return altitude_feet;}:-zhelp

Share this post


Link to post
Share on other sites

Because you declared them as int... a whole number with no fractional values. They need to be FLOAT64.You really, really need to learn the different variable types and what values they can support.


Ed Wilson

Mindstar Aviation
My Playland - I69

Share this post


Link to post
Share on other sites
Guest VAPilot

>Because you declared them as int... a whole number with no>fractional values. They need to be FLOAT64.>>You really, really need to learn the different variable types>and what values they can support.Caught that after I had posted, but to late to edit it :( Changed to FLOAT64 and still got 000, I think it is something to do with the maths in this altitude_hundreds = altitude_feet - (altitude_thousands *1000); but I am at work now so will have to try it later.

Share this post


Link to post
Share on other sites

Russellif altitude_thousands = altitude_feet / 1000 then altitude_thousands * 1000 = altitude_feetso altitude_hundreds = altitude_feet - altitude_feet = 0 The simplest way:altitude_hundreds = altitude_feet % 1000% is modulus operator. Tom

Share this post


Link to post
Share on other sites

I'm surprised no one noticed this:>In the PANEL_SERVICE_PRE_UPDATE>lookup_var(&current_altitude);>altitude_feet = (current_altitude.var_value.n);>>Then add this to the main gauge code:>>int altitude_thousands = 0;>int altitude_hundreds = 0;>>FLOAT64 FSAPI altitudestringhundred_cb (PELEMENT_STRING>pelement )>{ > altitude_feet = current_altitude.var_value.n;Why on earth are you getting "altitude_feet" in a lookup and then repeating it in the STRING callback?Also, you are using a "float" formatter in your string, for an "int" value: sprintf (pelement->string,"%03.0f",altitude_hundreds);should be: sprintf (pelement->string,"%03d",altitude_hundreds);or, cast the int type to float sprintf (pelement->string,"%03.0f",{float)altitude_hundreds);


Fr. Bill    

AOPA Member: 07141481 AARP Member: 3209010556


     Avsim Board of Directors | Avsim Forums Moderator

Share this post


Link to post
Share on other sites

In the Header:MODULE_VAR current_altitude = {ALT_FROM_BAROMETRIC_PRESSURE};float current_altitude = 0;int altitude_thousands = 0;int altitude_hundreds = 0;In the PANEL_SERVICE_PRE_UPDATElookup_var(&current_altitude);altitude_feet = (current_altitude.var_value.n);FLOAT64 FSAPI altitudestringhundred_cb (PELEMENT_STRINGpelement ){ altitude_thousands = altitude_feet / 1000 ; altitude_hundreds = altitude_feet - ( altitude_thousands *1000 ) ; sprintf (pelement->string,"%03.0f",(float)altitude_hundreds); return NULL;}The logic is sound...altitude_feet = 32500altitude_thousands = 32500 / 1000 = 32 (int type drops the decimal)altitude_hundreds = 32500 - ( 32 * 1000 ) = 500-----------------Are there other ways to skin this cat? Of course, but at least I know this code works, since I've been using it for years... ;)


Fr. Bill    

AOPA Member: 07141481 AARP Member: 3209010556


     Avsim Board of Directors | Avsim Forums Moderator

Share this post


Link to post
Share on other sites
Guest VAPilot

Got it :-) The problem I was having was due to the FLOAT64. I will try to explain. For the first string (The Big Numbers) that display the Tens of Thousands and Thousands I was looking up altitude_feet and then declaring FLOAT64 altitude_feet; I was then using this in a string callback like this altitude_feet = altitude_feet / 1000; this displayed correctly, then when I was trying to get the Hundreds, Tens and the units to show in the smaller string, I was using the same FLOAT64 altitude_feet; and then trying to keep it as a FLOAT64 which was not working. So I changed the FLOAT64 altitude_feet; to int altitude_feet; altered my big number string, and then used the code Bill suggested in the callback for the Hundreds, Tens and units and it worked. Hopefully you can understand this, just in case you can not I will post the code;int altitude_feet; // was FLOAT64 altitude_feet;int altitude_thousands; //was FLOAT64int altitude_hundreds; //was FLOAT64FLOAT64 FSAPI altitudestringhundreds_cb (PELEMENT_STRING pelement ){ altitude_thousands = altitude_feet / 1000; altitude_hundreds = altitude_feet - ( altitude_thousands * 1000 ); sprintf (pelement->string,"%03d", altitude_hundreds); return altitude_feet ;}FLOAT64 FSAPI altitudestringthousand_cb (PELEMENT_STRING pelement ){ altitude_thousands = altitude_feet / 1000; if( altitude_feet > GAUGE_STRING_MAX_HEIGHT) altitude_feet = GAUGE_STRING_MAX_HEIGHT; if( altitude_feet < GAUGE_STRING_MIN_HEIGHT) altitude_feet = GAUGE_STRING_MIN_HEIGHT; sprintf (pelement->string,"%02d", altitude_thousands); return altitude_feet ;} Notice Bill I am not looking up altitude_feet in the callback anymore, to be honest I thought it had to be put in the callback, learn something new every day :D. Thanks

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Tom Allensworth,
    Founder of AVSIM Online


  • Flight Simulation's Premier Resource!

    AVSIM is a free service to the flight simulation community. AVSIM is staffed completely by volunteers and all funds donated to AVSIM go directly back to supporting the community. Your donation here helps to pay our bandwidth costs, emergency funding, and other general costs that crop up from time to time. Thank you for your support!

    Click here for more information and to see all donations year to date.
×
×
  • Create New...