]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/kt_70.cxx
Improve transponder instrumentation: new version
[flightgear.git] / src / Instrumentation / kt_70.cxx
1 // kt-70.cxx -- class to impliment the King KT 70 panel-m transponder
2 //
3 // Written by Curtis Olson, started July 2002.
4 //
5 // Copyright (C) 2002  Curtis L. Olson - http://www.flightgear.org/~curt
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 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <iostream>
29 #include <string>
30 #include <sstream>
31
32 #include <simgear/compiler.h>
33 #include <simgear/math/sg_random.h>
34
35 #include "kt_70.hxx"
36
37
38 // Constructor
39 FGKT_70::FGKT_70(SGPropertyNode *node) :
40     r_flash_time(0.0),
41     ident_mode(false),
42     ident_btn(false),
43     last_ident_btn(false),
44     digit1(1), digit2(2), digit3(0), digit4(0),
45     func_knob(4),
46     id_code(1200),
47     flight_level(0),
48     fl_ann(0),
49     alt_ann(0),
50     gnd_ann(0),
51     on_ann(0),
52     sby_ann(0),
53     reply_ann(0),
54     name("kt-70"),
55     num(0)
56 {
57     int i;
58     for ( i = 0; i < node->nChildren(); ++i ) {
59         SGPropertyNode *child = node->getChild(i);
60         string cname = child->getName();
61         string cval = child->getStringValue();
62         if ( cname == "name" ) {
63             name = cval;
64         } else if ( cname == "number" ) {
65             num = child->getIntValue();
66         } else {
67             SG_LOG( SG_INSTR, SG_WARN, 
68                     "Error in kt-70 config logic" );
69             if ( name.length() ) {
70                 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
71             }
72         }
73     }
74
75 }
76
77
78 // Destructor
79 FGKT_70::~FGKT_70() { }
80
81
82 void FGKT_70::init ()
83 {
84     string branch;
85     branch = "/instrumentation/" + name;
86
87     SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
88     // Inputs
89     lon_node = fgGetNode("/position/longitude-deg", true);
90     lat_node = fgGetNode("/position/latitude-deg", true);
91     alt_node = fgGetNode("/position/altitude-ft", true);
92     bus_power = fgGetNode("/systems/electrical/outputs/transponder", true);
93     serviceable_node = (node->getChild("inputs", 0, true))
94                         ->getChild("serviceable", 0, true);
95     serviceable_node->setBoolValue(true);
96 }
97
98
99 void FGKT_70::bind ()
100 {
101     std::ostringstream temp;
102     string branch;
103     temp << num;
104     branch = "/instrumentation/" + name + "[" + temp.str() + "]";
105     _tiedProperties.setRoot(fgGetNode(branch, true));
106
107     // internal values
108
109     // modes
110     // input and buttons
111     _tiedProperties.Tie("inputs/ident-btn", this,
112                         &FGKT_70::get_ident_btn, &FGKT_70::set_ident_btn);
113     fgSetArchivable((branch + "/inputs/rotation-deg").c_str());
114     _tiedProperties.Tie("inputs/digit1", this,
115                         &FGKT_70::get_digit1, &FGKT_70::set_digit1);
116     fgSetArchivable((branch + "/inputs/digit1").c_str());
117     _tiedProperties.Tie("inputs/digit2", this,
118                         &FGKT_70::get_digit2, &FGKT_70::set_digit2);
119     fgSetArchivable((branch + "/inputs/digit2").c_str());
120     _tiedProperties.Tie("inputs/digit3", this,
121                         &FGKT_70::get_digit3, &FGKT_70::set_digit3);
122     fgSetArchivable((branch + "/inputs/digit3").c_str());
123     _tiedProperties.Tie("inputs/digit4", this,
124                         &FGKT_70::get_digit4, &FGKT_70::set_digit4);
125     fgSetArchivable((branch + "/inputs/digit4").c_str());
126     _tiedProperties.Tie("inputs/func-knob", this,
127                         &FGKT_70::get_func_knob, &FGKT_70::set_func_knob);
128     fgSetArchivable((branch + "/inputs/func-knob").c_str());
129
130     // outputs
131     _tiedProperties.Tie("outputs/id-code", this,
132                         &FGKT_70::get_id_code, &FGKT_70::set_id_code);
133     fgSetArchivable((branch + "/outputs/id-code").c_str());
134     _tiedProperties.Tie("outputs/flight-level", this,
135                         &FGKT_70::get_flight_level);
136
137     // annunciators
138     _tiedProperties.Tie("annunciators/fl", this,
139                         &FGKT_70::get_fl_ann );
140     _tiedProperties.Tie("annunciators/alt", this,
141                         &FGKT_70::get_alt_ann );
142     _tiedProperties.Tie("annunciators/gnd", this,
143                          &FGKT_70::get_gnd_ann );
144     _tiedProperties.Tie("annunciators/on", this,
145                         &FGKT_70::get_on_ann );
146     _tiedProperties.Tie("annunciators/sby", this,
147                         &FGKT_70::get_sby_ann );
148     _tiedProperties.Tie("annunciators/reply", this,
149                         &FGKT_70::get_reply_ann );
150 }
151
152
153 void FGKT_70::unbind () {
154     _tiedProperties.Untie();
155 }
156
157
158 // Update the various nav values based on position and valid tuned in navs
159 void FGKT_70::update( double dt ) {
160     // start with all annunciators off (reply ann is handled
161     // separately) and then turn on the ones we want
162     fl_ann = false;
163     alt_ann = false;
164     gnd_ann = false;
165     on_ann = false;
166     sby_ann = false;
167     reply_ann = false;
168
169     if ( has_power() && serviceable_node->getBoolValue() ) {
170         // sanity checks
171         if ( digit1 < 0 ) { digit1 = 0; }
172         if ( digit1 > 7 ) { digit1 = 7; }
173         if ( digit2 < 0 ) { digit2 = 0; }
174         if ( digit2 > 7 ) { digit2 = 7; }
175         if ( digit3 < 0 ) { digit3 = 0; }
176         if ( digit3 > 7 ) { digit3 = 7; }
177         if ( digit4 < 0 ) { digit4 = 0; }
178         if ( digit4 > 7 ) { digit4 = 7; }
179
180         id_code = digit1 * 1000 + digit2 * 100 + digit3 * 10 + digit4;
181
182         // flight level computation
183
184         // FIXME!!!! This needs to be computed relative to 29.92 inHg,
185         // but for the moment, until I figure out how to do that, I'll
186         // just use true altitude.
187         flight_level = (int)( (alt_node->getDoubleValue() + 50.0) / 100.0);
188
189         // ident button
190         if ( ident_btn && !last_ident_btn ) {
191             // ident button depressed
192             r_flash_time = 0.0;
193             ident_mode = true;
194         }
195         r_flash_time += dt;
196         if ( r_flash_time > 18.0 ) {
197             ident_mode = false;
198         }
199     
200         if ( ident_mode ) {
201             reply_ann = true;
202         } else {
203             reply_ann = false;
204         }
205
206         if ( func_knob == 1 ) {
207             sby_ann = true;
208         } else if ( func_knob == 2 ) { // selftest
209             fl_ann = true;
210             alt_ann = true;
211             gnd_ann = true;
212             on_ann = true;
213             sby_ann = true;
214             reply_ann = true;
215             id_code = 8888;
216             flight_level = 888;
217         } else if ( func_knob == 3 ) {
218             fl_ann = true;
219             gnd_ann = true;
220         } else if ( func_knob == 4 ) {
221             on_ann = true;
222         } else if ( func_knob == 5 ) {
223             fl_ann = true;
224             alt_ann = true;
225         }
226     }
227 }