1 // transponder.cxx -- class to impliment a transponder
3 // Written by Roy Vegard Ovesen, started September 2004.
5 // Copyright (C) 2004 Roy Vegard Ovesen - rvovesen@tiscali.no
6 // Copyright (C) 2013 Clement de l'Hamaide - clemaez@hotmail.fr
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 // Example invocation, in the instrumentation.xml file:
24 // <name>encoder</name>
26 // <mode>0</mode> // Mode A = 0, Mode C = 1, Mode S = 2
33 #include "transponder.hxx"
35 #include <simgear/compiler.h>
36 #include <simgear/sg_inlines.h>
37 #include <simgear/debug/logstream.hxx>
39 #include <Main/fg_props.hxx>
45 const double IDENT_TIMEOUT = 18.0; // 18 seconds
46 const int INVALID_ALTITUDE = -9999;
47 const int INVALID_ID = -9999;
49 Transponder::Transponder(SGPropertyNode *node)
51 _name(node->getStringValue("name", "transponder")),
52 _num(node->getIntValue("number", 0)),
53 _mode((Mode) node->getIntValue("mode", 1)),
56 _requiredBusVolts = node->getDoubleValue("bus-volts", 8.0);
57 _altitudeSourcePath = node->getStringValue("encoder-path", "/instrumentation/altimeter");
58 _kt70Compat = node->getBoolValue("kt70-compatability", false);
62 Transponder::~Transponder()
67 void Transponder::init()
69 SGPropertyNode *node = fgGetNode("/instrumentation/" + _name, _num, true );
72 _busPower_node = fgGetNode("/systems/electrical/outputs/transponder", true);
73 _pressureAltitude_node = fgGetNode(_altitudeSourcePath, true);
75 SGPropertyNode *in_node = node->getChild("inputs", 0, true);
76 for (int i=0; i<4;++i) {
77 _digit_node[i] = in_node->getChild("digit", i, true);
78 _digit_node[i]->addChangeListener(this);
81 _knob_node = in_node->getChild("knob-mode", 0, true);
82 if (!_knob_node->hasValue()) {
84 // default to, if aircraft wants to start dark, it can do this
86 _knob_node->setIntValue(_knob);
88 _knob = static_cast<KnobPosition>(_knob_node->getIntValue());
91 _knob_node->addChangeListener(this);
93 _mode_node = in_node->getChild("mode", 0, true);
94 _mode_node->setIntValue(_mode);
95 _mode_node->addChangeListener(this);
97 _identBtn_node = in_node->getChild("ident-btn", 0, true);
98 _identBtn_node->setBoolValue(false);
99 _identBtn_node->addChangeListener(this);
101 _serviceable_node = node->getChild("serviceable", 0, true);
102 _serviceable_node->setBoolValue(true);
104 _idCode_node = node->getChild("id-code", 0, true);
105 _idCode_node->addChangeListener(this);
106 // set default, but don't overwrite value from preferences.xml or -set.xml
107 if (!_idCode_node->hasValue()) {
108 _idCode_node->setIntValue(1200);
112 _altitude_node = node->getChild("altitude", 0, true);
113 _altitudeValid_node = node->getChild("altitude-valid", 0, true);
114 _ident_node = node->getChild("ident", 0, true);
115 _transmittedId_node = node->getChild("transmitted-id", 0, true);
118 // alias the properties through
119 SGPropertyNode_ptr output = node->getChild("outputs", 0, true);
120 output->getChild("flight-level", 0, true)->alias(_altitude_node);
121 output->getChild("id-code", 0, true)->alias(_idCode_node);
122 in_node->getChild("func-knob", 0, true)->alias(_knob_node);
126 void Transponder::bind()
129 SGPropertyNode *node = fgGetNode("/instrumentation/" + _name, _num, true );
130 _tiedProperties.setRoot(node);
132 _tiedProperties.Tie("annunciators/fl", this,
133 &Transponder::getFLAnnunciator );
134 _tiedProperties.Tie("annunciators/alt", this,
135 &Transponder::getAltAnnunciator );
136 _tiedProperties.Tie("annunciators/gnd", this,
137 &Transponder::getGroundAnnuciator );
138 _tiedProperties.Tie("annunciators/on", this,
139 &Transponder::getOnAnnunciator );
140 _tiedProperties.Tie("annunciators/sby", this,
141 &Transponder::getStandbyAnnunciator );
142 _tiedProperties.Tie("annunciators/reply", this,
143 &Transponder::getReplyAnnunciator );
144 } // of kt70 backwards compatability
147 void Transponder::unbind()
149 _tiedProperties.Untie();
153 void Transponder::update(double dt)
155 if (has_power() && _serviceable_node->getBoolValue())
157 // Mode C & S send also altitude
158 Mode effectiveMode = (_knob == KNOB_ALT) ? _mode : MODE_A;
159 SGPropertyNode* altitudeSource = NULL;
161 switch (effectiveMode) {
163 altitudeSource = _pressureAltitude_node->getChild("mode-c-alt-ft");
166 altitudeSource = _pressureAltitude_node->getChild("mode-s-alt-ft");
172 int alt = INVALID_ALTITUDE;
173 if (effectiveMode != MODE_A) {
174 if (altitudeSource) {
175 alt = altitudeSource->getIntValue();
177 // warn if altitude input is misconfigured
178 SG_LOG(SG_INSTR, SG_INFO, "transponder altitude input for mode " << _mode << " is missing");
182 _altitude_node->setIntValue(alt);
183 _altitudeValid_node->setBoolValue(alt != INVALID_ALTITUDE);
187 if ( _identTime > IDENT_TIMEOUT ) {
189 _ident_node->setBoolValue(false);
194 if (_knob > KNOB_ON) {
195 _transmittedId_node->setIntValue(_idCode_node->getIntValue());
197 _transmittedId_node->setIntValue(INVALID_ID);
202 _altitude_node->setIntValue(INVALID_ALTITUDE);
203 _altitudeValid_node->setBoolValue(false);
204 _ident_node->setBoolValue(false);
205 _transmittedId_node->setIntValue(INVALID_ID);
209 static int powersOf10[4] = {1, 10, 100, 1000};
211 static int extractCodeDigit(int code, int index)
213 return (code / powersOf10[index]) % 10;
216 static int modifyCodeDigit(int code, int index, int digitValue)
218 assert(digitValue >= 0 && digitValue < 8);
219 int p = powersOf10[index];
220 int codeWithoutDigit = code - (extractCodeDigit(code, index) * p);
221 return codeWithoutDigit + (digitValue * p);
224 void Transponder::valueChanged(SGPropertyNode *prop)
226 // Ident button pressed
227 if ((prop == _identBtn_node) && prop->getBoolValue()) {
229 _ident_node->setBoolValue(true);
234 if (prop == _mode_node) {
235 _mode = static_cast<Mode>(prop->getIntValue());
239 if (prop == _knob_node) {
240 _knob = static_cast<KnobPosition>(prop->getIntValue());
244 if (_listener_active)
249 if (prop == _idCode_node) {
250 // keep the digits in-sync
251 for (int i=0; i<4; ++i) {
252 _digit_node[i]->setIntValue(extractCodeDigit(prop->getIntValue(), i));
256 int index = prop->getIndex();
257 int digitValue = prop->getIntValue();
258 SG_CLAMP_RANGE<int>(digitValue, 0, 7);
259 _idCode_node->setIntValue(modifyCodeDigit(_idCode_node->getIntValue(), index, digitValue));
260 prop->setIntValue(digitValue);
266 bool Transponder::has_power() const
268 return (_knob_node->getIntValue() > KNOB_STANDBY) && (_busPower_node->getDoubleValue() > _requiredBusVolts);
271 bool Transponder::getFLAnnunciator() const
273 return (_knob == KNOB_ALT) || (_knob == KNOB_GROUND) || (_knob == KNOB_TEST);
276 bool Transponder::getAltAnnunciator() const
278 return (_knob == KNOB_ALT) || (_knob == KNOB_TEST);
281 bool Transponder::getGroundAnnuciator() const
283 return (_knob == KNOB_GROUND) || (_knob == KNOB_TEST);
286 bool Transponder::getOnAnnunciator() const
288 return (_knob == KNOB_ON) || (_knob == KNOB_TEST);
291 bool Transponder::getStandbyAnnunciator() const
293 return (_knob == KNOB_STANDBY) || (_knob == KNOB_TEST);
296 bool Transponder::getReplyAnnunciator() const
298 return _identMode || (_knob == KNOB_TEST);