| 1 | // kln89_page_*.[ch]xx - this file is one of the "pages" that |
| 2 | // are used in the KLN89 GPS unit simulation. |
| 3 | // |
| 4 | // Written by David Luff, started 2005. |
| 5 | // |
| 6 | // Copyright (C) 2005 - David C Luff - daveluff AT ntlworld.com |
| 7 | // |
| 8 | // This program is free software; you can redistribute it and/or |
| 9 | // modify it under the terms of the GNU General Public License as |
| 10 | // published by the Free Software Foundation; either version 2 of the |
| 11 | // License, or (at your option) any later version. |
| 12 | // |
| 13 | // This program is distributed in the hope that it will be useful, but |
| 14 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | // General Public License for more details. |
| 17 | // |
| 18 | // You should have received a copy of the GNU General Public License |
| 19 | // along with this program; if not, write to the Free Software |
| 20 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 | // |
| 22 | // $Id$ |
| 23 | |
| 24 | #ifdef HAVE_CONFIG_H |
| 25 | # include "config.h" |
| 26 | #endif |
| 27 | |
| 28 | #include <cstdio> |
| 29 | |
| 30 | #include "kln89_page_vor.hxx" |
| 31 | #include <Navaids/navrecord.hxx> |
| 32 | |
| 33 | using std::string; |
| 34 | |
| 35 | KLN89VorPage::KLN89VorPage(KLN89* parent) |
| 36 | : KLN89Page(parent) { |
| 37 | _nSubPages = 2; |
| 38 | _subPage = 0; |
| 39 | _name = "VOR"; |
| 40 | _vor_id = "OSI"; // TODO: check a property for an initial value to allow user-override. |
| 41 | np = NULL; |
| 42 | } |
| 43 | |
| 44 | KLN89VorPage::~KLN89VorPage() { |
| 45 | } |
| 46 | |
| 47 | void KLN89VorPage::Update(double dt) { |
| 48 | bool actPage = (_kln89->_activePage->GetName() == "ACT" ? true : false); |
| 49 | bool multi; // Not set by FindFirst... |
| 50 | bool exact = false; |
| 51 | if(_vor_id.size() == 3) exact = true; |
| 52 | if(np == NULL) { |
| 53 | np = _kln89->FindFirstVorById(_vor_id, multi, exact); |
| 54 | } else if(np->get_ident() != _vor_id) { |
| 55 | np = _kln89->FindFirstVorById(_vor_id, multi, exact); |
| 56 | } |
| 57 | //if(np == NULL) cout << "NULL... "; |
| 58 | //if(b == false) cout << "false...\n"; |
| 59 | /* |
| 60 | if(np && b) { |
| 61 | cout << "VOR FOUND!\n"; |
| 62 | } else { |
| 63 | cout << ":-(\n"; |
| 64 | } |
| 65 | */ |
| 66 | if(np) { |
| 67 | //cout << np->id << '\n'; |
| 68 | _vor_id = np->get_ident(); |
| 69 | if(_kln89->GetActiveWaypoint()) { |
| 70 | if(_vor_id == _kln89->GetActiveWaypoint()->id) { |
| 71 | if(!(_kln89->_waypointAlert && _kln89->_blink)) { |
| 72 | // Active waypoint arrow |
| 73 | _kln89->DrawSpecialChar(4, 2, 0, 3); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | if(_kln89->_mode != KLN89_MODE_CRSR) { |
| 78 | if(!_entInvert) { |
| 79 | if(!actPage) { |
| 80 | _kln89->DrawText(np->get_ident(), 2, 1, 3); |
| 81 | } else { |
| 82 | // If it's the ACT page, The ID is shifted slightly right to make space for the waypoint index. |
| 83 | _kln89->DrawText(np->get_ident(), 2, 4, 3); |
| 84 | char buf[3]; |
| 85 | int n = snprintf(buf, 3, "%i", _kln89->GetActiveWaypointIndex() + 1); |
| 86 | _kln89->DrawText((string)buf, 2, 3 - n, 3); |
| 87 | } |
| 88 | } else { |
| 89 | if(!_kln89->_blink) { |
| 90 | _kln89->DrawText(np->get_ident(), 2, 1, 3, false, 99); |
| 91 | _kln89->DrawEnt(); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | if(_subPage == 0) { |
| 96 | //// TODO: will almost certainly have to process freq below for FG |
| 97 | _kln89->DrawFreq(np->get_freq(), 2, 9, 3); |
| 98 | // TODO: trim VOR-DME from the name, convert to uppercase, abbreviate, etc |
| 99 | _kln89->DrawText(np->name(), 2, 0, 2); |
| 100 | //cout << np->lat << "... "; |
| 101 | _kln89->DrawLatitude(np->get_lat(), 2, 3, 1); |
| 102 | _kln89->DrawLongitude(np->get_lon(), 2, 3, 0); |
| 103 | } else { |
| 104 | _kln89->DrawText("Mag Var", 2, 0, 2); |
| 105 | ////float mvf = np->magvar * SG_RADIANS_TO_DEGREES; |
| 106 | //// TODO FIXME BELOW |
| 107 | float mvf = 0.0; |
| 108 | _kln89->DrawChar((mvf <= 0 ? 'E' : 'W'), 2, 9, 2); |
| 109 | int mvi = (int)(fabs(mvf) + 0.5); |
| 110 | string mvs = GPSitoa(mvi); |
| 111 | _kln89->DrawText(mvs, 2, 13 - mvs.size(), 2); |
| 112 | _kln89->DrawSpecialChar(0, 2, 13, 2); |
| 113 | _kln89->DrawDirDistField(np->get_lat() * SG_DEGREES_TO_RADIANS, np->get_lon() * SG_DEGREES_TO_RADIANS, 2, 0, 0, |
| 114 | _to_flag, (_kln89->_mode == KLN89_MODE_CRSR && _uLinePos == 4 ? true : false)); |
| 115 | } |
| 116 | } else { |
| 117 | if(_kln89->_mode != KLN89_MODE_CRSR) _kln89->DrawText(_vor_id, 2, 1, 3); |
| 118 | if(_subPage == 0) { |
| 119 | _kln89->DrawText("---.--", 2, 9, 3); |
| 120 | _kln89->DrawText("--------------", 2, 0, 2); |
| 121 | _kln89->DrawText("- -- --.--'", 2, 3, 1); |
| 122 | _kln89->DrawText("---- --.--'", 2, 3, 0); |
| 123 | _kln89->DrawSpecialChar(0, 2, 7, 1); |
| 124 | _kln89->DrawSpecialChar(0, 2, 7, 0); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if(_kln89->_mode == KLN89_MODE_CRSR) { |
| 129 | if(_uLinePos > 0 && _uLinePos < 4) { |
| 130 | // TODO: blink as well |
| 131 | _kln89->Underline(2, _uLinePos, 3, 1); |
| 132 | } |
| 133 | for(unsigned int i = 0; i < _vor_id.size(); ++i) { |
| 134 | if(_uLinePos != (i + 1)) { |
| 135 | _kln89->DrawChar(_vor_id[i], 2, i + 1, 3); |
| 136 | } else { |
| 137 | if(!_kln89->_blink) _kln89->DrawChar(_vor_id[i], 2, i + 1, 3); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | _id = _vor_id; |
| 143 | |
| 144 | KLN89Page::Update(dt); |
| 145 | } |
| 146 | |
| 147 | void KLN89VorPage::SetId(const string& s) { |
| 148 | _last_vor_id = _vor_id; |
| 149 | _save_vor_id = _vor_id; |
| 150 | _vor_id = s; |
| 151 | np = NULL; |
| 152 | } |
| 153 | |
| 154 | void KLN89VorPage::CrsrPressed() { |
| 155 | if(_kln89->_mode == KLN89_MODE_DISP) return; |
| 156 | if(_kln89->_obsMode) { |
| 157 | _uLinePos = 0; |
| 158 | } else { |
| 159 | _uLinePos = 1; |
| 160 | } |
| 161 | if(_subPage == 0) { |
| 162 | _maxULinePos = 17; |
| 163 | } else { |
| 164 | _maxULinePos = 4; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | void KLN89VorPage::ClrPressed() { |
| 169 | if(_subPage == 1 && _uLinePos == 4) { |
| 170 | _to_flag = !_to_flag; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void KLN89VorPage::EntPressed() { |
| 175 | if(_entInvert) { |
| 176 | _entInvert = false; |
| 177 | _entInvert = false; |
| 178 | if(_kln89->_dtoReview) { |
| 179 | _kln89->DtoInitiate(_vor_id); |
| 180 | } else { |
| 181 | _last_vor_id = _vor_id; |
| 182 | _vor_id = _save_vor_id; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | void KLN89VorPage::Knob2Left1() { |
| 188 | if(_kln89->_mode != KLN89_MODE_CRSR || _uLinePos == 0) { |
| 189 | KLN89Page::Knob2Left1(); |
| 190 | } else { |
| 191 | if(_uLinePos < 4) { |
| 192 | // Same logic for both pages - set the ID |
| 193 | _vor_id = _vor_id.substr(0, _uLinePos); |
| 194 | // ASSERT(_uLinePos > 0); |
| 195 | if(_uLinePos == (_vor_id.size() + 1)) { |
| 196 | _vor_id += '9'; |
| 197 | } else { |
| 198 | _vor_id[_uLinePos - 1] = _kln89->DecChar(_vor_id[_uLinePos - 1], (_uLinePos == 1 ? false : true)); |
| 199 | } |
| 200 | } else { |
| 201 | if(_subPage == 0) { |
| 202 | // set by name |
| 203 | } else { |
| 204 | // NO-OP - from/to field is switched by clr button, not inner knob. |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | void KLN89VorPage::Knob2Right1() { |
| 211 | if(_kln89->_mode != KLN89_MODE_CRSR || _uLinePos == 0) { |
| 212 | KLN89Page::Knob2Right1(); |
| 213 | } else { |
| 214 | if(_uLinePos < 4) { |
| 215 | // Same logic for both pages - set the ID |
| 216 | _vor_id = _vor_id.substr(0, _uLinePos); |
| 217 | // ASSERT(_uLinePos > 0); |
| 218 | if(_uLinePos == (_vor_id.size() + 1)) { |
| 219 | _vor_id += 'A'; |
| 220 | } else { |
| 221 | _vor_id[_uLinePos - 1] = _kln89->IncChar(_vor_id[_uLinePos - 1], (_uLinePos == 1 ? false : true)); |
| 222 | } |
| 223 | } else { |
| 224 | if(_subPage == 0) { |
| 225 | // set by name |
| 226 | } else { |
| 227 | // NO-OP - from/to field is switched by clr button, not inner knob. |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | } |