1 // kln89_page.cxx - 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.
8 // Written by David Luff, started 2005.
10 // Copyright (C) 2005 - David C Luff - david.luff@nottingham.ac.uk
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.
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.
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.
29 #include "kln89_page.hxx"
30 #include "kln89_page_apt.hxx"
31 #include "kln89_page_vor.hxx"
32 #include "kln89_page_ndb.hxx"
33 #include "kln89_page_int.hxx"
34 #include "kln89_page_usr.hxx"
35 #include "kln89_page_act.hxx"
36 #include "kln89_page_nav.hxx"
37 #include "kln89_page_fpl.hxx"
38 #include "kln89_page_cal.hxx"
39 #include "kln89_page_set.hxx"
40 #include "kln89_page_oth.hxx"
41 #include "kln89_page_dir.hxx"
42 #include "kln89_page_nrst.hxx"
43 #include "kln89_symbols.hxx"
46 #include <ATCDCL/ATCProjection.hxx>
47 #include <Main/fg_props.hxx>
48 #include <simgear/math/SGMath.hxx>
49 #include <simgear/structure/commands.hxx>
50 #include <Airports/simple.hxx>
54 // Command callbacks for FlightGear
56 static bool do_kln89_msg_pressed(const SGPropertyNode* arg) {
57 //cout << "do_kln89_msg_pressed called!\n";
58 KLN89* gps = (KLN89*)globals->get_subsystem("kln89");
63 static bool do_kln89_obs_pressed(const SGPropertyNode* arg) {
64 //cout << "do_kln89_obs_pressed called!\n";
65 KLN89* gps = (KLN89*)globals->get_subsystem("kln89");
70 static bool do_kln89_alt_pressed(const SGPropertyNode* arg) {
71 //cout << "do_kln89_alt_pressed called!\n";
72 KLN89* gps = (KLN89*)globals->get_subsystem("kln89");
77 static bool do_kln89_nrst_pressed(const SGPropertyNode* arg) {
78 KLN89* gps = (KLN89*)globals->get_subsystem("kln89");
83 static bool do_kln89_dto_pressed(const SGPropertyNode* arg) {
84 KLN89* gps = (KLN89*)globals->get_subsystem("kln89");
89 static bool do_kln89_clr_pressed(const SGPropertyNode* arg) {
90 KLN89* gps = (KLN89*)globals->get_subsystem("kln89");
95 static bool do_kln89_ent_pressed(const SGPropertyNode* arg) {
96 KLN89* gps = (KLN89*)globals->get_subsystem("kln89");
101 static bool do_kln89_crsr_pressed(const SGPropertyNode* arg) {
102 KLN89* gps = (KLN89*)globals->get_subsystem("kln89");
107 static bool do_kln89_knob1left1(const SGPropertyNode* arg) {
108 KLN89* gps = (KLN89*)globals->get_subsystem("kln89");
113 static bool do_kln89_knob1right1(const SGPropertyNode* arg) {
114 KLN89* gps = (KLN89*)globals->get_subsystem("kln89");
119 static bool do_kln89_knob2left1(const SGPropertyNode* arg) {
120 KLN89* gps = (KLN89*)globals->get_subsystem("kln89");
125 static bool do_kln89_knob2right1(const SGPropertyNode* arg) {
126 KLN89* gps = (KLN89*)globals->get_subsystem("kln89");
131 // End command callbacks
133 KLN89::KLN89(RenderArea2D* instrument)
134 : DCLGPS(instrument) {
135 _mode = KLN89_MODE_DISP;
142 // ..Field..[0] => no fields in action
143 _xFieldBorder[0] = 0;
144 _xFieldBorder[1] = 0;
145 _yFieldBorder[0] = 0;
146 _yFieldBorder[1] = 0;
147 _xFieldBorder[2] = 2;
148 _yFieldBorder[2] = 0;
151 _xFieldStart[2] = 45;
161 KLN89Page* apt_page = new KLN89AptPage(this);
162 _pages.push_back(apt_page);
163 KLN89Page* vor_page = new KLN89VorPage(this);
164 _pages.push_back(vor_page);
165 KLN89Page* ndb_page = new KLN89NDBPage(this);
166 _pages.push_back(ndb_page);
167 KLN89Page* int_page = new KLN89IntPage(this);
168 _pages.push_back(int_page);
169 KLN89Page* usr_page = new KLN89UsrPage(this);
170 _pages.push_back(usr_page);
171 KLN89Page* act_page = new KLN89ActPage(this);
172 _pages.push_back(act_page);
173 KLN89Page* nav_page = new KLN89NavPage(this);
174 _pages.push_back(nav_page);
175 KLN89Page* fpl_page = new KLN89FplPage(this);
176 _pages.push_back(fpl_page);
177 KLN89Page* cal_page = new KLN89CalPage(this);
178 _pages.push_back(cal_page);
179 KLN89Page* set_page = new KLN89SetPage(this);
180 _pages.push_back(set_page);
181 KLN89Page* oth_page = new KLN89OthPage(this);
182 _pages.push_back(oth_page);
183 _nPages = _pages.size();
187 _dir_page = new KLN89DirPage(this);
188 _nrst_page = new KLN89NrstPage(this);
190 _activePage = apt_page;
196 _maxFlightPlans = 26;
197 for(unsigned int i=0; i<_maxFlightPlans; ++i) {
198 GPSFlightPlan* fp = new GPSFlightPlan;
199 fp->waypoints.clear();
200 _flightPlans.push_back(fp);
202 _activeFP = _flightPlans[0];
206 _entRestoreCrsr = false;
213 _mapHeadingUpdateTimer = 0.0;
217 //_mapScaleIndex = 20;
218 _mapScaleIndex = 7; // I think that the above is more accurate for no-flightplan default, but this is more sane for initial testing!
219 _mapScaleAuto = true;
221 // 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
222 // TODO - do this better one day!
223 _airportTowns["KSFO"] = "San Francisco";
224 _airportTowns["KSQL"] = "San Carlos";
225 _airportTowns["KPAO"] = "Palo Alto";
226 _airportTowns["KNUQ"] = "Mountain View";
227 _airportTowns["KSJC"] = "San Jose";
228 _airportTowns["KRHV"] = "San Jose";
229 _airportTowns["E16"] = "San Martin";
230 _airportTowns["KWVI"] = "Watsonville";
231 _airportTowns["KOAK"] = "Oakland";
232 _airportTowns["KHWD"] = "Hayward";
233 _airportTowns["KLVK"] = "Livermore";
234 _airportTowns["KCCR"] = "Concord";
235 _airportTowns["KTCY"] = "Tracy";
236 _airportTowns["KSCK"] = "Stockton";
237 _airportTowns["KHAF"] = "Half Moon Bay";
239 _airportStates["KSFO"] = "CA";
240 _airportStates["KSQL"] = "CA";
241 _airportStates["KPAO"] = "CA";
242 _airportStates["KNUQ"] = "CA";
243 _airportStates["KSJC"] = "CA";
244 _airportStates["KRHV"] = "CA";
245 _airportStates["E16"] = "CA";
246 _airportStates["KWVI"] = "CA";
247 _airportStates["KOAK"] = "CA";
248 _airportStates["KHWD"] = "CA";
249 _airportStates["KLVK"] = "CA";
250 _airportStates["KCCR"] = "CA";
251 _airportStates["KTCY"] = "CA";
252 _airportStates["KSCK"] = "CA";
253 _airportStates["KHAF"] = "CA";
257 for(unsigned int i=0; i<_pages.size(); ++i) {
264 for(unsigned int i=0; i<_maxFlightPlans; ++i) {
266 delete _flightPlans[i];
271 fgTie("/instrumentation/gps/message-alert", this, &KLN89::GetMsgAlert);
275 void KLN89::unbind() {
276 fgUntie("/instrumentation/gps/message-alert");
281 globals->get_commands()->addCommand("kln89_msg_pressed", do_kln89_msg_pressed);
282 globals->get_commands()->addCommand("kln89_obs_pressed", do_kln89_obs_pressed);
283 globals->get_commands()->addCommand("kln89_alt_pressed", do_kln89_alt_pressed);
284 globals->get_commands()->addCommand("kln89_nrst_pressed", do_kln89_nrst_pressed);
285 globals->get_commands()->addCommand("kln89_dto_pressed", do_kln89_dto_pressed);
286 globals->get_commands()->addCommand("kln89_clr_pressed", do_kln89_clr_pressed);
287 globals->get_commands()->addCommand("kln89_ent_pressed", do_kln89_ent_pressed);
288 globals->get_commands()->addCommand("kln89_crsr_pressed", do_kln89_crsr_pressed);
289 globals->get_commands()->addCommand("kln89_knob1left1", do_kln89_knob1left1);
290 globals->get_commands()->addCommand("kln89_knob1right1", do_kln89_knob1right1);
291 globals->get_commands()->addCommand("kln89_knob2left1", do_kln89_knob2left1);
292 globals->get_commands()->addCommand("kln89_knob2right1", do_kln89_knob2right1);
297 void KLN89::update(double dt) {
298 // Run any positional calc's required first
314 _mapHeadingUpdateTimer += dt;
315 if(_mapHeadingUpdateTimer > 1.0) {
317 _mapHeadingUpdateTimer = 0.0;
320 _instrument->Flush();
321 _instrument->DrawBackground();
324 if(_messageStack.empty()) {
325 DrawText("No Message", 0, 5, 2);
327 // TODO - parse the message string for special strings that indicate degrees signs etc!
328 DrawText(*_messageStack.begin(), 0, 0, 3);
332 if(!_messageStack.empty()) {
337 if(_curPage == 6 && _activePage->GetSubPage() == 3) {
338 // Don't draw the bar on the nav-4 page
339 } else if(_activePage == _nrst_page) {
340 // Don't draw the bar on the nearest page
345 _activePage->Update(dt);
348 void KLN89::CreateDefaultFlightPlans() {
349 // TODO - read these in from preferences.xml or similar instead!!!!
350 // Create some hardwired default flightplans for testing.
352 vector<GPSWpType> wps;
356 ids.push_back("KLSN");
357 wps.push_back(GPS_WP_APT);
358 ids.push_back("VOLTA");
359 wps.push_back(GPS_WP_INT);
360 ids.push_back("C83");
361 wps.push_back(GPS_WP_APT);
362 CreateFlightPlan(_flightPlans[5], ids, wps);
366 ids.push_back("KCCR");
367 wps.push_back(GPS_WP_APT);
368 ids.push_back("KHAF");
369 wps.push_back(GPS_WP_APT);
370 CreateFlightPlan(_flightPlans[4], ids, wps);
374 ids.push_back("KLVK");
375 wps.push_back(GPS_WP_APT);
376 ids.push_back("OAK");
377 wps.push_back(GPS_WP_VOR);
378 ids.push_back("PORTE");
379 wps.push_back(GPS_WP_INT);
380 ids.push_back("KHAF");
381 wps.push_back(GPS_WP_APT);
382 CreateFlightPlan(_flightPlans[3], ids, wps);
386 ids.push_back("KDPA");
387 wps.push_back(GPS_WP_APT);
388 ids.push_back("OBK");
389 wps.push_back(GPS_WP_VOR);
390 ids.push_back("ENW");
391 wps.push_back(GPS_WP_VOR);
392 ids.push_back("KRAC");
393 wps.push_back(GPS_WP_APT);
394 CreateFlightPlan(_flightPlans[2], ids, wps);
395 //cout << "Size of FP2 WP list is " << _flightPlans[2]->waypoints.size() << '\n';
399 ids.push_back("KSFO");
400 ids.push_back("KOAK");
401 wps.push_back(GPS_WP_APT);
402 wps.push_back(GPS_WP_APT);
403 CreateFlightPlan(_flightPlans[1], ids, wps);
407 //ids.push_back("KOSH");
408 ids.push_back("KSFO");
409 ids.push_back("KHAF");
410 ids.push_back("OSI");
411 ids.push_back("KSQL");
412 //ids.push_back("KPAO");
413 //ids.push_back("KHWD");
414 wps.push_back(GPS_WP_APT);
415 wps.push_back(GPS_WP_APT);
416 wps.push_back(GPS_WP_VOR);
417 wps.push_back(GPS_WP_APT);
418 //wps.push_back(GPS_WP_APT);
419 //wps.push_back(GPS_WP_APT);
420 CreateFlightPlan(_flightPlans[0], ids, wps);
425 ids.push_back("KLVK");
426 ids.push_back("KHWD");
427 wps.push_back(GPS_WP_APT);
428 wps.push_back(GPS_WP_APT);
429 CreateFlightPlan(_flightPlans[0], ids, wps);
433 void KLN89::Knob1Right1() {
434 if(_mode == KLN89_MODE_DISP) {
435 _activePage->LooseFocus();
436 if(_cleanUpPage >= 0) {
437 _pages[(unsigned int)_cleanUpPage]->CleanUp();
441 if(_curPage >= _pages.size()) _curPage = 0;
442 _activePage = _pages[_curPage];
444 _activePage->Knob1Right1();
449 void KLN89::Knob1Left1() {
450 if(_mode == KLN89_MODE_DISP) {
451 _activePage->LooseFocus();
452 if(_cleanUpPage >= 0) {
453 _pages[(unsigned int)_cleanUpPage]->CleanUp();
457 _curPage = _pages.size() - 1;
461 _activePage = _pages[_curPage];
463 _activePage->Knob1Left1();
468 void KLN89::Knob2Left1() {
469 _activePage->Knob2Left1();
472 void KLN89::Knob2Right1() {
473 _activePage->Knob2Right1();
476 void KLN89::CrsrPressed() {
478 // CRSR cannot be switched off on nrst page.
479 if(_activePage == _nrst_page) { return; }
480 // CRSR is always off when inner-knob is out on nav4 page.
481 if(_curPage == 6 && _activePage->GetSubPage() == 3 && fgGetBool("/instrumentation/kln89/scan-pull")) { return; }
482 if(_cleanUpPage >= 0) {
483 _pages[(unsigned int)_cleanUpPage]->CleanUp();
486 _entRestoreCrsr = false;
488 ((KLN89Page*)_activePage)->SetEntInvert(false);
489 if(_mode == KLN89_MODE_DISP) {
490 _mode = KLN89_MODE_CRSR;
491 _activePage->CrsrPressed();
493 _mode = KLN89_MODE_DISP;
494 _activePage->CrsrPressed();
499 void KLN89::EntPressed() {
502 // one of the data pages. Signal ent pressed to it here, and ent pressed to the call back page a few lines further down.
503 // Ie. 2 ent pressed signals in this case is deliberate.
504 _activePage->EntPressed();
507 _activePage = _pages[(unsigned int)_entJump];
508 if(_entRestoreCrsr) _mode = KLN89_MODE_CRSR;
511 if(_activePage == _dir_page) {
512 _dir_page->EntPressed();
513 _mode = KLN89_MODE_DISP;
514 _activePage = _pages[_curPage];
516 _activePage->EntPressed();
520 void KLN89::ClrPressed() {
521 _activePage->ClrPressed();
524 void KLN89::DtoPressed() {
525 if(_activePage != _dir_page) {
526 // Figure out which waypoint the dir page should display
528 // Apt, Vor, Ndb, Int, Usr or Act
529 if(!_activePage->GetId().empty()) { // Guard against no user waypoints defined
530 _dir_page->SetId(_activePage->GetId());
532 _dir_page->SetId(_activeWaypoint.id);
534 } else if(_curPage == 6 && _activePage->GetSubPage() == 3 && fgGetBool("/instrumentation/kln89/scan-pull") && _activeFP->waypoints.size()) {
536 _dir_page->SetId(((KLN89NavPage*)_activePage)->GetNav4WpId());
537 } else if(_curPage == 7 && _activePage->GetSubPage() == 0 && _mode == KLN89_MODE_CRSR) {
538 //cout << "Checking the fpl page!\n";
540 if(!_activePage->GetId().empty()) {
541 //cout << "Not empty!!!\n";
542 _dir_page->SetId(_activePage->GetId());
544 //cout << "empty :-(\n";
545 _dir_page->SetId(_activeWaypoint.id);
548 _dir_page->SetId(_activeWaypoint.id);
550 // This need to come after the bit before otherwise the FPL or NAV4 page clears their current ID when it looses focus.
551 _activePage->LooseFocus();
552 _activePage = _dir_page;
553 _mode = KLN89_MODE_CRSR;
557 void KLN89::NrstPressed() {
558 if(_activePage != _nrst_page) {
559 _activePage->LooseFocus(); // TODO - check whether we should call loose focus here
560 _lastActivePage = _activePage;
561 _activePage = _nrst_page;
563 _mode = KLN89_MODE_CRSR;
565 _activePage = _lastActivePage;
570 void KLN89::AltPressed() {}
572 void KLN89::OBSPressed() {
576 _mode = KLN89_MODE_CRSR;
577 _activePage->OBSPressed();
581 void KLN89::MsgPressed() {
582 // TODO - handle persistent messages such as SUA alerting.
583 // (The message annunciation flashes before first view, but afterwards remains continuously lit with the message available
584 // until the potential conflict no longer pertains).
585 if(_dispMsg && _messageStack.size()) {
586 _messageStack.pop_front();
588 _dispMsg = !_dispMsg;
591 void KLN89::ToggleOBSMode() {
592 DCLGPS::ToggleOBSMode();
595 void KLN89::DrawBar(int page) {
596 int px = 1 + (page * 15);
598 for(int i=0; i<7; ++i) {
599 // Ugh - this is crude and inefficient!
600 _instrument->DrawPixel(px+i, py);
601 _instrument->DrawPixel(px+i, py+1);
605 // Convert moving map to instrument co-ordinates
606 void KLN89::MapToInstrument(int &x, int &y) {
607 x += _xBorder + _xFieldBorder[2] + _xFieldStart[2];
610 // Draw a pixel specified in instrument co-ords, but clipped to the map region
611 //void KLN89::DrawInstrMapPixel(int x, int y) {
614 // Clip, translate and draw a map pixel
615 // If we didn't need per-pixel clipping, it would be cheaper to translate object rather than pixel positions.
616 void KLN89::DrawMapPixel(int x, int y, bool invert) {
617 if(x < 0 || x > 111 || y < 0 || y > 39) return;
618 x += _xBorder + _xFieldBorder[2] + _xFieldStart[2];
619 _instrument->DrawPixel(x, y, invert);
623 // HACK - use something FG provides
624 static double gps_min(const double &a, const double &b) {
625 return(a <= b ? a : b);
628 static double gps_max(const double &a, const double &b) {
629 return(a >= b ? a : b);
632 void KLN89::UpdateMapHeading() {
633 switch(_mapOrientation) {
638 _mapHeading = _dtkTrue;
641 _mapHeading = _track;
646 // The screen area allocated to the moving map is 111 x 40 pixels.
647 // In North up mode, the user position marker is at 57, 20. (Map co-ords).
648 void KLN89::DrawMap(bool draw_avs) {
649 // Set the clipping region to the moving map part of the display
650 int xstart = _xBorder + _xFieldBorder[2] + _xFieldStart[2];
651 _instrument->SetClipRegion(xstart, 0, xstart + 110, 39);
653 _mapScaleUnits = (int)_distUnits;
654 _mapScale = (double)(KLN89MapScales[_mapScaleUnits][_mapScaleIndex]);
656 //cout << "Map scale = " << _mapScale << '\n';
658 double mapScaleMeters = _mapScale * (_mapScaleUnits == 0 ? SG_NM_TO_METER : 1000);
660 // TODO - use an aligned projection when either DTK or TK up!
661 FGATCAlignedProjection mapProj(SGGeod::fromRad(_gpsLon, _gpsLat), _mapHeading);
663 double meter_per_pix = (_mapOrientation == 0 ? mapScaleMeters / 20.0f : mapScaleMeters / 29.0f);
665 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));
666 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));
668 // Draw Airport labels first (but not one's that are waypoints)
669 // Draw Airports first (but not one's that are waypoints)
670 // Ditto for VORs (not sure if SUA/VOR/Airport ordering is important or not).
674 // Then waypoint labels (not sure if this should be before or after waypoints)
676 // Annotation then gets drawn by Nav page, NOT this function.
678 if(_drawApt && draw_avs) {
680 bool have_apt = _overlays->FindArpByRegion(&apt, bottomLeft.lat(), bottomLeft.lon(), topRight.lat(), topRight.lon());
681 //cout << "Vors enclosed are: ";
682 // Draw all the labels first...
683 for(unsigned int i=0; i<apt.size(); ++i) {
684 //cout << nav[i]->id << ' ';
685 Point3D p = mapProj.ConvertToLocal(Point3D(apt[i]->lon * SG_RADIANS_TO_DEGREES, apt[i]->lat * SG_RADIANS_TO_DEGREES, 0.0));
686 //cout << p << " .... ";
687 int mx = int(p.x() / meter_per_pix) + 56;
688 int my = int(p.y() / meter_per_pix) + (_mapOrientation == 0 ? 19 : 10);
689 //cout << "mx = " << mx << ", my = " << my << '\n';
690 bool right_align = (p.x() < 0.0);
691 DrawLabel(apt[i]->id, mx + (right_align ? -2 : 3), my + (p.y() < 0.0 ? -7 : 3), right_align);
692 // I think that we probably should have -1 in the right_align case above to match the real life instrument.
694 // ...and then all the Apts.
695 for(unsigned int i=0; i<apt.size(); ++i) {
696 //cout << nav[i]->id << ' ';
697 Point3D p = mapProj.ConvertToLocal(Point3D(apt[i]->lon * SG_RADIANS_TO_DEGREES, apt[i]->lat * SG_RADIANS_TO_DEGREES, 0.0));
698 //cout << p << " .... ";
699 int mx = int(p.x() / meter_per_pix) + 56;
700 int my = int(p.y() / meter_per_pix) + (_mapOrientation == 0 ? 19 : 10);
701 //cout << "mx = " << mx << ", my = " << my << '\n';
708 if(_drawVOR && draw_avs) {
709 Overlays::nav_array_type nav;
710 bool have_vor = _overlays->FindVorByRegion(&nav, bottomLeft.lat(), bottomLeft.lon(), topRight.lat(), topRight.lon());
711 //cout << "Vors enclosed are: ";
712 // Draw all the labels first...
713 for(unsigned int i=0; i<nav.size(); ++i) {
714 //cout << nav[i]->id << ' ';
715 Point3D p = mapProj.ConvertToLocal(Point3D(nav[i]->lon * SG_RADIANS_TO_DEGREES, nav[i]->lat * SG_RADIANS_TO_DEGREES, 0.0));
716 //cout << p << " .... ";
717 int mx = int(p.x() / meter_per_pix) + 56;
718 int my = int(p.y() / meter_per_pix) + (_mapOrientation == 0 ? 19 : 10);
719 //cout << "mx = " << mx << ", my = " << my << '\n';
720 bool right_align = (p.x() < 0.0);
721 DrawLabel(nav[i]->id, mx + (right_align ? -2 : 3), my + (p.y() < 0.0 ? -7 : 3), right_align);
722 // I think that we probably should have -1 in the right_align case above to match the real life instrument.
724 // ...and then all the VORs.
725 for(unsigned int i=0; i<nav.size(); ++i) {
726 //cout << nav[i]->id << ' ';
727 Point3D p = mapProj.ConvertToLocal(Point3D(nav[i]->lon * SG_RADIANS_TO_DEGREES, nav[i]->lat * SG_RADIANS_TO_DEGREES, 0.0));
728 //cout << p << " .... ";
729 int mx = int(p.x() / meter_per_pix) + 56;
730 int my = int(p.y() / meter_per_pix) + (_mapOrientation == 0 ? 19 : 10);
731 //cout << "mx = " << mx << ", my = " << my << '\n';
739 if(_activeFP->waypoints.size() > 1) {
740 vector<int> xvec, yvec, qvec; // qvec stores the quadrant that each waypoint label should
741 // be drawn in (relative to the waypoint).
742 // 1 = NE, 2 = SE, 3 = SW, 4 = NW.
743 double save_h = 0.0; // Each pass, save a heading from the previous one for label quadrant determination.
744 bool drawTrack = true;
745 for(unsigned int i=1; i<_activeFP->waypoints.size(); ++i) {
746 GPSWaypoint* wp0 = _activeFP->waypoints[i-1];
747 GPSWaypoint* wp1 = _activeFP->waypoints[i];
748 SGVec3d p0 = mapProj.ConvertToLocal(SGGeod::fromRad(wp0->lon, wp0->lat));
749 SGVec3d p1 = mapProj.ConvertToLocal(SGGeod::fromRad(wp1->lon, wp1->lat));
750 int mx0 = int(p0.x() / meter_per_pix) + 56;
751 int my0 = int(p0.y() / meter_per_pix) + (_mapOrientation == 0 ? 19 : 10);
752 int mx1 = int(p1.x() / meter_per_pix) + 56;
753 int my1 = int(p1.y() / meter_per_pix) + (_mapOrientation == 0 ? 19 : 10);
757 double h = GetGreatCircleCourse(wp0->lat, wp0->lon, wp1->lat, wp1->lon) * SG_RADIANS_TO_DEGREES;
758 // Adjust for map orientation
760 qvec.push_back(GetLabelQuadrant(h));
761 //cout << "i = " << i << ", h = " << h << ", qvec[0] = " << qvec[0] << '\n';
765 if(drawTrack) { DrawLine(mx0, my0, mx1, my1); }
767 double h = GetGreatCircleCourse(wp0->lat, wp0->lon, wp1->lat, wp1->lon) * SG_RADIANS_TO_DEGREES;
768 // Adjust for map orientation
770 qvec.push_back(GetLabelQuadrant(save_h, h));
772 save_h = GetGreatCircleCourse(wp1->lat, wp1->lon, wp0->lat, wp0->lon) * SG_RADIANS_TO_DEGREES;
773 // Adjust for map orientation
774 save_h -= _mapHeading;
775 if(i == _activeFP->waypoints.size() - 1) {
776 qvec.push_back(GetLabelQuadrant(save_h));
778 // Don't draw flight track beyond the missed approach point of an approach
779 if(_approachLoaded) {
780 //cout << "Waypoints are " << wp0->id << " and " << wp1->id << '\n';
781 //cout << "Types are " << wp0->appType << " and " << wp1->appType << '\n';
782 if(wp1->appType == GPS_MAP) {
787 // ASSERT(xvec.size() == yvec.size() == qvec.size() == _activeFP->waypoints.size());
788 for(unsigned int i=0; i<xvec.size(); ++i) {
789 DrawWaypoint(xvec[i], yvec[i]);
790 bool right_align = (qvec[i] > 2);
791 bool top = (qvec[i] == 1 || qvec[i] == 4);
792 // TODO - not sure if labels should be drawn in sequence with waypoints and flightpaths,
793 // or all before or all afterwards. Doesn't matter a huge deal though.
794 DrawLabel(_activeFP->waypoints[i]->id, xvec[i] + (right_align ? -2 : 3), yvec[i] + (top ? 3 : -7), right_align);
799 if(_mapOrientation == 0) {
802 } else if(_mapOrientation == 1) {
805 } else if(_mapOrientation == 2) {
810 // TODO - don't know what to do here!
813 // And finally, reset the clip region to stop the rest of the code going pear-shaped!
814 _instrument->ResetClipRegion();
817 // Get the quadrant to draw the label of the start or end waypoint (i.e. one with only one track from it).
818 // Heading specified FROM the waypoint.
822 int KLN89::GetLabelQuadrant(double h) {
823 while(h < 0.0) h += 360.0;
824 while(h > 360.0) h -= 360.0;
825 if(h < 90.0) return(3);
826 if(h < 180.0) return(4);
827 if(h < 270.0) return(1);
831 // Get the quadrant to draw the label of an en-route waypoint,
832 // with BOTH tracks specified as headings FROM the waypoint.
836 int KLN89::GetLabelQuadrant(double h1, double h2) {
837 while(h1 < 0.0) h1 += 360.0;
838 while(h1 > 360.0) h1 -= 360.0;
839 while(h2 < 0.0) h2 += 360.0;
840 while(h2 > 360.0) h2 -= 360.0;
841 double max_min_diff = 0.0;
843 for(int i=0; i<4; ++i) {
844 double h = 45 + (90 * i);
845 double diff1 = fabs(h - h1);
846 if(diff1 > 180) diff1 = 360 - diff1;
847 double diff2 = fabs(h - h2);
848 if(diff2 > 180) diff2 = 360 - diff2;
849 double min_diff = gps_min(diff1, diff2);
850 if(min_diff > max_min_diff) {
851 max_min_diff = min_diff;
855 //cout << "GetLabelQuadrant, h1 = " << h1 << ", h2 = " << h2 << ", quad = " << quad << '\n';
859 // Draw the diamond style of user pos
869 void KLN89::DrawUser1(int x, int y) {
870 MapToInstrument(x, y);
871 int min_j = 0, max_j = 0;
872 for(int i=-3; i<=3; ++i) {
873 for(int j=min_j; j<=max_j; ++j) {
874 _instrument->DrawPixel(x+j, y+i, (j == min_j || j == max_j ? true : false));
886 // Draw the airplane style of user pos
887 // Define the origin to be the midpoint of the *fuselage*
888 void KLN89::DrawUser2(int x, int y) {
889 MapToInstrument(x, y);
891 // Draw the background as three black quads first
892 _instrument->DrawQuad(x-2, y-3, x+2, y-1, true);
893 _instrument->DrawQuad(x-3, y, x+3, y+2, true);
894 _instrument->DrawQuad(x-1, y+3, x+1, y+3, true);
897 for(int j=y-2; j<=y+2; ++j) {
898 _instrument->DrawPixel(x, j);
900 for(int i=x-1; i<=x+1; ++i) {
901 _instrument->DrawPixel(i, y-2);
903 for(int i=x-2; i<=x+2; ++i) {
904 _instrument->DrawPixel(i, y+1);
907 _instrument->DrawQuad(x, y-2, x, y+2);
908 _instrument->DrawQuad(x-1, y-2, x+1, y-2);
909 _instrument->DrawQuad(x-2, y+1, x+2, y+1);
913 // Draw an airport symbol on the moving map
921 void KLN89::DrawApt(int x, int y) {
922 MapToInstrument(x, y);
926 for(i=x-1; i<=x+1; ++i) _instrument->DrawPixel(i, j, true);
928 for(i=x-2; i<=x+2; ++i) _instrument->DrawPixel(i, j, (i != x ? true : false));
930 for(i=x-2; i<=x+2; ++i) _instrument->DrawPixel(i, j, (abs(i - x) > 1 ? true : false));
932 for(i=x-2; i<=x+2; ++i) _instrument->DrawPixel(i, j, (i != x ? true : false));
934 for(i=x-1; i<=x+1; ++i) _instrument->DrawPixel(i, j, true);
937 // Draw a waypoint on the moving map
945 void KLN89::DrawWaypoint(int x, int y) {
946 MapToInstrument(x, y);
947 _instrument->SetDebugging(true);
949 // Draw black background
950 _instrument->DrawQuad(x-2, y-2, x+2, y+2, true);
952 // Draw the coloured square
954 for(int i=x-1; i<=x+1; ++i) {
955 for(int j=y-1; j<=y+1; ++j) {
956 _instrument->DrawPixel(i, j);
960 _instrument->DrawQuad(x-1, y-1, x+1, y+1);
962 _instrument->SetDebugging(false);
965 // Draw a VOR on the moving map
973 void KLN89::DrawVOR(int x, int y) {
974 // Cheat - draw a waypoint and then a black pixel in the middle.
975 // Need to call Waypoint draw *before* translating co-ords.
977 MapToInstrument(x, y);
978 _instrument->DrawPixel(x, y, true);
981 // Draw a line on the moving map
982 void KLN89::DrawLine(int x1, int y1, int x2, int y2) {
983 MapToInstrument(x1, y1);
984 MapToInstrument(x2, y2);
985 _instrument->DrawLine(x1, y1, x2, y2);
988 void KLN89::DrawMapUpArrow(int x, int y) {
989 MapToInstrument(x, y);
991 for(int j=0; j<7; ++j) {
992 _instrument->DrawPixel(x + 2, y + j);
995 _instrument->DrawQuad(x+2, y, x+2, y+6);
997 _instrument->DrawPixel(x, y+4);
998 _instrument->DrawPixel(x+1, y+5);
999 _instrument->DrawPixel(x+3, y+5);
1000 _instrument->DrawPixel(x+4, y+4);
1003 // Draw a quad on the moving map
1004 void KLN89::DrawMapQuad(int x1, int y1, int x2, int y2, bool invert) {
1005 MapToInstrument(x1, y1);
1006 MapToInstrument(x2, y2);
1007 _instrument->DrawQuad(x1, y1, x2, y2, invert);
1010 // Draw an airport or waypoint label on the moving map
1011 // Specify position by the map pixel co-ordinate of the left or right, bottom, of the *visible* portion of the label.
1012 // The black background quad will automatically overlap this by 1 pixel.
1013 void KLN89::DrawLabel(const string& s, int x1, int y1, bool right_align) {
1014 MapToInstrument(x1, y1);
1016 for(unsigned int i=0; i<s.size(); ++i) {
1018 x1 += DrawSmallChar(c, x1, y1);
1022 for(int i=(int)(s.size()-1); i>=0; --i) {
1024 x1 -= DrawSmallChar(c, x1, y1, right_align);
1030 void KLN89::DrawCDI() {
1032 for(int i=0; i<5; ++i) {
1033 DrawSpecialChar(2, 2, 3+i, 2);
1034 DrawSpecialChar(1, 2, 9+i, 2);
1038 int px = 8 * 7 + _xBorder + _xFieldBorder[field] + _xFieldStart[field] + 2;
1039 int py = 2 * 9 + _yBorder + _yFieldBorder[field] + _yFieldStart[field];
1042 // Every 7 pixels deflection left or right is one dot on the scale, and hence 1/5 FSD.
1043 // Maximum deflection is 37 pixels left, or 38 pixels right !?!
1044 double xtd = CalcCrossTrackDeviation();
1046 if(_cdiScaleTransition) {
1047 double dots = (xtd / _currentCdiScale) * 5.0;
1048 deflect = (int)(dots * 7.0 * -1.0);
1049 // TODO - for all these I think I should add 0.5 before casting to int, and *then* multiply by -1. Possibly!
1051 if(0 == _currentCdiScaleIndex) { // 5.0nm FSD => 1 nm per dot => 7 pixels per nm.
1052 deflect = (int)(xtd * 7.0 * -1.0); // The -1.0 is because we move the 'needle' indicating the course, not the plane.
1053 } else if(1 == _currentCdiScaleIndex) {
1054 deflect = (int)(xtd * 35.0 * -1.0);
1055 } else { // 0.3 == _cdiScale
1056 deflect = (int)(xtd * 116.6666666667 * -1.0);
1059 if(deflect > 38) deflect = 38;
1060 if(deflect < -37) deflect = -37;
1062 for(int j=0; j<9; ++j) {
1063 _instrument->DrawPixel(px + deflect, py+j);
1064 _instrument->DrawPixel(px + deflect + 1, py+j);
1067 _instrument->DrawQuad(px + deflect, py, px + deflect + 1, py + 8);
1070 // To/From indicator
1073 for(int j=4; j>=0; --j) {
1075 for(int i=0; i<k; ++i) {
1076 _instrument->DrawPixel(px+j+i, (_headingBugTo ? py+j : py+4-j));
1077 // At the extremities, draw the outlining dark pixel
1078 if(i == 0 || i == k-1) {
1079 _instrument->DrawPixel(px+j+i, (_headingBugTo ? py+j+1 : py+3-j), true);
1085 void KLN89::DrawLegTail(int py) {
1086 int px = 0 * 7 + _xBorder + _xFieldBorder[2] + _xFieldStart[2];
1087 py = py * 9 + _yBorder + _yFieldBorder[2] + _yFieldStart[2];
1091 py++; // Hack - not sure if this represents a border issue.
1093 for(int i=0; i<9; ++i) _instrument->DrawPixel(px, py+i);
1094 for(int i2=0; i2<5; ++i2) _instrument->DrawPixel(px+i2, py+9);
1097 void KLN89::DrawLongLegTail(int py) {
1098 int px = 0 * 7 + _xBorder + _xFieldBorder[2] + _xFieldStart[2];
1099 py = py * 9 + _yBorder + _yFieldBorder[2] + _yFieldStart[2];
1103 py++; // Hack - not sure if this represents a border issue.
1105 for(int i=0; i<18; ++i) _instrument->DrawPixel(px, py+i);
1106 for(int i2=0; i2<5; ++i2) _instrument->DrawPixel(px+i2, py+18);
1109 void KLN89::DrawHalfLegTail(int py) {
1112 void KLN89::DrawDivider() {
1113 int px = _xFieldStart[2] - 1;
1115 for(int i=0; i<36; ++i) {
1116 _instrument->DrawPixel(px, py+i);
1120 void KLN89::DrawEnt(int field, int px, int py) {
1121 px = px * 7 + _xBorder + _xFieldBorder[field] + _xFieldStart[field];
1122 py = py * 9 + _yBorder + _yFieldBorder[field] + _yFieldStart[field] + 1;
1124 px++; // Not sure why we need px++, but it seems to work!
1128 for(int i=0; i<5; ++i) _instrument->DrawPixel(px, py+i);
1129 _instrument->DrawPixel(px+1, py);
1130 _instrument->DrawPixel(px+2, py);
1131 _instrument->DrawPixel(px+1, py+2);
1132 _instrument->DrawPixel(px+1, py+4);
1133 _instrument->DrawPixel(px+2, py+4);
1137 for(int i=0; i<4; ++i) _instrument->DrawPixel(px, py+i);
1138 _instrument->DrawPixel(px+1, py+2);
1139 _instrument->DrawPixel(px+2, py+1);
1140 for(int i=0; i<4; ++i) _instrument->DrawPixel(px+3, py+i);
1144 _instrument->DrawPixel(px, py+3);
1145 for(int i=0; i<4; ++i) _instrument->DrawPixel(px+1, py+i);
1146 _instrument->DrawPixel(px+2, py+3);
1149 void KLN89::DrawMessageAlert() {
1150 // TODO - draw the proper message indicator
1152 int px = _xBorder + _xFieldBorder[1] + _xFieldStart[1];
1153 int py = 1 * 9 + _yBorder + _yFieldBorder[1] + _yFieldStart[1] + 1;
1155 px++; // Not sure why we need px++, but it seems to work!
1158 DrawText(" ", 1, 0, 1, false, 99);
1159 _instrument->DrawQuad(px+1, py-1, px+2, py+5, true);
1160 _instrument->DrawQuad(px+3, py+3, px+3, py+5, true);
1161 _instrument->DrawQuad(px+4, py+2, px+4, py+4, true);
1162 _instrument->DrawQuad(px+5, py+1, px+6, py+3, true);
1163 _instrument->DrawQuad(px+7, py+2, px+7, py+4, true);
1164 _instrument->DrawQuad(px+8, py+3, px+8, py+5, true);
1165 _instrument->DrawQuad(px+9, py-1, px+10, py+5, true);
1169 void KLN89::Underline(int field, int px, int py, int len) {
1170 px = px * 7 + _xBorder + _xFieldBorder[field] + _xFieldStart[field];
1171 py = py * 9 + _yBorder + _yFieldBorder[field] + _yFieldStart[field];
1172 for(int i=0; i<(len*7); ++i) {
1173 _instrument->DrawPixel(px, py);
1178 void KLN89::DrawKPH(int field, int cx, int cy) {
1180 int px = cx * 7 + _xBorder + _xFieldBorder[field] + _xFieldStart[field];
1181 int py = cy * 9 + _yBorder + _yFieldBorder[field] + _yFieldStart[field];
1186 for(int j=0; j<=4; ++j) {
1187 _instrument->DrawPixel(px, py + 2 +j);
1188 _instrument->DrawPixel(px + 8, py + j);
1190 _instrument->DrawPixel(px + 11, py + j);
1191 _instrument->DrawPixel(px + 9 + j, py + 2);
1195 for(int i=0; i<=6; ++i) {
1197 _instrument->DrawPixel(px + 1 + i, py + 4 + i);
1198 _instrument->DrawPixel(px + 1 + i, py + (4 - i));
1200 _instrument->DrawPixel(px + 2 + i, py + i);
1204 void KLN89::DrawDTO(int field, int cx, int cy) {
1205 DrawSpecialChar(6, field, cx, cy);
1206 if(!(_waypointAlert && _blink)) {
1207 DrawSpecialChar(3, field, cx+1, cy);
1210 int px = cx * 7 + _xBorder + _xFieldBorder[field] + _xFieldStart[field];
1211 int py = cy * 9 + _yBorder + _yFieldBorder[field] + _yFieldStart[field];
1216 // Fill in the gap between the 'D' and the arrow.
1217 _instrument->DrawPixel(px+5, py+3);
1220 // Takes character position
1221 void KLN89::DrawChar(char c, int field, int px, int py, bool bold, bool invert) {
1222 // Ignore field for now
1224 px = px * 7 + _xBorder + _xFieldBorder[field] + _xFieldStart[field];
1225 py = py * 9 + _yBorder + _yFieldBorder[field] + _yFieldStart[field];
1227 // Draw an orange background for inverted characters
1229 for(int i=0; i<7; ++i) {
1230 for(int j=0; j<9; ++j) {
1231 _instrument->DrawPixel(px + i, py + j);
1236 if(c < 33) return; // space
1238 // Render normal decimal points in bold floats
1239 if(c == '.') bold = false;
1241 ++py; // Shift the char up by one pixel
1242 for(int j=7; j>=0; --j) {
1243 char c1 = (bold ? NumbersBold[c-48][j] : UpperAlpha[c-33][j]);
1244 // Don't do the last column for now (ie. j = 1, not 0)
1245 for(int i=5; i>=0; --i) {
1246 if(c1 & (01 << i)) {
1247 _instrument->DrawPixel(px, py, invert);
1256 // Takes pixel position
1257 void KLN89::DrawFreeChar(char c, int x, int y, bool draw_background) {
1259 if(draw_background) {
1260 _instrument->DrawQuad(x, y, x+6, y+8, true);
1263 if(c < 33) return; // space
1265 ++y; // Shift the char up by one pixel
1266 for(int j=7; j>=0; --j) {
1267 char c1 = UpperAlpha[c-33][j];
1268 // Don't do the last column for now (ie. j = 1, not 0)
1269 for(int i=5; i>=0; --i) {
1270 if(c1 & (01 << i)) {
1271 _instrument->DrawPixel(x, y);
1280 // Takes instrument pixel co-ordinates.
1281 // Position is specified by the bottom of the *visible* portion, by default the left position unless align_right is true.
1282 // The return value is the pixel width of the visible portion
1283 int KLN89::DrawSmallChar(char c, int x, int y, bool align_right) {
1284 // calculate the index into the SmallChar array
1286 if(c > 47 && c < 58) {
1289 } else if(c > 64 && c < 91) {
1296 char n = SmallChar[idx][0]; // Width of visible portion
1297 if(align_right) x -= n;
1300 _instrument->DrawQuad(x - 1, y - 1, x + n, y + 5, true);
1302 for(int j=7; j>=3; --j) {
1303 char c1 = SmallChar[idx][j];
1304 for(int i=n-1; i>=0; --i) {
1305 if(c1 & (01 << i)) {
1306 _instrument->DrawPixel(x, y);
1317 // Takes character position
1318 void KLN89::DrawSpecialChar(char c, int field, int cx, int cy, bool bold) {
1320 cout << "ERROR - requested special char outside array bounds!\n";
1321 return; // Increment this as SpecialChar grows
1324 // Convert character to pixel position.
1325 // Ignore field for now
1327 int px = cx * 7 + _xBorder + _xFieldBorder[field] + _xFieldStart[field];
1328 int py = cy * 9 + _yBorder + _yFieldBorder[field] + _yFieldStart[field];
1329 ++py; // Total hack - the special chars were coming out 1 pixel too low!
1330 for(int i=7; i>=0; --i) {
1331 char c1 = SpecialChar[(int)c][i];
1332 // Don't do the last column for now (ie. j = 1, not 0)
1333 for(int j=5; j>=0; --j) {
1334 if(c1 & (01 << j)) {
1335 _instrument->DrawPixel(px, py);
1344 void KLN89::DrawText(const string& s, int field, int px, int py, bool bold, int invert) {
1345 for(int i = 0; i < (int)s.size(); ++i) {
1346 DrawChar(s[(unsigned int)i], field, px+i, py, bold, (invert == i || invert == 99));
1350 void KLN89::DrawMapText(const string& s, int x, int y, bool draw_background) {
1351 MapToInstrument(x, y);
1352 if(draw_background) {
1353 //_instrument->DrawQuad(x, y, x + (7 * s.size()) - 1, y + 8, true);
1354 _instrument->DrawQuad(x - 1, y, x + (7 * s.size()) - 2, y + 8, true);
1355 // 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!
1358 for(int i = 0; i < (int)s.size(); ++i) {
1359 DrawFreeChar(s[(unsigned int)i], x+(i * 7)-1, y);
1363 void KLN89::DrawLatitude(double d, int field, int px, int py) {
1364 DrawChar((d >= 0 ? 'N' : 'S'), field, px, py);
1367 // TODO - sanity check input to ensure major lat field can only ever by 2 chars wide
1369 // Don't know whether to zero pad the below for single digits or not?
1370 //cout << d << ", " << (int)d << '\n';
1371 // 3 not 2 in size before for trailing \0
1372 int n = snprintf(buf, 3, "%i", (int)d);
1374 //cout << s << "... " << n << '\n';
1375 DrawText(s, field, px+(3-n), py);
1376 n = snprintf(buf, 7, "%05.2f'", ((double)(d - (int)d)) * 60.0f);
1379 DrawSpecialChar(0, field, px, py); // Degrees symbol
1381 DrawText(s, field, px, py);
1384 void KLN89::DrawLongitude(double d, int field, int px, int py) {
1385 DrawChar((d >= 0 ? 'E' : 'W'), field, px, py);
1388 // TODO - sanity check input to ensure major lat field can only ever be 2 chars wide
1390 // Don't know whether to zero pad the below for single digits or not?
1391 //cout << d << ", " << (int)d << '\n';
1392 // 4 not 3 in size before for trailing \0
1393 int n = snprintf(buf, 4, "%i", (int)d);
1395 //cout << s << "... " << n << '\n';
1396 DrawText(s, field, px+(3-n), py);
1397 n = snprintf(buf, 7, "%05.2f'", ((double)(d - (int)d)) * 60.0f);
1400 DrawSpecialChar(0, field, px, py); // Degrees symbol
1402 DrawText(s, field, px, py);
1405 void KLN89::DrawFreq(double d, int field, int px, int py) {
1406 if(d >= 1000) d /= 100.0f;
1408 snprintf(buf, 7, "%6.2f", d);
1410 DrawText(s, field, px, py);
1413 void KLN89::DrawTime(double time, int field, int px, int py) {
1414 int hrs = (int)(time / 3600);
1415 int mins = (int)(ceil((time - (hrs * 3600)) / 60.0));
1420 n = snprintf(buf, 9, "%i:%02i", hrs, mins);
1423 n = snprintf(buf, 4, ":%02i", (int)time);
1426 DrawText(s, field, px - n + 1, py);
1429 void KLN89::DrawHeading(int h, int field, int px, int py) {
1431 snprintf(buf, 4, "%i", h);
1433 DrawText(s, field, px - s.size(), py);
1434 DrawSpecialChar(0, field, px, py); // Degrees symbol
1437 void KLN89::DrawDist(double d, int field, int px, int py) {
1438 d *= (_distUnits == GPS_DIST_UNITS_NM ? 1.0 : SG_NM_TO_METER * 0.001);
1440 snprintf(buf, 9, "%i", (int)(d + 0.5));
1442 s += (_distUnits == GPS_DIST_UNITS_NM ? "nm" : "Km");
1443 DrawText(s, field, px - s.size() + 1, py);
1446 void KLN89::DrawSpeed(double v, int field, int px, int py, int decimal) {
1447 // TODO - implement variable decimal places
1448 v *= (_velUnits == GPS_VEL_UNITS_KT ? 1.0 : 0.51444444444 * 0.001 * 3600.0);
1450 snprintf(buf, 9, "%i", (int)(v + 0.5));
1452 if(_velUnits == GPS_VEL_UNITS_KT) {
1454 DrawText(s, field, px - s.size() + 1, py);
1456 DrawText(s, field, px - s.size() - 1, py);
1457 DrawKPH(field, px - 1, py);
1461 void KLN89::DrawDirDistField(double lat, double lon, int field, int px, int py, bool to_flag, bool cursel) {
1462 DrawChar('>', field, px, py);
1466 h = GetMagHeadingFromTo(_gpsLat, _gpsLon, lat, lon);
1468 h = GetMagHeadingFromTo(lat, lon, _gpsLat, _gpsLon);
1470 while(h < 0.0) h += 360.0;
1471 while(h > 360.0) h -= 360.0;
1472 snprintf(buf, 4, "%3i", (int)(h + 0.5));
1474 if(!(cursel && _blink)) {
1475 DrawText(s, field, px + 4 - s.size(), py);
1476 DrawSpecialChar(0, field, px+4, py);
1477 DrawText((to_flag ? "To" : "Fr"), field, px+5, py);
1478 if(cursel) Underline(field, px + 1, py, 6);
1480 //double d = GetHorizontalSeparation(_gpsLat, _gpsLon, lat, lon);
1481 //d *= (_distUnits == GPS_DIST_UNITS_NM ? SG_METER_TO_NM : 0.001);
1482 double d = GetGreatCircleDistance(_gpsLat, _gpsLon, lat, lon);
1483 d *= (_distUnits == GPS_DIST_UNITS_NM ? 1.0 : SG_NM_TO_METER * 0.001);
1485 snprintf(buf, 7, "%5i", (int)(d + 0.5));
1487 snprintf(buf, 7, "%4.1f", d);
1490 DrawText(s, field, px + 12 - s.size(), py);
1491 DrawText((_distUnits == GPS_DIST_UNITS_NM ? "nm" : "Km"), field, px + 12, py);
1494 char KLN89::IncChar(char c, bool gap, bool wrap) {
1495 if(c == '9') return(wrap ? (gap ? ' ' : 'A') : '9');
1496 if(c == 'Z') return('0');
1497 if(c == ' ') return('A');
1501 char KLN89::DecChar(char c, bool gap, bool wrap) {
1502 if(c == 'A') return(wrap ? (gap ? ' ' : '9') : 'A');
1503 if(c == '0') return('Z');
1504 if(c == ' ') return('9');