]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/marker_beacon.cxx
Yank out all the glut dependencies and concentrate them in a (easily
[flightgear.git] / src / Cockpit / marker_beacon.cxx
1 // marker_beacon.cxx -- class to manage the marker beacons
2 //
3 // Written by Curtis Olson, started April 2000.
4 //
5 // Copyright (C) 2000  Curtis L. Olson - curt@flightgear.org
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 <stdio.h>      // snprintf
29
30 #include <simgear/compiler.h>
31 #include <simgear/math/sg_random.h>
32
33 #include <Aircraft/aircraft.hxx>
34 #include <Navaids/mkrbeacons.hxx>
35
36 #include "marker_beacon.hxx"
37
38 #include <string>
39 SG_USING_STD(string);
40
41
42 // Constructor
43 FGMarkerBeacon::FGMarkerBeacon() :
44     lon_node(fgGetNode("/position/longitude-deg", true)),
45     lat_node(fgGetNode("/position/latitude-deg", true)),
46     alt_node(fgGetNode("/position/altitude-ft", true)),
47     bus_power(fgGetNode("/systems/electrical/outputs/navcom[0]", true)),
48     power_btn(fgGetNode("/radios/marker-beacon/power-btn", true)),
49     audio_btn(fgGetNode("/radios/marker-beacon/audio-btn", true)),
50     serviceable(fgGetNode("/instrumentation/marker-beacons/serviceable", true)),
51                  
52     need_update(true),
53     outer_blink(false),
54     middle_blink(false),
55     inner_blink(false)
56 {
57     SGPath path( globals->get_fg_root() );
58     SGPath term = path;
59     term.append( "Navaids/range.term" );
60     SGPath low = path;
61     low.append( "Navaids/range.low" );
62     SGPath high = path;
63     high.append( "Navaids/range.high" );
64
65     term_tbl = new SGInterpTable( term.str() );
66     low_tbl = new SGInterpTable( low.str() );
67     high_tbl = new SGInterpTable( high.str() );
68
69     power_btn->setBoolValue( true );
70     audio_btn->setBoolValue( true );
71     serviceable->setBoolValue( true );
72 }
73
74
75 // Destructor
76 FGMarkerBeacon::~FGMarkerBeacon() 
77 {
78     delete term_tbl;
79     delete low_tbl;
80     delete high_tbl;
81 }
82
83
84 void
85 FGMarkerBeacon::init ()
86 {
87     morse.init();
88     beacon.init();
89     blink.stamp();
90 }
91
92
93 void
94 FGMarkerBeacon::bind ()
95 {
96
97     fgTie("/radios/marker-beacon/inner", this,
98           &FGMarkerBeacon::get_inner_blink);
99
100     fgTie("/radios/marker-beacon/middle", this,
101           &FGMarkerBeacon::get_middle_blink);
102
103     fgTie("/radios/marker-beacon/outer", this,
104           &FGMarkerBeacon::get_outer_blink);
105 }
106
107
108 void
109 FGMarkerBeacon::unbind ()
110 {
111     fgUntie("/radios/marker-beacon/inner");
112     fgUntie("/radios/marker-beacon/middle");
113     fgUntie("/radios/marker-beacon/outer");
114 }
115
116
117 // Update the various nav values based on position and valid tuned in navs
118 void 
119 FGMarkerBeacon::update(double dt) 
120 {
121     need_update = false;
122
123     if ( has_power() && serviceable->getBoolValue() ) {
124         // marker beacon blinking
125         bool light_on = ( outer_blink || middle_blink || inner_blink );
126         SGTimeStamp current;
127         current.stamp();
128
129         if ( light_on && (current - blink > 400000) ) {
130             light_on = false;
131             blink.stamp();
132         } else if ( !light_on && (current - blink > 100000) ) {
133             light_on = true;
134             blink.stamp();
135         }
136
137         if ( outer_marker ) {
138             outer_blink = light_on;
139         } else {
140             outer_blink = false;
141         }
142
143         if ( middle_marker ) {
144             middle_blink = light_on;
145         } else {
146             middle_blink = false;
147         }
148
149         if ( inner_marker ) {
150             inner_blink = light_on;
151         } else {
152             inner_blink = false;
153         }
154
155         // cout << outer_blink << " " << middle_blink << " "
156         //      << inner_blink << endl;
157     } else {
158         inner_blink = middle_blink = outer_blink = false;
159     }
160 }
161
162
163 // Update current nav/adf radio stations based on current postition
164 void FGMarkerBeacon::search() 
165 {
166     static FGMkrBeacon::fgMkrBeacType last_beacon = FGMkrBeacon::NOBEACON;
167
168     double lon = lon_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
169     double lat = lat_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
170     double elev = alt_node->getDoubleValue() * SG_FEET_TO_METER;
171
172     ////////////////////////////////////////////////////////////////////////
173     // Beacons.
174     ////////////////////////////////////////////////////////////////////////
175
176     FGMkrBeacon::fgMkrBeacType beacon_type
177         = current_beacons->query( lon * SGD_RADIANS_TO_DEGREES,
178                                   lat * SGD_RADIANS_TO_DEGREES, elev );
179
180     outer_marker = middle_marker = inner_marker = false;
181
182     if ( beacon_type == FGMkrBeacon::NOBEACON
183          || !has_power() || !serviceable->getBoolValue() )
184     {
185         // cout << "no marker" << endl;
186         beacon_type = FGMkrBeacon::NOBEACON;
187         globals->get_soundmgr()->stop( "outer-marker" );
188         globals->get_soundmgr()->stop( "middle-marker" );
189         globals->get_soundmgr()->stop( "inner-marker" );
190     } else if ( beacon_type == FGMkrBeacon::OUTER ) {
191         outer_marker = true;
192         // cout << "OUTER MARKER" << endl;
193         if ( last_beacon != FGMkrBeacon::OUTER ) {
194             if ( ! globals->get_soundmgr()->exists( "outer-marker" ) ) {
195                 SGSimpleSound *sound = beacon.get_outer();
196                 sound->set_volume( 0.3 );
197                 globals->get_soundmgr()->add( sound, "outer-marker" );
198             }
199         }
200         if ( audio_btn->getBoolValue() ) {
201             if ( !globals->get_soundmgr()->is_playing("outer-marker") ) {
202                 globals->get_soundmgr()->play_looped( "outer-marker" );
203             }
204         } else {
205             globals->get_soundmgr()->stop( "outer-marker" );
206         }
207     } else if ( beacon_type == FGMkrBeacon::MIDDLE ) {
208         middle_marker = true;
209         // cout << "MIDDLE MARKER" << endl;
210         if ( last_beacon != FGMkrBeacon::MIDDLE ) {
211             if ( ! globals->get_soundmgr()->exists( "middle-marker" ) ) {
212                 SGSimpleSound *sound = beacon.get_middle();
213                 sound->set_volume( 0.3 );
214                 globals->get_soundmgr()->add( sound, "middle-marker" );
215             }
216         }
217         if ( audio_btn->getBoolValue() ) {
218             if ( !globals->get_soundmgr()->is_playing("middle-marker") ) {
219                 globals->get_soundmgr()->play_looped( "middle-marker" );
220             }
221         } else {
222             globals->get_soundmgr()->stop( "middle-marker" );
223         }
224     } else if ( beacon_type == FGMkrBeacon::INNER ) {
225         inner_marker = true;
226         // cout << "INNER MARKER" << endl;
227         if ( last_beacon != FGMkrBeacon::INNER ) {
228             if ( ! globals->get_soundmgr()->exists( "inner-marker" ) ) {
229                 SGSimpleSound *sound = beacon.get_inner();
230                 sound->set_volume( 0.3 );
231                 globals->get_soundmgr()->add( sound, "inner-marker" );
232             }
233         }
234         if ( audio_btn->getBoolValue() ) {
235             if ( !globals->get_soundmgr()->is_playing("inner-marker") ) {
236                 globals->get_soundmgr()->play_looped( "inner-marker" );
237             }
238         } else {
239             globals->get_soundmgr()->stop( "inner-marker" );
240         }
241     }
242     last_beacon = beacon_type;
243 }