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);
309 info_add_row(1, "GPS ground speed", "%8.1f m/s %3d°",
310 state.gps.ground_speed,
312 info_add_row(1, "GPS climb rate", "%8.1f m/s",
313 state.gps.climb_rate);
314 info_add_row(1, "GPS hdop", "%8.1f", state.gps.hdop);
315 info_add_row(1, "GPS error", "%6d m(h)%3d m(v)",
316 state.gps.h_error, state.gps.v_error);
317 if (state.npad > 0) {
318 if (state.from_pad != null) {
319 info_add_row(1, "Distance from pad", "%6.0f m", state.from_pad.distance);
320 info_add_row(1, "Direction from pad", "%6.0f°", state.from_pad.bearing);
322 info_add_row(1, "Distance from pad", "unknown");
323 info_add_row(1, "Direction from pad", "unknown");
325 info_add_deg(1, "Pad latitude", state.pad_lat, 'N', 'S');
326 info_add_deg(1, "Pad longitude", state.pad_lon, 'E', 'W');
327 info_add_row(1, "Pad GPS alt", "%9.2fm", state.pad_alt);
329 info_add_row(1, "GPS date", "%04d-%02d-%02d",
330 state.gps.gps_time.year,
331 state.gps.gps_time.month,
332 state.gps.gps_time.day);
333 info_add_row(1, "GPS time", " %02d:%02d:%02d",
334 state.gps.gps_time.hour,
335 state.gps.gps_time.minute,
336 state.gps.gps_time.second);
340 if (state.gps.cc_gps_sat == null)
341 info_add_row(2, "Satellites Visible", "%4d", 0);
343 info_add_row(2, "Satellites Visible", "%4d", state.gps.cc_gps_sat.length);
344 for (c = 0; c < state.gps.cc_gps_sat.length; c++) {
345 info_add_row(2, "Satellite id,C/N0",
347 state.gps.cc_gps_sat[c].svid,
348 state.gps.cc_gps_sat[c].c_n0);
356 final JFileChooser deviceChooser = new JFileChooser();
357 final JFileChooser logdirChooser = new JFileChooser();
358 final String logdirName = "TeleMetrum";
361 private void setLogdir() {
363 logdir = new File(logdirChooser.getCurrentDirectory(), logdirName);
364 logdirChooser.setCurrentDirectory(logdir);
367 private void makeLogdir() {
369 if (!logdir.exists()) {
370 if (!logdir.mkdirs())
371 JOptionPane.showMessageDialog(AltosUI.this,
373 "Cannot create directory",
374 JOptionPane.ERROR_MESSAGE);
375 } else if (!logdir.isDirectory()) {
376 JOptionPane.showMessageDialog(AltosUI.this,
378 "Is not a directory",
379 JOptionPane.ERROR_MESSAGE);
383 private void PickSerialDevice() {
384 java.util.Enumeration<CommPortIdentifier> port_list = CommPortIdentifier.getPortIdentifiers();
385 while (port_list.hasMoreElements()) {
386 CommPortIdentifier identifier = port_list.nextElement();
387 System.out.println("Serial port " + identifier.getName());
391 private void ConnectToDevice() {
393 int returnVal = deviceChooser.showOpenDialog(AltosUI.this);
395 if (returnVal == JFileChooser.APPROVE_OPTION) {
396 File file = deviceChooser.getSelectedFile();
398 serialLine.open(file);
399 } catch (FileNotFoundException ee) {
400 JOptionPane.showMessageDialog(AltosUI.this,
402 "Cannot open serial port",
403 JOptionPane.ERROR_MESSAGE);
408 String readline(FileInputStream s) throws IOException {
412 while ((c = s.read()) != -1) {
417 line = line + (char) c;
423 * Open an existing telemetry file and replay it in realtime
426 class ReplayThread extends Thread {
427 FileInputStream replay;
430 ReplayThread(FileInputStream in, String name) {
437 AltosState state = null;
439 while ((line = readline(replay)) != null) {
441 AltosTelemetry t = new AltosTelemetry(line);
442 state = new AltosState(t, state);
445 if (state.state > AltosTelemetry.ao_flight_pad)
446 Thread.sleep((int) (state.time_change * 1000));
447 } catch (InterruptedException e) {}
448 } catch (ParseException pp) {
449 JOptionPane.showMessageDialog(AltosUI.this,
452 JOptionPane.ERROR_MESSAGE);
456 } catch (IOException ee) {
457 JOptionPane.showMessageDialog(AltosUI.this,
460 JOptionPane.ERROR_MESSAGE);
464 } catch (IOException e) {}
469 private void Replay() {
471 logdirChooser.setDialogTitle("Select Telemetry File");
472 logdirChooser.setFileFilter(new FileNameExtensionFilter("Telemetry file", "telem"));
473 int returnVal = logdirChooser.showOpenDialog(AltosUI.this);
475 if (returnVal == JFileChooser.APPROVE_OPTION) {
476 File file = logdirChooser.getSelectedFile();
478 System.out.println("No file selected?");
479 String filename = file.getName();
481 FileInputStream replay = new FileInputStream(file);
482 ReplayThread thread = new ReplayThread(replay, filename);
484 } catch (FileNotFoundException ee) {
485 JOptionPane.showMessageDialog(AltosUI.this,
487 "Cannot open serial port",
488 JOptionPane.ERROR_MESSAGE);
493 private void SaveFlightData() {
496 private void createMenu() {
497 JMenuBar menubar = new JMenuBar();
500 JRadioButtonMenuItem radioitem;
504 menu = new JMenu("File");
505 menu.setMnemonic(KeyEvent.VK_F);
508 item = new JMenuItem("Quit",KeyEvent.VK_Q);
509 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
510 ActionEvent.CTRL_MASK));
511 item.addActionListener(new ActionListener() {
512 public void actionPerformed(ActionEvent e) {
521 menu = new JMenu("Device");
522 menu.setMnemonic(KeyEvent.VK_D);
525 item = new JMenuItem("Connect to Device",KeyEvent.VK_C);
526 item.addActionListener(new ActionListener() {
527 public void actionPerformed(ActionEvent e) {
533 item = new JMenuItem("Disconnect from Device",KeyEvent.VK_D);
534 item.addActionListener(new ActionListener() {
535 public void actionPerformed(ActionEvent e) {
543 item = new JMenuItem("Save Flight Data",KeyEvent.VK_S);
544 item.addActionListener(new ActionListener() {
545 public void actionPerformed(ActionEvent e) {
551 item = new JMenuItem("Replay",KeyEvent.VK_R);
552 item.addActionListener(new ActionListener() {
553 public void actionPerformed(ActionEvent e) {
561 menu = new JMenu("Log");
562 menu.setMnemonic(KeyEvent.VK_L);
565 item = new JMenuItem("New Log",KeyEvent.VK_N);
566 item.addActionListener(new ActionListener() {
567 public void actionPerformed(ActionEvent e) {
572 item = new JMenuItem("Configure Log",KeyEvent.VK_C);
573 item.addActionListener(new ActionListener() {
574 public void actionPerformed(ActionEvent e) {
581 menu = new JMenu("Voice", true);
582 menu.setMnemonic(KeyEvent.VK_V);
585 radioitem = new JRadioButtonMenuItem("Enable Voice");
586 radioitem.addActionListener(new ActionListener() {
587 public void actionPerformed(ActionEvent e) {
595 menu = new JMenu("Channel", true);
596 menu.setMnemonic(KeyEvent.VK_C);
598 ButtonGroup group = new ButtonGroup();
600 for (int c = 0; c <= 9; c++) {
601 radioitem = new JRadioButtonMenuItem(String.format("Channel %1d (%7.3fMHz)", c,
604 radioitem.setActionCommand(String.format("%d", c));
605 radioitem.addActionListener(new ActionListener() {
606 public void actionPerformed(ActionEvent e) {
607 System.out.println("Command: " + e.getActionCommand() + " param: " +
612 group.add(radioitem);
616 this.setJMenuBar(menubar);
619 public static void main(final String[] args) {
620 AltosUI altosui = new AltosUI();
621 altosui.setVisible(true);