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;
32 import altosui.AltosSerial;
33 import altosui.AltosSerialMonitor;
34 import altosui.AltosTelemetry;
35 import altosui.AltosState;
36 import altosui.AltosDeviceDialog;
38 class AltosFlightStatusTableModel extends AbstractTableModel {
39 private String[] columnNames = {"Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" };
40 private Object[] data = { 0, "idle", 0, 0 };
42 public int getColumnCount() { return columnNames.length; }
43 public int getRowCount() { return 2; }
44 public Object getValueAt(int row, int col) {
46 return columnNames[col];
50 public void setValueAt(Object value, int col) {
52 fireTableCellUpdated(1, col);
55 public void setValueAt(Object value, int row, int col) {
56 setValueAt(value, col);
59 public void set(AltosState state) {
60 setValueAt(String.format("%1.0f", state.height), 0);
61 setValueAt(state.data.state, 1);
62 setValueAt(state.data.rssi, 2);
63 double speed = state.baro_speed;
66 setValueAt(String.format("%1.0f", speed), 3);
70 class AltosFlightInfoTableModel extends AbstractTableModel {
71 private String[] columnNames = {"Field", "Value"};
77 public InfoLine(String n, String v) {
83 private ArrayList<InfoLine> rows = new ArrayList<InfoLine>();
85 public int getColumnCount() { return columnNames.length; }
86 public String getColumnName(int col) { return columnNames[col]; }
88 public int getRowCount() { return 20; }
90 public Object getValueAt(int row, int col) {
91 if (row >= rows.size())
94 return rows.get(row).name;
96 return rows.get(row).value;
100 int prev_num_rows = 0;
102 public void resetRow() {
105 public void addRow(String name, String value) {
106 if (current_row >= rows.size())
107 rows.add(current_row, new InfoLine(name, value));
109 rows.set(current_row, new InfoLine(name, value));
112 public void finish() {
113 if (current_row > prev_num_rows) {
114 fireTableRowsInserted(prev_num_rows, current_row - 1);
115 prev_num_rows = current_row;
117 fireTableDataChanged();
121 public class AltosUI extends JFrame {
122 private int channel = -1;
124 private AltosFlightStatusTableModel flightStatusModel;
125 private JTable flightStatus;
127 static final int info_columns = 3;
129 private AltosFlightInfoTableModel[] flightInfoModel;
130 private JTable[] flightInfo;
131 private AltosSerial serialLine;
136 private Font statusFont = new Font("SansSerif", Font.BOLD, 24);
137 private Font infoLabelFont = new Font("SansSerif", Font.PLAIN, 14);
138 private Font infoValueFont = new Font("Monospaced", Font.PLAIN, 14);
142 String[] statusNames = { "Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" };
143 Object[][] statusData = { { "0", "pad", "-50", "0" } };
145 vbox = Box.createVerticalBox();
148 flightStatusModel = new AltosFlightStatusTableModel();
149 flightStatus = new JTable(flightStatusModel);
150 flightStatus.setFont(statusFont);
151 TableColumnModel tcm = flightStatus.getColumnModel();
152 for (int i = 0; i < flightStatusModel.getColumnCount(); i++) {
153 DefaultTableCellRenderer r = new DefaultTableCellRenderer();
154 r.setFont(statusFont);
155 r.setHorizontalAlignment(SwingConstants.CENTER);
156 tcm.getColumn(i).setCellRenderer(r);
159 FontMetrics statusMetrics = flightStatus.getFontMetrics(statusFont);
160 int statusHeight = (statusMetrics.getHeight() + statusMetrics.getLeading()) * 15 / 10;
161 flightStatus.setRowHeight(statusHeight);
162 flightStatus.setShowGrid(false);
164 vbox.add(flightStatus);
166 hbox = Box.createHorizontalBox();
169 flightInfo = new JTable[3];
170 flightInfoModel = new AltosFlightInfoTableModel[3];
172 FontMetrics infoValueMetrics = flightStatus.getFontMetrics(infoValueFont);
173 int infoHeight = (infoValueMetrics.getHeight() + infoValueMetrics.getLeading()) * 20 / 10;
175 for (int i = 0; i < info_columns; i++) {
176 ibox[i] = Box.createVerticalBox();
177 flightInfoModel[i] = new AltosFlightInfoTableModel();
178 flightInfo[i] = new JTable(flightInfoModel[i]);
179 flightInfo[i].setFont(infoValueFont);
180 flightInfo[i].setRowHeight(infoHeight);
181 flightInfo[i].setShowGrid(true);
182 ibox[i].add(flightInfo[i].getTableHeader());
183 ibox[i].add(flightInfo[i]);
191 serialLine = new AltosSerial();
192 int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
193 this.setSize(new Dimension (infoValueMetrics.charWidth('0') * 6 * 20,
194 statusHeight * 4 + infoHeight * 17));
196 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
197 addWindowListener(new WindowAdapter() {
199 public void windowClosing(WindowEvent e) {
205 public void info_reset() {
206 for (int i = 0; i < info_columns; i++)
207 flightInfoModel[i].resetRow();
210 public void info_add_row(int col, String name, String value) {
211 flightInfoModel[col].addRow(name, value);
214 public void info_add_row(int col, String name, String format, Object value) {
215 flightInfoModel[col].addRow(name, String.format(format, value));
218 public void info_add_row(int col, String name, String format, Object v1, Object v2) {
219 flightInfoModel[col].addRow(name, String.format(format, v1, v2));
222 public void info_add_row(int col, String name, String format, Object v1, Object v2, Object v3) {
223 flightInfoModel[col].addRow(name, String.format(format, v1, v2, v3));
226 public void info_add_deg(int col, String name, double v, int pos, int neg) {
232 double deg = Math.floor(v);
233 double min = (v - deg) * 60;
235 flightInfoModel[col].addRow(name, String.format("%3.0f°%08.5f'", deg, min));
238 public void info_finish() {
239 for (int i = 0; i < info_columns; i++)
240 flightInfoModel[i].finish();
243 static final int MIN_PAD_SAMPLES = 10;
245 public void show(AltosState state) {
246 flightStatusModel.set(state);
249 if (state.npad >= MIN_PAD_SAMPLES)
250 info_add_row(0, "Ground state", "%s", "ready");
252 info_add_row(0, "Ground state", "wait (%d)",
253 MIN_PAD_SAMPLES - state.npad);
254 info_add_row(0, "Rocket state", "%s", state.data.state);
255 info_add_row(0, "Callsign", "%s", state.data.callsign);
256 info_add_row(0, "Rocket serial", "%6d", state.data.serial);
257 info_add_row(0, "Rocket flight", "%6d", state.data.flight);
259 info_add_row(0, "RSSI", "%6d dBm", state.data.rssi);
260 info_add_row(0, "Height", "%6.0f m", state.height);
261 info_add_row(0, "Max height", "%6.0f m", state.max_height);
262 info_add_row(0, "Acceleration", "%8.1f m/s²", state.acceleration);
263 info_add_row(0, "Max acceleration", "%8.1f m/s²", state.max_acceleration);
264 info_add_row(0, "Speed", "%8.1f m/s", state.ascent ? state.speed : state.baro_speed);
265 info_add_row(0, "Max Speed", "%8.1f m/s", state.max_speed);
266 info_add_row(0, "Temperature", "%9.2f °C", state.temperature);
267 info_add_row(0, "Battery", "%9.2f V", state.battery);
268 info_add_row(0, "Drogue", "%9.2f V", state.drogue_sense);
269 info_add_row(0, "Main", "%9.2f V", state.main_sense);
270 info_add_row(0, "Pad altitude", "%6.0f m", state.ground_altitude);
271 if (state.gps == null) {
272 info_add_row(1, "GPS", "not available");
274 if (state.data.gps.gps_locked)
275 info_add_row(1, "GPS", " locked");
276 else if (state.data.gps.gps_connected)
277 info_add_row(1, "GPS", " unlocked");
279 info_add_row(1, "GPS", " missing");
280 info_add_row(1, "Satellites", "%6d", state.data.gps.nsat);
281 info_add_deg(1, "Latitude", state.gps.lat, 'N', 'S');
282 info_add_deg(1, "Longitude", state.gps.lon, 'E', 'W');
283 info_add_row(1, "GPS altitude", "%6d", state.gps.alt);
284 info_add_row(1, "GPS height", "%6.0f", state.gps_height);
286 /* The SkyTraq GPS doesn't report these values */
288 info_add_row(1, "GPS ground speed", "%8.1f m/s %3d°",
289 state.gps.ground_speed,
291 info_add_row(1, "GPS climb rate", "%8.1f m/s",
292 state.gps.climb_rate);
293 info_add_row(1, "GPS error", "%6d m(h)%3d m(v)",
294 state.gps.h_error, state.gps.v_error);
296 info_add_row(1, "GPS hdop", "%8.1f", state.gps.hdop);
298 if (state.npad > 0) {
299 if (state.from_pad != null) {
300 info_add_row(1, "Distance from pad", "%6.0f m", state.from_pad.distance);
301 info_add_row(1, "Direction from pad", "%6.0f°", state.from_pad.bearing);
303 info_add_row(1, "Distance from pad", "unknown");
304 info_add_row(1, "Direction from pad", "unknown");
306 info_add_deg(1, "Pad latitude", state.pad_lat, 'N', 'S');
307 info_add_deg(1, "Pad longitude", state.pad_lon, 'E', 'W');
308 info_add_row(1, "Pad GPS alt", "%6.0f m", state.pad_alt);
310 info_add_row(1, "GPS date", "%04d-%02d-%02d",
311 state.gps.gps_time.year,
312 state.gps.gps_time.month,
313 state.gps.gps_time.day);
314 info_add_row(1, "GPS time", " %02d:%02d:%02d",
315 state.gps.gps_time.hour,
316 state.gps.gps_time.minute,
317 state.gps.gps_time.second);
321 if (state.gps.cc_gps_sat == null)
322 info_add_row(2, "Satellites Visible", "%4d", 0);
324 info_add_row(2, "Satellites Visible", "%4d", state.gps.cc_gps_sat.length);
325 for (c = 0; c < state.gps.cc_gps_sat.length; c++) {
326 info_add_row(2, "Satellite id,C/N0",
328 state.gps.cc_gps_sat[c].svid,
329 state.gps.cc_gps_sat[c].c_n0);
336 /* User Preferences */
337 Preferences altosui_preferences = Preferences.userNodeForPackage(this.getClass());
340 private File logdir = null;
342 /* logdir preference name */
343 final static String logdirPreference = "LOGDIR";
345 /* Default logdir is ~/TeleMetrum */
346 final static String logdirName = "TeleMetrum";
348 /* Initialize logdir from preferences */
350 String logdir_string = altosui_preferences.get(logdirPreference, null);
351 if (logdir_string != null)
352 logdir = new File(logdir_string);
354 /* a hack -- make the file chooser tell us what the default directory
355 * would be and stick our logdir in a subdirectory of that.
357 logdir = new File(new JFileChooser().getCurrentDirectory(), logdirName);
360 private void set_logdir(File new_logdir) {
362 System.out.printf("Set logdir to %s\n", logdir.toString());
363 synchronized (altosui_preferences) {
364 altosui_preferences.put(logdirPreference, logdir.getPath());
366 altosui_preferences.flush();
367 } catch (BackingStoreException ee) {
368 JOptionPane.showMessageDialog(AltosUI.this,
369 altosui_preferences.absolutePath(),
370 "Cannot save prefernces",
371 JOptionPane.ERROR_MESSAGE);
376 private boolean check_dir(File dir) {
379 JOptionPane.showMessageDialog(AltosUI.this,
381 "Cannot create directory",
382 JOptionPane.ERROR_MESSAGE);
385 } else if (!dir.isDirectory()) {
386 JOptionPane.showMessageDialog(AltosUI.this,
388 "Is not a directory",
389 JOptionPane.ERROR_MESSAGE);
395 class DisplayThread extends Thread {
396 String read() throws InterruptedException { return null; }
400 void update(AltosState state) throws InterruptedException { }
404 AltosState state = null;
409 while ((line = read()) != null) {
411 AltosTelemetry t = new AltosTelemetry(line);
412 state = new AltosState(t, state);
415 } catch (ParseException pp) {
416 System.out.printf("Parse error on %s\n", line);
417 System.out.println("exception " + pp);
420 } catch (InterruptedException ee) {
427 class DeviceThread extends DisplayThread {
429 LinkedBlockingQueue<String> telem;
431 String read() throws InterruptedException {
437 serial.remove_monitor(telem);
438 System.out.println("DisplayThread done");
441 public DeviceThread(AltosSerial s) {
443 telem = new LinkedBlockingQueue<String>();
444 serial.add_monitor(telem);
448 private void ConnectToDevice() {
449 AltosDevice device = AltosDeviceDialog.show(AltosUI.this, "TeleDongle");
451 if (device != null) {
453 serialLine.connect(device.tty);
454 DeviceThread thread = new DeviceThread(serialLine);
456 } catch (FileNotFoundException ee) {
457 JOptionPane.showMessageDialog(AltosUI.this,
459 "Cannot open serial port",
460 JOptionPane.ERROR_MESSAGE);
461 } catch (NoSuchPortException ee) {
462 JOptionPane.showMessageDialog(AltosUI.this,
464 "No such serial port",
465 JOptionPane.ERROR_MESSAGE);
466 } catch (PortInUseException ee) {
467 JOptionPane.showMessageDialog(AltosUI.this,
470 JOptionPane.ERROR_MESSAGE);
471 } catch (IOException ee) {
472 JOptionPane.showMessageDialog(AltosUI.this,
475 JOptionPane.ERROR_MESSAGE);
480 void DisconnectFromDevice () {
484 String readline(FileInputStream s) throws IOException {
488 while ((c = s.read()) != -1) {
493 line = line + (char) c;
499 * Open an existing telemetry file and replay it in realtime
502 class ReplayThread extends DisplayThread {
503 FileInputStream replay;
506 ReplayThread(FileInputStream in, String name) {
513 return readline(replay);
514 } catch (IOException ee) {
515 JOptionPane.showMessageDialog(AltosUI.this,
518 JOptionPane.ERROR_MESSAGE);
526 } catch (IOException ee) {
530 void update(AltosState state) throws InterruptedException {
531 /* Make it run in realtime after the rocket leaves the pad */
532 if (state.state > AltosTelemetry.ao_flight_pad)
533 Thread.sleep((int) (state.time_change * 1000));
537 Thread display_thread;
539 private void stop_display() {
540 if (display_thread != null && display_thread.isAlive())
541 display_thread.interrupt();
542 display_thread = null;
545 private void run_display(Thread thread) {
547 display_thread = thread;
548 display_thread.start();
552 * Replay a flight from telemetry data
554 private void Replay() {
555 JFileChooser logfile_chooser = new JFileChooser();
557 logfile_chooser.setDialogTitle("Select Telemetry File");
558 logfile_chooser.setFileFilter(new FileNameExtensionFilter("Telemetry file", "telem"));
559 logfile_chooser.setCurrentDirectory(logdir);
560 int returnVal = logfile_chooser.showOpenDialog(AltosUI.this);
562 if (returnVal == JFileChooser.APPROVE_OPTION) {
563 File file = logfile_chooser.getSelectedFile();
565 System.out.println("No file selected?");
566 String filename = file.getName();
568 FileInputStream replay = new FileInputStream(file);
569 ReplayThread thread = new ReplayThread(replay, filename);
571 } catch (FileNotFoundException ee) {
572 JOptionPane.showMessageDialog(AltosUI.this,
574 "Cannot open serial port",
575 JOptionPane.ERROR_MESSAGE);
580 /* Connect to TeleMetrum, either directly or through
581 * a TeleDongle over the packet link
583 private void SaveFlightData() {
586 /* Configure the log directory. This is where all telemetry and eeprom files
587 * will be written to, and where replay will look for telemetry files
589 private void ConfigureLog() {
590 JFileChooser logdir_chooser = new JFileChooser();
592 logdir_chooser.setDialogTitle("Configure Data Logging Directory");
593 logdir_chooser.setCurrentDirectory(logdir.getParentFile());
594 logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
596 if (logdir_chooser.showDialog(AltosUI.this, "Select Directory") == JFileChooser.APPROVE_OPTION) {
597 File dir = logdir_chooser.getSelectedFile();
603 /* Create the AltosUI menus
605 private void createMenu() {
606 JMenuBar menubar = new JMenuBar();
609 JRadioButtonMenuItem radioitem;
613 menu = new JMenu("File");
614 menu.setMnemonic(KeyEvent.VK_F);
617 item = new JMenuItem("Quit",KeyEvent.VK_Q);
618 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
619 ActionEvent.CTRL_MASK));
620 item.addActionListener(new ActionListener() {
621 public void actionPerformed(ActionEvent e) {
630 menu = new JMenu("Device");
631 menu.setMnemonic(KeyEvent.VK_D);
634 item = new JMenuItem("Connect to Device",KeyEvent.VK_C);
635 item.addActionListener(new ActionListener() {
636 public void actionPerformed(ActionEvent e) {
642 item = new JMenuItem("Disconnect from Device",KeyEvent.VK_D);
643 item.addActionListener(new ActionListener() {
644 public void actionPerformed(ActionEvent e) {
645 DisconnectFromDevice();
652 item = new JMenuItem("Save Flight Data",KeyEvent.VK_S);
653 item.addActionListener(new ActionListener() {
654 public void actionPerformed(ActionEvent e) {
660 item = new JMenuItem("Replay",KeyEvent.VK_R);
661 item.addActionListener(new ActionListener() {
662 public void actionPerformed(ActionEvent e) {
670 menu = new JMenu("Log");
671 menu.setMnemonic(KeyEvent.VK_L);
674 item = new JMenuItem("New Log",KeyEvent.VK_N);
675 item.addActionListener(new ActionListener() {
676 public void actionPerformed(ActionEvent e) {
681 item = new JMenuItem("Configure Log",KeyEvent.VK_C);
682 item.addActionListener(new ActionListener() {
683 public void actionPerformed(ActionEvent e) {
691 menu = new JMenu("Voice", true);
692 menu.setMnemonic(KeyEvent.VK_V);
695 radioitem = new JRadioButtonMenuItem("Enable Voice");
696 radioitem.addActionListener(new ActionListener() {
697 public void actionPerformed(ActionEvent e) {
705 menu = new JMenu("Channel", true);
706 menu.setMnemonic(KeyEvent.VK_C);
708 ButtonGroup group = new ButtonGroup();
710 for (int c = 0; c <= 9; c++) {
711 radioitem = new JRadioButtonMenuItem(String.format("Channel %1d (%7.3fMHz)", c,
714 radioitem.setActionCommand(String.format("%d", c));
715 radioitem.addActionListener(new ActionListener() {
716 public void actionPerformed(ActionEvent e) {
717 System.out.println("Command: " + e.getActionCommand() + " param: " +
722 group.add(radioitem);
726 this.setJMenuBar(menubar);
729 public static void main(final String[] args) {
730 AltosUI altosui = new AltosUI();
731 altosui.setVisible(true);