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