altos: Need to use 16-bit counts for ao_xmem functions
[fw/altos] / altosui / AltosInfoTable.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 import org.altusmetrum.AltosLib.*;
31
32 public class AltosInfoTable extends JTable {
33         private AltosFlightInfoTableModel model;
34
35         static final int info_columns = 3;
36         static final int info_rows = 17;
37
38         int desired_row_height() {
39                 FontMetrics     infoValueMetrics = getFontMetrics(Altos.table_value_font);
40                 return (infoValueMetrics.getHeight() + infoValueMetrics.getLeading()) * 18 / 10;
41         }
42
43         int text_width(String t) {
44                 FontMetrics     infoValueMetrics = getFontMetrics(Altos.table_value_font);
45
46                 return infoValueMetrics.stringWidth(t);
47         }
48
49         void set_layout() {
50                 setRowHeight(desired_row_height());
51                 for (int i = 0; i < info_columns * 2; i++)
52                 {
53                         TableColumn column = getColumnModel().getColumn(i);
54
55                         if ((i & 1) == 0)
56                                 column.setPreferredWidth(text_width(" Satellites Visible "));
57                         else
58                                 column.setPreferredWidth(text_width(" 179°59.99999' "));
59                 }
60         }
61
62         public AltosInfoTable() {
63                 super(new AltosFlightInfoTableModel(info_rows, info_columns));
64                 model = (AltosFlightInfoTableModel) getModel();
65                 setFont(Altos.table_value_font);
66                 setAutoResizeMode(AUTO_RESIZE_ALL_COLUMNS);
67                 setShowGrid(true);
68                 set_layout();
69                 doLayout();
70         }
71
72         public void set_font() {
73                 setFont(Altos.table_value_font);
74                 set_layout();
75                 doLayout();
76         }
77
78         public Dimension getPreferredScrollableViewportSize() {
79                 return getPreferredSize();
80         }
81
82         void info_reset() {
83                 model.reset();
84         }
85
86         void info_add_row(int col, String name, String value) {
87                 model.addRow(col, name, value);
88         }
89
90         void info_add_row(int col, String name, String format, Object... parameters) {
91                 info_add_row (col, name, String.format(format, parameters));
92         }
93
94         void info_add_deg(int col, String name, double v, int pos, int neg) {
95                 int     c = pos;
96                 if (v < 0) {
97                         c = neg;
98                         v = -v;
99                 }
100                 double  deg = Math.floor(v);
101                 double  min = (v - deg) * 60;
102
103                 info_add_row(col, name, String.format("%3.0f°%08.5f'", deg, min));
104         }
105
106         void info_finish() {
107                 model.finish();
108         }
109
110         public void clear() {
111                 model.clear();
112         }
113
114         public void show(AltosState state, int crc_errors) {
115                 if (state == null)
116                         return;
117                 info_reset();
118                 info_add_row(0, "Altitude", "%6.0f    m", state.altitude);
119                 info_add_row(0, "Pad altitude", "%6.0f    m", state.ground_altitude);
120                 info_add_row(0, "Height", "%6.0f    m", state.height);
121                 info_add_row(0, "Max height", "%6.0f    m", state.max_height);
122                 info_add_row(0, "Acceleration", "%8.1f  m/s²", state.acceleration);
123                 info_add_row(0, "Max acceleration", "%8.1f  m/s²", state.max_acceleration);
124                 info_add_row(0, "Speed", "%8.1f  m/s", state.ascent ? state.speed : state.baro_speed);
125                 info_add_row(0, "Max Speed", "%8.1f  m/s", state.max_speed);
126                 info_add_row(0, "Temperature", "%9.2f °C", state.temperature);
127                 info_add_row(0, "Battery", "%9.2f V", state.battery);
128                 if (state.drogue_sense != AltosRecord.MISSING)
129                         info_add_row(0, "Drogue", "%9.2f V", state.drogue_sense);
130                 if (state.main_sense != AltosRecord.MISSING)
131                         info_add_row(0, "Main", "%9.2f V", state.main_sense);
132                 info_add_row(0, "CRC Errors", "%6d", crc_errors);
133
134                 if (state.gps == null || !state.gps.connected) {
135                         info_add_row(1, "GPS", "not available");
136                 } else {
137                         if (state.gps_ready)
138                                 info_add_row(1, "GPS state", "%s", "ready");
139                         else
140                                 info_add_row(1, "GPS state", "wait (%d)",
141                                              state.gps_waiting);
142                         if (state.data.gps.locked)
143                                 info_add_row(1, "GPS", "   locked");
144                         else if (state.data.gps.connected)
145                                 info_add_row(1, "GPS", " unlocked");
146                         else
147                                 info_add_row(1, "GPS", "  missing");
148                         info_add_row(1, "Satellites", "%6d", state.data.gps.nsat);
149                         info_add_deg(1, "Latitude", state.gps.lat, 'N', 'S');
150                         info_add_deg(1, "Longitude", state.gps.lon, 'E', 'W');
151                         info_add_row(1, "GPS altitude", "%6d", state.gps.alt);
152                         info_add_row(1, "GPS height", "%6.0f", state.gps_height);
153
154                         /* The SkyTraq GPS doesn't report these values */
155                         if (false) {
156                                 info_add_row(1, "GPS ground speed", "%8.1f m/s %3d°",
157                                              state.gps.ground_speed,
158                                              state.gps.course);
159                                 info_add_row(1, "GPS climb rate", "%8.1f m/s",
160                                              state.gps.climb_rate);
161                                 info_add_row(1, "GPS error", "%6d m(h)%3d m(v)",
162                                              state.gps.h_error, state.gps.v_error);
163                         }
164                         info_add_row(1, "GPS hdop", "%8.1f", state.gps.hdop);
165
166                         if (state.npad > 0) {
167                                 if (state.from_pad != null) {
168                                         info_add_row(1, "Distance from pad", "%6d m",
169                                                      (int) (state.from_pad.distance + 0.5));
170                                         info_add_row(1, "Direction from pad", "%6d°",
171                                                      (int) (state.from_pad.bearing + 0.5));
172                                         info_add_row(1, "Elevation from pad", "%6d°",
173                                                      (int) (state.elevation + 0.5));
174                                         info_add_row(1, "Range from pad", "%6d m",
175                                                      (int) (state.range + 0.5));
176                                 } else {
177                                         info_add_row(1, "Distance from pad", "unknown");
178                                         info_add_row(1, "Direction from pad", "unknown");
179                                         info_add_row(1, "Elevation from pad", "unknown");
180                                         info_add_row(1, "Range from pad", "unknown");
181                                 }
182                                 info_add_deg(1, "Pad latitude", state.pad_lat, 'N', 'S');
183                                 info_add_deg(1, "Pad longitude", state.pad_lon, 'E', 'W');
184                                 info_add_row(1, "Pad GPS alt", "%6.0f m", state.pad_alt);
185                         }
186                         info_add_row(1, "GPS date", "%04d-%02d-%02d",
187                                        state.gps.year,
188                                        state.gps.month,
189                                        state.gps.day);
190                         info_add_row(1, "GPS time", "  %02d:%02d:%02d",
191                                        state.gps.hour,
192                                        state.gps.minute,
193                                        state.gps.second);
194                         int     nsat_vis = 0;
195                         int     c;
196
197                         if (state.gps.cc_gps_sat == null)
198                                 info_add_row(2, "Satellites Visible", "%4d", 0);
199                         else {
200                                 info_add_row(2, "Satellites Visible", "%4d", state.gps.cc_gps_sat.length);
201                                 for (c = 0; c < state.gps.cc_gps_sat.length; c++) {
202                                         info_add_row(2, "Satellite id,C/N0",
203                                                      "%4d, %4d",
204                                                      state.gps.cc_gps_sat[c].svid,
205                                                      state.gps.cc_gps_sat[c].c_n0);
206                                 }
207                         }
208                 }
209                 info_finish();
210         }
211 }