altoslib: Use symbols in AltosRomconfig instead of fixed offsets
[fw/altos] / altosui / AltosAscent.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package altosui;
19
20 import java.awt.*;
21 import javax.swing.*;
22 import org.altusmetrum.altoslib_2.*;
23
24 public class AltosAscent extends JComponent implements AltosFlightDisplay {
25         GridBagLayout   layout;
26         JLabel                  cur, max;
27
28         public class AscentStatus {
29                 JLabel          label;
30                 JTextField      value;
31                 AltosLights     lights;
32
33                 void show() {
34                         value.setVisible(true);
35                         lights.setVisible(true);
36                         label.setVisible(true);
37                 }
38
39                 void hide() {
40                         value.setVisible(false);
41                         lights.setVisible(false);
42                         label.setVisible(false);
43                 }
44
45                 void show(AltosState state, AltosListenerState listener_state) {}
46
47                 void show(String s) {
48                         show();
49                         value.setText(s);
50                 }
51                 
52                 void show(AltosUnits units, double v) {
53                         show(units.show(8, v));
54                 }
55
56                 void show(String format, double v) {
57                         show(String.format(format, v));
58                 }
59
60                 void reset() {
61                         value.setText("");
62                         lights.set(false);
63                 }
64
65                 void set_font() {
66                         label.setFont(Altos.label_font);
67                         value.setFont(Altos.value_font);
68                 }
69
70                 public AscentStatus (GridBagLayout layout, int y, String text) {
71                         GridBagConstraints      c = new GridBagConstraints();
72                         c.weighty = 1;
73
74                         lights = new AltosLights();
75                         c.gridx = 0; c.gridy = y;
76                         c.anchor = GridBagConstraints.CENTER;
77                         c.fill = GridBagConstraints.VERTICAL;
78                         c.weightx = 0;
79                         layout.setConstraints(lights, c);
80                         add(lights);
81
82                         label = new JLabel(text);
83                         label.setFont(Altos.label_font);
84                         label.setHorizontalAlignment(SwingConstants.LEFT);
85                         c.gridx = 1; c.gridy = y;
86                         c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
87                         c.anchor = GridBagConstraints.WEST;
88                         c.fill = GridBagConstraints.VERTICAL;
89                         c.weightx = 0;
90                         layout.setConstraints(label, c);
91                         add(label);
92
93                         value = new JTextField(Altos.text_width);
94                         value.setFont(Altos.value_font);
95                         value.setHorizontalAlignment(SwingConstants.RIGHT);
96                         c.gridx = 2; c.gridy = y;
97                         c.gridwidth = 2;
98                         c.anchor = GridBagConstraints.WEST;
99                         c.fill = GridBagConstraints.BOTH;
100                         c.weightx = 1;
101                         layout.setConstraints(value, c);
102                         add(value);
103
104                 }
105         }
106
107         public class AscentValue {
108                 JLabel          label;
109                 JTextField      value;
110                 void show(AltosState state, AltosListenerState listener_state) {}
111
112                 void reset() {
113                         value.setText("");
114                 }
115
116                 void show() {
117                         label.setVisible(true);
118                         value.setVisible(true);
119                 }
120
121                 void show(String s) {
122                         show();
123                         value.setText(s);
124                 }
125                 
126                 void show(AltosUnits units, double v) {
127                         show(units.show(8, v));
128                 }
129
130                 void show(String format, double v) {
131                         show(String.format(format, v));
132                 }
133
134                 void hide() {
135                         label.setVisible(false);
136                         value.setVisible(false);
137                 }
138                 void set_font() {
139                         label.setFont(Altos.label_font);
140                         value.setFont(Altos.value_font);
141                 }
142
143                 public AscentValue (GridBagLayout layout, int y, String text) {
144                         GridBagConstraints      c = new GridBagConstraints();
145                         c.weighty = 1;
146
147                         label = new JLabel(text);
148                         label.setFont(Altos.label_font);
149                         label.setHorizontalAlignment(SwingConstants.LEFT);
150                         c.gridx = 1; c.gridy = y;
151                         c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
152                         c.anchor = GridBagConstraints.WEST;
153                         c.fill = GridBagConstraints.VERTICAL;
154                         c.weightx = 0;
155                         layout.setConstraints(label, c);
156                         add(label);
157
158                         value = new JTextField(Altos.text_width);
159                         value.setFont(Altos.value_font);
160                         value.setHorizontalAlignment(SwingConstants.RIGHT);
161                         c.gridx = 2; c.gridy = y;
162                         c.anchor = GridBagConstraints.WEST;
163                         c.fill = GridBagConstraints.BOTH;
164                         c.gridwidth = 2;
165                         c.weightx = 1;
166                         layout.setConstraints(value, c);
167                         add(value);
168                 }
169         }
170
171         public class AscentValueHold {
172                 JLabel          label;
173                 JTextField      value;
174                 JTextField      max_value;
175                 double          max;
176
177                 void show(AltosState state, AltosListenerState listener_state) {}
178
179                 void reset() {
180                         value.setText("");
181                         max_value.setText("");
182                         max = AltosLib.MISSING;
183                 }
184
185                 void set_font() {
186                         label.setFont(Altos.label_font);
187                         value.setFont(Altos.value_font);
188                         max_value.setFont(Altos.value_font);
189                 }
190
191                 void show(AltosUnits units, double v) {
192                         if (v == AltosLib.MISSING) {
193                                 value.setText("Missing");
194                                 max_value.setText("Missing");
195                         } else {
196                                 value.setText(units.show(8, v));
197                                 if (v > max || max == AltosLib.MISSING) {
198                                         max_value.setText(units.show(8, v));
199                                         max = v;
200                                 }
201                         }
202                 }
203                 public AscentValueHold (GridBagLayout layout, int y, String text) {
204                         GridBagConstraints      c = new GridBagConstraints();
205                         c.weighty = 1;
206
207                         label = new JLabel(text);
208                         label.setFont(Altos.label_font);
209                         label.setHorizontalAlignment(SwingConstants.LEFT);
210                         c.gridx = 1; c.gridy = y;
211                         c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
212                         c.anchor = GridBagConstraints.WEST;
213                         c.fill = GridBagConstraints.VERTICAL;
214                         c.weightx = 0;
215                         layout.setConstraints(label, c);
216                         add(label);
217
218                         value = new JTextField(Altos.text_width);
219                         value.setFont(Altos.value_font);
220                         value.setHorizontalAlignment(SwingConstants.RIGHT);
221                         c.gridx = 2; c.gridy = y;
222                         c.anchor = GridBagConstraints.EAST;
223                         c.fill = GridBagConstraints.BOTH;
224                         c.weightx = 1;
225                         layout.setConstraints(value, c);
226                         add(value);
227
228                         max_value = new JTextField(Altos.text_width);
229                         max_value.setFont(Altos.value_font);
230                         max_value.setHorizontalAlignment(SwingConstants.RIGHT);
231                         c.gridx = 3; c.gridy = y;
232                         c.anchor = GridBagConstraints.EAST;
233                         c.fill = GridBagConstraints.BOTH;
234                         c.weightx = 1;
235                         layout.setConstraints(max_value, c);
236                         add(max_value);
237                 }
238         }
239
240
241         class Height extends AscentValueHold {
242                 void show (AltosState state, AltosListenerState listener_state) {
243                         show(AltosConvert.height, state.height());
244                 }
245                 public Height (GridBagLayout layout, int y) {
246                         super (layout, y, "Height");
247                 }
248         }
249
250         Height  height;
251
252         class Speed extends AscentValueHold {
253                 void show (AltosState state, AltosListenerState listener_state) {
254                         show(AltosConvert.speed, state.speed());
255                 }
256                 public Speed (GridBagLayout layout, int y) {
257                         super (layout, y, "Speed");
258                 }
259         }
260
261         Speed   speed;
262
263         class Accel extends AscentValueHold {
264                 void show (AltosState state, AltosListenerState listener_state) {
265                         show(AltosConvert.accel, state.acceleration());
266                 }
267                 public Accel (GridBagLayout layout, int y) {
268                         super (layout, y, "Acceleration");
269                 }
270         }
271
272         Accel   accel;
273
274         String pos(double p, String pos, String neg) {
275                 String  h = pos;
276                 if (p < 0) {
277                         h = neg;
278                         p = -p;
279                 }
280                 int deg = (int) Math.floor(p);
281                 double min = (p - Math.floor(p)) * 60.0;
282                 return String.format("%s %4d° %9.6f", h, deg, min);
283         }
284
285         class Apogee extends AscentStatus {
286                 void show (AltosState state, AltosListenerState listener_state) {
287                         show("%4.2f V", state.apogee_voltage);
288                         lights.set(state.apogee_voltage > 3.7);
289                 }
290                 public Apogee (GridBagLayout layout, int y) {
291                         super(layout, y, "Apogee Igniter Voltage");
292                 }
293         }
294
295         Apogee apogee;
296
297         class Main extends AscentStatus {
298                 void show (AltosState state, AltosListenerState listener_state) {
299                         show("%4.2f V", state.main_voltage);
300                         lights.set(state.main_voltage > 3.7);
301                 }
302                 public Main (GridBagLayout layout, int y) {
303                         super(layout, y, "Main Igniter Voltage");
304                 }
305         }
306
307         Main main;
308
309         class Lat extends AscentValue {
310                 void show (AltosState state, AltosListenerState listener_state) {
311                         if (state.gps != null && state.gps.connected && state.gps.lat != AltosLib.MISSING)
312                                 show(pos(state.gps.lat,"N", "S"));
313                         else
314                                 show("???");
315                 }
316                 public Lat (GridBagLayout layout, int y) {
317                         super (layout, y, "Latitude");
318                 }
319         }
320
321         Lat lat;
322
323         class Lon extends AscentValue {
324                 void show (AltosState state, AltosListenerState listener_state) {
325                         if (state.gps != null && state.gps.connected && state.gps.lon != AltosLib.MISSING)
326                                 show(pos(state.gps.lon,"E", "W"));
327                         else
328                                 show("???");
329                 }
330                 public Lon (GridBagLayout layout, int y) {
331                         super (layout, y, "Longitude");
332                 }
333         }
334
335         Lon lon;
336
337         public void reset() {
338                 lat.reset();
339                 lon.reset();
340                 main.reset();
341                 apogee.reset();
342                 height.reset();
343                 speed.reset();
344                 accel.reset();
345         }
346
347         public void set_font() {
348                 cur.setFont(Altos.label_font);
349                 max.setFont(Altos.label_font);
350                 lat.set_font();
351                 lon.set_font();
352                 main.set_font();
353                 apogee.set_font();
354                 height.set_font();
355                 speed.set_font();
356                 accel.set_font();
357         }
358
359         public void show(AltosState state, AltosListenerState listener_state) {
360                 if (state.gps != null && state.gps.connected) {
361                         lat.show(state, listener_state);
362                         lon.show(state, listener_state);
363                 } else {
364                         lat.hide();
365                         lon.hide();
366                 }
367                 height.show(state, listener_state);
368                 if (state.main_voltage != AltosLib.MISSING)
369                         main.show(state, listener_state);
370                 else
371                         main.hide();
372                 if (state.apogee_voltage != AltosLib.MISSING)
373                         apogee.show(state, listener_state);
374                 else
375                         apogee.hide();
376                 speed.show(state, listener_state);
377                 accel.show(state, listener_state);
378         }
379
380         public void labels(GridBagLayout layout, int y) {
381                 GridBagConstraints      c;
382
383                 cur = new JLabel("Current");
384                 cur.setFont(Altos.label_font);
385                 c = new GridBagConstraints();
386                 c.gridx = 2; c.gridy = y;
387                 c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
388                 layout.setConstraints(cur, c);
389                 add(cur);
390
391                 max = new JLabel("Maximum");
392                 max.setFont(Altos.label_font);
393                 c.gridx = 3; c.gridy = y;
394                 layout.setConstraints(max, c);
395                 add(max);
396         }
397
398         public String getName() {
399                 return "Ascent";
400         }
401
402         public AltosAscent() {
403                 layout = new GridBagLayout();
404
405                 setLayout(layout);
406
407                 /* Elements in ascent display:
408                  *
409                  * lat
410                  * lon
411                  * height
412                  */
413                 labels(layout, 0);
414                 height = new Height(layout, 1);
415                 speed = new Speed(layout, 2);
416                 accel = new Accel(layout, 3);
417                 lat = new Lat(layout, 4);
418                 lon = new Lon(layout, 5);
419                 apogee = new Apogee(layout, 6);
420                 main = new Main(layout, 7);
421         }
422 }