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