7525b655e43f770acad3aafde74cebb844ad5d61
[fw/altos] / ao-tools / altosui / AltosAscent.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 AltosAscent extends JComponent implements AltosFlightDisplay {
32         GridBagLayout   layout;
33         Font            label_font;
34         Font            value_font;
35
36         public class AscentValue {
37                 JLabel          label;
38                 JTextField      value;
39                 void show(AltosState state, int crc_errors) {}
40
41                 void reset() {
42                         value.setText("");
43                 }
44                 public AscentValue (GridBagLayout layout, int y, String text) {
45                         GridBagConstraints      c = new GridBagConstraints();
46                         c.weighty = 1;
47
48                         label = new JLabel(text);
49                         label.setFont(label_font);
50                         label.setHorizontalAlignment(SwingConstants.LEFT);
51                         c.gridx = 0; c.gridy = y;
52                         c.insets = new Insets(10, 10, 10, 10);
53                         c.anchor = GridBagConstraints.WEST;
54                         c.fill = GridBagConstraints.VERTICAL;
55                         c.weightx = 0;
56                         layout.setConstraints(label, c);
57                         add(label);
58
59                         value = new JTextField(30);
60                         value.setFont(value_font);
61                         value.setHorizontalAlignment(SwingConstants.RIGHT);
62                         c.gridx = 1; c.gridy = y;
63                         c.anchor = GridBagConstraints.WEST;
64                         c.fill = GridBagConstraints.BOTH;
65                         c.gridwidth = 2;
66                         c.weightx = 1;
67                         layout.setConstraints(value, c);
68                         add(value);
69                 }
70         }
71
72         public class AscentValueHold {
73                 JLabel          label;
74                 JTextField      value;
75                 JTextField      max_value;
76                 double          max;
77
78                 void show(AltosState state, int crc_errors) {}
79
80                 void reset() {
81                         value.setText("");
82                         max_value.setText("");
83                         max = 0;
84                 }
85
86                 void show(String format, double v) {
87                         value.setText(String.format(format, v));
88                         if (v > max) {
89                                 max_value.setText(String.format(format, v));
90                                 max = v;
91                         }
92                 }
93                 public AscentValueHold (GridBagLayout layout, int y, String text) {
94                         GridBagConstraints      c = new GridBagConstraints();
95                         c.weighty = 1;
96
97                         label = new JLabel(text);
98                         label.setFont(label_font);
99                         label.setHorizontalAlignment(SwingConstants.LEFT);
100                         c.gridx = 0; c.gridy = y;
101                         c.insets = new Insets(10, 10, 10, 10);
102                         c.anchor = GridBagConstraints.WEST;
103                         c.fill = GridBagConstraints.VERTICAL;
104                         c.weightx = 0;
105                         layout.setConstraints(label, c);
106                         add(label);
107
108                         value = new JTextField(15);
109                         value.setFont(value_font);
110                         value.setHorizontalAlignment(SwingConstants.RIGHT);
111                         c.gridx = 1; c.gridy = y;
112                         c.anchor = GridBagConstraints.EAST;
113                         c.fill = GridBagConstraints.BOTH;
114                         c.weightx = 1;
115                         layout.setConstraints(value, c);
116                         add(value);
117
118                         max_value = new JTextField(15);
119                         max_value.setFont(value_font);
120                         max_value.setHorizontalAlignment(SwingConstants.RIGHT);
121                         c.gridx = 2; c.gridy = y;
122                         c.anchor = GridBagConstraints.EAST;
123                         c.fill = GridBagConstraints.BOTH;
124                         c.weightx = 1;
125                         layout.setConstraints(max_value, c);
126                         add(max_value);
127                 }
128         }
129
130
131         class Height extends AscentValueHold {
132                 void show (AltosState state, int crc_errors) {
133                         show("%6.0f m", state.height);
134                 }
135                 public Height (GridBagLayout layout, int y) {
136                         super (layout, y, "Height");
137                 }
138         }
139
140         Height  height;
141
142         class Speed extends AscentValueHold {
143                 void show (AltosState state, int crc_errors) {
144                         double speed = state.speed;
145                         if (!state.ascent)
146                                 speed = state.baro_speed;
147                         show("%6.0f m/s", speed);
148                 }
149                 public Speed (GridBagLayout layout, int y) {
150                         super (layout, y, "Speed");
151                 }
152         }
153
154         Speed   speed;
155
156         class Accel extends AscentValueHold {
157                 void show (AltosState state, int crc_errors) {
158                         show("%6.0f m/s²", state.acceleration);
159                 }
160                 public Accel (GridBagLayout layout, int y) {
161                         super (layout, y, "Acceleration");
162                 }
163         }
164
165         Accel   accel;
166
167         String pos(double p, String pos, String neg) {
168                 String  h = pos;
169                 if (p < 0) {
170                         h = neg;
171                         p = -p;
172                 }
173                 int deg = (int) Math.floor(p);
174                 double min = (p - Math.floor(p)) * 60.0;
175                 return String.format("%s %4d° %9.6f", h, deg, min);
176         }
177
178         class Lat extends AscentValue {
179                 void show (AltosState state, int crc_errors) {
180                         if (state.gps != null)
181                                 value.setText(pos(state.gps.lat,"N", "S"));
182                         else
183                                 value.setText("???");
184                 }
185                 public Lat (GridBagLayout layout, int y) {
186                         super (layout, y, "Latitude");
187                 }
188         }
189
190         Lat lat;
191
192         class Lon extends AscentValue {
193                 void show (AltosState state, int crc_errors) {
194                         if (state.gps != null)
195                                 value.setText(pos(state.gps.lon,"E", "W"));
196                         else
197                                 value.setText("???");
198                 }
199                 public Lon (GridBagLayout layout, int y) {
200                         super (layout, y, "Longitude");
201                 }
202         }
203
204         Lon lon;
205
206         public void reset() {
207                 lat.reset();
208                 lon.reset();
209                 height.reset();
210                 speed.reset();
211                 accel.reset();
212         }
213
214         public void show(AltosState state, int crc_errors) {
215                 lat.show(state, crc_errors);
216                 lon.show(state, crc_errors);
217                 height.show(state, crc_errors);
218                 speed.show(state, crc_errors);
219                 accel.show(state, crc_errors);
220         }
221
222         public void labels(GridBagLayout layout, int y) {
223                 GridBagConstraints      c;
224                 JLabel                  cur, max;
225
226                 cur = new JLabel("Current");
227                 cur.setFont(label_font);
228                 c = new GridBagConstraints();
229                 c.gridx = 1; c.gridy = y;
230                 c.insets = new Insets(10, 10, 10, 10);
231                 layout.setConstraints(cur, c);
232                 add(cur);
233
234                 max = new JLabel("Maximum");
235                 max.setFont(label_font);
236                 c.gridx = 2; c.gridy = y;
237                 layout.setConstraints(max, c);
238                 add(max);
239         }
240
241         public AltosAscent() {
242                 layout = new GridBagLayout();
243
244                 label_font = new Font("Dialog", Font.PLAIN, 24);
245                 value_font = new Font("Monospaced", Font.PLAIN, 24);
246                 setLayout(layout);
247
248                 /* Elements in ascent display:
249                  *
250                  * lat
251                  * lon
252                  * height
253                  */
254                 labels(layout, 0);
255                 height = new Height(layout, 1);
256                 speed = new Speed(layout, 2);
257                 accel = new Accel(layout, 3);
258                 lat = new Lat(layout, 4);
259                 lon = new Lon(layout, 5);
260         }
261 }