707d8fccb38414d3dd9d5fb96675853e88735f50
[fw/altos] / altosui / AltosLanded.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 java.awt.event.*;
22 import javax.swing.*;
23 import java.io.*;
24 import org.altusmetrum.altoslib_4.*;
25 import org.altusmetrum.altosuilib_2.*;
26
27 public class AltosLanded extends JComponent implements AltosFlightDisplay, ActionListener {
28         GridBagLayout   layout;
29
30         public class LandedValue {
31                 JLabel          label;
32                 JTextField      value;
33                 void show(AltosState state, AltosListenerState listener_state) {}
34
35                 void reset() {
36                         value.setText("");
37                 }
38
39                 void show() {
40                         label.setVisible(true);
41                         value.setVisible(true);
42                 }
43
44                 void show(String s) {
45                         show();
46                         value.setText(s);
47                 }
48
49                 void show(AltosUnits units, double v) {
50                         show(units.show(8, v));
51                 }
52
53                 void show(String format, double v) {
54                         show(String.format(format, v));
55                 }
56
57                 public void set_font() {
58                         label.setFont(Altos.label_font);
59                         value.setFont(Altos.value_font);
60                 }
61
62                 void hide() {
63                         label.setVisible(false);
64                         value.setVisible(false);
65                 }
66
67                 public LandedValue (GridBagLayout layout, int y, String text) {
68                         GridBagConstraints      c = new GridBagConstraints();
69                         c.weighty = 1;
70
71                         label = new JLabel(text);
72                         label.setFont(Altos.label_font);
73                         label.setHorizontalAlignment(SwingConstants.LEFT);
74                         c.gridx = 0; c.gridy = y;
75                         c.insets = new Insets(10, 10, 10, 10);
76                         c.anchor = GridBagConstraints.WEST;
77                         c.weightx = 0;
78                         c.fill = GridBagConstraints.VERTICAL;
79                         layout.setConstraints(label, c);
80                         add(label);
81
82                         value = new JTextField(Altos.text_width);
83                         value.setFont(Altos.value_font);
84                         value.setHorizontalAlignment(SwingConstants.RIGHT);
85                         c.gridx = 1; c.gridy = y;
86                         c.anchor = GridBagConstraints.WEST;
87                         c.weightx = 1;
88                         c.fill = GridBagConstraints.BOTH;
89                         layout.setConstraints(value, c);
90                         add(value);
91                 }
92         }
93
94         String pos(double p, String pos, String neg) {
95                 String  h = pos;
96                 if (p < 0) {
97                         h = neg;
98                         p = -p;
99                 }
100                 int deg = (int) Math.floor(p);
101                 double min = (p - Math.floor(p)) * 60.0;
102                 return String.format("%s %4d° %9.6f", h, deg, min);
103         }
104
105         class Lat extends LandedValue {
106                 void show (AltosState state, AltosListenerState listener_state) {
107                         show();
108                         if (state.gps != null && state.gps.connected && state.gps.lat != AltosLib.MISSING)
109                                 show(pos(state.gps.lat,"N", "S"));
110                         else
111                                 show("???");
112                 }
113                 public Lat (GridBagLayout layout, int y) {
114                         super (layout, y, "Latitude");
115                 }
116         }
117
118         Lat lat;
119
120         class Lon extends LandedValue {
121                 void show (AltosState state, AltosListenerState listener_state) {
122                         show();
123                         if (state.gps != null && state.gps.connected && state.gps.lon != AltosLib.MISSING)
124                                 show(pos(state.gps.lon,"E", "W"));
125                         else
126                                 show("???");
127                 }
128                 public Lon (GridBagLayout layout, int y) {
129                         super (layout, y, "Longitude");
130                 }
131         }
132
133         Lon lon;
134
135         class Bearing extends LandedValue {
136                 void show (AltosState state, AltosListenerState listener_state) {
137                         show();
138                         if (state.from_pad != null)
139                                 show("%3.0f°", state.from_pad.bearing);
140                         else
141                                 show("???");
142                 }
143                 public Bearing (GridBagLayout layout, int y) {
144                         super (layout, y, "Bearing");
145                 }
146         }
147
148         Bearing bearing;
149
150         class Distance extends LandedValue {
151                 void show (AltosState state, AltosListenerState listener_state) {
152                         show();
153                         if (state.from_pad != null)
154                                 show(AltosConvert.distance, state.from_pad.distance);
155                         else
156                                 show("???");
157                 }
158                 public Distance (GridBagLayout layout, int y) {
159                         super (layout, y, "Distance");
160                 }
161         }
162
163         Distance distance;
164
165         class Height extends LandedValue {
166                 void show (AltosState state, AltosListenerState listener_state) {
167                         show(AltosConvert.height, state.max_height());
168                 }
169                 public Height (GridBagLayout layout, int y) {
170                         super (layout, y, "Maximum Height");
171                 }
172         }
173
174         Height  height;
175
176         class Speed extends LandedValue {
177                 void show (AltosState state, AltosListenerState listener_state) {
178                         show(AltosConvert.speed, state.max_speed());
179                 }
180                 public Speed (GridBagLayout layout, int y) {
181                         super (layout, y, "Maximum Speed");
182                 }
183         }
184
185         Speed   speed;
186
187         class Accel extends LandedValue {
188                 void show (AltosState state, AltosListenerState listener_state) {
189                         show(AltosConvert.accel, state.max_acceleration());
190                 }
191                 public Accel (GridBagLayout layout, int y) {
192                         super (layout, y, "Maximum Acceleration");
193                 }
194         }
195
196         Accel   accel;
197
198         public void reset() {
199                 lat.reset();
200                 lon.reset();
201                 bearing.reset();
202                 distance.reset();
203                 height.reset();
204                 speed.reset();
205                 accel.reset();
206         }
207
208         public void set_font() {
209                 lat.set_font();
210                 lon.set_font();
211                 bearing.set_font();
212                 distance.set_font();
213                 height.set_font();
214                 speed.set_font();
215                 accel.set_font();
216         }
217
218         public void show(AltosState state, AltosListenerState listener_state) {
219                 if (state.gps != null && state.gps.connected) {
220                         bearing.show(state, listener_state);
221                         distance.show(state, listener_state);
222                         lat.show(state, listener_state);
223                         lon.show(state, listener_state);
224                 } else {
225                         bearing.hide();
226                         distance.hide();
227                         lat.hide();
228                         lon.hide();
229                 }
230                 height.show(state, listener_state);
231                 speed.show(state, listener_state);
232                 accel.show(state, listener_state);
233                 if (reader.backing_file() != null)
234                         graph.setEnabled(true);
235         }
236
237         JButton graph;
238         AltosFlightReader reader;
239
240         public void actionPerformed(ActionEvent e) {
241                 String  cmd = e.getActionCommand();
242
243                 if (cmd.equals("graph")) {
244                         File    file = reader.backing_file();
245                         if (file != null) {
246                                 String  filename = file.getName();
247                                 try {
248                                         AltosStateIterable states = null;
249                                         if (filename.endsWith("eeprom")) {
250                                                 FileInputStream in = new FileInputStream(file);
251                                                 states = new AltosEepromFile(in);
252                                         } else if (filename.endsWith("telem")) {
253                                                 FileInputStream in = new FileInputStream(file);
254                                                 states = new AltosTelemetryFile(in);
255                                         } else {
256                                                 throw new FileNotFoundException(filename);
257                                         }
258                                         try {
259                                                 new AltosGraphUI(states, file);
260                                         } catch (InterruptedException ie) {
261                                         } catch (IOException ie) {
262                                         }
263                                 } catch (FileNotFoundException fe) {
264                                         JOptionPane.showMessageDialog(null,
265                                                                       fe.getMessage(),
266                                                                       "Cannot open file",
267                                                                       JOptionPane.ERROR_MESSAGE);
268                                 }
269                         }
270                 }
271         }
272
273         public String getName() {
274                 return "Landed";
275         }
276
277         public AltosLanded(AltosFlightReader in_reader) {
278                 layout = new GridBagLayout();
279
280                 reader = in_reader;
281
282                 setLayout(layout);
283
284                 /* Elements in descent display */
285                 bearing = new Bearing(layout, 0);
286                 distance = new Distance(layout, 1);
287                 lat = new Lat(layout, 2);
288                 lon = new Lon(layout, 3);
289                 height = new Height(layout, 4);
290                 speed = new Speed(layout, 5);
291                 accel = new Accel(layout, 6);
292
293                 graph = new JButton ("Graph Flight");
294                 graph.setActionCommand("graph");
295                 graph.addActionListener(this);
296                 graph.setEnabled(false);
297
298                 GridBagConstraints      c = new GridBagConstraints();
299
300                 c.gridx = 0; c.gridy = 7;
301                 c.insets = new Insets(10, 10, 10, 10);
302                 c.anchor = GridBagConstraints.WEST;
303                 c.weightx = 0;
304                 c.weighty = 0;
305                 c.fill = GridBagConstraints.VERTICAL;
306                 add(graph, c);
307         }
308 }