| 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 --D0T-- 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 <cstdlib> |
| 29 | #include <cstdio> |
| 30 | |
| 31 | #include <Main/fg_props.hxx> |
| 32 | #include "kln89_page_cal.hxx" |
| 33 | |
| 34 | using std::string; |
| 35 | |
| 36 | KLN89CalPage::KLN89CalPage(KLN89* parent) |
| 37 | : KLN89Page(parent) { |
| 38 | _nSubPages = 8; |
| 39 | _subPage = 0; |
| 40 | _name = "CAL"; |
| 41 | _nFp0 = 0; |
| 42 | _ground_speed_ms = 110 * 0.514444444444; |
| 43 | _alarmAnnotate = false; |
| 44 | _alarmSet = false; |
| 45 | } |
| 46 | |
| 47 | KLN89CalPage::~KLN89CalPage() { |
| 48 | } |
| 49 | |
| 50 | void KLN89CalPage::Update(double dt) { |
| 51 | bool crsr = (_kln89->_mode == KLN89_MODE_CRSR); |
| 52 | bool blink = _kln89->_blink; |
| 53 | if(_subPage == 0) { |
| 54 | if(1) { // TODO: fix this hardwiring! |
| 55 | // Flightplan calc |
| 56 | _kln89->DrawText(">Fpl:", 2, 0, 3); |
| 57 | _kln89->DrawText("0", 2, 6, 3);// TODO: fix this hardwiring! |
| 58 | GPSFlightPlan* fp = _kln89->_flightPlans[_nFp0]; |
| 59 | if(fp) { |
| 60 | unsigned int n = fp->waypoints.size(); |
| 61 | if(n < 2) { |
| 62 | // TODO: check that this is what really happens |
| 63 | _kln89->DrawText("----", 2, 9, 3); |
| 64 | _kln89->DrawText("----", 2, 9, 2); |
| 65 | } else { |
| 66 | _kln89->DrawText(fp->waypoints[0]->id, 2, 9, 3); |
| 67 | _kln89->DrawText(fp->waypoints[n-1]->id, 2, 9, 2); |
| 68 | double cum_tot_m = 0.0; |
| 69 | for(unsigned int i = 1; i < fp->waypoints.size(); ++i) { |
| 70 | cum_tot_m += _kln89->GetGreatCircleDistance(fp->waypoints[i-1]->lat, fp->waypoints[i-1]->lon, |
| 71 | fp->waypoints[i]->lat, fp->waypoints[i]->lon); |
| 72 | } |
| 73 | double ete = (cum_tot_m * SG_NM_TO_METER) / _ground_speed_ms; |
| 74 | _kln89->DrawDist(cum_tot_m, 2, 5, 1); |
| 75 | _kln89->DrawSpeed(_ground_speed_ms / 0.5144444444, 2, 5, 0); |
| 76 | _kln89->DrawTime(ete, 2, 14, 0); |
| 77 | } |
| 78 | } else { |
| 79 | _kln89->DrawText("----", 2, 9, 3); |
| 80 | _kln89->DrawText("----", 2, 9, 2); |
| 81 | } |
| 82 | } else { |
| 83 | _kln89->DrawText(">Wpt:", 2, 0, 3); |
| 84 | } |
| 85 | _kln89->DrawText("To", 2, 6, 2); |
| 86 | _kln89->DrawText("ESA ----'", 2, 7, 1); // TODO: implement an ESA calc |
| 87 | _kln89->DrawText("ETE", 2, 7, 0); |
| 88 | } else if(_subPage == 1) { |
| 89 | _kln89->DrawText(">Fpl: 0", 2, 0, 3); // TODO: fix this hardwiring! |
| 90 | _kln89->DrawText("FF:", 2, 0, 2); |
| 91 | _kln89->DrawText("Res:", 2, 7, 1); |
| 92 | _kln89->DrawText("Fuel Req", 2, 0, 0); |
| 93 | } else if(_subPage == 2) { |
| 94 | _kln89->DrawText("Time:", 2, 0, 3); |
| 95 | // TODO: hardwired to UTC at the moment |
| 96 | if(!(_uLinePos == 1 && crsr && blink)) { _kln89->DrawText("UTC", 2, 6, 3); } |
| 97 | if(_uLinePos == 1 && crsr) { _kln89->Underline(2, 6, 3, 3); } |
| 98 | string th = fgGetString("/instrumentation/clock/indicated-hour"); |
| 99 | string tm = fgGetString("/instrumentation/clock/indicated-min"); |
| 100 | ClockTime t(std::stoi(th), std::stoi(tm)); |
| 101 | if(th.size() == 1) th = "0" + th; |
| 102 | if(tm.size() == 1) tm = "0" + tm; |
| 103 | _kln89->DrawText(th + tm, 2, 11, 3); |
| 104 | |
| 105 | char buf[12]; |
| 106 | _kln89->DrawText("Alarm at:", 2, 0, 2); |
| 107 | _kln89->DrawText("in:", 2, 6, 1); |
| 108 | if(_alarmAnnotate) { |
| 109 | _alarmIn = _alarmTime - t; |
| 110 | snprintf(buf, 5, "%02i", _alarmTime.hr()); |
| 111 | if(!(_uLinePos == 2 && crsr && blink)) { _kln89->DrawText((string)buf, 2, 11, 2); } |
| 112 | snprintf(buf, 5, "%02i", _alarmTime.min()); |
| 113 | if(!(_uLinePos == 3 && crsr && blink)) { _kln89->DrawText((string)buf, 2, 13, 2); } |
| 114 | } else { |
| 115 | if(!(_uLinePos == 2 && crsr && blink)) { _kln89->DrawText("--", 2, 11, 2); } |
| 116 | if(!(_uLinePos == 3 && crsr && blink)) { _kln89->DrawText("--", 2, 13, 2); } |
| 117 | } |
| 118 | if(_alarmAnnotate && _alarmIn.hr() < 10) { |
| 119 | snprintf(buf, sizeof(buf), "%01i", _alarmIn.hr()); |
| 120 | if(!(_uLinePos == 4 && crsr && blink)) { _kln89->DrawText((string)buf, 2, 11, 1); } |
| 121 | snprintf(buf, sizeof(buf), "%02i", _alarmIn.min()); |
| 122 | if(!(_uLinePos == 5 && crsr && blink)) { _kln89->DrawText((string)buf, 2, 13, 1); } |
| 123 | } else { |
| 124 | if(!(_uLinePos == 4 && crsr && blink)) { _kln89->DrawText("-", 2, 11, 1); } |
| 125 | if(!(_uLinePos == 5 && crsr && blink)) { _kln89->DrawText("--", 2, 13, 1); } |
| 126 | } |
| 127 | _kln89->DrawText(":", 2, 12, 1); |
| 128 | if(crsr) { |
| 129 | if(_uLinePos == 2) { _kln89->Underline(2, 11, 2, 2); } |
| 130 | if(_uLinePos == 3) { _kln89->Underline(2, 13, 2, 2); } |
| 131 | if(_uLinePos == 4) { _kln89->Underline(2, 11, 1, 1); } |
| 132 | if(_uLinePos == 5) { _kln89->Underline(2, 13, 1, 2); } |
| 133 | } |
| 134 | |
| 135 | // TODO |
| 136 | _kln89->DrawText("Elapsed", 2, 0, 0); |
| 137 | ClockTime te = t - _kln89->_powerOnTime; |
| 138 | // There is the possibility that when we reset it we may end up a minute ahead of the |
| 139 | // alarm time for a second, so treat 23:59 as 0.00 |
| 140 | if(te.hr() == 23 && te.min() == 59) { |
| 141 | te.set_hr(0); |
| 142 | te.set_min(0); |
| 143 | } |
| 144 | if(!(_uLinePos == 6 && crsr && blink)) { |
| 145 | if(te.hr() > 9) { |
| 146 | // The elapsed time blanks out |
| 147 | // when past 9:59 on the kln89 sim. |
| 148 | _kln89->DrawText("-:--", 2, 11, 0); |
| 149 | } else { |
| 150 | snprintf(buf, 12, "%01i:%02i", te.hr(), te.min()); |
| 151 | _kln89->DrawText((string)buf, 2, 11, 0); |
| 152 | } |
| 153 | } |
| 154 | if(_uLinePos == 6 && crsr) { _kln89->Underline(2, 11, 0, 4); } |
| 155 | } else if(_subPage == 3) { |
| 156 | _kln89->DrawText("PRESSURE ALT", 2, 1, 3); |
| 157 | _kln89->DrawText("Ind:", 2, 0, 2); |
| 158 | _kln89->DrawText("Baro:", 2, 0, 1); |
| 159 | _kln89->DrawText("Prs", 2, 0, 0); |
| 160 | } else if(_subPage == 4) { |
| 161 | _kln89->DrawText("DENSITY ALT", 2, 1, 3); |
| 162 | _kln89->DrawText("Prs:", 2, 0, 2); |
| 163 | _kln89->DrawText("Temp:", 2, 0, 1); |
| 164 | _kln89->DrawText("Den", 2, 0, 0); |
| 165 | } else if(_subPage == 5) { |
| 166 | _kln89->DrawText("CAS:", 2, 0, 3); |
| 167 | _kln89->DrawText("Prs:", 2, 0, 2); |
| 168 | _kln89->DrawText("Temp:", 2, 0, 1); |
| 169 | _kln89->DrawText("TAS", 2, 0, 0); |
| 170 | } else if(_subPage == 6) { |
| 171 | _kln89->DrawText("TAS:", 2, 0, 3); |
| 172 | _kln89->DrawText("Hdg:", 2, 0, 2); |
| 173 | _kln89->DrawText("Headwind:", 2, 0, 1); |
| 174 | _kln89->DrawText("True", 2, 4, 0); |
| 175 | } else { |
| 176 | _kln89->DrawText("SUNRISE", 2, 0, 1); |
| 177 | _kln89->DrawText("SUNSET", 2, 0, 0); |
| 178 | } |
| 179 | |
| 180 | KLN89Page::Update(dt); |
| 181 | } |
| 182 | |
| 183 | void KLN89CalPage::CrsrPressed() { |
| 184 | if(_kln89->_obsMode) { |
| 185 | _uLinePos = 0; |
| 186 | } else { |
| 187 | _uLinePos = 1; |
| 188 | } |
| 189 | if(_subPage == 2) { |
| 190 | _maxULinePos = 6; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void KLN89CalPage::ClrPressed() { |
| 195 | if(_kln89->_mode != KLN89_MODE_CRSR) { |
| 196 | KLN89Page::ClrPressed(); |
| 197 | } |
| 198 | if(_subPage == 2 && _uLinePos == 6) { |
| 199 | _kln89->ResetPowerOnTimer(); |
| 200 | } else { |
| 201 | KLN89Page::ClrPressed(); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | void KLN89CalPage::Knob2Left1() { |
| 206 | if(_kln89->_mode != KLN89_MODE_CRSR) { |
| 207 | KLN89Page::Knob2Left1(); |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | if(_subPage == 2) { |
| 212 | if(_uLinePos == 1) { |
| 213 | // TODO: allow time zone to be changed |
| 214 | } else if(_uLinePos == 2) { |
| 215 | ClockTime t(1,0); |
| 216 | if(_alarmAnnotate) { |
| 217 | _alarmTime = _alarmTime - t; |
| 218 | } else { |
| 219 | _alarmTime.set_hr(std::stoi(fgGetString("/instrumentation/clock/indicated-hour"))); |
| 220 | _alarmTime.set_min(std::stoi(fgGetString("/instrumentation/clock/indicated-min"))); |
| 221 | _alarmTime = _alarmTime - t; |
| 222 | _alarmAnnotate = true; |
| 223 | } |
| 224 | _alarmSet = true; |
| 225 | } else if(_uLinePos == 3) { |
| 226 | ClockTime t(0,1); |
| 227 | if(_alarmAnnotate) { |
| 228 | _alarmTime = _alarmTime - t; |
| 229 | } else { |
| 230 | _alarmTime.set_hr(std::stoi(fgGetString("/instrumentation/clock/indicated-hour"))); |
| 231 | _alarmTime.set_min(std::stoi(fgGetString("/instrumentation/clock/indicated-min"))); |
| 232 | _alarmTime = _alarmTime - t; |
| 233 | _alarmAnnotate = true; |
| 234 | } |
| 235 | _alarmSet = true; |
| 236 | } else if(_uLinePos == 4) { |
| 237 | ClockTime t(1,0); |
| 238 | // If the _alarmIn time is dashed out due to being > 9:59 |
| 239 | // then changing it starts from zero again. |
| 240 | if(_alarmAnnotate && _alarmIn.hr() < 10) { |
| 241 | _alarmIn = _alarmIn - t; |
| 242 | if(_alarmIn.hr() > 9) { _alarmIn.set_hr(9); } |
| 243 | } else { |
| 244 | _alarmIn.set_hr(9); |
| 245 | _alarmIn.set_min(0); |
| 246 | _alarmAnnotate = true; |
| 247 | } |
| 248 | _alarmSet = true; |
| 249 | t.set_hr(std::stoi(fgGetString("/instrumentation/clock/indicated-hour"))); |
| 250 | t.set_min(std::stoi(fgGetString("/instrumentation/clock/indicated-min"))); |
| 251 | _alarmTime = t + _alarmIn; |
| 252 | } else if(_uLinePos == 5) { |
| 253 | ClockTime t(0,1); |
| 254 | if(_alarmAnnotate && _alarmIn.hr() < 10) { |
| 255 | _alarmIn = _alarmIn - t; |
| 256 | if(_alarmIn.hr() > 9) { _alarmIn.set_hr(9); } |
| 257 | } else { |
| 258 | _alarmIn.set_hr(9); |
| 259 | _alarmIn.set_min(59); |
| 260 | _alarmAnnotate = true; |
| 261 | } |
| 262 | _alarmSet = true; |
| 263 | t.set_hr(std::stoi(fgGetString("/instrumentation/clock/indicated-hour"))); |
| 264 | t.set_min(std::stoi(fgGetString("/instrumentation/clock/indicated-min"))); |
| 265 | _alarmTime = t + _alarmIn; |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | void KLN89CalPage::Knob2Right1() { |
| 271 | if(_kln89->_mode != KLN89_MODE_CRSR) { |
| 272 | KLN89Page::Knob2Right1(); |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | if(_subPage == 2) { |
| 277 | if(_uLinePos == 1) { |
| 278 | // TODO: allow time zone to be changed |
| 279 | } else if(_uLinePos == 2) { |
| 280 | ClockTime t(1,0); |
| 281 | if(_alarmAnnotate) { |
| 282 | _alarmTime = _alarmTime + t; |
| 283 | } else { |
| 284 | _alarmTime.set_hr(std::stoi(fgGetString("/instrumentation/clock/indicated-hour"))); |
| 285 | _alarmTime.set_min(std::stoi(fgGetString("/instrumentation/clock/indicated-min"))); |
| 286 | _alarmTime = _alarmTime + t; |
| 287 | _alarmAnnotate = true; |
| 288 | } |
| 289 | _alarmSet = true; |
| 290 | } else if(_uLinePos == 3) { |
| 291 | ClockTime t(0,1); |
| 292 | if(_alarmAnnotate) { |
| 293 | _alarmTime = _alarmTime + t; |
| 294 | } else { |
| 295 | _alarmTime.set_hr(std::stoi(fgGetString("/instrumentation/clock/indicated-hour"))); |
| 296 | _alarmTime.set_min(std::stoi(fgGetString("/instrumentation/clock/indicated-min"))); |
| 297 | _alarmTime = _alarmTime + t; |
| 298 | _alarmAnnotate = true; |
| 299 | } |
| 300 | _alarmSet = true; |
| 301 | } else if(_uLinePos == 4) { |
| 302 | ClockTime t(1,0); |
| 303 | if(_alarmAnnotate && _alarmIn.hr() < 10) { |
| 304 | _alarmIn = _alarmIn + t; |
| 305 | if(_alarmIn.hr() > 9) { _alarmIn.set_hr(0); } |
| 306 | } else { |
| 307 | _alarmIn.set_hr(1); |
| 308 | _alarmIn.set_min(0); |
| 309 | _alarmAnnotate = true; |
| 310 | } |
| 311 | _alarmSet = true; |
| 312 | t.set_hr(std::stoi(fgGetString("/instrumentation/clock/indicated-hour"))); |
| 313 | t.set_min(std::stoi(fgGetString("/instrumentation/clock/indicated-min"))); |
| 314 | _alarmTime = t + _alarmIn; |
| 315 | } else if(_uLinePos == 5) { |
| 316 | ClockTime t(0,1); |
| 317 | if(_alarmAnnotate && _alarmIn.hr() < 10) { |
| 318 | _alarmIn = _alarmIn + t; |
| 319 | if(_alarmIn.hr() > 9) { _alarmIn.set_hr(0); } |
| 320 | } else { |
| 321 | _alarmIn.set_hr(0); |
| 322 | _alarmIn.set_min(1); |
| 323 | _alarmAnnotate = true; |
| 324 | } |
| 325 | _alarmSet = true; |
| 326 | t.set_hr(std::stoi(fgGetString("/instrumentation/clock/indicated-hour"))); |
| 327 | t.set_min(std::stoi(fgGetString("/instrumentation/clock/indicated-min"))); |
| 328 | _alarmTime = t + _alarmIn; |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | void KLN89CalPage::LooseFocus() { |
| 334 | if(_alarmSet) { |
| 335 | _kln89->SetAlarm(_alarmTime.hr(), _alarmTime.min()); |
| 336 | _alarmSet = false; |
| 337 | } |
| 338 | } |