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