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