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