]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/mk_viii.hxx
Merge commit 'refs/merge-requests/13' of git://gitorious.org/fg/flightgear into next
[flightgear.git] / src / Instrumentation / mk_viii.hxx
1 // mk_viii.hxx -- Honeywell MK VIII EGPWS emulation
2 //
3 // Written by Jean-Yves Lefort, started September 2005.
4 //
5 // Copyright (C) 2005, 2006  Jean-Yves Lefort - jylefort@FreeBSD.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
22 #ifndef __INSTRUMENTS_MK_VIII_HXX
23 #define __INSTRUMENTS_MK_VIII_HXX
24
25 #include <assert.h>
26
27 #include <vector>
28 #include <deque>
29 #include <map>
30
31 #include <simgear/props/props.hxx>
32 #include <simgear/sound/sample_openal.hxx>
33 #include <simgear/structure/subsystem_mgr.hxx>
34
35 using std::vector;
36 using std::deque;
37 using std::map;
38
39 class SGSampleGroup;
40
41 #include <Airports/runways.hxx>
42 #include <Airports/simple.hxx>
43 #include <Main/globals.hxx>
44
45 #ifdef _MSC_VER
46 #  pragma warning( push )
47 #  pragma warning( disable: 4355 )
48 #endif
49
50 ///////////////////////////////////////////////////////////////////////////////
51 // MK_VIII ////////////////////////////////////////////////////////////////////
52 ///////////////////////////////////////////////////////////////////////////////
53
54 class MK_VIII : public SGSubsystem
55 {
56   // keep in sync with Mode6Handler::altitude_callout_definitions[]
57   static const unsigned n_altitude_callouts = 11;
58
59   /////////////////////////////////////////////////////////////////////////////
60   // MK_VIII::RawValueMethodsData /////////////////////////////////////////////
61   /////////////////////////////////////////////////////////////////////////////
62
63   template <class C, class VT, class DT>
64   class RawValueMethodsData : public SGRawValue<VT>
65   {
66   public:
67     typedef VT (C::*getter_t) (DT) const;
68     typedef void (C::*setter_t) (DT, VT);
69
70     RawValueMethodsData (C &obj, DT data, getter_t getter = 0, setter_t setter = 0)
71       : _obj(obj), _data(data), _getter(getter), _setter(setter) {}
72
73     virtual VT getValue () const
74     {
75       if (_getter)
76         return (_obj.*_getter)(_data);
77       else
78         return SGRawValue<VT>::DefaultValue();
79     }
80     virtual bool setValue (VT value)
81     {
82       if (_setter)
83         {
84           (_obj.*_setter)(_data, value);
85           return true;
86         }
87       else
88         return false;
89     }
90     virtual SGRawValue<VT> *clone () const 
91     {
92       return new RawValueMethodsData<C,VT,DT>(_obj, _data, _getter, _setter);
93     }
94
95   private:
96     C           &_obj;
97     DT          _data;
98     getter_t    _getter;
99     setter_t    _setter;
100   };
101
102   /////////////////////////////////////////////////////////////////////////////
103   // MK_VIII::Parameter ///////////////////////////////////////////////////////
104   /////////////////////////////////////////////////////////////////////////////
105
106   template <class T>
107   class Parameter
108   {
109     T _value;
110
111   public:
112     bool ncd;
113
114     inline Parameter ()
115       : _value(0), ncd(true) {}
116
117     inline T get () const { assert(! ncd); return _value; }
118     inline T *get_pointer () { return &_value; }
119     inline void set (T value) { ncd = false; _value = value; }
120     inline void unset () { ncd = true; }
121
122     inline void set (const Parameter<T> *parameter)
123     {
124       if (parameter->ncd)
125         unset();
126       else
127         set(parameter->get());
128     }
129
130     inline void set (const Parameter<double> *parameter, double factor)
131     {
132       if (parameter->ncd)
133         unset();
134       else
135         set(parameter->get() * factor);
136     }
137   };
138
139   /////////////////////////////////////////////////////////////////////////////
140   // MK_VIII::Sample //////////////////////////////////////////////////////////
141   /////////////////////////////////////////////////////////////////////////////
142
143   template <class T>
144   class Sample
145   {
146   public:
147     double      timestamp;
148     T           value;
149
150     inline Sample (T _value)
151       : timestamp(globals->get_sim_time_sec()), value(_value) {}
152   };
153
154   /////////////////////////////////////////////////////////////////////////////
155   // MK_VIII::Timer ///////////////////////////////////////////////////////////
156   /////////////////////////////////////////////////////////////////////////////
157
158   class Timer
159   {
160     double      start_time;
161
162   public:
163     bool        running;
164
165     inline Timer ()
166       : running(false) {}
167
168     inline void start () { running = true; start_time = globals->get_sim_time_sec(); }
169     inline void stop () { running = false; }
170     inline double elapsed () const { assert(running); return globals->get_sim_time_sec() - start_time; }
171     inline double start_or_elapsed ()
172     {
173       if (running)
174         return elapsed();
175       else
176         {
177           start();
178           return 0;
179         }
180     }
181   };
182
183   /////////////////////////////////////////////////////////////////////////////
184   // MK_VIII::PropertiesHandler ///////////////////////////////////////////////
185   /////////////////////////////////////////////////////////////////////////////
186
187   class PropertiesHandler
188   {
189     MK_VIII *mk;
190
191     vector<SGPropertyNode_ptr> tied_properties;
192
193   public:
194     struct
195     {
196       SGPropertyNode_ptr ai_caged;
197       SGPropertyNode_ptr ai_roll;
198       SGPropertyNode_ptr ai_serviceable;
199       SGPropertyNode_ptr altimeter_altitude;
200       SGPropertyNode_ptr altimeter_serviceable;
201       SGPropertyNode_ptr altitude;
202       SGPropertyNode_ptr altitude_agl;
203       SGPropertyNode_ptr altitude_gear_agl;
204       SGPropertyNode_ptr orientation_roll;
205       SGPropertyNode_ptr asi_serviceable;
206       SGPropertyNode_ptr asi_speed;
207       SGPropertyNode_ptr autopilot_heading_lock;
208       SGPropertyNode_ptr flaps;
209       SGPropertyNode_ptr gear_down;
210       SGPropertyNode_ptr latitude;
211       SGPropertyNode_ptr longitude;
212       SGPropertyNode_ptr nav0_cdi_serviceable;
213       SGPropertyNode_ptr nav0_gs_distance;
214       SGPropertyNode_ptr nav0_gs_needle_deflection;
215       SGPropertyNode_ptr nav0_gs_serviceable;
216       SGPropertyNode_ptr nav0_has_gs;
217       SGPropertyNode_ptr nav0_heading_needle_deflection;
218       SGPropertyNode_ptr nav0_in_range;
219       SGPropertyNode_ptr nav0_nav_loc;
220       SGPropertyNode_ptr nav0_serviceable;
221       SGPropertyNode_ptr power;
222       SGPropertyNode_ptr replay_state;
223       SGPropertyNode_ptr vs;
224     } external_properties;
225
226     inline PropertiesHandler (MK_VIII *device)
227       : mk(device) {}
228
229     template <class T>
230     inline void tie (SGPropertyNode *node, const SGRawValue<T> &raw_value)
231     {
232       node->tie(raw_value);
233       tied_properties.push_back(node);
234     }
235
236     template <class T>
237     inline void tie (SGPropertyNode *node,
238                      const char *relative_path,
239                      const SGRawValue<T> &raw_value)
240     {
241       tie(node->getNode(relative_path, true), raw_value);
242     }
243
244     PropertiesHandler() {};
245
246     void init ();
247     void unbind ();
248   };
249
250 public:
251   PropertiesHandler     properties_handler;
252
253 private:
254   /////////////////////////////////////////////////////////////////////////////
255   // MK_VIII::PowerHandler ////////////////////////////////////////////////////
256   /////////////////////////////////////////////////////////////////////////////
257
258   class PowerHandler
259   {
260     MK_VIII *mk;
261
262     bool serviceable;
263     bool powered;
264
265     Timer power_loss_timer;
266     Timer abnormal_timer;
267     Timer low_surge_timer;
268     Timer high_surge_timer;
269     Timer very_high_surge_timer;
270
271     bool handle_abnormal_voltage (bool abnormal,
272                                   Timer *timer,
273                                   double max_duration);
274
275     void power_on ();
276     void power_off ();
277
278   public:
279     inline PowerHandler (MK_VIII *device)
280       : mk(device), serviceable(false), powered(false) {}
281
282     void bind (SGPropertyNode *node);
283     void update ();
284   };
285
286   /////////////////////////////////////////////////////////////////////////////
287   // MK_VIII::SystemHandler ///////////////////////////////////////////////////
288   /////////////////////////////////////////////////////////////////////////////
289
290   class SystemHandler
291   {
292     MK_VIII *mk;
293
294     double      boot_delay;
295     Timer       boot_timer;
296
297     int         last_replay_state;
298     Timer       reposition_timer;
299
300   public:
301     typedef enum
302     {
303       STATE_OFF,
304       STATE_BOOTING,
305       STATE_ON,
306       STATE_REPOSITION
307     } State;
308
309     State state;
310
311     inline SystemHandler (MK_VIII *device)
312       : mk(device), state(STATE_OFF) {}
313
314     void power_on ();
315     void power_off ();
316     void update ();
317   };
318
319   /////////////////////////////////////////////////////////////////////////////
320   // MK_VIII::ConfigurationModule /////////////////////////////////////////////
321   /////////////////////////////////////////////////////////////////////////////
322
323   class ConfigurationModule
324   {
325   public:
326     // keep in sync with IOHandler::present_status()
327     typedef enum 
328     {
329       CATEGORY_AIRCRAFT_MODE_TYPE_SELECT,
330       CATEGORY_AIR_DATA_INPUT_SELECT,
331       CATEGORY_POSITION_INPUT_SELECT,
332       CATEGORY_ALTITUDE_CALLOUTS,
333       CATEGORY_AUDIO_MENU_SELECT,
334       CATEGORY_TERRAIN_DISPLAY_SELECT,
335       CATEGORY_OPTIONS_SELECT_GROUP_1,
336       CATEGORY_RADIO_ALTITUDE_INPUT_SELECT,
337       CATEGORY_NAVIGATION_INPUT_SELECT,
338       CATEGORY_ATTITUDE_INPUT_SELECT,
339       CATEGORY_HEADING_INPUT_SELECT,
340       CATEGORY_WINDSHEAR_INPUT_SELECT,
341       CATEGORY_INPUT_OUTPUT_DISCRETE_TYPE_SELECT,
342       CATEGORY_AUDIO_OUTPUT_LEVEL,
343       CATEGORY_UNDEFINED_INPUT_SELECT_1,
344       CATEGORY_UNDEFINED_INPUT_SELECT_2,
345       CATEGORY_UNDEFINED_INPUT_SELECT_3,
346       N_CATEGORIES
347     } Category;
348
349     typedef enum
350     {
351       STATE_OK,
352       STATE_INVALID_DATABASE,
353       STATE_INVALID_AIRCRAFT_TYPE
354     } State;
355     State state;
356
357     int effective_categories[N_CATEGORIES];
358
359     ConfigurationModule (MK_VIII *device);
360
361     void boot ();
362     void bind (SGPropertyNode *node);
363
364   private:
365     MK_VIII *mk;
366
367     int categories[N_CATEGORIES];
368
369     bool read_aircraft_mode_type_select (int value);
370     bool read_air_data_input_select (int value);
371     bool read_position_input_select (int value);
372     bool read_altitude_callouts (int value);
373     bool read_audio_menu_select (int value);
374     bool read_terrain_display_select (int value);
375     bool read_options_select_group_1 (int value);
376     bool read_radio_altitude_input_select (int value);
377     bool read_navigation_input_select (int value);
378     bool read_attitude_input_select (int value);
379     bool read_heading_input_select (int value);
380     bool read_windshear_input_select (int value);
381     bool read_input_output_discrete_type_select (int value);
382     bool read_audio_output_level (int value);
383     bool read_undefined_input_select (int value);
384
385     static bool m6_t2_is_bank_angle (Parameter<double> *agl,
386                                      double abs_roll_deg,
387                                      bool ap_engaged);
388     static bool m6_t4_is_bank_angle (Parameter<double> *agl,
389                                      double abs_roll_deg,
390                                      bool ap_engaged);
391   };
392
393   /////////////////////////////////////////////////////////////////////////////
394   // MK_VIII::FaultHandler ////////////////////////////////////////////////////
395   /////////////////////////////////////////////////////////////////////////////
396
397   class FaultHandler
398   {
399     enum
400     {
401       INOP_GPWS         = 1 << 0,
402       INOP_TAD          = 1 << 1
403     };
404
405     MK_VIII *mk;
406
407     static const unsigned int fault_inops[];
408
409     bool has_faults (unsigned int inop);
410
411   public:
412     // keep in sync with IOHandler::present_status()
413     typedef enum
414     {
415       FAULT_ALL_MODES_INHIBIT,
416       FAULT_GEAR_SWITCH,
417       FAULT_FLAPS_SWITCH,
418       FAULT_MOMENTARY_FLAP_OVERRIDE_INVALID,
419       FAULT_SELF_TEST_INVALID,
420       FAULT_GLIDESLOPE_CANCEL_INVALID,
421       FAULT_STEEP_APPROACH_INVALID,
422       FAULT_GPWS_INHIBIT,
423       FAULT_TA_TCF_INHIBIT,
424       FAULT_MODES14_INPUTS_INVALID,
425       FAULT_MODE5_INPUTS_INVALID,
426       FAULT_MODE6_INPUTS_INVALID,
427       FAULT_BANK_ANGLE_INPUTS_INVALID,
428       FAULT_TCF_INPUTS_INVALID,
429       N_FAULTS
430     } Fault;
431
432     bool faults[N_FAULTS];
433
434     inline FaultHandler (MK_VIII *device)
435       : mk(device) {}
436
437     void boot ();
438
439     void set_fault (Fault fault);
440     void unset_fault (Fault fault);
441
442     bool has_faults () const;
443   };
444
445   /////////////////////////////////////////////////////////////////////////////
446   // MK_VIII::IOHandler ///////////////////////////////////////////////////////
447   /////////////////////////////////////////////////////////////////////////////
448
449 public:
450   class IOHandler
451   {
452   public:
453     enum Lamp
454     {
455       LAMP_NONE,
456       LAMP_GLIDESLOPE,
457       LAMP_CAUTION,
458       LAMP_WARNING
459     };
460
461     struct LampConfiguration
462     {
463       bool format2;
464       bool flashing;
465     };
466
467     struct FaultsConfiguration
468     {
469       double max_flaps_down_airspeed;
470       double max_gear_down_airspeed;
471     };
472
473     struct _s_Conf
474     {
475       const LampConfiguration   *lamp;
476       const FaultsConfiguration *faults;
477       bool                      flap_reversal;
478       bool                      steep_approach_enabled;
479       bool                      gpws_inhibit_enabled;
480       bool                      momentary_flap_override_enabled;
481       bool                      alternate_steep_approach;
482       bool                      use_internal_gps;
483       bool                      localizer_enabled;
484       bool                      use_gear_altitude;
485       bool                      use_attitude_indicator;
486     } conf;
487
488     struct _s_input_feeders
489     {
490       struct _s_discretes
491       {
492         bool landing_gear;
493         bool landing_flaps;
494         bool glideslope_inhibit;
495         bool decision_height;
496         bool autopilot_engaged;
497       } discretes;
498
499       struct _s_arinc429
500       {
501         bool uncorrected_barometric_altitude;
502         bool barometric_altitude_rate;
503         bool radio_altitude;
504         bool glideslope_deviation;
505         bool roll_angle;
506         bool localizer_deviation;
507         bool computed_airspeed;
508         bool decision_height;
509       } arinc429;
510     } input_feeders;
511
512     struct _s_inputs
513     {
514       struct _s_discretes
515       {
516         bool landing_gear;              // appendix E 6.6.2, 3.15.1.4
517         bool landing_flaps;             // appendix E 6.6.4, 3.15.1.2
518         bool momentary_flap_override;   // appendix E 6.6.6, 3.15.1.6
519         bool self_test;                 // appendix E 6.6.7, 3.15.1.10
520         bool glideslope_inhibit;        // appendix E 6.6.11, 3.15.1.1
521         bool glideslope_cancel;         // appendix E 6.6.13, 3.15.1.5
522         bool decision_height;           // appendix E 6.6.14, 3.10.2
523         bool mode6_low_volume;          // appendix E 6.6.15, 3.15.1.7
524         bool audio_inhibit;             // appendix E 6.6.16, 3.15.1.3
525         bool ta_tcf_inhibit;            // appendix E 6.6.20, 3.15.1.9
526         bool autopilot_engaged;         // appendix E 6.6.21, 3.15.1.8
527         bool steep_approach;            // appendix E 6.6.25, 3.15.1.11
528         bool gpws_inhibit;              // appendix E 6.6.27, 3.15.1.12
529       } discretes;
530
531       struct _s_arinc429
532       {
533         Parameter<double> uncorrected_barometric_altitude;      // appendix E 6.2.1
534         Parameter<double> barometric_altitude_rate;             // appendix E 6.2.2
535         Parameter<double> gps_altitude;                         // appendix E 6.2.4
536         Parameter<double> gps_latitude;                         // appendix E 6.2.7
537         Parameter<double> gps_longitude;                        // appendix E 6.2.8
538         Parameter<double> gps_vertical_figure_of_merit;         // appendix E 6.2.13
539         Parameter<double> radio_altitude;                       // appendix E 6.2.29
540         Parameter<double> glideslope_deviation;                 // appendix E 6.2.30
541         Parameter<double> roll_angle;                           // appendix E 6.2.31
542         Parameter<double> localizer_deviation;                  // appendix E 6.2.33
543         Parameter<double> computed_airspeed;                    // appendix E 6.2.39
544         Parameter<double> decision_height;                      // appendix E 6.2.41
545       } arinc429;
546     } inputs;
547
548     struct Outputs
549     {
550       struct _s_discretes
551       {
552         bool gpws_warning;              // appendix E 7.4.1, 3.15.2.5
553         bool gpws_alert;                // appendix E 7.4.1, 3.15.2.6
554         bool audio_on;                  // appendix E 7.4.2, 3.15.2.10
555         bool gpws_inop;                 // appendix E 7.4.3, 3.15.2.3
556         bool tad_inop;                  // appendix E 7.4.3, 3.15.2.4
557         bool flap_override;             // appendix E 7.4.5, 3.15.2.8
558         bool glideslope_cancel;         // appendix E 7.4.6, 3.15.2.7
559         bool steep_approach;            // appendix E 7.4.12, 3.15.2.9
560       } discretes;
561
562       struct _s_arinc429
563       {
564         int egpws_alert_discrete_1;     // appendix E 7.1.1.1
565         int egpwc_logic_discretes;      // appendix E 7.1.1.2
566         int mode6_callouts_discrete_1;  // appendix E 7.1.1.3
567         int mode6_callouts_discrete_2;  // appendix E 7.1.1.4
568         int egpws_alert_discrete_2;     // appendix E 7.1.1.5
569         int egpwc_alert_discrete_3;     // appendix E 7.1.1.6
570       } arinc429;
571     };
572
573     Outputs outputs;
574
575     struct _s_data
576     {
577       Parameter<double> barometric_altitude_rate;
578       Parameter<double> decision_height;
579       Parameter<double> geometric_altitude;
580       Parameter<double> glideslope_deviation_dots;
581       Parameter<double> gps_altitude;
582       Parameter<double> gps_latitude;
583       Parameter<double> gps_longitude;
584       Parameter<double> gps_vertical_figure_of_merit;
585       Parameter<double> localizer_deviation_dots;
586       Parameter<double> radio_altitude;
587       Parameter<double> roll_angle;
588       Parameter<double> terrain_clearance;
589     } data;
590
591     IOHandler (MK_VIII *device);
592
593     void boot ();
594     void post_boot ();
595     void power_off ();
596
597     void enter_ground ();
598     void enter_takeoff ();
599
600     void update_inputs ();
601     void update_input_faults ();
602     void update_alternate_discrete_input (bool *ptr);
603     void update_internal_latches ();
604
605     void update_egpws_alert_discrete_1 ();
606     void update_egpwc_logic_discretes ();
607     void update_mode6_callouts_discrete_1 ();
608     void update_mode6_callouts_discrete_2 ();
609     void update_egpws_alert_discrete_2 ();
610     void update_egpwc_alert_discrete_3 ();
611     void update_outputs ();
612     void reposition ();
613
614     void update_lamps ();
615     void set_lamp (Lamp lamp);
616
617     bool gpws_inhibit () const;
618     bool real_flaps_down () const;
619     bool flaps_down () const;
620     bool flap_override () const;
621     bool steep_approach () const;
622     bool momentary_steep_approach_enabled () const;
623
624     void bind (SGPropertyNode *node);
625
626     MK_VIII *mk;
627
628   private:
629
630     ///////////////////////////////////////////////////////////////////////////
631     // MK_VIII::IOHandler::TerrainClearanceFilter /////////////////////////////
632     ///////////////////////////////////////////////////////////////////////////
633
634     class TerrainClearanceFilter
635     {
636       typedef deque< Sample<double> > samples_type;
637       samples_type              samples;
638       double                    value;
639       double                    last_update;
640
641     public:
642       inline TerrainClearanceFilter ()
643         : value(0.0), last_update(-1.0) {}
644
645       double update (double agl);
646       void reset ();
647     };
648
649     ///////////////////////////////////////////////////////////////////////////
650     // MK_VIII::IOHandler (continued) /////////////////////////////////////////
651     ///////////////////////////////////////////////////////////////////////////
652
653     TerrainClearanceFilter terrain_clearance_filter;
654
655     Lamp        _lamp;
656     Timer       lamp_timer;
657
658     Timer audio_inhibit_fault_timer;
659     Timer landing_gear_fault_timer;
660     Timer flaps_down_fault_timer;
661     Timer momentary_flap_override_fault_timer;
662     Timer self_test_fault_timer;
663     Timer glideslope_cancel_fault_timer;
664     Timer steep_approach_fault_timer;
665     Timer gpws_inhibit_fault_timer;
666     Timer ta_tcf_inhibit_fault_timer;
667
668     bool last_landing_gear;
669     bool last_real_flaps_down;
670
671     typedef deque< Sample< Parameter<double> > > altitude_samples_type;
672     altitude_samples_type altitude_samples;
673
674     struct
675     {
676       bool glideslope_cancel;
677     } power_saved;
678
679     void update_terrain_clearance ();
680     void reset_terrain_clearance ();
681
682     void handle_input_fault (bool test, FaultHandler::Fault fault);
683     void handle_input_fault (bool test,
684                              Timer *timer,
685                              double max_duration,
686                              FaultHandler::Fault fault);
687
688     void tie_input (SGPropertyNode *node,
689                     const char *name,
690                     bool *input,
691                     bool *feed = NULL);
692     void tie_input (SGPropertyNode *node,
693                     const char *name,
694                     Parameter<double> *input,
695                     bool *feed = NULL);
696     void tie_output (SGPropertyNode *node,
697                      const char *name,
698                      bool *output);
699     void tie_output (SGPropertyNode *node,
700                      const char *name,
701                      int *output);
702
703   public:
704
705     bool get_discrete_input (bool *ptr) const;
706     void set_discrete_input (bool *ptr, bool value);
707
708     void present_status ();
709     void present_status_section (const char *name);
710     void present_status_item (const char *name, const char *value = NULL);
711     void present_status_subitem (const char *name);
712
713     bool get_present_status () const;
714     void set_present_status (bool value);
715
716     bool *get_lamp_output (Lamp lamp);
717   };
718
719   /////////////////////////////////////////////////////////////////////////////
720   // MK_VIII::VoicePlayer /////////////////////////////////////////////////////
721   /////////////////////////////////////////////////////////////////////////////
722
723   class VoicePlayer
724   {
725   public:
726
727     ///////////////////////////////////////////////////////////////////////////
728     // MK_VIII::VoicePlayer::Voice ////////////////////////////////////////////
729     ///////////////////////////////////////////////////////////////////////////
730
731     class Voice
732     {
733     public:
734
735       /////////////////////////////////////////////////////////////////////////
736       // MK_VIII::VoicePlayer::Voice::Element ////////////////////////////////////////
737       /////////////////////////////////////////////////////////////////////////
738
739       class Element
740       {
741       public:
742         bool silence;
743
744         virtual inline void play (float volume) {}
745         virtual inline void stop () {}
746         virtual bool is_playing () = 0;
747         virtual inline void set_volume (float volume) {}
748       };
749
750       /////////////////////////////////////////////////////////////////////////
751       // MK_VIII::VoicePlayer::Voice::SampleElement ///////////////////////////
752       /////////////////////////////////////////////////////////////////////////
753
754       class SampleElement : public Element
755       {
756         SGSharedPtr<SGSoundSample>      _sample;
757         float                           _volume;
758
759       public:
760         inline SampleElement (SGSharedPtr<SGSoundSample> sample, float volume = 1.0)
761           : _sample(sample), _volume(volume) { silence = false; }
762
763         virtual inline void play (float volume) { if (_sample && (volume > 0.05)) { set_volume(volume); _sample->play_once(); } }
764         virtual inline void stop () { if (_sample) _sample->stop(); }
765         virtual inline bool is_playing () { return _sample ? _sample->is_playing() : false; }
766         virtual inline void set_volume (float volume) { if (_sample) _sample->set_volume(volume * _volume); }
767       };
768
769       /////////////////////////////////////////////////////////////////////////
770       // MK_VIII::VoicePlayer::Voice::SilenceElement //////////////////////////
771       /////////////////////////////////////////////////////////////////////////
772
773       class SilenceElement : public Element
774       {
775         double _duration;
776         double start_time;
777
778       public:
779         inline SilenceElement (double duration)
780           : _duration(duration) { silence = true; }
781
782         virtual inline void play (float volume) { start_time = globals->get_sim_time_sec(); }
783         virtual inline bool is_playing () { return globals->get_sim_time_sec() - start_time < _duration; }
784       };
785
786       /////////////////////////////////////////////////////////////////////////
787       // MK_VIII::VoicePlayer::Voice (continued) //////////////////////////////
788       /////////////////////////////////////////////////////////////////////////
789
790       Element *element;
791
792       inline Voice (VoicePlayer *_player)
793         : element(NULL), player(_player), volume(1.0) {}
794
795       ~Voice ();
796
797       inline void append (Element *_element) { elements.push_back(_element); }
798
799       void play ();
800       void stop (bool now);
801       void set_volume (float _volume);
802       void volume_changed ();
803       void update ();
804
805     private:
806       VoicePlayer *player;
807
808       float volume;
809
810       vector<Element *>                 elements;
811       vector<Element *>::iterator       iter;
812
813       inline float get_volume () const { return player->volume * player->speaker.volume * volume; }
814     };
815
816     ///////////////////////////////////////////////////////////////////////////
817     // MK_VIII::VoicePlayer (continued) ///////////////////////////////////////
818     ///////////////////////////////////////////////////////////////////////////
819
820     struct
821     {
822       float volume;
823     } conf;
824
825     float volume;
826
827     Voice *voice;
828     Voice *next_voice;
829
830     struct
831     {
832       Voice *application_data_base_failed;
833       Voice *bank_angle;
834       Voice *bank_angle_bank_angle;
835       Voice *bank_angle_bank_angle_3;
836       Voice *bank_angle_inop;
837       Voice *bank_angle_pause_bank_angle;
838       Voice *bank_angle_pause_bank_angle_3;
839       Voice *callouts_inop;
840       Voice *configuration_type_invalid;
841       Voice *dont_sink;
842       Voice *dont_sink_pause_dont_sink;
843       Voice *five_hundred_above;
844       Voice *glideslope;
845       Voice *glideslope_inop;
846       Voice *gpws_inop;
847       Voice *hard_glideslope;
848       Voice *minimums;
849       Voice *minimums_minimums;
850       Voice *pull_up;
851       Voice *sink_rate;
852       Voice *sink_rate_pause_sink_rate;
853       Voice *soft_glideslope;
854       Voice *terrain;
855       Voice *terrain_pause_terrain;
856       Voice *too_low_flaps;
857       Voice *too_low_gear;
858       Voice *too_low_terrain;
859       Voice *altitude_callouts[n_altitude_callouts];
860     } voices;
861
862     inline VoicePlayer (MK_VIII *device)
863       : voice(NULL), next_voice(NULL),  mk(device), speaker(this) {}
864
865     ~VoicePlayer ();
866
867     void init ();
868
869     enum
870     {
871       PLAY_NOW          = 1 << 0,
872       PLAY_LOOPED       = 1 << 1
873     };
874     void play (Voice *_voice, unsigned int flags = 0);
875
876     enum
877     {
878       STOP_NOW          = 1 << 0
879     };
880     void stop (unsigned int flags = 0);
881
882     void set_volume (float _volume);
883     void update ();
884
885     inline void bind (SGPropertyNode *node) { speaker.bind(node); }
886
887   public:
888
889     ///////////////////////////////////////////////////////////////////////////
890     // MK_VIII::VoicePlayer::Speaker //////////////////////////////////////////
891     ///////////////////////////////////////////////////////////////////////////
892
893     class Speaker
894     {
895       VoicePlayer *player;
896
897       double    pitch;
898
899       template <class T>
900       inline void tie (SGPropertyNode *node, const char *name, T *ptr)
901       {
902         player->mk->properties_handler.tie
903           (node, (string("speaker/") + name).c_str(),
904            RawValueMethodsData<MK_VIII::VoicePlayer::Speaker,T,T*>
905            (*this, ptr,
906             &MK_VIII::VoicePlayer::Speaker::get_property,
907             &MK_VIII::VoicePlayer::Speaker::set_property));
908       }
909
910     public:
911       template <class T>
912       inline void set_property (T *ptr, T value) { *ptr = value; update_configuration(); }
913
914       template <class T>
915       inline T get_property (T *ptr) const { return *ptr; }
916
917       float volume;
918
919       inline Speaker (VoicePlayer *_player)
920         : player(_player),
921           pitch(1),
922           volume(1)
923       {
924       }
925
926       void bind (SGPropertyNode *node);
927       void update_configuration ();
928     };
929
930   private:
931     ///////////////////////////////////////////////////////////////////////////
932     // MK_VIII::VoicePlayer (continued) ///////////////////////////////////////
933     ///////////////////////////////////////////////////////////////////////////
934
935     MK_VIII *mk;
936
937     SGSharedPtr<SGSampleGroup> _sgr;
938     Speaker speaker;
939
940     map< string, SGSharedPtr<SGSoundSample> >   samples;
941     vector<Voice *>                     _voices;
942
943     bool looped;
944     bool next_looped;
945
946     SGSoundSample *get_sample (const char *name);
947
948     inline void append (Voice *voice, Voice::Element *element) { voice->append(element); }
949     inline void append (Voice *voice, const char *sample_name) { voice->append(new Voice::SampleElement(get_sample(sample_name))); }
950     inline void append (Voice *voice, double silence) { voice->append(new Voice::SilenceElement(silence)); }
951
952     inline void make_voice (Voice **voice) { *voice = new Voice(this); _voices.push_back(*voice); }
953
954     template <class T1>
955     inline void make_voice (Voice **voice, T1 e1) { make_voice(voice); append(*voice, e1); }
956     template <class T1, class T2>
957     inline void make_voice (Voice **voice, T1 e1, T2 e2) { make_voice(voice, e1); append(*voice, e2); }
958     template <class T1, class T2, class T3>
959     inline void make_voice (Voice **voice, T1 e1, T2 e2, T3 e3) { make_voice(voice, e1, e2); append(*voice, e3); }
960     template <class T1, class T2, class T3, class T4>
961     inline void make_voice (Voice **voice, T1 e1, T2 e2, T3 e3, T4 e4) { make_voice(voice, e1, e2, e3); append(*voice, e4); }
962   };
963
964 private:
965   /////////////////////////////////////////////////////////////////////////////
966   // MK_VIII::SelfTestHandler /////////////////////////////////////////////////
967   /////////////////////////////////////////////////////////////////////////////
968
969   class SelfTestHandler
970   {
971     MK_VIII *mk;
972
973     typedef enum
974     {
975       CANCEL_NONE,
976       CANCEL_SHORT,
977       CANCEL_LONG
978     } Cancel;
979
980     enum
981     {
982       ACTION_SLEEP              = 1 << 0,
983       ACTION_VOICE              = 1 << 1,
984       ACTION_DISCRETE_ON_OFF    = 1 << 2,
985       ACTION_DONE               = 1 << 3
986     };
987
988     typedef struct
989     {
990       unsigned int      flags;
991       double            sleep_duration;
992       bool              *discrete;
993     } Action;
994
995     Cancel              cancel;
996     Action              action;
997     int                 current;
998     bool                button_pressed;
999     double              button_press_timestamp;
1000     IOHandler::Outputs  saved_outputs;
1001     double              sleep_start;
1002
1003     bool _was_here (int position);
1004
1005     Action sleep (double duration);
1006     Action play (VoicePlayer::Voice *voice);
1007     Action discrete_on (bool *discrete, double duration);
1008     Action discrete_on_off (bool *discrete, double duration);
1009     Action discrete_on_off (bool *discrete, VoicePlayer::Voice *voice);
1010     Action done ();
1011
1012     Action run ();
1013
1014     void start ();
1015     void stop ();
1016     void shutdown ();
1017
1018   public:
1019     typedef enum
1020     {
1021       STATE_NONE,
1022       STATE_START,
1023       STATE_RUNNING
1024     } State;
1025
1026     State state;
1027
1028     inline SelfTestHandler (MK_VIII *device)
1029       : mk(device), button_pressed(false), state(STATE_NONE) {}
1030
1031     inline void power_off () { stop(); }
1032     inline void set_inop () { stop(); }
1033     void handle_button_event (bool value);
1034     bool update ();
1035   };
1036
1037   /////////////////////////////////////////////////////////////////////////////
1038   // MK_VIII::AlertHandler ////////////////////////////////////////////////////
1039   /////////////////////////////////////////////////////////////////////////////
1040
1041   class AlertHandler
1042   {
1043     MK_VIII *mk;
1044
1045     unsigned int old_alerts;
1046     unsigned int voice_alerts;
1047     unsigned int repeated_alerts;
1048     VoicePlayer::Voice *altitude_callout_voice;
1049
1050     void reset ();
1051     inline bool has_alerts (unsigned int test) const { return (alerts & test) != 0; }
1052     inline bool has_old_alerts (unsigned int test) const { return (old_alerts & test) != 0; }
1053     inline bool must_play_voice (unsigned int test) const { return ! has_old_alerts(test) || (repeated_alerts & test) != 0; }
1054     bool select_voice_alerts (unsigned int test);
1055
1056   public:
1057     enum
1058     {
1059       ALERT_MODE1_PULL_UP                               = 1 << 0,
1060       ALERT_MODE1_SINK_RATE                             = 1 << 1,
1061
1062       ALERT_MODE2A_PREFACE                              = 1 << 2,
1063       ALERT_MODE2B_PREFACE                              = 1 << 3,
1064       ALERT_MODE2A                                      = 1 << 4,
1065       ALERT_MODE2B                                      = 1 << 5,
1066       ALERT_MODE2B_LANDING_MODE                         = 1 << 6,
1067       ALERT_MODE2A_ALTITUDE_GAIN                        = 1 << 7,
1068       ALERT_MODE2A_ALTITUDE_GAIN_TERRAIN_CLOSING        = 1 << 8,
1069
1070       ALERT_MODE3                                       = 1 << 9,
1071
1072       ALERT_MODE4_TOO_LOW_FLAPS                         = 1 << 10,
1073       ALERT_MODE4_TOO_LOW_GEAR                          = 1 << 11,
1074       ALERT_MODE4AB_TOO_LOW_TERRAIN                     = 1 << 12,
1075       ALERT_MODE4C_TOO_LOW_TERRAIN                      = 1 << 13,
1076
1077       ALERT_MODE5_SOFT                                  = 1 << 14,
1078       ALERT_MODE5_HARD                                  = 1 << 15,
1079
1080       ALERT_MODE6_MINIMUMS                              = 1 << 16,
1081       ALERT_MODE6_ALTITUDE_CALLOUT                      = 1 << 17,
1082       ALERT_MODE6_LOW_BANK_ANGLE_1                      = 1 << 18,
1083       ALERT_MODE6_HIGH_BANK_ANGLE_1                     = 1 << 19,
1084       ALERT_MODE6_LOW_BANK_ANGLE_2                      = 1 << 20,
1085       ALERT_MODE6_HIGH_BANK_ANGLE_2                     = 1 << 21,
1086       ALERT_MODE6_LOW_BANK_ANGLE_3                      = 1 << 22,
1087       ALERT_MODE6_HIGH_BANK_ANGLE_3                     = 1 << 23,
1088
1089       ALERT_TCF_TOO_LOW_TERRAIN                         = 1 << 24
1090     };
1091
1092     enum
1093     {
1094       ALERT_FLAG_REPEAT = 1 << 0
1095     };
1096
1097     unsigned int alerts;
1098
1099     inline AlertHandler (MK_VIII *device)
1100       : mk(device) {}
1101
1102     void boot ();
1103     void reposition ();
1104     void update ();
1105
1106     void set_alerts (unsigned int _alerts,
1107                      unsigned int flags = 0,
1108                      VoicePlayer::Voice *_altitude_callout_voice = NULL);
1109     void unset_alerts (unsigned int _alerts);
1110
1111     inline void repeat_alert (unsigned int alert) { set_alerts(alert, ALERT_FLAG_REPEAT); }
1112     inline void set_altitude_callout_alert (VoicePlayer::Voice *voice) { set_alerts(ALERT_MODE6_ALTITUDE_CALLOUT, 0, voice); }
1113   };
1114
1115   /////////////////////////////////////////////////////////////////////////////
1116   // MK_VIII::StateHandler ////////////////////////////////////////////////////
1117   /////////////////////////////////////////////////////////////////////////////
1118
1119   class StateHandler
1120   {
1121     MK_VIII *mk;
1122
1123     Timer potentially_airborne_timer;
1124
1125     void update_ground ();
1126     void enter_ground ();
1127     void leave_ground ();
1128
1129     void update_takeoff ();
1130     void enter_takeoff ();
1131     void leave_takeoff ();
1132
1133   public:
1134     bool ground;
1135     bool takeoff;
1136
1137     inline StateHandler (MK_VIII *device)
1138       : mk(device), ground(true), takeoff(true) {}
1139
1140     void post_reposition ();
1141     void update ();
1142   };
1143
1144   /////////////////////////////////////////////////////////////////////////////
1145   // MK_VIII::Mode1Handler ////////////////////////////////////////////////////
1146   /////////////////////////////////////////////////////////////////////////////
1147
1148   class Mode1Handler
1149   {
1150     MK_VIII *mk;
1151
1152     Timer pull_up_timer;
1153     Timer sink_rate_timer;
1154
1155     double sink_rate_tti;       // time-to-impact in minutes
1156
1157     double get_pull_up_bias ();
1158     bool is_pull_up ();
1159
1160     double get_sink_rate_bias ();
1161     bool is_sink_rate ();
1162     double get_sink_rate_tti ();
1163
1164     void update_pull_up ();
1165     void update_sink_rate ();
1166
1167   public:
1168     typedef struct
1169     {
1170       bool      flap_override_bias;
1171       int       min_agl;
1172       double    (*pull_up_min_agl1) (double vs);
1173       int       pull_up_max_agl1;
1174       double    (*pull_up_min_agl2) (double vs);
1175       int       pull_up_max_agl2;
1176     } EnvelopesConfiguration;
1177
1178     struct
1179     {
1180       const EnvelopesConfiguration *envelopes;
1181     } conf;
1182
1183     inline Mode1Handler (MK_VIII *device)
1184       : mk(device) {}
1185
1186     void update ();
1187   };
1188
1189   /////////////////////////////////////////////////////////////////////////////
1190   // MK_VIII::Mode2Handler ////////////////////////////////////////////////////
1191   /////////////////////////////////////////////////////////////////////////////
1192
1193   class Mode2Handler
1194   {
1195
1196     ///////////////////////////////////////////////////////////////////////////
1197     // MK_VIII::Mode2Handler::ClosureRateFilter ///////////////////////////////
1198     ///////////////////////////////////////////////////////////////////////////
1199
1200     class ClosureRateFilter
1201     {
1202       /////////////////////////////////////////////////////////////////////////
1203       // MK_VIII::Mode2Handler::ClosureRateFilter::PassFilter /////////////////
1204       /////////////////////////////////////////////////////////////////////////
1205
1206       class PassFilter
1207       {
1208         double a0;
1209         double a1;
1210         double b1;
1211
1212         double last_input;
1213         double last_output;
1214
1215       public:
1216         inline PassFilter (double _a0, double _a1, double _b1)
1217           : a0(_a0), a1(_a1), b1(_b1) {}
1218
1219         inline double filter (double input)
1220         {
1221           last_output = a0 * input + a1 * last_input + b1 * last_output;
1222           last_input = input;
1223
1224           return last_output;
1225         }
1226
1227         inline void reset ()
1228         {
1229           last_input = 0;
1230           last_output = 0;
1231         }
1232       };
1233
1234       /////////////////////////////////////////////////////////////////////////
1235       // MK_VIII::Mode2Handler::ClosureRateFilter (continued) /////////////////
1236       /////////////////////////////////////////////////////////////////////////
1237
1238       MK_VIII *mk;
1239
1240       Timer             timer;
1241       Parameter<double> last_ra;        // last radio altitude
1242       Parameter<double> last_ba;        // last barometric altitude
1243       PassFilter        ra_filter;      // radio altitude rate filter
1244       PassFilter        ba_filter;      // barometric altitude rate filter
1245
1246       double limit_radio_altitude_rate (double r);
1247
1248     public:
1249       Parameter<double> output;
1250
1251       inline ClosureRateFilter (MK_VIII *device)
1252         : mk(device),
1253           ra_filter(0.05, 0, 0.95),             // low-pass filter
1254           ba_filter(0.93, -0.93, 0.86) {}       // high-pass-filter
1255
1256       void init ();
1257       void update ();
1258     };
1259
1260     ///////////////////////////////////////////////////////////////////////////
1261     // MK_VIII::Mode2Handler (continued) //////////////////////////////////////
1262     ///////////////////////////////////////////////////////////////////////////
1263
1264     MK_VIII *mk;
1265
1266     ClosureRateFilter closure_rate_filter;
1267
1268     Timer takeoff_timer;
1269     Timer pull_up_timer;
1270
1271     double      a_start_time;
1272     Timer       a_altitude_gain_timer;
1273     double      a_altitude_gain_alt;
1274
1275     void check_pull_up (unsigned int preface_alert, unsigned int alert);
1276
1277     bool b_conditions ();
1278
1279     bool is_a ();
1280     bool is_b ();
1281
1282     void update_a ();
1283     void update_b ();
1284
1285   public:
1286     typedef struct
1287     {
1288       int airspeed1;
1289       int airspeed2;
1290     } Configuration;
1291
1292     const Configuration *conf;
1293
1294     inline Mode2Handler (MK_VIII *device)
1295       : mk(device), closure_rate_filter(device) {}
1296
1297     void boot ();
1298     void power_off ();
1299     void leave_ground ();
1300     void enter_takeoff ();
1301     void update ();
1302   };
1303
1304   /////////////////////////////////////////////////////////////////////////////
1305   // MK_VIII::Mode3Handler ////////////////////////////////////////////////////
1306   /////////////////////////////////////////////////////////////////////////////
1307
1308   class Mode3Handler
1309   {
1310     MK_VIII *mk;
1311
1312     bool        armed;
1313     bool        has_descent_alt;
1314     double      descent_alt;
1315     double      bias;
1316
1317     double max_alt_loss (double _bias);
1318     double get_bias (double initial_bias, double alt_loss);
1319     bool is (double *alt_loss);
1320
1321   public:
1322     typedef struct
1323     {
1324       int       min_agl;
1325       int       (*max_agl) (bool flap_override);
1326       double    (*max_alt_loss) (bool flap_override, double agl);
1327     } Configuration;
1328
1329     const Configuration *conf;
1330
1331     inline Mode3Handler (MK_VIII *device)
1332       : mk(device), armed(false), has_descent_alt(false) {}
1333
1334     void enter_takeoff ();
1335     void update ();
1336   };
1337
1338   /////////////////////////////////////////////////////////////////////////////
1339   // MK_VIII::Mode4Handler ////////////////////////////////////////////////////
1340   /////////////////////////////////////////////////////////////////////////////
1341
1342   class Mode4Handler
1343   {
1344   public:
1345     typedef struct
1346     {
1347       int       airspeed1;
1348       int       airspeed2;
1349       int       min_agl1;
1350       double    (*min_agl2) (double airspeed);
1351       int       min_agl3;
1352     } EnvelopesConfiguration;
1353
1354     typedef struct
1355     {
1356       const EnvelopesConfiguration *ac;
1357       const EnvelopesConfiguration *b;
1358     } ModesConfiguration;
1359
1360     struct
1361     {
1362       VoicePlayer::Voice        *voice_too_low_gear;
1363       const ModesConfiguration  *modes;
1364     } conf;
1365
1366     inline Mode4Handler (MK_VIII *device)
1367       : mk(device),ab_bias(0.0),ab_expanded_bias(0.0),c_bias(0.0) {}
1368
1369     double get_upper_agl (const EnvelopesConfiguration *c);
1370     void update ();
1371
1372   private:
1373     MK_VIII *mk;
1374
1375     double ab_bias;
1376     double ab_expanded_bias;
1377     double c_bias;
1378
1379     const EnvelopesConfiguration *get_ab_envelope ();
1380     double get_bias (double initial_bias, double min_agl);
1381     void handle_alert (unsigned int alert, double min_agl, double *bias);
1382
1383     void update_ab ();
1384     void update_ab_expanded ();
1385     void update_c ();
1386   };
1387
1388   /////////////////////////////////////////////////////////////////////////////
1389   // MK_VIII::Mode5Handler ////////////////////////////////////////////////////
1390   /////////////////////////////////////////////////////////////////////////////
1391
1392   class Mode5Handler
1393   {
1394     MK_VIII *mk;
1395
1396     Timer hard_timer;
1397     Timer soft_timer;
1398
1399     double soft_bias;
1400
1401     bool is_hard ();
1402     bool is_soft (double bias);
1403
1404     double get_soft_bias (double initial_bias);
1405
1406     void update_hard (bool is);
1407     void update_soft (bool is);
1408
1409   public:
1410     inline Mode5Handler (MK_VIII *device)
1411       : mk(device), soft_bias(0.0) {}
1412
1413     void update ();
1414   };
1415
1416   /////////////////////////////////////////////////////////////////////////////
1417   // MK_VIII::Mode6Handler ////////////////////////////////////////////////////
1418   /////////////////////////////////////////////////////////////////////////////
1419
1420   class Mode6Handler
1421   {
1422   public:
1423     // keep in sync with altitude_callout_definitions[]
1424     typedef enum
1425     {
1426       ALTITUDE_CALLOUT_1000,
1427       ALTITUDE_CALLOUT_500,
1428       ALTITUDE_CALLOUT_400,
1429       ALTITUDE_CALLOUT_300,
1430       ALTITUDE_CALLOUT_200,
1431       ALTITUDE_CALLOUT_100,
1432       ALTITUDE_CALLOUT_50,
1433       ALTITUDE_CALLOUT_40,
1434       ALTITUDE_CALLOUT_30,
1435       ALTITUDE_CALLOUT_20,
1436       ALTITUDE_CALLOUT_10
1437     } AltitudeCallout;
1438
1439     typedef bool (*BankAnglePredicate) (Parameter<double> *agl,
1440                                         double abs_roll_deg,
1441                                         bool ap_engaged);
1442
1443     struct
1444     {
1445       bool                      minimums_enabled;
1446       bool                      smart_500_enabled;
1447       VoicePlayer::Voice        *above_field_voice;
1448
1449       bool                      altitude_callouts_enabled[n_altitude_callouts];
1450       bool                      bank_angle_enabled;
1451       BankAnglePredicate        is_bank_angle;
1452     } conf;
1453
1454     static const int altitude_callout_definitions[];
1455
1456     inline Mode6Handler (MK_VIII *device)
1457       : mk(device) {}
1458
1459     void boot ();
1460     void power_off ();
1461     void enter_takeoff ();
1462     void leave_takeoff ();
1463     void set_volume (float volume);
1464     bool altitude_callouts_enabled ();
1465     void update ();
1466
1467   private:
1468     MK_VIII *mk;
1469
1470     bool                last_decision_height;
1471     Parameter<double>   last_radio_altitude;
1472     Parameter<double>   last_altitude_above_field;
1473
1474     bool altitude_callouts_issued[n_altitude_callouts];
1475     bool minimums_issued;
1476     bool above_field_issued;
1477
1478     Timer               runway_timer;
1479     Parameter<bool>     has_runway;
1480
1481     struct
1482     {
1483       double elevation;         // elevation in feet
1484     } runway;
1485
1486     void reset_minimums ();
1487     void reset_altitude_callouts ();
1488     bool is_playing_altitude_callout ();
1489     bool is_near_minimums (double callout);
1490     bool is_outside_band (double elevation, double callout);
1491     bool inhibit_smart_500 ();
1492
1493     void update_minimums ();
1494     void update_altitude_callouts ();
1495
1496     bool test_runway (const FGRunway *_runway);
1497     bool test_airport (const FGAirport *airport);
1498     void update_runway ();
1499
1500     void get_altitude_above_field (Parameter<double> *parameter);
1501     void update_above_field_callout ();
1502
1503     bool is_bank_angle (double abs_roll_angle, double bias);
1504     bool is_high_bank_angle ();
1505     unsigned int get_bank_angle_alerts ();
1506     void update_bank_angle ();
1507     
1508     class AirportFilter : public FGAirport::AirportFilter
1509     {
1510     public: 
1511       AirportFilter(Mode6Handler *s)
1512         : self(s) {}
1513         
1514       virtual bool passAirport(FGAirport *a) const;
1515       
1516       virtual FGPositioned::Type maxType() const {
1517         return FGPositioned::AIRPORT;
1518       }
1519       
1520     private:
1521       Mode6Handler* self;
1522     };
1523   };
1524
1525   /////////////////////////////////////////////////////////////////////////////
1526   // MK_VIII::TCFHandler //////////////////////////////////////////////////////
1527   /////////////////////////////////////////////////////////////////////////////
1528
1529   class TCFHandler
1530   {
1531     typedef struct
1532     {
1533       double latitude;          // latitude in degrees
1534       double longitude;         // longitude in degrees
1535     } Position;
1536
1537     typedef struct
1538     {
1539       Position  position;       // position of threshold
1540       double    heading;        // runway heading
1541     } RunwayEdge;
1542
1543     MK_VIII *mk;
1544
1545     static const double k;
1546
1547     Timer       runway_timer;
1548     bool        has_runway;
1549
1550     struct
1551     {
1552       Position          center;         // center point
1553       double            elevation;      // elevation in feet
1554       double            half_length;    // runway half length, in nautical miles
1555       RunwayEdge        edges[2];       // runway threshold and end
1556       Position          bias_area[4];   // vertices of the bias area
1557     } runway;
1558
1559     double bias;
1560     double *reference;
1561     double initial_value;
1562
1563     double get_azimuth_difference (double from_lat,
1564                                    double from_lon,
1565                                    double to_lat,
1566                                    double to_lon,
1567                                    double to_heading);
1568     double get_azimuth_difference (const FGRunway *_runway);
1569
1570     FGRunway* select_runway (const FGAirport *airport);
1571     void update_runway ();
1572
1573     void get_bias_area_edges (Position *edge,
1574                               double reciprocal,
1575                               double half_width_m,
1576                               Position *bias_edge1,
1577                               Position *bias_edge2);
1578
1579     bool is_inside_edge_triangle (RunwayEdge *edge);
1580     bool is_inside_bias_area ();
1581
1582     bool is_tcf ();
1583     bool is_rfcf ();
1584
1585     class AirportFilter : public FGAirport::AirportFilter
1586     {
1587     public: 
1588       AirportFilter(MK_VIII *device)
1589         : mk(device) {}
1590         
1591       virtual bool passAirport(FGAirport *a) const;
1592     private:
1593       MK_VIII* mk;
1594     };
1595   public:
1596     struct
1597     {
1598       bool enabled;
1599     } conf;
1600
1601     inline TCFHandler (MK_VIII *device)
1602       : mk(device) {}
1603
1604     void update ();
1605   };
1606
1607   /////////////////////////////////////////////////////////////////////////////
1608   // MK_VIII (continued) //////////////////////////////////////////////////////
1609   /////////////////////////////////////////////////////////////////////////////
1610
1611   string        name;
1612   int           num;
1613
1614   PowerHandler          power_handler;
1615   SystemHandler         system_handler;
1616   ConfigurationModule   configuration_module;
1617   FaultHandler          fault_handler;
1618   IOHandler             io_handler;
1619   VoicePlayer           voice_player;
1620   SelfTestHandler       self_test_handler;
1621   AlertHandler          alert_handler;
1622   StateHandler          state_handler;
1623   Mode1Handler          mode1_handler;
1624   Mode2Handler          mode2_handler;
1625   Mode3Handler          mode3_handler;
1626   Mode4Handler          mode4_handler;
1627   Mode5Handler          mode5_handler;
1628   Mode6Handler          mode6_handler;
1629   TCFHandler            tcf_handler;
1630
1631   struct
1632   {
1633     int runway_database;
1634   } conf;
1635
1636 public:
1637   MK_VIII (SGPropertyNode *node);
1638
1639   virtual void init ();
1640   virtual void bind ();
1641   virtual void unbind ();
1642   virtual void update (double dt);
1643 };
1644
1645 #ifdef _MSC_VER
1646 #  pragma warning( pop )
1647 #endif
1648
1649 #endif // __INSTRUMENTS_MK_VIII_HXX