1 // kln89_page.hxx - a class to manage the simulation of a KLN89
2 //                  GPS unit.  Note that this is primarily the
3 //                  simulation of the user interface and display
4 //                  - the core GPS calculations such as position
5 //                  and waypoint sequencing are done (or should
6 //                  be done) by FG code.
7 //
8 // Written by David Luff, started 2005.
9 //
10 // Copyright (C) 2005 - David C Luff - daveluff AT ntlworld.com
11 //
12 // This program is free software; you can redistribute it and/or
13 // modify it under the terms of the GNU General Public License as
14 // published by the Free Software Foundation; either version 2 of the
15 // License, or (at your option) any later version.
16 //
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 // General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with this program; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
25 //
26 // $Id$
27 
28 #ifndef _KLN89_HXX
29 #define _KLN89_HXX
30 
31 #include <Instrumentation/dclgps.hxx>
32 #include "kln89_page.hxx"
33 
34 class KLN89Page;
35 
36 const int KLN89MapScales[2][21] = {{1, 2, 3, 5, 7, 10, 12, 15, 17, 20, 25, 30, 40, 60, 80, 100, 120, 160, 240, 320, 500},
37                                    {2, 4, 6, 9, 13, 18, 22, 28, 32, 37, 46, 55, 75, 110, 150, 185, 220, 300, 440, 600, 925}};
38 
39 enum KLN89Mode {
40     KLN89_MODE_DISP,
41     KLN89_MODE_CRSR
42 };
43 
44 enum KLN89DistanceUnits {
45     GPS_DIST_UNITS_NM = 0,
46     GPS_DIST_UNITS_KM
47 };
48 
49 enum KLN89SpeedUnits {
50     GPS_VEL_UNITS_KT,
51     GPS_VEL_UNITS_KPH
52 };
53 
54 enum KLN89AltitudeUnits {
55     GPS_ALT_UNITS_FT,
56     GPS_ALT_UNITS_M
57 };
58 
59 enum KLN89PressureUnits {
60     GPS_PRES_UNITS_IN = 1,
61     GPS_PRES_UNITS_MB,
62     GPS_PRES_UNITS_HP
63 };
64 
65 /*
66 const char* KLN89TimeCodes[20] = { "UTC", "GST", "GDT", "ATS", "ATD", "EST", "EDT", "CST", "CDT", "MST",
67                                    "MDT", "PST", "PDT", "AKS", "AKD", "HAS", "HAD", "SST", "SDT", "LCL" };
68 */
69 
70 // Used for storing airport town and county mapped by ID, since currently FG does not store this
71 typedef std::map<std::string, std::string> airport_id_str_map_type;
72 typedef airport_id_str_map_type::iterator airport_id_str_map_iterator;
73 
74 typedef std::vector<KLN89Page*> kln89_page_list_type;
75 typedef kln89_page_list_type::iterator kln89_page_list_itr;
76 
77 class KLN89 : public DCLGPS
78 {
79     friend class KLN89Page;
80     friend class KLN89AptPage;
81     friend class KLN89VorPage;
82     friend class KLN89NDBPage;
83     friend class KLN89IntPage;
84     friend class KLN89UsrPage;
85     friend class KLN89ActPage;
86     friend class KLN89NavPage;
87     friend class KLN89FplPage;
88     friend class KLN89CalPage;
89     friend class KLN89SetPage;
90     friend class KLN89OthPage;
91     friend class KLN89AltPage;
92     friend class KLN89DirPage;
93     friend class KLN89NrstPage;
94 
95 public:
96     KLN89(RenderArea2D* instrument);
97     ~KLN89();
98 
99     // Subsystem API.
100     void bind() override;
101     void init() override;
102     void unbind() override;
103     void update(double dt) override;
104 
105     // Subsystem identification.
staticSubsystemClassId()106     static const char* staticSubsystemClassId() { return "KLN89"; }
107 
108     // Set Units
109     // m if true, ft if false
SetAltUnitsSI(bool b)110     inline void SetAltUnitsSI(bool b) { _altUnits = (b ? GPS_ALT_UNITS_M : GPS_ALT_UNITS_FT); }
111     // Returns true if alt units are SI (m), false if ft
GetAltUnitsSI()112     inline bool GetAltUnitsSI() { return(_altUnits == GPS_ALT_UNITS_M ? true : false); }
113     // km and k/h if true, nm and kt if false
SetDistVelUnitsSI(bool b)114     inline void SetDistVelUnitsSI(bool b) { _distUnits = (b ? GPS_DIST_UNITS_KM : GPS_DIST_UNITS_NM); _velUnits = (b ? GPS_VEL_UNITS_KPH : GPS_VEL_UNITS_KT); }
115     // Returns true if dist/vel units are SI
GetDistVelUnitsSI()116     inline bool GetDistVelUnitsSI() { return(_distUnits == GPS_DIST_UNITS_KM && _velUnits == GPS_VEL_UNITS_KPH ? true : false); }
117     // Set baro units - 1 = in, 2 = mB, 3 = hP   Wrapping if for the convienience of the GPS setter.
118     void SetBaroUnits(int n, bool wrap = false);
119     // Get baro units: 1 = in, 2 = mB, 3 = hP
GetBaroUnits()120     inline int GetBaroUnits() { return((int)_baroUnits); }
121 
SetTurnAnticipation(bool b)122     inline void SetTurnAnticipation(bool b) { _turnAnticipationEnabled = b; }
GetTurnAnticipation()123     inline bool GetTurnAnticipation() { return(_turnAnticipationEnabled); }
124 
SetSuaAlertEnabled(bool b)125     inline void SetSuaAlertEnabled(bool b) { _suaAlertEnabled = b; }
GetSuaAlertEnabled()126     inline bool GetSuaAlertEnabled() { return(_suaAlertEnabled); }
127 
SetAltAlertEnabled(bool b)128     inline void SetAltAlertEnabled(bool b) { _altAlertEnabled = b; }
GetAltAlertEnabled()129     inline bool GetAltAlertEnabled() { return(_altAlertEnabled); }
130 
131     void SetMinDisplayBrightness(int n);    // Set minDisplayBrightness (between 1 and 9)
132     void DecrementMinDisplayBrightness();   // Decrease by 1
133     void IncrementMinDisplayBrightness();   // Increase by 1
GetMinDisplayBrightness()134     inline int GetMinDisplayBrightness() { return(_minDisplayBrightness); }
135 
GetMsgAlert() const136     inline bool GetMsgAlert() const { return(!_messageStack.empty()); }
137 
138     void Knob1Right1();
139     void Knob1Left1();
140     void Knob2Right1();
141     void Knob2Left1();
142     void CrsrPressed();
143     void EntPressed();
144     void ClrPressed();
145     void DtoPressed();
146     void NrstPressed();
147     void AltPressed();
148     void OBSPressed();
149     void MsgPressed();
150 
151     void CreateDefaultFlightPlans();
152 
153 private:
154     void ToggleOBSMode();
155 
156     // Initiate Direct To operation to the supplied ID.
157     void DtoInitiate(const std::string& id);
158 
159     //----------------------- Drawing functions which take CHARACTER units -------------------------
160     // Render string s in display field field at position x, y
161     // WHERE POSITION IS IN CHARACTER UNITS!
162     // zero y at bottom?
163     // invert: -1 => no inversion, 0 -> n => 1 char - s[invert] gets inverted, 99 => entire string gets inverted
164     void DrawText(const std::string& s, int field, int px, int py, bool bold = false, int invert = -1);
165 
166     void DrawLatitude(double d, int field, int px, int py);
167     void DrawLongitude(double d, int field, int px, int py);
168 
169     // Draw a frequency as xxx.xx
170     void DrawFreq(double d, int field, int px, int py);
171 
172     // Draw a time in seconds as hh:mm
173     // NOTE: px is RIGHT JUSTIFIED!
174     void DrawTime(double time, int field, int px, int py);
175 
176     // Draw an integer heading, where px specifies the position of the degrees sign at the RIGHT of the value.
177     void DrawHeading(int h, int field, int px, int py);
178 
179     // Draw a distance spec'd as nm as an integer (TODO - may need 1 decimal place if < 100) where px specifies RHS of units.
180     // Some uses definately don't want decimal place though (as at present), so would have to be arg.
181     void DrawDist(double d, int field, int px, int py);
182 
183     // Draw a speed specifed in knots.  px is RHS of the units.  Can draw up to 2 decimal places.
184     void DrawSpeed(double v, int field, int px, int py, int decimals = 0);
185 
186     void Underline(int field, int px, int py, int len);
187 
188     // Render a char at a given position as above (position in CHARACTER units)
189     void DrawChar(char c, int field, int px, int py, bool bold = false, bool invert = false);
190     void DrawSpecialChar(char c, int field, int cx, int cy, bool bold = false);
191 
192     // Draws the dir/dist field at the bottom of the main field
193     void DrawDirDistField(double lat, double lon, int field, int px, int py, bool to_flag = true, bool cursel = false);
194     //
195     //--------------------------------- end char units -----------------------------------------------
196 
197     //----------------------- Drawing functions which take PIXEL units ------------------------------
198     //
199     // Takes instrument *pixel* co-ordinates NOT character units
200     // Position is specified by the bottom of the *visible* portion, by default the left position unless align_right is true.
201     // The return value is the pixel width of the visible portion
202     int DrawSmallChar(char c, int x, int y, bool align_right = false);
203 
204     void DrawFreeChar(char c, int x, int y, bool draw_background = false);
205     //
206     //----------------------------------- end pixel unit functions -----------------------------------
207 
208     void DrawDivider();
209 
210     void DrawEnt(int field = 1, int px = 0, int py = 1);
211 
212     void DrawMessageAlert();
213 
214     void DrawKPH(int field, int cx, int cy);
215 
216     void DrawDTO(int field, int cx, int cy);
217 
218     // Draw the bar that indicates which page we're on (zero-based)
219     void DrawBar(int page);
220 
221     void DrawCDI();
222 
223     void DrawLegTail(int py);
224     void DrawLongLegTail(int py);
225     void DrawHalfLegTail(int py);
226 
227     void UpdateMapHeading();
228 
229     // Draw the moving map
230     // Apt, VOR and SUA drawing can be suspended by setting draw_avs to false, without affecting the stored drawing preference state.
231     void DrawMap(bool draw_avs = true);
232 
233     // Set whether the display should be drawn pixelated (more primitives, but might be closer to real-life)
234     // or not (in which case it is assumed that pixels are square and can be merged into quads).
235     bool _pixelated;
236 
237     // Flashing output should be hidden when blink is true
238     bool _blink;
239 
240     double _cum_dt;
241 
242     // In Crsr mode, CRSR pressed events are passed to the active page, in disp mode they change which page is active
243     KLN89Mode _mode;
244     // And the facility to save a mode
245     KLN89Mode _lastMode;
246 
247     // Increment/Decrement a character in the KLN89 A-Z,0-9 scheme.
248     // Set gap to true to get a space between A and 9 when wrapping, set wrap to false to disable wrap.
249     char IncChar(char c, bool gap = false, bool wrap = true);
250     char DecChar(char c, bool gap = false, bool wrap = true);
251 
252     // ==================== Page organisation stuff =============
253     // The list of cyclical pages that the user can cycle through
254     kln89_page_list_type _pages;
255 
256     // The currently active page
257     KLN89Page* _activePage;
258     // And a facility to save the immediately preceding active page
259     KLN89Page* _lastActivePage;
260 
261     // Ugly hack.  Housekeeping to allow us to temporarily display one page, while remembering which
262     // other page to "jump" back to.  Used when the waypoint pages are used to review waypoint entry
263     // from the flightplan page.
264     int _entJump;   // The page to jump back to if ENT is pressed.  -1 indicates no jump.
265     int _clrJump;   // The page to jump back to if CLR is pressed.  -1 indicates no jump.
266     bool _jumpRestoreCrsr;  // Indicates that jump back at this point should restore cursor mode.
267 
268     // Misc pages that aren't in the cyclic list.
269     // ALT
270     KLN89Page* _alt_page;
271     // Direct To
272     KLN89Page* _dir_page;
273     // Nearest
274     KLN89Page* _nrst_page;
275     // ====================== end of page stuff ===================
276 
277     // Moving-map display stuff
278     int _mapOrientation;    // 0 => North (true) up, 1 => DTK up, 2 => TK up, 3 => heading up (only when connected to external heading source).
279     double _mapHeading;     // Degrees.  The actual map heading gets updated at a lower frequency than DrawMap() is called at, hence we need to store it.
280     double _mapHeadingUpdateTimer;  // Timer to determine when to update the above.
281     bool _mapScaleAuto;     // Indicates that map should autoscale when true.
282     int _mapScaleIndex;     // Index into array of available map scales.
283     int _mapScaleUnits;     // 0 => nm, 1 => km.
284     double _mapScale;   // nm or km from aircraft position to top of map.
285                         // Note that aircraft position differs depending on orientation, but 'scale' retains the same meaning,
286                         // so the scale per pixel alters to suit the defined scale when the rendered aircraft position changes.
287     bool _drawSUA;  // special user airspace
288     bool _drawVOR;
289     bool _drawApt;
290 
291     // Convert map to instrument coordinates
292     void MapToInstrument(int &x, int &y);
293 
294     // The following map drawing functions all take MAP co-ordinates, NOT instrument co-ordinates!
295 
296     // Draw the diamond style of user pos
297     void DrawUser1(int x, int y);
298 
299     // Draw the airplane style of user pos
300     void DrawUser2(int x, int y);
301 
302     // Draw an airport symbol on the moving map
303     void DrawApt(int x, int y);
304 
305     // Draw a waypoint on the moving map
306     void DrawWaypoint(int x, int y);
307 
308     // Draw a VOR on the moving map
309     void DrawVOR(int x, int y);
310 
311     // Draw an airport or waypoint label on the moving map
312     // Specify position by the map pixel co-ordinate of the left or right, bottom, of the *visible* portion of the label.
313     // The black background quad will automatically overlap this by 1 pixel.
314     void DrawLabel(const std::string& s, int x1, int y1, bool right_align = false);
315 
316     int GetLabelQuadrant(double h);
317     int GetLabelQuadrant(double h1, double h2);
318 
319     // Draw a line on the moving map
320     void DrawLine(int x1, int y1, int x2, int y2);
321 
322     // Draw normal sized text on the moving map
323     void DrawMapText(const std::string& s, int x, int y, bool draw_background = false);
324 
325     void DrawMapUpArrow(int x, int y);
326 
327     // Draw a Quad on the moving map
328     void DrawMapQuad(int x1, int y1, int x2, int y2, bool invert = false);
329 
330     // Airport town and state mapped by ID, since currently FG does not store this
331     airport_id_str_map_type _airportTowns;
332     airport_id_str_map_type _airportStates;
333 
334     // NOTE - It is a deliberate decision not to have a proper message page class,
335     // since button events get directed to the page that was active before the
336     // message was displayed, not the message page itself.
337     bool _dispMsg;  // Set true while the message page is being displayed
338 
339     // Sometimes the datapages can be used to review a waypoint whilst the user makes a decision,
340     // and we need to remember why.
341     bool _dtoReview;    // Set true when we a reviewing a waypoint for DTO operation.
342 
343     // Configuration settings that the user can set via. the KLN89 SET pages.
344     KLN89SpeedUnits _velUnits;
345     KLN89DistanceUnits _distUnits;
346     KLN89PressureUnits _baroUnits;
347     KLN89AltitudeUnits _altUnits;
348     bool _suaAlertEnabled;      // Alert user to potential SUA entry
349     bool _altAlertEnabled;      // Alert user to min safe alt violation
350     int _minDisplayBrightness;  // Minimum display brightness in low light.
351     char _defaultFirstChar;     // Default first waypoint character.
352 
353     // The user-settable barometric pressure.
354     // This can be set in the range 22.00 -> 32.99", or 745 -> 1117mB/hPa.
355     // For user input, we maintain a single integer value that is either between 2200 and 3299 (")
356     // or between 745 and 1117 (mB/hPa).  It gets converted from one to the other only when the
357     // units are changed.
358     // For internal VNAV calculations (which we don't currently do) this will be converted to a floating
359     // point value before use.
360     int _userBaroSetting;
361 };
362 
363 #endif  // _KLN89_HXX
364