]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/kt_70.cxx
Merge branch 'master' of git://gitorious.org/fg/flightgear into next
[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     std::ostringstream temp;
101     string branch;
102     temp << num;
103     branch = "/instrumentation/" + name + "[" + temp.str() + "]";
104     // internal values
105
106     // modes
107
108     // input and buttons
109     fgTie((branch + "/inputs/ident-btn").c_str(), this,
110           &FGKT_70::get_ident_btn, &FGKT_70::set_ident_btn);
111     fgSetArchivable((branch + "/inputs/rotation-deg").c_str());
112     fgTie((branch + "/inputs/digit1").c_str(), this,
113           &FGKT_70::get_digit1, &FGKT_70::set_digit1);
114     fgSetArchivable((branch + "/inputs/digit1").c_str());
115     fgTie((branch + "/inputs/digit2").c_str(), this,
116           &FGKT_70::get_digit2, &FGKT_70::set_digit2);
117     fgSetArchivable((branch + "/inputs/digit2").c_str());
118     fgTie((branch + "/inputs/digit3").c_str(), this,
119           &FGKT_70::get_digit3, &FGKT_70::set_digit3);
120     fgSetArchivable((branch + "/inputs/digit3").c_str());
121     fgTie((branch + "/inputs/digit4").c_str(), this,
122           &FGKT_70::get_digit4, &FGKT_70::set_digit4);
123     fgSetArchivable((branch + "/inputs/digit4").c_str());
124     fgTie((branch + "/inputs/func-knob").c_str(), this,
125           &FGKT_70::get_func_knob, &FGKT_70::set_func_knob);
126     fgSetArchivable((branch + "/inputs/func-knob").c_str());
127
128     // outputs
129     fgTie((branch + "/outputs/id-code").c_str(), this,
130           &FGKT_70::get_id_code, &FGKT_70::set_id_code);
131     fgSetArchivable((branch + "/outputs/id-code").c_str());
132     fgTie((branch + "/outputs/flight-level").c_str(), this,
133           &FGKT_70::get_flight_level);
134
135     // annunciators
136     fgTie((branch + "/annunciators/fl").c_str(), this, 
137           &FGKT_70::get_fl_ann );
138     fgTie((branch + "/annunciators/alt").c_str(), this, 
139           &FGKT_70::get_alt_ann );
140     fgTie((branch + "/annunciators/gnd").c_str(), this, 
141            &FGKT_70::get_gnd_ann );
142     fgTie((branch + "/annunciators/on").c_str(), this, 
143           &FGKT_70::get_on_ann );
144     fgTie((branch + "/annunciators/sby").c_str(), this, 
145           &FGKT_70::get_sby_ann );
146     fgTie((branch + "/annunciators/reply").c_str(), this, 
147           &FGKT_70::get_reply_ann );
148 }
149
150
151 void FGKT_70::unbind () {
152     std::ostringstream temp;
153     string branch;
154     temp << num;
155     branch = "/instrumentation/" + name + "[" + temp.str() + "]";
156     // internal values
157
158     // modes
159
160     // input and buttons
161     fgUntie((branch + "/inputs/ident-btn").c_str());
162     fgUntie((branch + "/inputs/digit1").c_str());
163     fgUntie((branch + "/inputs/digit2").c_str());
164     fgUntie((branch + "/inputs/digit3").c_str());
165     fgUntie((branch + "/inputs/digit4").c_str());
166     fgUntie((branch + "/inputs/func-knob").c_str());
167
168     // outputs
169     fgUntie((branch + "/outputs/id-code").c_str());
170     fgUntie((branch + "/outputs/flight-level").c_str());
171
172     // annunciators
173     fgUntie((branch + "/annunciators/fl").c_str());
174     fgUntie((branch + "/annunciators/alt").c_str());
175     fgUntie((branch + "/annunciators/gnd").c_str());
176     fgUntie((branch + "/annunciators/on").c_str());
177     fgUntie((branch + "/annunciators/sby").c_str());
178     fgUntie((branch + "/annunciators/reply").c_str());
179 }
180
181
182 // Update the various nav values based on position and valid tuned in navs
183 void FGKT_70::update( double dt ) {
184     // start with all annunciators off (reply ann is handled
185     // separately) and then turn on the ones we want
186     fl_ann = false;
187     alt_ann = false;
188     gnd_ann = false;
189     on_ann = false;
190     sby_ann = false;
191     reply_ann = false;
192
193     if ( has_power() && serviceable_node->getBoolValue() ) {
194         // sanity checks
195         if ( digit1 < 0 ) { digit1 = 0; }
196         if ( digit1 > 7 ) { digit1 = 7; }
197         if ( digit2 < 0 ) { digit2 = 0; }
198         if ( digit2 > 7 ) { digit2 = 7; }
199         if ( digit3 < 0 ) { digit3 = 0; }
200         if ( digit3 > 7 ) { digit3 = 7; }
201         if ( digit4 < 0 ) { digit4 = 0; }
202         if ( digit4 > 7 ) { digit4 = 7; }
203
204         id_code = digit1 * 1000 + digit2 * 100 + digit3 * 10 + digit4;
205
206         // flight level computation
207
208         // FIXME!!!! This needs to be computed relative to 29.92 inHg,
209         // but for the moment, until I figure out how to do that, I'll
210         // just use true altitude.
211         flight_level = (int)( (alt_node->getDoubleValue() + 50.0) / 100.0);
212
213         // ident button
214         if ( ident_btn && !last_ident_btn ) {
215             // ident button depressed
216             r_flash_time = 0.0;
217             ident_mode = true;
218         }
219         r_flash_time += dt;
220         if ( r_flash_time > 18.0 ) {
221             ident_mode = false;
222         }
223     
224         if ( ident_mode ) {
225             reply_ann = true;
226         } else {
227             reply_ann = false;
228         }
229
230         if ( func_knob == 1 ) {
231             sby_ann = true;
232         } else if ( func_knob == 2 ) { // selftest
233             fl_ann = true;
234             alt_ann = true;
235             gnd_ann = true;
236             on_ann = true;
237             sby_ann = true;
238             reply_ann = true;
239             id_code = 8888;
240             flight_level = 888;
241         } else if ( func_knob == 3 ) {
242             fl_ann = true;
243             gnd_ann = true;
244         } else if ( func_knob == 4 ) {
245             on_ann = true;
246         } else if ( func_knob == 5 ) {
247             fl_ann = true;
248             alt_ann = true;
249         }
250     }
251 }