XML: Decimal To BCD Macro - Set Nav/Com Freq

From FSDeveloper Wiki
Jump to: navigation, search

XML:Decimal to BCD Macro

Decimal to BCD Macro:

A very simple but useful @macro to convert any decimal frequency to BCD format to allow direct SETting of COM and NAV frequencies in FS:

<Macro Name="Dec2Bcd">
  @1 100.001 * 10000 % int
  d 10 % r 10 / int
  d 10 % r 10 / int
  d 10 % r 10 / int
  16 * +
  16 * +
  16 * +
</Macro>

Usage is then as simple as passing the decimal frequency to the @macro the issuing the appropriate _SET key event:

<Click>
    @Dec2Bcd(112.30) (>K:NAV2_RADIO_SET)
</Click>

You could also use a custom L:var to pass a "tuned" frequency:

<Click>
    @Dec2Bcd((L:Nav2Freq,enum)) (>K:NAV2_RADIO_SET)
</Click>

where L:Nav2Freq,enum is in MHz


Alternatively, let FS handle the Units conversion:

Suppose you want to tune COM2 to 128.70 MHz, then:

128.7  0.00001 + (>L:COM2_Freq, Mhz)
(L:COM2_Freq, Frequency BCD16) (>K:COM2_RADIO_SET)

Note the use of the correction factor 0.00001 which is added to mitigate rounding errors that can occur. It is discussed in further detail by Tom Aguilo in this FSDeveloper thread.

The same issue is mitigated in the macro, above, in its first line. Note that it uses 100.001, not 100. This is described by Roman Stoviak in this FSDeveloper thread.