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