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 gnu.io.CommPortIdentifier;
30 import altosui.AltosSerial;
31 import altosui.AltosSerialMonitor;
32 import altosui.AltosTelemetry;
33 import altosui.AltosState;
35 class AltosUIMonitor implements AltosSerialMonitor {
36 public void data(String data) {
37 System.out.println(data);
41 class AltosFlightStatusTableModel extends AbstractTableModel {
42 private String[] columnNames = {"Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" };
43 private Object[] data = { 0, "idle", 0, 0 };
45 public int getColumnCount() { return columnNames.length; }
46 public int getRowCount() { return 2; }
47 public Object getValueAt(int row, int col) {
49 return columnNames[col];
53 public void setValueAt(Object value, int col) {
55 fireTableCellUpdated(1, col);
58 public void setValueAt(Object value, int row, int col) {
59 setValueAt(value, col);
62 public void set(AltosState state) {
63 setValueAt(String.format("%1.0f", state.height), 0);
64 setValueAt(state.data.state, 1);
65 setValueAt(state.data.rssi, 2);
66 double speed = state.baro_speed;
69 setValueAt(String.format("%1.0f", speed), 3);
73 class AltosFlightStatusCellRenderer extends DefaultTableCellRenderer {
75 static Font statusFont = new Font("SansSerif", Font.BOLD, 24);
77 @Override public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected,
78 boolean hasFocus, int row, int column)
80 Component cell = super.getTableCellRendererComponent
81 (table, value, isSelected, hasFocus, row, column);
82 System.out.println("Selecting new font for cell " + row + " " + column + " " + statusFont);
83 cell.setFont(statusFont);
87 public AltosFlightStatusCellRenderer () {
89 System.out.println("Made a status cell renderer\n");
93 class AltosFlightInfoTableModel extends AbstractTableModel {
94 private String[] columnNames = {"Field", "Value"};
100 public InfoLine(String n, String v) {
106 private ArrayList<InfoLine> rows = new ArrayList<InfoLine>();
108 public int getColumnCount() { return columnNames.length; }
109 public String getColumnName(int col) { return columnNames[col]; }
111 public int getRowCount() { return 20; }
113 public Object getValueAt(int row, int col) {
114 if (row >= rows.size())
117 return rows.get(row).name;
119 return rows.get(row).value;
123 int prev_num_rows = 0;
125 public void resetRow() {
128 public void addRow(String name, String value) {
129 if (current_row >= rows.size())
130 rows.add(current_row, new InfoLine(name, value));
132 rows.set(current_row, new InfoLine(name, value));
135 public void finish() {
136 if (current_row > prev_num_rows) {
137 fireTableRowsInserted(prev_num_rows, current_row - 1);
138 prev_num_rows = current_row;
140 fireTableDataChanged();
144 public class AltosUI extends JFrame {
145 private int channel = -1;
147 private AltosFlightStatusTableModel flightStatusModel;
148 private JTable flightStatus;
150 static final int info_columns = 3;
152 private AltosFlightInfoTableModel[] flightInfoModel;
153 private JTable[] flightInfo;
154 private AltosSerial serialLine;
159 private Font statusFont = new Font("SansSerif", Font.BOLD, 24);
160 private Font infoLabelFont = new Font("SansSerif", Font.PLAIN, 14);
161 private Font infoValueFont = new Font("Monospaced", Font.PLAIN, 14);
165 String[] statusNames = { "Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" };
166 Object[][] statusData = { { "0", "pad", "-50", "0" } };
168 vbox = Box.createVerticalBox();
171 flightStatusModel = new AltosFlightStatusTableModel();
172 flightStatus = new JTable(flightStatusModel);
173 flightStatus.setFont(statusFont);
174 TableColumnModel tcm = flightStatus.getColumnModel();
175 for (int i = 0; i < flightStatusModel.getColumnCount(); i++) {
176 DefaultTableCellRenderer r = new DefaultTableCellRenderer();
177 r.setFont(statusFont);
178 r.setHorizontalAlignment(SwingConstants.CENTER);
179 tcm.getColumn(i).setCellRenderer(r);
182 FontMetrics statusMetrics = flightStatus.getFontMetrics(statusFont);
183 int statusHeight = (statusMetrics.getHeight() + statusMetrics.getLeading()) * 15 / 10;
184 flightStatus.setRowHeight(statusHeight);
185 flightStatus.setShowGrid(false);
187 vbox.add(flightStatus);
189 hbox = Box.createHorizontalBox();
192 flightInfo = new JTable[3];
193 flightInfoModel = new AltosFlightInfoTableModel[3];
195 FontMetrics infoValueMetrics = flightStatus.getFontMetrics(infoValueFont);
196 int infoHeight = (infoValueMetrics.getHeight() + infoValueMetrics.getLeading()) * 20 / 10;
198 for (int i = 0; i < info_columns; i++) {
199 ibox[i] = Box.createVerticalBox();
200 flightInfoModel[i] = new AltosFlightInfoTableModel();
201 flightInfo[i] = new JTable(flightInfoModel[i]);
202 flightInfo[i].setFont(infoValueFont);
203 flightInfo[i].setRowHeight(infoHeight);
204 flightInfo[i].setShowGrid(true);
205 ibox[i].add(flightInfo[i].getTableHeader());
206 ibox[i].add(flightInfo[i]);
214 serialLine = new AltosSerial();
215 serialLine.monitor(new AltosUIMonitor());
216 int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
217 this.setSize(new Dimension (infoValueMetrics.charWidth('0') * 6 * 20,
218 statusHeight * 4 + infoHeight * 17));
220 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
221 addWindowListener(new WindowAdapter() {
223 public void windowClosing(WindowEvent e) {
229 public void info_reset() {
230 for (int i = 0; i < info_columns; i++)
231 flightInfoModel[i].resetRow();
234 public void info_add_row(int col, String name, String value) {
235 flightInfoModel[col].addRow(name, value);
238 public void info_add_row(int col, String name, String format, Object value) {
239 flightInfoModel[col].addRow(name, String.format(format, value));
242 public void info_add_row(int col, String name, String format, Object v1, Object v2) {
243 flightInfoModel[col].addRow(name, String.format(format, v1, v2));
246 public void info_add_row(int col, String name, String format, Object v1, Object v2, Object v3) {
247 flightInfoModel[col].addRow(name, String.format(format, v1, v2, v3));
250 public void info_add_deg(int col, String name, double v, int pos, int neg) {
256 double deg = Math.floor(v);
257 double min = (v - deg) * 60;
259 flightInfoModel[col].addRow(name, String.format("%3.0f°%08.5f'", deg, min));
262 public void info_finish() {
263 for (int i = 0; i < info_columns; i++)
264 flightInfoModel[i].finish();
267 static final int MIN_PAD_SAMPLES = 10;
269 public void show(AltosState state) {
270 flightStatusModel.set(state);
273 if (state.npad >= MIN_PAD_SAMPLES)
274 info_add_row(0, "Ground state", "%s", "ready");
276 info_add_row(0, "Ground state", "wait (%d)",
277 MIN_PAD_SAMPLES - state.npad);
278 info_add_row(0, "Rocket state", "%s", state.data.state);
279 info_add_row(0, "Callsign", "%s", state.data.callsign);
280 info_add_row(0, "Rocket serial", "%6d", state.data.serial);
281 info_add_row(0, "Rocket flight", "%6d", state.data.flight);
283 info_add_row(0, "RSSI", "%6d dBm", state.data.rssi);
284 info_add_row(0, "Height", "%6.0f m", state.height);
285 info_add_row(0, "Max height", "%6.0f m", state.max_height);
286 info_add_row(0, "Acceleration", "%8.1f m/s²", state.acceleration);
287 info_add_row(0, "Max acceleration", "%8.1f m/s²", state.max_acceleration);
288 info_add_row(0, "Speed", "%8.1f m/s", state.ascent ? state.speed : state.baro_speed);
289 info_add_row(0, "Max Speed", "%8.1f m/s", state.max_speed);
290 info_add_row(0, "Temperature", "%9.2f °C", state.temperature);
291 info_add_row(0, "Battery", "%9.2f V", state.battery);
292 info_add_row(0, "Drogue", "%9.2f V", state.drogue_sense);
293 info_add_row(0, "Main", "%9.2f V", state.main_sense);
294 info_add_row(0, "Pad altitude", "%6.0f m", state.ground_altitude);
295 if (state.gps == null) {
296 info_add_row(1, "GPS", "not available");
298 if (state.data.gps.gps_locked)
299 info_add_row(1, "GPS", " locked");
300 else if (state.data.gps.gps_connected)
301 info_add_row(1, "GPS", " unlocked");
303 info_add_row(1, "GPS", " missing");
304 info_add_row(1, "Satellites", "%6d", state.data.gps.nsat);
305 info_add_deg(1, "Latitude", state.gps.lat, 'N', 'S');
306 info_add_deg(1, "Longitude", state.gps.lon, 'E', 'W');
307 info_add_row(1, "GPS altitude", "%6d", state.gps.alt);
308 info_add_row(1, "GPS height", "%6.0f", state.gps_height);
310 /* The SkyTraq GPS doesn't report these values */
312 info_add_row(1, "GPS ground speed", "%8.1f m/s %3d°",
313 state.gps.ground_speed,
315 info_add_row(1, "GPS climb rate", "%8.1f m/s",
316 state.gps.climb_rate);
317 info_add_row(1, "GPS error", "%6d m(h)%3d m(v)",
318 state.gps.h_error, state.gps.v_error);
320 info_add_row(1, "GPS hdop", "%8.1f", state.gps.hdop);
322 if (state.npad > 0) {
323 if (state.from_pad != null) {
324 info_add_row(1, "Distance from pad", "%6.0f m", state.from_pad.distance);
325 info_add_row(1, "Direction from pad", "%6.0f°", state.from_pad.bearing);
327 info_add_row(1, "Distance from pad", "unknown");
328 info_add_row(1, "Direction from pad", "unknown");
330 info_add_deg(1, "Pad latitude", state.pad_lat, 'N', 'S');
331 info_add_deg(1, "Pad longitude", state.pad_lon, 'E', 'W');
332 info_add_row(1, "Pad GPS alt", "%6.0f m", state.pad_alt);
334 info_add_row(1, "GPS date", "%04d-%02d-%02d",
335 state.gps.gps_time.year,
336 state.gps.gps_time.month,
337 state.gps.gps_time.day);
338 info_add_row(1, "GPS time", " %02d:%02d:%02d",
339 state.gps.gps_time.hour,
340 state.gps.gps_time.minute,
341 state.gps.gps_time.second);
345 if (state.gps.cc_gps_sat == null)
346 info_add_row(2, "Satellites Visible", "%4d", 0);
348 info_add_row(2, "Satellites Visible", "%4d", state.gps.cc_gps_sat.length);
349 for (c = 0; c < state.gps.cc_gps_sat.length; c++) {
350 info_add_row(2, "Satellite id,C/N0",
352 state.gps.cc_gps_sat[c].svid,
353 state.gps.cc_gps_sat[c].c_n0);
361 final JFileChooser deviceChooser = new JFileChooser();
362 final JFileChooser logdirChooser = new JFileChooser();
363 final String logdirName = "TeleMetrum";
366 private void setLogdir() {
368 logdir = new File(logdirChooser.getCurrentDirectory(), logdirName);
369 logdirChooser.setCurrentDirectory(logdir);
372 private void makeLogdir() {
374 if (!logdir.exists()) {
375 if (!logdir.mkdirs())
376 JOptionPane.showMessageDialog(AltosUI.this,
378 "Cannot create directory",
379 JOptionPane.ERROR_MESSAGE);
380 } else if (!logdir.isDirectory()) {
381 JOptionPane.showMessageDialog(AltosUI.this,
383 "Is not a directory",
384 JOptionPane.ERROR_MESSAGE);
388 private void PickSerialDevice() {
389 java.util.Enumeration<CommPortIdentifier> port_list = CommPortIdentifier.getPortIdentifiers();
390 while (port_list.hasMoreElements()) {
391 CommPortIdentifier identifier = port_list.nextElement();
392 System.out.println("Serial port " + identifier.getName());
396 private void ConnectToDevice() {
398 int returnVal = deviceChooser.showOpenDialog(AltosUI.this);
400 if (returnVal == JFileChooser.APPROVE_OPTION) {
401 File file = deviceChooser.getSelectedFile();
403 serialLine.open(file);
404 } catch (FileNotFoundException ee) {
405 JOptionPane.showMessageDialog(AltosUI.this,
407 "Cannot open serial port",
408 JOptionPane.ERROR_MESSAGE);
413 String readline(FileInputStream s) throws IOException {
417 while ((c = s.read()) != -1) {
422 line = line + (char) c;
428 * Open an existing telemetry file and replay it in realtime
431 class ReplayThread extends Thread {
432 FileInputStream replay;
435 ReplayThread(FileInputStream in, String name) {
442 AltosState state = null;
444 while ((line = readline(replay)) != null) {
446 AltosTelemetry t = new AltosTelemetry(line);
447 state = new AltosState(t, state);
450 if (state.state > AltosTelemetry.ao_flight_pad)
451 Thread.sleep((int) (state.time_change * 1000));
452 } catch (InterruptedException e) {}
453 } catch (ParseException pp) {
454 JOptionPane.showMessageDialog(AltosUI.this,
457 JOptionPane.ERROR_MESSAGE);
461 } catch (IOException ee) {
462 JOptionPane.showMessageDialog(AltosUI.this,
465 JOptionPane.ERROR_MESSAGE);
469 } catch (IOException e) {}
474 private void Replay() {
476 logdirChooser.setDialogTitle("Select Telemetry File");
477 logdirChooser.setFileFilter(new FileNameExtensionFilter("Telemetry file", "telem"));
478 int returnVal = logdirChooser.showOpenDialog(AltosUI.this);
480 if (returnVal == JFileChooser.APPROVE_OPTION) {
481 File file = logdirChooser.getSelectedFile();
483 System.out.println("No file selected?");
484 String filename = file.getName();
486 FileInputStream replay = new FileInputStream(file);
487 ReplayThread thread = new ReplayThread(replay, filename);
489 } catch (FileNotFoundException ee) {
490 JOptionPane.showMessageDialog(AltosUI.this,
492 "Cannot open serial port",
493 JOptionPane.ERROR_MESSAGE);
498 private void SaveFlightData() {
501 private void createMenu() {
502 JMenuBar menubar = new JMenuBar();
505 JRadioButtonMenuItem radioitem;
509 menu = new JMenu("File");
510 menu.setMnemonic(KeyEvent.VK_F);
513 item = new JMenuItem("Quit",KeyEvent.VK_Q);
514 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
515 ActionEvent.CTRL_MASK));
516 item.addActionListener(new ActionListener() {
517 public void actionPerformed(ActionEvent e) {
526 menu = new JMenu("Device");
527 menu.setMnemonic(KeyEvent.VK_D);
530 item = new JMenuItem("Connect to Device",KeyEvent.VK_C);
531 item.addActionListener(new ActionListener() {
532 public void actionPerformed(ActionEvent e) {
538 item = new JMenuItem("Disconnect from Device",KeyEvent.VK_D);
539 item.addActionListener(new ActionListener() {
540 public void actionPerformed(ActionEvent e) {
548 item = new JMenuItem("Save Flight Data",KeyEvent.VK_S);
549 item.addActionListener(new ActionListener() {
550 public void actionPerformed(ActionEvent e) {
556 item = new JMenuItem("Replay",KeyEvent.VK_R);
557 item.addActionListener(new ActionListener() {
558 public void actionPerformed(ActionEvent e) {
566 menu = new JMenu("Log");
567 menu.setMnemonic(KeyEvent.VK_L);
570 item = new JMenuItem("New Log",KeyEvent.VK_N);
571 item.addActionListener(new ActionListener() {
572 public void actionPerformed(ActionEvent e) {
577 item = new JMenuItem("Configure Log",KeyEvent.VK_C);
578 item.addActionListener(new ActionListener() {
579 public void actionPerformed(ActionEvent e) {
586 menu = new JMenu("Voice", true);
587 menu.setMnemonic(KeyEvent.VK_V);
590 radioitem = new JRadioButtonMenuItem("Enable Voice");
591 radioitem.addActionListener(new ActionListener() {
592 public void actionPerformed(ActionEvent e) {
600 menu = new JMenu("Channel", true);
601 menu.setMnemonic(KeyEvent.VK_C);
603 ButtonGroup group = new ButtonGroup();
605 for (int c = 0; c <= 9; c++) {
606 radioitem = new JRadioButtonMenuItem(String.format("Channel %1d (%7.3fMHz)", c,
609 radioitem.setActionCommand(String.format("%d", c));
610 radioitem.addActionListener(new ActionListener() {
611 public void actionPerformed(ActionEvent e) {
612 System.out.println("Command: " + e.getActionCommand() + " param: " +
617 group.add(radioitem);
621 this.setJMenuBar(menubar);
624 public static void main(final String[] args) {
625 AltosUI altosui = new AltosUI();
626 altosui.setVisible(true);