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