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