]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/transponder.cxx
- cleanup of the day (more finegrained change history in my local cvs)
[flightgear.git] / src / Instrumentation / transponder.cxx
1 // transponder.cxx -- class to impliment a transponder
2 //
3 // Written by Roy Vegard Ovesen, started September 2004.
4 //
5 // Copyright (C) 2004  Roy Vegard Ovesen - rvovesen@tiscali.no
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include "transponder.hxx"
26
27 Transponder::Transponder(SGPropertyNode *node)
28     :
29     name("transponder"),
30     num(0),
31     encoder("/instrumentation/encoder")
32 {
33     int i;
34     for ( i = 0; i < node->nChildren(); ++i ) {
35         SGPropertyNode *child = node->getChild(i);
36         string cname = child->getName();
37         string cval = child->getStringValue();
38         if ( cname == "name" ) {
39             name = cval;
40         } else if ( cname == "number" ) {
41             num = child->getIntValue();
42         } else if ( cname == "encoder" ) {
43             encoder = cval;
44         } else {
45             SG_LOG( SG_INSTR, SG_WARN, 
46                     "Error in transponder config logic" );
47             if ( name.length() ) {
48                 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
49             }
50         }
51     }
52 }
53
54
55 Transponder::~Transponder()
56 {
57 }
58
59
60 void Transponder::init()
61 {
62     string branch;
63     branch = "/instrumentation/" + name;
64     encoder += "/mode-c-alt-ft";
65
66     SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
67     // Inputs
68     pressureAltitudeNode = fgGetNode(encoder.c_str(), true);
69     busPowerNode = fgGetNode("/systems/electrical/outputs/transponder", true);
70     serviceableNode = node->getChild("serviceable", 0, true);
71     // Outputs
72     idCodeNode = node->getChild("id-code", 0, true);
73     flightLevelNode = node->getChild("flight-level", 0, true);
74 }
75
76
77 void Transponder::update(double dt)
78 {
79     if (serviceableNode->getBoolValue())
80     {
81         int idCode = idCodeNode->getIntValue();
82         if (idCode < 0)
83             idCode = 0;
84         int firstDigit  = idCode % 10;
85         int secondDigit = (idCode/10) % 10;
86         int thirdDigit  = (idCode/100) % 10;
87         int fourthDigit = (idCode/1000) % 10;
88
89         if (firstDigit-7 > 0)
90             idCode -= firstDigit-7;
91         if (secondDigit-7 > 0)
92             idCode -= (secondDigit-7) * 10;
93         if (thirdDigit-7 > 0)
94             idCode -= (thirdDigit-7) * 100;
95         if (fourthDigit-7 > 0)
96             idCode -= (fourthDigit-7) * 1000;
97
98         if (idCode > 7777)
99             idCode = 7777;
100         else if (idCode < 0)
101             idCode = 0;
102
103         idCodeNode->setIntValue(idCode);
104
105         int pressureAltitude = pressureAltitudeNode->getIntValue();
106         int flightLevel = pressureAltitude / 100;
107         flightLevelNode->setIntValue(flightLevel);
108     }
109 }