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