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