# Useful Tips, Code Samples & Tricks

## Recommended Posts

Here is a collection of useful Tips, Code Samples and Tricks for gauge development...

Bill Leaming http://smileys.sur-la-toile.com/repository/Combat/0054.gif

Gauge Programming - 3d Modeling Military Visualizations

Flightsim.com Panels & Gauges Forum Moderator

Flightsim Rig: Intel Core i7-2600K - 8GB DDR3 1333 - EVGA GTX770 4GB - Win7 64bit Home Premium

Development Rig1: Intel Core i7-3770k - 16GB DDR3 - Dual Radeon HD7770 SLI 1GB - Win7 64bit Professional

Development Rig2: Intel Core i7-860 - 8GB DDR3 Corsair - GeForce GTS240 1GB - Win7 64bit Home Premium

NOTE: Unless explicitly stated in the post, everything written by my hand is MY opinion. I do NOT speak for any company, real or imagined...

##### Share on other sites

Cut-and-Paste the following code to your gauges.h file, or save it as BCD2DEC.h, place it in your \inc folder and then #include it somewhere in your gauge project.

/*--------------------------------------*/

#include

#define Bcd2Dec(BcdNum) HornerScheme(BcdNum,0x10,10)

#define Dec2Bcd(DecNum) HornerScheme(DecNum,10,0x10)

UINT32 HornerScheme(UINT32 Num,UINT32 Divider,UINT32 Factor)

{

UINT32 Remainder=0,Quotient=0,Result=0;

Remainder=Num%Divider;

Quotient=Num/Divider;

if(!(Quotient==0&&Remainder==0))

Result+=HornerScheme(Quotient,Divider,Factor)*Factor+Remainder;

return Result;

}

/*--------------------------------------*/

Usage is quite simple...

To convert the FS COM or NAV variable to Decimal:

COM1freq = Bcd2Dec(COM_FREQUENCYvar.var_value.n);

COM1freq = COM1freq/100 + 100;

To convert a decimal frequency to BCD:

COM1_bcd = (COM_dec - 100) * 1000;

COM1_bcd = Dec2Bcd( COM1_dec);

Bill Leaming http://smileys.sur-la-toile.com/repository/Combat/0054.gif

Gauge Programming - 3d Modeling Military Visualizations

Flightsim.com Panels & Gauges Forum Moderator

Flightsim Rig: Intel Core i7-2600K - 8GB DDR3 1333 - EVGA GTX770 4GB - Win7 64bit Home Premium

Development Rig1: Intel Core i7-3770k - 16GB DDR3 - Dual Radeon HD7770 SLI 1GB - Win7 64bit Professional

Development Rig2: Intel Core i7-860 - 8GB DDR3 Corsair - GeForce GTS240 1GB - Win7 64bit Home Premium

NOTE: Unless explicitly stated in the post, everything written by my hand is MY opinion. I do NOT speak for any company, real or imagined...

##### Share on other sites

Reposted here from http://blogs.technet.com/engauged/default.aspx

Displaying Text in Gauges

Formatting numbers

To display the value of a sim variable in text, use the following syntax:

%(VARIABLE NAME)%!XY!

Where:

VARIABLE NAME is any of the variables listed in parameters.doc. Be sure to include parentheses around the variable name in addition to the parentheses included with the %â€™s. (see example below) Note that there must not be a space between the â€˜%â€™ and the â€˜!â€™.

Y (required, case-sensitive) is the formatting of the variable, where:

* s = string.

* d = number (integer). If the number is not an integer, it is rounded to the nearest integer. Note that rounding, not truncation occurs.

* f = number (floating point)

X is the minimum number of digits to display (optional)

If Y=â€™dâ€™, the following rules apply:

* If X is preceded by the digit â€˜0â€™, then leading zeros are added if necessary.

* If X is preceded by â€˜-â€˜, text is left-aligned

* If X is preceded by â€˜+â€™, a â€˜+â€™ is indicated in front of the number when the number is greater than 0 (a â€˜-â€˜ is always used to indicate numbers less than 0)

* If X is preceded by â€˜ â€˜ (space), leading spaces are added if necessary.

If Y=â€™fâ€™, the following rule applies:

* If a decimal point is used in the number X, the digit after the decimal point specifies the number of digits to display after the decimal point. See examples below,

Script Result

%( 12.34 )%!4.3f! 12.340

%( 12.34 )%!04.3f! 12.340 (Leading â€˜0â€™s not added to floating point numbers)

%( 12345.6789 )%!4.3f! 12345.679 (Number before decimal point does not limit the number of digits displayed before decimal point)

%( 34.56 )%!+d! +35 (Note that rounding, not truncation, occurs)

%(234)%!5d! 234 (note 2 leading spaces)

%( â€˜fooâ€™ )%!5s! foo (note 2 leading spaces)

%( 234 )%!3s! 234

Using Sim variables and expressions in text

For example:

%((A:Indicated Altitude, feet))%!05f!

Any of the operators described above (Boolean, string, arithmetic, etc.) may be used within the %() to create the intended expression. In other words,

%(EXPRESSION)%!XY!

Is valid, where EXPRESSION is any combination of valid sim variables and operators.

For example, the following string will display the current altitude in hundreds of feet or hundreds of meters, depending on the current international settings,

%( (P:Units of measure, enum) 2 ==

if{ (A:Indicated Altitude, meters) }

els{ (A:Indicated Altitude, feet) }

100 / )%!5d!

Keep in mind that the element must be a child of a element, for example:

X="80" Y="20"

Bright="Yes"

Length="10"

Font="Quartz"

Color="#E0E0E0" >

String>05d: %((P:Local time,hours) flr)%!02d!

Bill Leaming http://smileys.sur-la-toile.com/repository/Combat/0054.gif

Gauge Programming - 3d Modeling Military Visualizations

Flightsim.com Panels & Gauges Forum Moderator

Flightsim Rig: Intel Core i7-2600K - 8GB DDR3 1333 - EVGA GTX770 4GB - Win7 64bit Home Premium

Development Rig1: Intel Core i7-3770k - 16GB DDR3 - Dual Radeon HD7770 SLI 1GB - Win7 64bit Professional

Development Rig2: Intel Core i7-860 - 8GB DDR3 Corsair - GeForce GTS240 1GB - Win7 64bit Home Premium

NOTE: Unless explicitly stated in the post, everything written by my hand is MY opinion. I do NOT speak for any company, real or imagined...

##### Share on other sites

EDITOR'S NOTE: Jean-Luc from Reality-XP has kindly offered this alternate method for BCD-to-DEC, DEC-to-BCD, and BCD-to-TXT conversions

Here is a simpler way to convert an FS BCD to a

string (the same can easily be used from FS BCD -> number)

void RXP_FREQBCD2TXT(char * str, UINT32 value)

{

str[0]='1';

str[1]=((value >> 12) & 0xF) + 0x30;

str[2]=((value >> 8) & 0xF) + 0x30;

str[3]='.';

str[4]=((value >> 4) & 0xF) + 0x30;

str[5]=((value) & 0xF) + 0x30;

str[6]=0;

}

Here is an example with INT instead CHAR * (and I'm sure this

can be further optimized)

UINT32 RXP_6DEC2FSBCD(UINT32 val)

{

UINT32 bcd;

val /=10;

bcd = (val % 10);

val /= 10;

bcd |= ((val % 10)

val /= 10;

bcd |= ((val % 10)

val /= 10;

bcd |= ((val % 10)

return(bcd);

}

UINT32 RXP_FSBCD26DEC(UINT32 bcd)

{

UINT32 decval;

decval = 100000;

decval +=((bcd >> 12) & 0xF) * 10000;

decval +=((bcd >> 8) & 0xF) * 1000;

decval +=((bcd >> 4) & 0xF) * 100;

decval +=((bcd) & 0xF) * 10;

if (((bcd & 0xF) == 2) || ((bcd & 0xF) == 7)) decval

+= 5;

return(decval);

}

Bill Leaming http://smileys.sur-la-toile.com/repository/Combat/0054.gif

Gauge Programming - 3d Modeling Military Visualizations

Flightsim.com Panels & Gauges Forum Moderator

Flightsim Rig: Intel Core i7-2600K - 8GB DDR3 1333 - EVGA GTX770 4GB - Win7 64bit Home Premium

Development Rig1: Intel Core i7-3770k - 16GB DDR3 - Dual Radeon HD7770 SLI 1GB - Win7 64bit Professional

Development Rig2: Intel Core i7-860 - 8GB DDR3 Corsair - GeForce GTS240 1GB - Win7 64bit Home Premium

NOTE: Unless explicitly stated in the post, everything written by my hand is MY opinion. I do NOT speak for any company, real or imagined...

##### Share on other sites

/!-- COM or NAV Frequency Decimal to BCD --/

123.45 (*any frequency here*)

100 * 10000 % int (* 123.45 to 2345 *)

d 10 % r 10 / int (* stack: 234 -> 5 *)

d 10 % r 10 / int (* stack: 23 -> 4 -> 5 *)

d 10 % r 10 / int (* stack: 2 -> 3 -> 4 -> 5 *)

16 * + (* stack: 2*16+3 -> 4 -> 5 *)

16 * + (* stack: (2*16+3)*16+4 -> 5 *)

16 * + (* stack: ((2*16+3)*16+4)*16+5 *)

For BCD to Decimal, no 'tricks' are needed, since the 'units'

parameter of the (A:variable,units) will do the conversion

for you... ;)

/!-- ADF Frequency Decimal to Octal --/

1234.5 (*or any frequency here*)

d (* duplicate for later use *)

10 * 10000 % int (* 1234.5 to 2345 *)

d 10 % r 10 / int (* stack: 234 -> 5 *)

d 10 % r 10 / int (* stack: 23 -> 4 -> 5 *)

d 10 % r 10 / int (* stack: 2 -> 3 -> 4 -> 5 *)

16 * + (* stack: 2*16+3 -> 4 -> 5 *)

16 * + (* stack: (2*16+3)*16+4 -> 5 *)

16 * + (* stack: ((2*16+3)*16+4)*16+5 *)

r (* get the original frequency back*)

1000

higher need ADF_HIGHRANGE_SET *)

if{ (>K:ADF_LOWRANGE_SET) } els{

(Editor's Note: Thanks to Arne Bartels for the cogent examples!)

Bill Leaming http://smileys.sur-la-toile.com/repository/Combat/0054.gif

Gauge Programming - 3d Modeling Military Visualizations

Flightsim.com Panels & Gauges Forum Moderator

Flightsim Rig: Intel Core i7-2600K - 8GB DDR3 1333 - EVGA GTX770 4GB - Win7 64bit Home Premium

Development Rig1: Intel Core i7-3770k - 16GB DDR3 - Dual Radeon HD7770 SLI 1GB - Win7 64bit Professional

Development Rig2: Intel Core i7-860 - 8GB DDR3 Corsair - GeForce GTS240 1GB - Win7 64bit Home Premium

NOTE: Unless explicitly stated in the post, everything written by my hand is MY opinion. I do NOT speak for any company, real or imagined...

##### Share on other sites

• 2 weeks later...

The following is applicable to both C and XML programming... ;)

Question:

>I understand modulus to a degree, like

>123 10 % strips out the 3 and places it on the stack.

>

>what does, say, 2 % do?

The "result" is only obvious whenever you use 10 as a divisor, because it effectively bitshifts the decimal, and the remainder is simply the decimal portion! ;)

With other numbers as a divisor however, the operation is more opaque... :9

The modulus, or remainder, operator divides number1 by number2 and returns only the remainder as result. The sign of result is the same as the sign of number1. The value of result is between 0 and the absolute value of number2.

For example, in the following expression, A (which is result) equals 5.6.

A = 19 6.7 %

This is equivalent to:

19 / 6.7 = 2.8358208

We throw away the fraction leaving 2 as the result

Now we have:

A = 19 - (6.7 * 2)

Therefore A = 5.6

The above is an example where the result is certainly not obvious! ;)

The "%" (or "mod") operator in computer languages is simply the remainder. For

example,

17 3 % = 2

because

17 / 3 = 5 rem 2

which in turn means

17 = 3 * 5 + 2

There are some tricky issues when negative numbers are used, but that

shouldn't ordinarily be necessary.

In math (number theory), the term is used a little differently. The

"modulus" is actually not the remainder, but the number you are

dividing by; and "mod" is not an operator, but a label telling "in

what sense two quantities are considered congruent, or equal." For

example, we would say

17 = 11 (mod 3)

(read as "17 is congruent to 11, modulo 3"), meaning that 17 and 11

both leave the SAME remainder when divided by 3. You probably won't

see this usage if you are only reading about programming, but it's

worth being aware of if you look deeper into the math behind it.

The expression a = b (% n) means that n is a divisor of a - b. This

sentence is read, "a is congruent to b modulo n." It is something

like a remainder, because if you subtract a remainder from the

dividend, the divisor will go into the result evenly.

Examples: 100 = 86 (% 7), because 100 - 86 = 14 has 7 as a divisor.

On the other hand, if you divide 100 by 7, the quotient is 14 and

remainder is 2, and 100 = 2 (% 7), too.

Now, back to your original question, what would 123 2 % be?

A = 123 2 %

123 / 2 = 61.5, therefore:

A = 123 - (61 * 2)

A = 1

:9

Bill Leaming http://smileys.sur-la-toile.com/repository/Combat/0054.gif

Gauge Programming - 3d Modeling Military Visualizations

Flightsim.com Panels & Gauges Forum Moderator

Flightsim Rig: Intel Core i7-2600K - 8GB DDR3 1333 - EVGA GTX770 4GB - Win7 64bit Home Premium

Development Rig1: Intel Core i7-3770k - 16GB DDR3 - Dual Radeon HD7770 SLI 1GB - Win7 64bit Professional

Development Rig2: Intel Core i7-860 - 8GB DDR3 Corsair - GeForce GTS240 1GB - Win7 64bit Home Premium

NOTE: Unless explicitly stated in the post, everything written by my hand is MY opinion. I do NOT speak for any company, real or imagined...

##### Share on other sites

This topic is now closed to further replies.
• ### Recently Browsing   0 members

• No registered users viewing this page.
×
×
• Create New...