Debian Codegraph @

src/Instrumentation/KLN89/kln89.cxx

raw symbols SCIP
1 /*
2 * SPDX-FileName: kln89.cxx
3 * SPDX-FileComment: <text>a class to manage the simulation of a KLN89
4 * GPS unit. Note that this is primarily the
5 * simulation of the user interface and display
6 * - the core GPS calculations such as position
7 * and waypoint sequencing are done (or should
8 * be done) by FG code.</text>
9 * SPDX-FileCopyrightText: Copyright (C) 2005 - David C Luff - daveluff AT ntlworld.com
10 * SPDX-License-Identifier: GPL-2.0-or-later
11 */
12
13 #include "kln89.hxx"
14 #include "kln89_page.hxx"
15 #include "kln89_page_apt.hxx"
16 #include "kln89_page_vor.hxx"
17 #include "kln89_page_ndb.hxx"
18 #include "kln89_page_int.hxx"
19 #include "kln89_page_usr.hxx"
20 #include "kln89_page_act.hxx"
21 #include "kln89_page_nav.hxx"
22 #include "kln89_page_fpl.hxx"
23 #include "kln89_page_cal.hxx"
24 #include "kln89_page_set.hxx"
25 #include "kln89_page_oth.hxx"
26 #include "kln89_page_alt.hxx"
27 #include "kln89_page_dir.hxx"
28 #include "kln89_page_nrst.hxx"
29 #include "kln89_symbols.hxx"
30 #include <iostream>
31
32 #include <Main/fg_props.hxx>
33 #include <simgear/structure/commands.hxx>
34 #include <Airports/airport.hxx>
35
36 #include <cstdio>
37
38 using std::cout;
39 using std::string;
40
41 // Command callbacks for FlightGear
42
43 static bool do_kln89_msg_pressed(const SGPropertyNode * arg, SGPropertyNode * root) {
44 //cout << "do_kln89_msg_pressed called!\n";
45 auto gps = globals->get_subsystem<KLN89>();
46 gps->MsgPressed();
47 return(true);
48 }
49
50 static bool do_kln89_obs_pressed(const SGPropertyNode * arg, SGPropertyNode * root) {
51 //cout << "do_kln89_obs_pressed called!\n";
52 auto gps = globals->get_subsystem<KLN89>();
53 gps->OBSPressed();
54 return(true);
55 }
56
57 static bool do_kln89_alt_pressed(const SGPropertyNode * arg, SGPropertyNode * root) {
58 //cout << "do_kln89_alt_pressed called!\n";
59 auto gps = globals->get_subsystem<KLN89>();
60 gps->AltPressed();
61 return(true);
62 }
63
64 static bool do_kln89_nrst_pressed(const SGPropertyNode * arg, SGPropertyNode * root) {
65 auto gps = globals->get_subsystem<KLN89>();
66 gps->NrstPressed();
67 return(true);
68 }
69
70 static bool do_kln89_dto_pressed(const SGPropertyNode * arg, SGPropertyNode * root) {
71 auto gps = globals->get_subsystem<KLN89>();
72 gps->DtoPressed();
73 return(true);
74 }
75
76 static bool do_kln89_clr_pressed(const SGPropertyNode * arg, SGPropertyNode * root) {
77 auto gps = globals->get_subsystem<KLN89>();
78 gps->ClrPressed();
79 return(true);
80 }
81
82 static bool do_kln89_ent_pressed(const SGPropertyNode * arg, SGPropertyNode * root) {
83 auto gps = globals->get_subsystem<KLN89>();
84 gps->EntPressed();
85 return(true);
86 }
87
88 static bool do_kln89_crsr_pressed(const SGPropertyNode * arg, SGPropertyNode * root) {
89 auto gps = globals->get_subsystem<KLN89>();
90 gps->CrsrPressed();
91 return(true);
92 }
93
94 static bool do_kln89_knob1left1(const SGPropertyNode * arg, SGPropertyNode * root) {
95 auto gps = globals->get_subsystem<KLN89>();
96 gps->Knob1Left1();
97 return(true);
98 }
99
100 static bool do_kln89_knob1right1(const SGPropertyNode * arg, SGPropertyNode * root) {
101 auto gps = globals->get_subsystem<KLN89>();
102 gps->Knob1Right1();
103 return(true);
104 }
105
106 static bool do_kln89_knob2left1(const SGPropertyNode * arg, SGPropertyNode * root) {
107 auto gps = globals->get_subsystem<KLN89>();
108 gps->Knob2Left1();
109 return(true);
110 }
111
112 static bool do_kln89_knob2right1(const SGPropertyNode * arg, SGPropertyNode * root) {
113 auto gps = globals->get_subsystem<KLN89>();
114 gps->Knob2Right1();
115 return(true);
116 }
117
118 // End command callbacks
119
120 KLN89::KLN89(RenderArea2D* instrument)
121 : DCLGPS(instrument) {
122 _mode = KLN89_MODE_DISP;
123 _blink = false;
124 _cum_dt = 0.0;
125 _nFields = 2;
126 _maxFields = 2;
127 _xBorder = 0;
128 _yBorder = 4;
129 // ..Field..[0] => no fields in action
130 _xFieldBorder[0] = 0;
131 _xFieldBorder[1] = 0;
132 _yFieldBorder[0] = 0;
133 _yFieldBorder[1] = 0;
134 _xFieldBorder[2] = 2;
135 _yFieldBorder[2] = 0;
136 _xFieldStart[0] = 0;
137 _xFieldStart[1] = 0;
138 _xFieldStart[2] = 45;
139 _yFieldStart[0] = 0;
140 _yFieldStart[1] = 0;
141 _yFieldStart[2] = 0;
142
143 //_pixelated = true;
144 _pixelated = false;
145
146 // Cyclic pages
147 _pages.clear();
148 KLN89Page* apt_page = new KLN89AptPage(this);
149 _pages.push_back(apt_page);
150 KLN89Page* vor_page = new KLN89VorPage(this);
151 _pages.push_back(vor_page);
152 KLN89Page* ndb_page = new KLN89NDBPage(this);
153 _pages.push_back(ndb_page);
154 KLN89Page* int_page = new KLN89IntPage(this);
155 _pages.push_back(int_page);
156 KLN89Page* usr_page = new KLN89UsrPage(this);
157 _pages.push_back(usr_page);
158 KLN89Page* act_page = new KLN89ActPage(this);
159 _pages.push_back(act_page);
160 KLN89Page* nav_page = new KLN89NavPage(this);
161 _pages.push_back(nav_page);
162 KLN89Page* fpl_page = new KLN89FplPage(this);
163 _pages.push_back(fpl_page);
164 KLN89Page* cal_page = new KLN89CalPage(this);
165 _pages.push_back(cal_page);
166 KLN89Page* set_page = new KLN89SetPage(this);
167 _pages.push_back(set_page);
168 KLN89Page* oth_page = new KLN89OthPage(this);
169 _pages.push_back(oth_page);
170 _nPages = _pages.size();
171 _curPage = 0;
172
173 // Other pages
174 _alt_page = new KLN89AltPage(this);
175 _dir_page = new KLN89DirPage(this);
176 _nrst_page = new KLN89NrstPage(this);
177
178 _activePage = apt_page;
179 _obsMode = false;
180 _dto = false;
181 _fullLegMode = true;
182 _obsHeading = 215;
183
184 // User-settable configuration. Eventually this should be user-achivable in order that settings can be persistent between sessions.
185 _altUnits = GPS_ALT_UNITS_FT;
186 _baroUnits = GPS_PRES_UNITS_IN;
187 _velUnits = GPS_VEL_UNITS_KT;
188 _distUnits = GPS_DIST_UNITS_NM;
189 _suaAlertEnabled = false;
190 _altAlertEnabled = false;
191 _minDisplayBrightness = 4;
192 _defaultFirstChar = 'A';
193
194 if(_baroUnits == GPS_PRES_UNITS_IN) {
195 _userBaroSetting = 2992;
196 } else {
197 _userBaroSetting = 1013;
198 }
199
200 _maxFlightPlans = 26;
201 for(unsigned int i=0; i<_maxFlightPlans; ++i) {
202 GPSFlightPlan* fp = new GPSFlightPlan;
203 fp->waypoints.clear();
204 _flightPlans.push_back(fp);
205 }
206 _activeFP = _flightPlans[0];
207
208 _entJump = _clrJump = -1;
209 _jumpRestoreCrsr = false;
210
211 _dispMsg = false;
212
213 _dtoReview = false;
214
215 // Moving map stuff
216 _mapOrientation = 0;
217 _mapHeading = 0.0;
218 _mapHeadingUpdateTimer = 0.0;
219 _drawSUA = false;
220 _drawVOR = false;
221 _drawApt = true;
222 //_mapScaleIndex = 20;
223 _mapScaleIndex = 7; // I think that the above is more accurate for no-flightplan default, but this is more sane for initial testing!
224 _mapScaleAuto = true;
225
226 // Mega-hack - hardwire airport town and state names for the FG base area since we don't have any data for these at the moment
227 // TODO: do this better one day!
228 _airportTowns["KSFO"] = "San Francisco";
229 _airportTowns["KSQL"] = "San Carlos";
230 _airportTowns["KPAO"] = "Palo Alto";
231 _airportTowns["KNUQ"] = "Mountain View";
232 _airportTowns["KSJC"] = "San Jose";
233 _airportTowns["KRHV"] = "San Jose";
234 _airportTowns["E16"] = "San Martin";
235 _airportTowns["KWVI"] = "Watsonville";
236 _airportTowns["KOAK"] = "Oakland";
237 _airportTowns["KHWD"] = "Hayward";
238 _airportTowns["KLVK"] = "Livermore";
239 _airportTowns["KCCR"] = "Concord";
240 _airportTowns["KTCY"] = "Tracy";
241 _airportTowns["KSCK"] = "Stockton";
242 _airportTowns["KHAF"] = "Half Moon Bay";
243
244 _airportStates["KSFO"] = "CA";
245 _airportStates["KSQL"] = "CA";
246 _airportStates["KPAO"] = "CA";
247 _airportStates["KNUQ"] = "CA";
248 _airportStates["KSJC"] = "CA";
249 _airportStates["KRHV"] = "CA";
250 _airportStates["E16"] = "CA";
251 _airportStates["KWVI"] = "CA";
252 _airportStates["KOAK"] = "CA";
253 _airportStates["KHWD"] = "CA";
254 _airportStates["KLVK"] = "CA";
255 _airportStates["KCCR"] = "CA";
256 _airportStates["KTCY"] = "CA";
257 _airportStates["KSCK"] = "CA";
258 _airportStates["KHAF"] = "CA";
259 }
260
261 KLN89::~KLN89() {
262 for(unsigned int i=0; i<_pages.size(); ++i) {
263 delete _pages[i];
264 }
265
266 delete _alt_page;
267 delete _dir_page;
268 delete _nrst_page;
269
270 for(unsigned int i=0; i<_maxFlightPlans; ++i) {
271 ClearFlightPlan(i);
272 delete _flightPlans[i];
273 }
274 }
275
276 void KLN89::bind() {
277 fgTie("/instrumentation/gps/message-alert", this, &KLN89::GetMsgAlert);
278 DCLGPS::bind();
279 }
280
281 void KLN89::unbind() {
282 fgUntie("/instrumentation/gps/message-alert");
283 DCLGPS::unbind();
284 }
285
286 void KLN89::init() {
287 globals->get_commands()->addCommand("kln89_msg_pressed", do_kln89_msg_pressed);
288 globals->get_commands()->addCommand("kln89_obs_pressed", do_kln89_obs_pressed);
289 globals->get_commands()->addCommand("kln89_alt_pressed", do_kln89_alt_pressed);
290 globals->get_commands()->addCommand("kln89_nrst_pressed", do_kln89_nrst_pressed);
291 globals->get_commands()->addCommand("kln89_dto_pressed", do_kln89_dto_pressed);
292 globals->get_commands()->addCommand("kln89_clr_pressed", do_kln89_clr_pressed);
293 globals->get_commands()->addCommand("kln89_ent_pressed", do_kln89_ent_pressed);
294 globals->get_commands()->addCommand("kln89_crsr_pressed", do_kln89_crsr_pressed);
295 globals->get_commands()->addCommand("kln89_knob1left1", do_kln89_knob1left1);
296 globals->get_commands()->addCommand("kln89_knob1right1", do_kln89_knob1right1);
297 globals->get_commands()->addCommand("kln89_knob2left1", do_kln89_knob2left1);
298 globals->get_commands()->addCommand("kln89_knob2right1", do_kln89_knob2right1);
299
300 DCLGPS::init();
301 }
302
303 void KLN89::update(double dt) {
304 // Run any positional calc's required first
305 DCLGPS::update(dt);
306
307 // Set the display brightness. This should be reduced in response to falling light
308 // (i.e. nighttime), or the user covering the photocell that detects the light level.
309 // At the moment I don't know how to detect nighttime or actual light level, so only
310 // respond to the photocell being obscured.
311 // TODO: reduce the brightness in response to nighttime / lowlight.
312 float rgba[4] = {1.0, 0.0, 0.0, 1.0};
313 if(fgGetBool("/instrumentation/kln89/photocell-obscured")) {
314 rgba[0] -= (9 - _minDisplayBrightness) * 0.05;
315 }
316 _instrument->SetPixelColor(rgba);
317
318 _cum_dt += dt;
319 if(_blink) {
320 if(_cum_dt > 0.2) {
321 _cum_dt = 0.0;
322 _blink = false;
323 }
324 } else {
325 if(_cum_dt > 0.8) {
326 _cum_dt = 0.0;
327 _blink = true;
328 }
329 }
330
331 _mapHeadingUpdateTimer += dt;
332 if(_mapHeadingUpdateTimer > 1.0) {
333 UpdateMapHeading();
334 _mapHeadingUpdateTimer = 0.0;
335 }
336
337 _instrument->Flush();
338 _instrument->DrawBackground();
339
340 if(_dispMsg) {
341 if(_messageStack.empty()) {
342 DrawText("No Message", 0, 5, 2);
343 } else {
344 // TODO: parse the message string for special strings that indicate degrees signs etc!
345 DrawText(*_messageStack.begin(), 0, 0, 3);
346 }
347 return;
348 } else {
349 if(!_messageStack.empty()) {
350 DrawMessageAlert();
351 }
352 }
353
354 // Draw the indicator that shows which page we are on.
355 if(_curPage == 6 && _activePage->GetSubPage() == 3) {
356 // Don't draw the bar on the nav-4 page
357 } else if((_activePage != _nrst_page) && (_activePage != _dir_page) && (_activePage != _alt_page) && (!_dispMsg)) {
358 // Don't draw the bar on the NRST, DTO or MSG pages
359 DrawBar(_curPage);
360 }
361
362 _activePage->Update(dt);
363 }
364
365 void KLN89::CreateDefaultFlightPlans() {
366 // TODO: read these in from preferences.xml or similar instead!!!!
367 // Create some hardwired default flightplans for testing.
368 std::vector<std::string> ids;
369 std::vector<GPSWpType> wps;
370
371 ids.clear();
372 wps.clear();
373 ids.push_back("KLSN");
374 wps.push_back(GPS_WP_APT);
375 ids.push_back("VOLTA");
376 wps.push_back(GPS_WP_INT);
377 ids.push_back("C83");
378 wps.push_back(GPS_WP_APT);
379 CreateFlightPlan(_flightPlans[5], ids, wps);
380
381 ids.clear();
382 wps.clear();
383 ids.push_back("KCCR");
384 wps.push_back(GPS_WP_APT);
385 ids.push_back("KHAF");
386 wps.push_back(GPS_WP_APT);
387 CreateFlightPlan(_flightPlans[4], ids, wps);
388
389 ids.clear();
390 wps.clear();
391 ids.push_back("KLVK");
392 wps.push_back(GPS_WP_APT);
393 ids.push_back("OAK");
394 wps.push_back(GPS_WP_VOR);
395 ids.push_back("PORTE");
396 wps.push_back(GPS_WP_INT);
397 ids.push_back("KHAF");
398 wps.push_back(GPS_WP_APT);
399 CreateFlightPlan(_flightPlans[3], ids, wps);
400
401 ids.clear();
402 wps.clear();
403 ids.push_back("KDPA");
404 wps.push_back(GPS_WP_APT);
405 ids.push_back("OBK");
406 wps.push_back(GPS_WP_VOR);
407 ids.push_back("ENW");
408 wps.push_back(GPS_WP_VOR);
409 ids.push_back("KRAC");
410 wps.push_back(GPS_WP_APT);
411 CreateFlightPlan(_flightPlans[2], ids, wps);
412 //cout << "Size of FP2 WP list is " << _flightPlans[2]->waypoints.size() << '\n';
413
414 ids.clear();
415 wps.clear();
416 ids.push_back("KSFO");
417 ids.push_back("KOAK");
418 wps.push_back(GPS_WP_APT);
419 wps.push_back(GPS_WP_APT);
420 CreateFlightPlan(_flightPlans[1], ids, wps);
421
422 ids.clear();
423 wps.clear();
424 //ids.push_back("KOSH");
425 ids.push_back("KSFO");
426 ids.push_back("KHAF");
427 ids.push_back("OSI");
428 ids.push_back("KSQL");
429 //ids.push_back("KPAO");
430 //ids.push_back("KHWD");
431 wps.push_back(GPS_WP_APT);
432 wps.push_back(GPS_WP_APT);
433 wps.push_back(GPS_WP_VOR);
434 wps.push_back(GPS_WP_APT);
435 //wps.push_back(GPS_WP_APT);
436 //wps.push_back(GPS_WP_APT);
437 CreateFlightPlan(_flightPlans[0], ids, wps);
438
439 /*
440 ids.clear();
441 wps.clear();
442 ids.push_back("KLVK");
443 ids.push_back("KHWD");
444 wps.push_back(GPS_WP_APT);
445 wps.push_back(GPS_WP_APT);
446 CreateFlightPlan(_flightPlans[0], ids, wps);
447 */
448 }
449
450 void KLN89::SetBaroUnits(int n, bool wrap) {
451 if(n < 1) {
452 _baroUnits = (KLN89PressureUnits)(wrap ? 3 : 1);
453 } else if(n > 3) {
454 _baroUnits = (KLN89PressureUnits)(wrap ? 1 : 3);
455 } else {
456 _baroUnits = (KLN89PressureUnits)n;
457 }
458 }
459
460 void KLN89::Knob1Right1() {
461 if(_mode == KLN89_MODE_DISP) {
462 _activePage->LooseFocus();
463 if(_cleanUpPage >= 0) {
464 _pages[(unsigned int)_cleanUpPage]->CleanUp();
465 _cleanUpPage = -1;
466 }
467 _curPage++;
468 if(_curPage >= _pages.size()) _curPage = 0;
469 _activePage = _pages[_curPage];
470 } else {
471 _activePage->Knob1Right1();
472 }
473 update(0.0);
474 }
475
476 void KLN89::Knob1Left1() {
477 if(_mode == KLN89_MODE_DISP) {
478 _activePage->LooseFocus();
479 if(_cleanUpPage >= 0) {
480 _pages[(unsigned int)_cleanUpPage]->CleanUp();
481 _cleanUpPage = -1;
482 }
483 if(_curPage == 0) {
484 _curPage = _pages.size() - 1;
485 } else {
486 _curPage--;
487 }
488 _activePage = _pages[_curPage];
489 } else {
490 _activePage->Knob1Left1();
491 }
492 update(0.0);
493 }
494
495 void KLN89::Knob2Left1() {
496 _activePage->Knob2Left1();
497 }
498
499 void KLN89::Knob2Right1() {
500 _activePage->Knob2Right1();
501 }
502
503 void KLN89::CrsrPressed() {
504 _dispMsg = false;
505 // CRSR cannot be switched off on nrst page.
506 if(_activePage == _nrst_page) { return; }
507 // CRSR is always off when inner-knob is out on nav4 page.
508 if(_curPage == 6 && _activePage->GetSubPage() == 3 && fgGetBool("/instrumentation/kln89/scan-pull")) { return; }
509 if(_cleanUpPage >= 0) {
510 _pages[(unsigned int)_cleanUpPage]->CleanUp();
511 _cleanUpPage = -1;
512 }
513 _jumpRestoreCrsr = false;
514 _entJump = _clrJump = -1;
515 ((KLN89Page*)_activePage)->SetEntInvert(false);
516 if(_mode == KLN89_MODE_DISP) {
517 _mode = KLN89_MODE_CRSR;
518 _activePage->CrsrPressed();
519 } else {
520 _mode = KLN89_MODE_DISP;
521 _activePage->CrsrPressed();
522 }
523 update(0.0);
524 }
525
526 void KLN89::EntPressed() {
527 if(_entJump >= 0) {
528 if(_curPage < 5) {
529 // one of the data pages. Signal ent pressed to it here, and ent pressed to the call back page a few lines further down.
530 // Ie. 2 ent pressed signals in this case is deliberate.
531 _activePage->EntPressed();
532 }
533 _curPage = _entJump;
534 _activePage = _pages[(unsigned int)_entJump];
535 if(_jumpRestoreCrsr) _mode = KLN89_MODE_CRSR;
536 _entJump = _clrJump = -1;
537 }
538 if(_activePage == _dir_page) {
539 _dir_page->EntPressed();
540 _mode = KLN89_MODE_DISP;
541 _activePage = _pages[_curPage];
542 } else {
543 _activePage->EntPressed();
544 }
545 }
546
547 void KLN89::ClrPressed() {
548 if(_clrJump >= 0) {
549 _curPage = _clrJump;
550 _activePage = _pages[(unsigned int)_clrJump];
551 if(_jumpRestoreCrsr) _mode = KLN89_MODE_CRSR;
552 _entJump = _clrJump = -1;
553 }
554 _activePage->ClrPressed();
555 }
556
557 void KLN89::DtoPressed() {
558 if(_activePage != _dir_page) {
559 // Figure out which waypoint the dir page should display, according to the following rules:
560 // 1. If the FPL 0 page is displayed AND the cursor is over one of the waypoints, display that waypoint.
561 // 2. If the NAV 4 page is displayed with the inner knob pulled out, display the waypoint highlighted in the lower RH corner of the nav page.
562 // 3. If any of APT, VOR, NDB, INT, USR or ACT pages is displayed then display the waypoint being viewed.
563 // 4. If none of the above, display the active waypoint, unless the active waypoint is the MAP of an approach and it has been flown past
564 // (no waypoint sequence past the MAP), in which case display the first waypoint of the missed approach procedure.
565 // 5. If none of the above (i.e. no active waypoint) then display blanks.
566 if(_curPage <= 5) {
567 // APT, VOR, NDB, INT, USR or ACT
568 if(!_activePage->GetId().empty()) { // Guard against no user waypoints defined
569 _dir_page->SetId(_activePage->GetId());
570 } else {
571 _dir_page->SetId(_activeWaypoint.id);
572 }
573 } else if(_curPage == 6 && _activePage->GetSubPage() == 3 && fgGetBool("/instrumentation/kln89/scan-pull") && ! _activeFP->waypoints.empty()) {
574 // NAV 4
575 _dir_page->SetId(((KLN89NavPage*)_activePage)->GetNav4WpId());
576 } else if(_curPage == 7 && _activePage->GetSubPage() == 0 && _mode == KLN89_MODE_CRSR) {
577 // FPL 0
578 if(!_activePage->GetId().empty()) {
579 //cout << "Not empty!!!\n";
580 _dir_page->SetId(_activePage->GetId());
581 } else {
582 //cout << "empty :-(\n";
583 _dir_page->SetId(_activeWaypoint.id);
584 }
585 } else {
586 _dir_page->SetId(_activeWaypoint.id);
587 }
588 // This need to come after the bit before otherwise the FPL or NAV4 page clears their current ID when it looses focus.
589 _activePage->LooseFocus();
590 _activePage = _dir_page;
591 _mode = KLN89_MODE_CRSR;
592 }
593 }
594
595 void KLN89::NrstPressed() {
596 if(_activePage != _nrst_page) {
597 _activePage->LooseFocus(); // TODO: check whether we should call loose focus here
598 _lastActivePage = _activePage;
599 _activePage = _nrst_page;
600 _lastMode = _mode;
601 _mode = KLN89_MODE_CRSR;
602 } else {
603 _activePage = _lastActivePage;
604 _mode = _lastMode;
605 }
606 }
607
608 void KLN89::AltPressed() {
609 if(_activePage != _alt_page) {
610 _activePage->LooseFocus(); // TODO: check whether we should call loose focus here
611 _lastActivePage = _activePage;
612 _alt_page->SetSubPage(0);
613 _activePage = _alt_page;
614 _lastMode = _mode;
615 _mode = KLN89_MODE_CRSR;
616 } else {
617 _alt_page->LooseFocus();
618 if(_alt_page->GetSubPage() == 0) {
619 _alt_page->SetSubPage(1);
620 _mode = KLN89_MODE_CRSR;
621 } else {
622 _activePage = _lastActivePage;
623 _mode = _lastMode;
624 }
625 }
626 }
627
628 void KLN89::OBSPressed() {
629 ToggleOBSMode();
630 if(_obsMode) {
631 if(!fgGetBool("/instrumentation/nav/slaved-to-gps")) {
632 // NOTE: this only applies to ORS 02 firmware, in ORS 01
633 // CRSR mode is not automatically set when OBS is started.
634 _mode = KLN89_MODE_CRSR;
635 }
636 _activePage->OBSPressed();
637 }
638 }
639
640 void KLN89::MsgPressed() {
641 // TODO: handle persistent messages such as SUA alerting.
642 // (The message annunciation flashes before first view, but afterwards remains continuously lit with the message available
643 // until the potential conflict no longer pertains).
644 if(_dispMsg && ! _messageStack.empty()) {
645 _messageStack.pop_front();
646 }
647 _dispMsg = !_dispMsg;
648 }
649
650 void KLN89::ToggleOBSMode() {
651 DCLGPS::ToggleOBSMode();
652 }
653
654 void KLN89::DtoInitiate(const string& id) {
655 _dtoReview = false;
656 // Set the current page to NAV1
657 _curPage = 6;
658 _activePage = _pages[_curPage];
659 _activePage->SetSubPage(0);
660 // TODO: need to output a scratchpad message with the new course, but we don't know it yet!
661 // Call the base class to actually initiate the DTO.
662 DCLGPS::DtoInitiate(id);
663 }
664
665 void KLN89::SetMinDisplayBrightness(int n) {
666 _minDisplayBrightness = n;
667 if(_minDisplayBrightness < 1) _minDisplayBrightness = 1;
668 if(_minDisplayBrightness > 9) _minDisplayBrightness = 9;
669 }
670
671 void KLN89::DecrementMinDisplayBrightness() {
672 _minDisplayBrightness--;
673 if(_minDisplayBrightness < 1) _minDisplayBrightness = 1;
674 }
675
676 void KLN89::IncrementMinDisplayBrightness() {
677 _minDisplayBrightness++;
678 if(_minDisplayBrightness > 9) _minDisplayBrightness = 9;
679 }
680
681 void KLN89::DrawBar(int page) {
682 int px = 1 + (page * 15);
683 int py = 1;
684 for(int i=0; i<7; ++i) {
685 // Ugh - this is crude and inefficient!
686 _instrument->DrawPixel(px+i, py);
687 _instrument->DrawPixel(px+i, py+1);
688 }
689 }
690
691 // Convert moving map to instrument co-ordinates
692 void KLN89::MapToInstrument(int &x, int &y) {
693 x += _xBorder + _xFieldBorder[2] + _xFieldStart[2];
694 }
695
696 // Draw a pixel specified in instrument co-ords, but clipped to the map region
697 //void KLN89::DrawInstrMapPixel(int x, int y) {
698
699 /*
700 // Clip, translate and draw a map pixel
701 // If we didn't need per-pixel clipping, it would be cheaper to translate object rather than pixel positions.
702 void KLN89::DrawMapPixel(int x, int y, bool invert) {
703 if(x < 0 || x > 111 || y < 0 || y > 39) return;
704 x += _xBorder + _xFieldBorder[2] + _xFieldStart[2];
705 _instrument->DrawPixel(x, y, invert);
706 }
707 */
708
709 // HACK - use something FG provides
710 static double gps_min(const double &a, const double &b) {
711 return(a <= b ? a : b);
712 }
713
714 #if 0
715 static double gps_max(const double &a, const double &b) {
716 return(a >= b ? a : b);
717 }
718 #endif
719
720 void KLN89::UpdateMapHeading() {
721 switch(_mapOrientation) {
722 case 0: // North up
723 _mapHeading = 0.0;
724 break;
725 case 1: // DTK up
726 _mapHeading = _dtkTrue;
727 break;
728 case 2: // Track up
729 _mapHeading = _track;
730 break;
731 }
732 }
733
734 // The screen area allocated to the moving map is 111 x 40 pixels.
735 // In North up mode, the user position marker is at 57, 20. (Map co-ords).
736 void KLN89::DrawMap(bool draw_avs) {
737 // Set the clipping region to the moving map part of the display
738 int xstart = _xBorder + _xFieldBorder[2] + _xFieldStart[2];
739 _instrument->SetClipRegion(xstart, 0, xstart + 110, 39);
740
741 _mapScaleUnits = (int)_distUnits;
742 _mapScale = (double)(KLN89MapScales[_mapScaleUnits][_mapScaleIndex]);
743
744 //cout << "Map scale = " << _mapScale << '\n';
745
746 double mapScaleMeters = _mapScale * (_mapScaleUnits == 0 ? SG_NM_TO_METER : 1000);
747
748 // TODO: use an aligned projection when either DTK or TK up!
749 AlignedProjection mapProj(SGGeod::fromRad(_gpsLon, _gpsLat), _mapHeading);
750 double meter_per_pix = (_mapOrientation == 0 ? mapScaleMeters / 20.0f : mapScaleMeters / 29.0f);
751 // SGGeod bottomLeft = mapProj.ConvertFromLocal(SGVec3d(gps_max(-57.0 * meter_per_pix, -50000), gps_max((_mapOrientation == 0 ? -20.0 * meter_per_pix : -11.0 * meter_per_pix), -25000), 0.0));
752 // SGGeod topRight = mapProj.ConvertFromLocal(SGVec3d(gps_min(54.0 * meter_per_pix, 50000), gps_min((_mapOrientation == 0 ? 20.0 * meter_per_pix : 29.0 * meter_per_pix), 25000), 0.0));
753
754
755
756
757 // Draw Airport labels first (but not one's that are waypoints)
758 // Draw Airports first (but not one's that are waypoints)
759 // Ditto for VORs (not sure if SUA/VOR/Airport ordering is important or not).
760 // Ditto for SUA
761 // Then flighttrack
762 // Then waypoints
763 // Then waypoint labels (not sure if this should be before or after waypoints)
764 // Then user pos.
765 // Annotation then gets drawn by Nav page, NOT this function.
766
767 if(_drawApt && draw_avs) {
768 /*
769 bool have_apt = _overlays->FindArpByRegion(&apt, bottomLeft.lat(), bottomLeft.lon(), topRight.lat(), topRight.lon());
770 //cout << "Vors enclosed are: ";
771 // Draw all the labels first...
772 for(unsigned int i=0; i<apt.size(); ++i) {
773 //cout << nav[i]->id << ' ';
774 Point3D p = mapProj.ConvertToLocal(Point3D(apt[i]->lon * SG_RADIANS_TO_DEGREES, apt[i]->lat * SG_RADIANS_TO_DEGREES, 0.0));
775 //cout << p << " .... ";
776 int mx = int(p.x() / meter_per_pix) + 56;
777 int my = int(p.y() / meter_per_pix) + (_mapOrientation == 0 ? 19 : 10);
778 //cout << "mx = " << mx << ", my = " << my << '\n';
779 bool right_align = (p.x() < 0.0);
780 DrawLabel(apt[i]->id, mx + (right_align ? -2 : 3), my + (p.y() < 0.0 ? -7 : 3), right_align);
781 // I think that we probably should have -1 in the right_align case above to match the real life instrument.
782 }
783 // ...and then all the Apts.
784 for(unsigned int i=0; i<apt.size(); ++i) {
785 //cout << nav[i]->id << ' ';
786 Point3D p = mapProj.ConvertToLocal(Point3D(apt[i]->lon * SG_RADIANS_TO_DEGREES, apt[i]->lat * SG_RADIANS_TO_DEGREES, 0.0));
787 //cout << p << " .... ";
788 int mx = int(p.x() / meter_per_pix) + 56;
789 int my = int(p.y() / meter_per_pix) + (_mapOrientation == 0 ? 19 : 10);
790 //cout << "mx = " << mx << ", my = " << my << '\n';
791 DrawApt(mx, my);
792 }
793 //cout << '\n';
794 */
795 }
796 /*
797 if(_drawVOR && draw_avs) {
798 Overlays::nav_array_type nav;
799 bool have_vor = _overlays->FindVorByRegion(&nav, bottomLeft.lat(), bottomLeft.lon(), topRight.lat(), topRight.lon());
800 //cout << "Vors enclosed are: ";
801 // Draw all the labels first...
802 for(unsigned int i=0; i<nav.size(); ++i) {
803 //cout << nav[i]->id << ' ';
804 Point3D p = mapProj.ConvertToLocal(Point3D(nav[i]->lon * SG_RADIANS_TO_DEGREES, nav[i]->lat * SG_RADIANS_TO_DEGREES, 0.0));
805 //cout << p << " .... ";
806 int mx = int(p.x() / meter_per_pix) + 56;
807 int my = int(p.y() / meter_per_pix) + (_mapOrientation == 0 ? 19 : 10);
808 //cout << "mx = " << mx << ", my = " << my << '\n';
809 bool right_align = (p.x() < 0.0);
810 DrawLabel(nav[i]->id, mx + (right_align ? -2 : 3), my + (p.y() < 0.0 ? -7 : 3), right_align);
811 // I think that we probably should have -1 in the right_align case above to match the real life instrument.
812 }
813 // ...and then all the VORs.
814 for(unsigned int i=0; i<nav.size(); ++i) {
815 //cout << nav[i]->id << ' ';
816 Point3D p = mapProj.ConvertToLocal(Point3D(nav[i]->lon * SG_RADIANS_TO_DEGREES, nav[i]->lat * SG_RADIANS_TO_DEGREES, 0.0));
817 //cout << p << " .... ";
818 int mx = int(p.x() / meter_per_pix) + 56;
819 int my = int(p.y() / meter_per_pix) + (_mapOrientation == 0 ? 19 : 10);
820 //cout << "mx = " << mx << ", my = " << my << '\n';
821 DrawVOR(mx, my);
822 }
823 //cout << '\n';
824 }
825 */
826
827 // FlightTrack
828 if(_activeFP->waypoints.size() > 1) {
829 std::vector<int> xvec, yvec, qvec; // qvec stores the quadrant that each waypoint label should
830 // be drawn in (relative to the waypoint).
831 // 1 = NE, 2 = SE, 3 = SW, 4 = NW.
832 double save_h = 0.0; // Each pass, save a heading from the previous one for label quadrant determination.
833 bool drawTrack = true;
834 for(unsigned int i=1; i<_activeFP->waypoints.size(); ++i) {
835 GPSWaypoint* wp0 = _activeFP->waypoints[i-1];
836 GPSWaypoint* wp1 = _activeFP->waypoints[i];
837 SGVec3d p0 = mapProj.ConvertToLocal(SGGeod::fromRad(wp0->lon, wp0->lat));
838 SGVec3d p1 = mapProj.ConvertToLocal(SGGeod::fromRad(wp1->lon, wp1->lat));
839 int mx0 = int(p0.x() / meter_per_pix + 0.5) + 56;
840 int my0 = int(p0.y() / meter_per_pix + 0.5) + (_mapOrientation == 0 ? 19 : 10);
841 int mx1 = int(p1.x() / meter_per_pix + 0.5) + 56;
842 int my1 = int(p1.y() / meter_per_pix + 0.5) + (_mapOrientation == 0 ? 19 : 10);
843 if(i == 1) {
844 xvec.push_back(mx0);
845 yvec.push_back(my0);
846 double h = GetGreatCircleCourse(wp0->lat, wp0->lon, wp1->lat, wp1->lon) * SG_RADIANS_TO_DEGREES;
847 // Adjust for map orientation
848 h -= _mapHeading;
849 qvec.push_back(GetLabelQuadrant(h));
850 //cout << "i = " << i << ", h = " << h << ", qvec[0] = " << qvec[0] << '\n';
851 }
852 xvec.push_back(mx1);
853 yvec.push_back(my1);
854 if(drawTrack) { DrawLine(mx0, my0, mx1, my1); }
855 if(i != 1) {
856 double h = GetGreatCircleCourse(wp0->lat, wp0->lon, wp1->lat, wp1->lon) * SG_RADIANS_TO_DEGREES;
857 // Adjust for map orientation
858 h -= _mapHeading;
859 qvec.push_back(GetLabelQuadrant(save_h, h));
860 }
861 save_h = GetGreatCircleCourse(wp1->lat, wp1->lon, wp0->lat, wp0->lon) * SG_RADIANS_TO_DEGREES;
862 // Adjust for map orientation
863 save_h -= _mapHeading;
864 if(i == _activeFP->waypoints.size() - 1) {
865 qvec.push_back(GetLabelQuadrant(save_h));
866 }
867 // Don't draw flight track beyond the missed approach point of an approach
868 if(_approachLoaded) {
869 //cout << "Waypoints are " << wp0->id << " and " << wp1->id << '\n';
870 //cout << "Types are " << wp0->appType << " and " << wp1->appType << '\n';
871 if(wp1->appType == GPS_MAP) {
872 drawTrack = false;
873 }
874 }
875 }
876 // ASSERT(xvec.size() == yvec.size() == qvec.size() == _activeFP->waypoints.size());
877 for(unsigned int i=0; i<xvec.size(); ++i) {
878 DrawWaypoint(xvec[i], yvec[i]);
879 bool right_align = (qvec[i] > 2);
880 bool top = (qvec[i] == 1 || qvec[i] == 4);
881 // TODO: not sure if labels should be drawn in sequence with waypoints and flightpaths,
882 // or all before or all afterwards. Doesn't matter a huge deal though.
883 DrawLabel(_activeFP->waypoints[i]->id, xvec[i] + (right_align ? -2 : 3), yvec[i] + (top ? 3 : -7), right_align);
884 }
885 }
886
887 // User pos
888 if(_mapOrientation == 0) {
889 // North up
890 DrawUser1(56, 19);
891 } else if(_mapOrientation == 1) {
892 // DTK up
893 DrawUser1(56, 10);
894 } else if(_mapOrientation == 2) {
895 // TK up
896 DrawUser2(56, 10);
897 } else {
898 // Heading up
899 // TODO: don't know what to do here!
900 }
901
902 // And finally, reset the clip region to stop the rest of the code going pear-shaped!
903 _instrument->ResetClipRegion();
904 }
905
906 // Get the quadrant to draw the label of the start or end waypoint (i.e. one with only one track from it).
907 // Heading specified FROM the waypoint.
908 // 4 | 1
909 // -----
910 // 3 | 2
911 int KLN89::GetLabelQuadrant(double h) {
912 while(h < 0.0) h += 360.0;
913 while(h > 360.0) h -= 360.0;
914 if(h < 90.0) return(3);
915 if(h < 180.0) return(4);
916 if(h < 270.0) return(1);
917 return(2);
918 }
919
920 // Get the quadrant to draw the label of an en-route waypoint,
921 // with BOTH tracks specified as headings FROM the waypoint.
922 // 4 | 1
923 // -----
924 // 3 | 2
925 int KLN89::GetLabelQuadrant(double h1, double h2) {
926 while(h1 < 0.0) h1 += 360.0;
927 while(h1 > 360.0) h1 -= 360.0;
928 while(h2 < 0.0) h2 += 360.0;
929 while(h2 > 360.0) h2 -= 360.0;
930 double max_min_diff = 0.0;
931 int quad = 1;
932 for(int i=0; i<4; ++i) {
933 double h = 45 + (90 * i);
934 double diff1 = fabs(h - h1);
935 if(diff1 > 180) diff1 = 360 - diff1;
936 double diff2 = fabs(h - h2);
937 if(diff2 > 180) diff2 = 360 - diff2;
938 double min_diff = gps_min(diff1, diff2);
939 if(min_diff > max_min_diff) {
940 max_min_diff = min_diff;
941 quad = i + 1;
942 }
943 }
944 //cout << "GetLabelQuadrant, h1 = " << h1 << ", h2 = " << h2 << ", quad = " << quad << '\n';
945 return(quad);
946 }
947
948 // Draw the diamond style of user pos
949 //
950 // o
951 // oxo
952 // oxxxo
953 // oxxxxxo
954 // oxxxo
955 // oxo
956 // o
957 //
958 void KLN89::DrawUser1(int x, int y) {
959 MapToInstrument(x, y);
960 int min_j = 0, max_j = 0;
961 for(int i=-3; i<=3; ++i) {
962 for(int j=min_j; j<=max_j; ++j) {
963 _instrument->DrawPixel(x+j, y+i, (j == min_j || j == max_j ? true : false));
964 }
965 if(i < 0) {
966 min_j--;
967 max_j++;
968 } else {
969 min_j++;
970 max_j--;
971 }
972 }
973 }
974
975 // Draw the airplane style of user pos
976 // Define the origin to be the midpoint of the *fuselage*
977 void KLN89::DrawUser2(int x, int y) {
978 MapToInstrument(x, y);
979
980 // Draw the background as three black quads first
981 _instrument->DrawQuad(x-2, y-3, x+2, y-1, true);
982 _instrument->DrawQuad(x-3, y, x+3, y+2, true);
983 _instrument->DrawQuad(x-1, y+3, x+1, y+3, true);
984
985 if(_pixelated) {
986 for(int j=y-2; j<=y+2; ++j) {
987 _instrument->DrawPixel(x, j);
988 }
989 for(int i=x-1; i<=x+1; ++i) {
990 _instrument->DrawPixel(i, y-2);
991 }
992 for(int i=x-2; i<=x+2; ++i) {
993 _instrument->DrawPixel(i, y+1);
994 }
995 } else {
996 _instrument->DrawQuad(x, y-2, x, y+2);
997 _instrument->DrawQuad(x-1, y-2, x+1, y-2);
998 _instrument->DrawQuad(x-2, y+1, x+2, y+1);
999 }
1000 }
1001
1002 // Draw an airport symbol on the moving map
1003 //
1004 // ooo
1005 // ooxoo
1006 // oxxxo
1007 // ooxoo
1008 // ooo
1009 //
1010 void KLN89::DrawApt(int x, int y) {
1011 MapToInstrument(x, y);
1012
1013 int j = y-2;
1014 int i;
1015 for(i=x-1; i<=x+1; ++i) _instrument->DrawPixel(i, j, true);
1016 ++j;
1017 for(i=x-2; i<=x+2; ++i) _instrument->DrawPixel(i, j, (i != x ? true : false));
1018 ++j;
1019 for(i=x-2; i<=x+2; ++i) _instrument->DrawPixel(i, j, (std::abs(i - x) > 1 ? true : false));
1020 ++j;
1021 for(i=x-2; i<=x+2; ++i) _instrument->DrawPixel(i, j, (i != x ? true : false));
1022 ++j;
1023 for(i=x-1; i<=x+1; ++i) _instrument->DrawPixel(i, j, true);
1024 }
1025
1026 // Draw a waypoint on the moving map
1027 //
1028 // ooooo
1029 // oxxxo
1030 // oxxxo
1031 // oxxxo
1032 // ooooo
1033 //
1034 void KLN89::DrawWaypoint(int x, int y) {
1035 MapToInstrument(x, y);
1036 _instrument->SetDebugging(true);
1037
1038 // Draw black background
1039 _instrument->DrawQuad(x-2, y-2, x+2, y+2, true);
1040
1041 // Draw the coloured square
1042 if(_pixelated) {
1043 for(int i=x-1; i<=x+1; ++i) {
1044 for(int j=y-1; j<=y+1; ++j) {
1045 _instrument->DrawPixel(i, j);
1046 }
1047 }
1048 } else {
1049 _instrument->DrawQuad(x-1, y-1, x+1, y+1);
1050 }
1051 _instrument->SetDebugging(false);
1052 }
1053
1054 // Draw a VOR on the moving map
1055 //
1056 // ooooo
1057 // oxxxo
1058 // oxoxo
1059 // oxxxo
1060 // ooooo
1061 //
1062 void KLN89::DrawVOR(int x, int y) {
1063 // Cheat - draw a waypoint and then a black pixel in the middle.
1064 // Need to call Waypoint draw *before* translating co-ords.
1065 DrawWaypoint(x, y);
1066 MapToInstrument(x, y);
1067 _instrument->DrawPixel(x, y, true);
1068 }
1069
1070 // Draw a line on the moving map
1071 void KLN89::DrawLine(int x1, int y1, int x2, int y2) {
1072 MapToInstrument(x1, y1);
1073 MapToInstrument(x2, y2);
1074 _instrument->DrawLine(x1, y1, x2, y2);
1075 }
1076
1077 void KLN89::DrawMapUpArrow(int x, int y) {
1078 MapToInstrument(x, y);
1079 if(_pixelated) {
1080 for(int j=0; j<7; ++j) {
1081 _instrument->DrawPixel(x + 2, y + j);
1082 }
1083 } else {
1084 _instrument->DrawQuad(x+2, y, x+2, y+6);
1085 }
1086 _instrument->DrawPixel(x, y+4);
1087 _instrument->DrawPixel(x+1, y+5);
1088 _instrument->DrawPixel(x+3, y+5);
1089 _instrument->DrawPixel(x+4, y+4);
1090 }
1091
1092 // Draw a quad on the moving map
1093 void KLN89::DrawMapQuad(int x1, int y1, int x2, int y2, bool invert) {
1094 MapToInstrument(x1, y1);
1095 MapToInstrument(x2, y2);
1096 _instrument->DrawQuad(x1, y1, x2, y2, invert);
1097 }
1098
1099 // Draw an airport or waypoint label on the moving map
1100 // Specify position by the map pixel co-ordinate of the left or right, bottom, of the *visible* portion of the label.
1101 // The black background quad will automatically overlap this by 1 pixel.
1102 void KLN89::DrawLabel(const string& s, int x1, int y1, bool right_align) {
1103 MapToInstrument(x1, y1);
1104 if(!right_align) {
1105 for(unsigned int i=0; i<s.size(); ++i) {
1106 char c = s[i];
1107 x1 += DrawSmallChar(c, x1, y1);
1108 x1 ++;
1109 }
1110 } else {
1111 for(int i=(int)(s.size()-1); i>=0; --i) {
1112 char c = s[i];
1113 x1 -= DrawSmallChar(c, x1, y1, right_align);
1114 x1--;
1115 }
1116 }
1117 }
1118
1119 void KLN89::DrawCDI() {
1120 // Scale
1121 for(int i=0; i<5; ++i) {
1122 DrawSpecialChar(2, 2, 3+i, 2);
1123 DrawSpecialChar(1, 2, 9+i, 2);
1124 }
1125
1126 int field = 2;
1127 int px = 8 * 7 + _xBorder + _xFieldBorder[field] + _xFieldStart[field] + 2;
1128 int py = 2 * 9 + _yBorder + _yFieldBorder[field] + _yFieldStart[field];
1129
1130 // Deflection bar
1131 // Every 7 pixels deflection left or right is one dot on the scale, and hence 1/5 FSD.
1132 // Maximum deflection is 37 pixels left, or 38 pixels right !?!
1133 double xtd = CalcCrossTrackDeviation();
1134 int deflect;
1135 if(_cdiScaleTransition) {
1136 double dots = (xtd / _currentCdiScale) * 5.0;
1137 deflect = (int)(dots * 7.0 * -1.0);
1138 // TODO: for all these I think I should add 0.5 before casting to int, and *then* multiply by -1. Possibly!
1139 } else {
1140 if(0 == _currentCdiScaleIndex) { // 5.0nm FSD => 1 nm per dot => 7 pixels per nm.
1141 deflect = (int)(xtd * 7.0 * -1.0); // The -1.0 is because we move the 'needle' indicating the course, not the plane.
1142 } else if(1 == _currentCdiScaleIndex) {
1143 deflect = (int)(xtd * 35.0 * -1.0);
1144 } else { // 0.3 == _cdiScale
1145 deflect = (int)(xtd * 116.6666666667 * -1.0);
1146 }
1147 }
1148 if(deflect > 38) deflect = 38;
1149 if(deflect < -37) deflect = -37;
1150 if(_pixelated) {
1151 for(int j=0; j<9; ++j) {
1152 _instrument->DrawPixel(px + deflect, py+j);
1153 _instrument->DrawPixel(px + deflect + 1, py+j);
1154 }
1155 } else {
1156 _instrument->DrawQuad(px + deflect, py, px + deflect + 1, py + 8);
1157 }
1158
1159 // To/From indicator
1160 px-=4;
1161 py+=2;
1162 for(int j=4; j>=0; --j) {
1163 int k = 10 - (2*j);
1164 for(int i=0; i<k; ++i) {
1165 _instrument->DrawPixel(px+j+i, (_headingBugTo ? py+j : py+4-j));
1166 // At the extremities, draw the outlining dark pixel
1167 if(i == 0 || i == k-1) {
1168 _instrument->DrawPixel(px+j+i, (_headingBugTo ? py+j+1 : py+3-j), true);
1169 }
1170 }
1171 }
1172 }
1173
1174 void KLN89::DrawLegTail(int py) {
1175 int px = 0 * 7 + _xBorder + _xFieldBorder[2] + _xFieldStart[2];
1176 py = py * 9 + _yBorder + _yFieldBorder[2] + _yFieldStart[2];
1177
1178 px++;
1179 py+=3;
1180 py++; // Hack - not sure if this represents a border issue.
1181
1182 for(int i=0; i<9; ++i) _instrument->DrawPixel(px, py+i);
1183 for(int i2=0; i2<5; ++i2) _instrument->DrawPixel(px+i2, py+9);
1184 }
1185
1186 void KLN89::DrawLongLegTail(int py) {
1187 int px = 0 * 7 + _xBorder + _xFieldBorder[2] + _xFieldStart[2];
1188 py = py * 9 + _yBorder + _yFieldBorder[2] + _yFieldStart[2];
1189
1190 px++;
1191 py+=3;
1192 py++; // Hack - not sure if this represents a border issue.
1193
1194 for(int i=0; i<18; ++i) _instrument->DrawPixel(px, py+i);
1195 for(int i2=0; i2<5; ++i2) _instrument->DrawPixel(px+i2, py+18);
1196 }
1197
1198 void KLN89::DrawHalfLegTail(int py) {
1199 }
1200
1201 void KLN89::DrawDivider() {
1202 int px = _xFieldStart[2] - 1;
1203 int py = _yBorder;
1204 for(int i=0; i<36; ++i) {
1205 _instrument->DrawPixel(px, py+i);
1206 }
1207 }
1208
1209 void KLN89::DrawEnt(int field, int px, int py) {
1210 px = px * 7 + _xBorder + _xFieldBorder[field] + _xFieldStart[field];
1211 py = py * 9 + _yBorder + _yFieldBorder[field] + _yFieldStart[field] + 1;
1212
1213 px++; // Not sure why we need px++, but it seems to work!
1214 py++;
1215
1216 // E
1217 for(int i=0; i<5; ++i) _instrument->DrawPixel(px, py+i);
1218 _instrument->DrawPixel(px+1, py);
1219 _instrument->DrawPixel(px+2, py);
1220 _instrument->DrawPixel(px+1, py+2);
1221 _instrument->DrawPixel(px+1, py+4);
1222 _instrument->DrawPixel(px+2, py+4);
1223
1224 px += 4;
1225 // N
1226 for(int i=0; i<4; ++i) _instrument->DrawPixel(px, py+i);
1227 _instrument->DrawPixel(px+1, py+2);
1228 _instrument->DrawPixel(px+2, py+1);
1229 for(int i=0; i<4; ++i) _instrument->DrawPixel(px+3, py+i);
1230
1231 px += 5;
1232 // T
1233 _instrument->DrawPixel(px, py+3);
1234 for(int i=0; i<4; ++i) _instrument->DrawPixel(px+1, py+i);
1235 _instrument->DrawPixel(px+2, py+3);
1236 }
1237
1238 void KLN89::DrawMessageAlert() {
1239 // TODO: draw the proper message indicator
1240 if(!_blink) {
1241 int px = _xBorder + _xFieldBorder[1] + _xFieldStart[1];
1242 int py = 1 * 9 + _yBorder + _yFieldBorder[1] + _yFieldStart[1] + 1;
1243
1244 px++; // Not sure why we need px++, but it seems to work!
1245 py++;
1246
1247 DrawText(" ", 1, 0, 1, false, 99);
1248 _instrument->DrawQuad(px+1, py-1, px+2, py+5, true);
1249 _instrument->DrawQuad(px+3, py+3, px+3, py+5, true);
1250 _instrument->DrawQuad(px+4, py+2, px+4, py+4, true);
1251 _instrument->DrawQuad(px+5, py+1, px+6, py+3, true);
1252 _instrument->DrawQuad(px+7, py+2, px+7, py+4, true);
1253 _instrument->DrawQuad(px+8, py+3, px+8, py+5, true);
1254 _instrument->DrawQuad(px+9, py-1, px+10, py+5, true);
1255 }
1256 }
1257
1258 void KLN89::Underline(int field, int px, int py, int len) {
1259 px = px * 7 + _xBorder + _xFieldBorder[field] + _xFieldStart[field];
1260 py = py * 9 + _yBorder + _yFieldBorder[field] + _yFieldStart[field];
1261 for(int i=0; i<(len*7); ++i) {
1262 _instrument->DrawPixel(px, py);
1263 ++px;
1264 }
1265 }
1266
1267 void KLN89::DrawKPH(int field, int cx, int cy) {
1268 // Add some border
1269 int px = cx * 7 + _xBorder + _xFieldBorder[field] + _xFieldStart[field];
1270 int py = cy * 9 + _yBorder + _yFieldBorder[field] + _yFieldStart[field];
1271
1272 px++;
1273 py++;
1274
1275 for(int j=0; j<=4; ++j) {
1276 _instrument->DrawPixel(px, py + 2 +j);
1277 _instrument->DrawPixel(px + 8, py + j);
1278 if(j <= 1) {
1279 _instrument->DrawPixel(px + 11, py + j);
1280 _instrument->DrawPixel(px + 9 + j, py + 2);
1281 }
1282 }
1283
1284 for(int i=0; i<=6; ++i) {
1285 if(i <= 2) {
1286 _instrument->DrawPixel(px + 1 + i, py + 4 + i);
1287 _instrument->DrawPixel(px + 1 + i, py + (4 - i));
1288 }
1289 _instrument->DrawPixel(px + 2 + i, py + i);
1290 }
1291 }
1292
1293 void KLN89::DrawDTO(int field, int cx, int cy) {
1294 DrawSpecialChar(6, field, cx, cy);
1295 if(!(_waypointAlert && _blink)) {
1296 DrawSpecialChar(3, field, cx+1, cy);
1297 }
1298
1299 int px = cx * 7 + _xBorder + _xFieldBorder[field] + _xFieldStart[field];
1300 int py = cy * 9 + _yBorder + _yFieldBorder[field] + _yFieldStart[field];
1301
1302 px++;
1303 py++;
1304
1305 // Fill in the gap between the 'D' and the arrow.
1306 _instrument->DrawPixel(px+5, py+3);
1307 }
1308
1309 // Takes character position
1310 void KLN89::DrawChar(char c, int field, int px, int py, bool bold, bool invert) {
1311 // Ignore field for now
1312 // Add some border
1313 px = px * 7 + _xBorder + _xFieldBorder[field] + _xFieldStart[field];
1314 py = py * 9 + _yBorder + _yFieldBorder[field] + _yFieldStart[field];
1315
1316 // Draw an orange background for inverted characters
1317 if(invert) {
1318 for(int i=0; i<7; ++i) {
1319 for(int j=0; j<9; ++j) {
1320 _instrument->DrawPixel(px + i, py + j);
1321 }
1322 }
1323 }
1324
1325 if(c < 33) return; // space
1326
1327 // Render normal decimal points in bold floats
1328 if(c == '.') bold = false;
1329
1330 ++py; // Shift the char up by one pixel
1331 for(int j=7; j>=0; --j) {
1332 char c1 = (bold ? NumbersBold[c-48][j] : UpperAlpha[c-33][j]);
1333 // Don't do the last column for now (ie. j = 1, not 0)
1334 for(int i=5; i>=0; --i) {
1335 if(c1 & (01 << i)) {
1336 _instrument->DrawPixel(px, py, invert);
1337 }
1338 ++px;
1339 }
1340 px -= 6;
1341 ++py;
1342 }
1343 }
1344
1345 // Takes pixel position
1346 void KLN89::DrawFreeChar(char c, int x, int y, bool draw_background) {
1347
1348 if(draw_background) {
1349 _instrument->DrawQuad(x, y, x+6, y+8, true);
1350 }
1351
1352 if(c < 33) return; // space
1353
1354 ++y; // Shift the char up by one pixel
1355 for(int j=7; j>=0; --j) {
1356 char c1 = UpperAlpha[c-33][j];
1357 // Don't do the last column for now (ie. j = 1, not 0)
1358 for(int i=5; i>=0; --i) {
1359 if(c1 & (01 << i)) {
1360 _instrument->DrawPixel(x, y);
1361 }
1362 ++x;
1363 }
1364 x -= 6;
1365 ++y;
1366 }
1367 }
1368
1369 // Takes instrument pixel co-ordinates.
1370 // Position is specified by the bottom of the *visible* portion, by default the left position unless align_right is true.
1371 // The return value is the pixel width of the visible portion
1372 int KLN89::DrawSmallChar(char c, int x, int y, bool align_right) {
1373 // calculate the index into the SmallChar array
1374 int idx;
1375 if(c > 47 && c < 58) {
1376 // number
1377 idx = c - 48;
1378 } else if(c > 64 && c < 91) {
1379 // Uppercase letter
1380 idx = c - 55;
1381 } else {
1382 return(0);
1383 }
1384
1385 char n = SmallChar[idx][0]; // Width of visible portion
1386 if(align_right) x -= n;
1387
1388 // background
1389 _instrument->DrawQuad(x - 1, y - 1, x + n, y + 5, true);
1390
1391 for(int j=7; j>=3; --j) {
1392 char c1 = SmallChar[idx][j];
1393 for(int i=n-1; i>=0; --i) {
1394 if(c1 & (01 << i)) {
1395 _instrument->DrawPixel(x, y);
1396 }
1397 ++x;
1398 }
1399 x -= n;
1400 ++y;
1401 }
1402
1403 return(n);
1404 }
1405
1406 // Takes character position
1407 void KLN89::DrawSpecialChar(char c, int field, int cx, int cy, bool bold) {
1408 if(c > 7) {
1409 cout << "ERROR - requested special char outside array bounds!\n";
1410 return; // Increment this as SpecialChar grows
1411 }
1412
1413 // Convert character to pixel position.
1414 // Ignore field for now
1415 // Add some border
1416 int px = cx * 7 + _xBorder + _xFieldBorder[field] + _xFieldStart[field];
1417 int py = cy * 9 + _yBorder + _yFieldBorder[field] + _yFieldStart[field];
1418 ++py; // Total hack - the special chars were coming out 1 pixel too low!
1419 for(int i=7; i>=0; --i) {
1420 char c1 = SpecialChar[(int)c][i];
1421 // Don't do the last column for now (ie. j = 1, not 0)
1422 for(int j=5; j>=0; --j) {
1423 if(c1 & (01 << j)) {
1424 _instrument->DrawPixel(px, py);
1425 }
1426 ++px;
1427 }
1428 px -= 6;
1429 ++py;
1430 }
1431 }
1432
1433 void KLN89::DrawText(const string& s, int field, int px, int py, bool bold, int invert) {
1434 for(int i = 0; i < (int)s.size(); ++i) {
1435 DrawChar(s[(unsigned int)i], field, px+i, py, bold, (invert == i || invert == 99));
1436 }
1437 }
1438
1439 void KLN89::DrawMapText(const string& s, int x, int y, bool draw_background) {
1440 MapToInstrument(x, y);
1441 if(draw_background) {
1442 //_instrument->DrawQuad(x, y, x + (7 * s.size()) - 1, y + 8, true);
1443 _instrument->DrawQuad(x - 1, y, x + (7 * s.size()) - 2, y + 8, true);
1444 // The minus 1 and minus 2 are an ugly hack to disguise the fact that I've lost track of exactly what's going on!
1445 }
1446
1447 for(int i = 0; i < (int)s.size(); ++i) {
1448 DrawFreeChar(s[(unsigned int)i], x+(i * 7)-1, y);
1449 }
1450 }
1451
1452 void KLN89::DrawLatitude(double d, int field, int px, int py) {
1453 DrawChar((d >= 0 ? 'N' : 'S'), field, px, py);
1454 d = fabs(d);
1455 px += 1;
1456 // TODO: sanity check input to ensure major lat field can only ever by 2 chars wide
1457 char buf[8];
1458 // Don't know whether to zero pad the below for single digits or not?
1459 //cout << d << ", " << (int)d << '\n';
1460 // 3 not 2 in size before for trailing \0
1461 int n = snprintf(buf, 3, "%i", (int)d);
1462 string s = buf;
1463 //cout << s << "... " << n << '\n';
1464 DrawText(s, field, px+(3-n), py);
1465 n = snprintf(buf, 7, "%05.2f'", ((double)(d - (int)d)) * 60.0f);
1466 s = buf;
1467 px += 3;
1468 DrawSpecialChar(0, field, px, py); // Degrees symbol
1469 px++;
1470 DrawText(s, field, px, py);
1471 }
1472
1473 void KLN89::DrawLongitude(double d, int field, int px, int py) {
1474 DrawChar((d >= 0 ? 'E' : 'W'), field, px, py);
1475 d = fabs(d);
1476 px += 1;
1477 // TODO: sanity check input to ensure major lat field can only ever be 2 chars wide
1478 char buf[8];
1479 // Don't know whether to zero pad the below for single digits or not?
1480 //cout << d << ", " << (int)d << '\n';
1481 // 4 not 3 in size before for trailing \0
1482 int n = snprintf(buf, 4, "%i", (int)d);
1483 string s = buf;
1484 //cout << s << "... " << n << '\n';
1485 DrawText(s, field, px+(3-n), py);
1486 n = snprintf(buf, 7, "%05.2f'", ((double)(d - (int)d)) * 60.0f);
1487 s = buf;
1488 px += 3;
1489 DrawSpecialChar(0, field, px, py); // Degrees symbol
1490 px++;
1491 DrawText(s, field, px, py);
1492 }
1493
1494 void KLN89::DrawFreq(double d, int field, int px, int py) {
1495 if(d >= 1000) d /= 100.0f;
1496 char buf[8];
1497 snprintf(buf, 7, "%6.2f", d);
1498 string s = buf;
1499 DrawText(s, field, px, py);
1500 }
1501
1502 void KLN89::DrawTime(double time, int field, int px, int py) {
1503 int hrs = (int)(time / 3600);
1504 int mins = (int)(ceil((time - (hrs * 3600)) / 60.0));
1505 char buf[10];
1506 int n;
1507 if(time >= 60.0) {
1508 // Draw hr:min
1509 n = snprintf(buf, 9, "%i:%02i", hrs, mins);
1510 } else {
1511 // Draw :secs
1512 n = snprintf(buf, 4, ":%02i", (int)time);
1513 }
1514 string s = buf;
1515 DrawText(s, field, px - n + 1, py);
1516 }
1517
1518 void KLN89::DrawHeading(int h, int field, int px, int py) {
1519 char buf[4];
1520 snprintf(buf, 4, "%i", h);
1521 string s = buf;
1522 DrawText(s, field, px - s.size(), py);
1523 DrawSpecialChar(0, field, px, py); // Degrees symbol
1524 }
1525
1526 void KLN89::DrawDist(double d, int field, int px, int py) {
1527 d *= (_distUnits == GPS_DIST_UNITS_NM ? 1.0 : SG_NM_TO_METER * 0.001);
1528 char buf[10];
1529 snprintf(buf, 9, "%i", (int)(d + 0.5));
1530 string s = buf;
1531 s += (_distUnits == GPS_DIST_UNITS_NM ? "nm" : "Km");
1532 DrawText(s, field, px - s.size() + 1, py);
1533 }
1534
1535 void KLN89::DrawSpeed(double v, int field, int px, int py, int decimal) {
1536 // TODO: implement variable decimal places
1537 v *= (_velUnits == GPS_VEL_UNITS_KT ? 1.0 : 0.51444444444 * 0.001 * 3600.0);
1538 char buf[10];
1539 snprintf(buf, 9, "%i", (int)(v + 0.5));
1540 string s = buf;
1541 if(_velUnits == GPS_VEL_UNITS_KT) {
1542 s += "kt";
1543 DrawText(s, field, px - s.size() + 1, py);
1544 } else {
1545 DrawText(s, field, px - s.size() - 1, py);
1546 DrawKPH(field, px - 1, py);
1547 }
1548 }
1549
1550 void KLN89::DrawDirDistField(double lat, double lon, int field, int px, int py, bool to_flag, bool cursel) {
1551 DrawChar('>', field, px, py);
1552 char buf[8];
1553 double h;
1554 if(to_flag) {
1555 h = GetMagHeadingFromTo(_gpsLat, _gpsLon, lat, lon);
1556 } else {
1557 h = GetMagHeadingFromTo(lat, lon, _gpsLat, _gpsLon);
1558 }
1559 while(h < 0.0) h += 360.0;
1560 while(h > 360.0) h -= 360.0;
1561 snprintf(buf, 4, "%3i", (int)(h + 0.5));
1562 string s = buf;
1563 if(!(cursel && _blink)) {
1564 DrawText(s, field, px + 4 - s.size(), py);
1565 DrawSpecialChar(0, field, px+4, py);
1566 DrawText((to_flag ? "To" : "Fr"), field, px+5, py);
1567 if(cursel) Underline(field, px + 1, py, 6);
1568 }
1569 //double d = GetHorizontalSeparation(_gpsLat, _gpsLon, lat, lon);
1570 //d *= (_distUnits == GPS_DIST_UNITS_NM ? SG_METER_TO_NM : 0.001);
1571 double d = GetGreatCircleDistance(_gpsLat, _gpsLon, lat, lon);
1572 d *= (_distUnits == GPS_DIST_UNITS_NM ? 1.0 : SG_NM_TO_METER * 0.001);
1573 if(d >= 100.0) {
1574 snprintf(buf, 7, "%5i", (int)(d + 0.5));
1575 } else {
1576 snprintf(buf, 7, "%4.1f", d);
1577 }
1578 s = buf;
1579 DrawText(s, field, px + 12 - s.size(), py);
1580 DrawText((_distUnits == GPS_DIST_UNITS_NM ? "nm" : "Km"), field, px + 12, py);
1581 }
1582
1583 char KLN89::IncChar(char c, bool gap, bool wrap) {
1584 if(c == '9') return(wrap ? (gap ? ' ' : 'A') : '9');
1585 if(c == 'Z') return('0');
1586 if(c == ' ') return('A');
1587 return(c + 1);
1588 }
1589
1590 char KLN89::DecChar(char c, bool gap, bool wrap) {
1591 if(c == 'A') return(wrap ? (gap ? ' ' : '9') : 'A');
1592 if(c == '0') return('Z');
1593 if(c == ' ') return('9');
1594 return(c - 1);
1595 }
1596
1597
1598 // Register the subsystem.
1599 #if 0
1600 SGSubsystemMgr::InstancedRegistrant<KLN89> registrantKLN89;
1601 #endif