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