2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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.
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.
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.
21 import java.awt.event.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.LinkedBlockingQueue;
31 public class AltosAscent extends JComponent implements AltosFlightDisplay {
36 public class AscentValue {
39 void show(AltosState state, int crc_errors) {}
44 public AscentValue (GridBagLayout layout, int y, String text) {
45 GridBagConstraints c = new GridBagConstraints();
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;
56 layout.setConstraints(label, c);
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;
67 layout.setConstraints(value, c);
72 public class AscentValueHold {
78 void show(AltosState state, int crc_errors) {}
82 max_value.setText("");
86 void show(String format, double v) {
87 value.setText(String.format(format, v));
89 max_value.setText(String.format(format, v));
93 public AscentValueHold (GridBagLayout layout, int y, String text) {
94 GridBagConstraints c = new GridBagConstraints();
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;
105 layout.setConstraints(label, c);
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;
115 layout.setConstraints(value, c);
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;
125 layout.setConstraints(max_value, c);
131 class Height extends AscentValueHold {
132 void show (AltosState state, int crc_errors) {
133 show("%6.0f m", state.height);
135 public Height (GridBagLayout layout, int y) {
136 super (layout, y, "Height");
142 class Speed extends AscentValueHold {
143 void show (AltosState state, int crc_errors) {
144 double speed = state.speed;
146 speed = state.baro_speed;
147 show("%6.0f m/s", speed);
149 public Speed (GridBagLayout layout, int y) {
150 super (layout, y, "Speed");
156 class Accel extends AscentValueHold {
157 void show (AltosState state, int crc_errors) {
158 show("%6.0f m/s²", state.acceleration);
160 public Accel (GridBagLayout layout, int y) {
161 super (layout, y, "Acceleration");
167 String pos(double p, String pos, String neg) {
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);
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"));
183 value.setText("???");
185 public Lat (GridBagLayout layout, int y) {
186 super (layout, y, "Latitude");
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"));
197 value.setText("???");
199 public Lon (GridBagLayout layout, int y) {
200 super (layout, y, "Longitude");
206 public void reset() {
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);
222 public void labels(GridBagLayout layout, int y) {
223 GridBagConstraints c;
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);
234 max = new JLabel("Maximum");
235 max.setFont(label_font);
236 c.gridx = 2; c.gridy = y;
237 layout.setConstraints(max, c);
241 public AltosAscent() {
242 layout = new GridBagLayout();
244 label_font = new Font("Dialog", Font.PLAIN, 24);
245 value_font = new Font("Monospaced", Font.PLAIN, 24);
248 /* Elements in ascent display:
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);