swift
bcdconversions.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
7 #include "misc/pq/units.h"
8 
9 using namespace swift::misc::physical_quantities;
10 using namespace swift::misc::aviation;
11 
12 namespace swift::misc::simulation::fscommon
13 {
14  quint32 CBcdConversions::comFrequencyToBcdHz(const swift::misc::physical_quantities::CFrequency &comFrequency)
15  {
16  // FSX documentation is wrong, we need to use kHz + 2 digits, not Hz
17  const double f = comFrequency.valueRounded(CFrequencyUnit::kHz(), 0) / 10;
18  const quint32 fInt = dec2Bcd(static_cast<quint32>(f));
19  return fInt;
20  }
21 
22  quint32 CBcdConversions::transponderCodeToBcd(const swift::misc::aviation::CTransponder &transponder)
23  {
24  // FSX documentation is wrong, we need to use kHz + 2 digits, not Hz
25  quint32 t = static_cast<quint32>(transponder.getTransponderCode());
26  t = dec2Bcd(t);
27  return t;
28  }
29 
30  quint32 CBcdConversions::hornerScheme(quint32 num, quint32 divider, quint32 factor)
31  {
32  quint32 remainder = 0, quotient = 0, result = 0;
33  remainder = num % divider;
34  quotient = num / divider;
35  if (!(quotient == 0 && remainder == 0))
36  {
37  result += hornerScheme(quotient, divider, factor) * factor + remainder;
38  }
39  return result;
40  }
41 } // namespace swift::misc::simulation::fscommon
int getTransponderCode() const
Transponder code.
Definition: transponder.h:107
static CFrequencyUnit kHz()
Kilohertz.
Definition: units.h:379
double valueRounded(MU unit, int digits=-1) const
Rounded value in given unit.