altoslib: Move computed state from AltosRecord to AltosState
[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                 void show(String s) {
44                         show();
45                         value.setText(s);
46                 }
47                 
48                 void show(AltosUnits units, double v) {
49                         show(units.show(8, v));
50                 }
51
52                 void show(String format, double v) {
53                         show(String.format(format, v));
54                 }
55
56                 public void set_font() {
57                         label.setFont(Altos.label_font);
58                         value.setFont(Altos.value_font);
59                 }
60
61                 void hide() {
62                         label.setVisible(false);
63                         value.setVisible(false);
64                 }
65
66                 public LandedValue (GridBagLayout layout, int y, String text) {
67                         GridBagConstraints      c = new GridBagConstraints();
68                         c.weighty = 1;
69
70                         label = new JLabel(text);
71                         label.setFont(Altos.label_font);
72                         label.setHorizontalAlignment(SwingConstants.LEFT);
73                         c.gridx = 0; c.gridy = y;
74                         c.insets = new Insets(10, 10, 10, 10);
75                         c.anchor = GridBagConstraints.WEST;
76                         c.weightx = 0;
77                         c.fill = GridBagConstraints.VERTICAL;
78                         layout.setConstraints(label, c);
79                         add(label);
80
81                         value = new JTextField(Altos.text_width);
82                         value.setFont(Altos.value_font);
83                         value.setHorizontalAlignment(SwingConstants.RIGHT);
84                         c.gridx = 1; c.gridy = y;
85                         c.anchor = GridBagConstraints.WEST;
86                         c.weightx = 1;
87                         c.fill = GridBagConstraints.BOTH;
88                         layout.setConstraints(value, c);
89                         add(value);
90                 }
91         }
92
93         String pos(double p, String pos, String neg) {
94                 String  h = pos;
95                 if (p < 0) {
96                         h = neg;
97                         p = -p;
98                 }
99                 int deg = (int) Math.floor(p);
100                 double min = (p - Math.floor(p)) * 60.0;
101                 return String.format("%s %4d° %9.6f", h, deg, min);
102         }
103
104         class Lat extends LandedValue {
105                 void show (AltosState state, int crc_errors) {
106                         if (state.gps != null && state.gps.connected)
107                                 show(pos(state.gps.lat,"N", "S"));
108                         else
109                                 show("???");
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                                 show(pos(state.gps.lon,"E", "W"));
123                         else
124                                 show("???");
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                                 show("???");
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(AltosConvert.distance, state.from_pad.distance);
153                         else
154                                 show("???");
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(AltosConvert.height, 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(AltosConvert.speed, 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(AltosConvert.accel, 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 }