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