add compass bearing to voice output
[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.anchor = GridBagConstraints.WEST;
66                         c.fill = GridBagConstraints.HORIZONTAL;
67                         layout.setConstraints(value, c);
68                         add(value);
69                 }
70         }
71
72         class Height extends DescentValue {
73                 void show (AltosState state, int crc_errors) {
74                         show("%6.0f m", state.height);
75                 }
76                 public Height (GridBagLayout layout, int y) {
77                         super (layout, y, "Height");
78                 }
79         }
80
81         Height  height;
82
83         class Speed extends DescentValue {
84                 void show (AltosState state, int crc_errors) {
85                         double speed = state.speed;
86                         if (!state.ascent)
87                                 speed = state.baro_speed;
88                         show("%6.0f m/s", speed);
89                 }
90                 public Speed (GridBagLayout layout, int y) {
91                         super (layout, y, "Speed");
92                 }
93         }
94
95         Speed   speed;
96
97         String pos(double p, String pos, String neg) {
98                 String  h = pos;
99                 if (p < 0) {
100                         h = neg;
101                         p = -p;
102                 }
103                 int deg = (int) Math.floor(p);
104                 double min = (p - Math.floor(p)) * 60.0;
105                 return String.format("%s %4d° %9.6f", h, deg, min);
106         }
107
108         class Lat extends DescentValue {
109                 void show (AltosState state, int crc_errors) {
110                         if (state.gps != null)
111                                 value.setText(pos(state.gps.lat,"N", "S"));
112                         else
113                                 value.setText("???");
114                 }
115                 public Lat (GridBagLayout layout, int y) {
116                         super (layout, y, "Latitude");
117                 }
118         }
119
120         Lat lat;
121
122         class Lon extends DescentValue {
123                 void show (AltosState state, int crc_errors) {
124                         if (state.gps != null)
125                                 value.setText(pos(state.gps.lon,"E", "W"));
126                         else
127                                 value.setText("???");
128                 }
129                 public Lon (GridBagLayout layout, int y) {
130                         super (layout, y, "Longitude");
131                 }
132         }
133
134         Lon lon;
135
136         class Bearing extends DescentValue {
137                 void show (AltosState state, int crc_errors) {
138                         if (state.from_pad != null)
139                                 show("%3.0f°", state.from_pad.bearing);
140                         else
141                                 value.setText("???");
142                 }
143                 public Bearing (GridBagLayout layout, int y) {
144                         super (layout, y, "Bearing");
145                 }
146         }
147
148         Bearing bearing;
149
150         class Elevation extends DescentValue {
151                 void show (AltosState state, int crc_errors) {
152                         if (state.from_pad != null)
153                                 show("%3.0f°", state.elevation);
154                         else
155                                 value.setText("???");
156                 }
157                 public Elevation (GridBagLayout layout, int y) {
158                         super (layout, y, "Elevation");
159                 }
160         }
161
162         Elevation elevation;
163
164         class Range extends DescentValue {
165                 void show (AltosState state, int crc_errors) {
166                         show("%6.0f m", state.range);
167                 }
168                 public Range (GridBagLayout layout, int y) {
169                         super (layout, y, "Range");
170                 }
171         }
172
173         Range range;
174
175         public void reset() {
176                 lat.reset();
177                 lon.reset();
178                 height.reset();
179                 speed.reset();
180                 bearing.reset();
181                 elevation.reset();
182                 range.reset();
183         }
184
185         public void show(AltosState state, int crc_errors) {
186                 height.show(state, crc_errors);
187                 speed.show(state, crc_errors);
188                 bearing.show(state, crc_errors);
189                 elevation.show(state, crc_errors);
190                 range.show(state, crc_errors);
191                 lat.show(state, crc_errors);
192                 lon.show(state, crc_errors);
193         }
194
195         public AltosDescent() {
196                 layout = new GridBagLayout();
197
198                 label_font = new Font("Dialog", Font.PLAIN, 24);
199                 value_font = new Font("Monospaced", Font.PLAIN, 24);
200                 setLayout(layout);
201
202                 /* Elements in descent display */
203                 speed = new Speed(layout, 0);
204                 height = new Height(layout, 1);
205                 bearing = new Bearing(layout, 2);
206                 elevation = new Elevation(layout, 3);
207                 range = new Range(layout, 4);
208                 lat = new Lat(layout, 5);
209                 lon = new Lon(layout, 6);
210         }
211 }