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