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.*;
31 import altosui.AltosSerial;
32 import altosui.AltosSerialMonitor;
33 import altosui.AltosTelemetry;
34 import altosui.AltosState;
36 class AltosUIMonitor implements AltosSerialMonitor {
37 public void data(String data) {
38 System.out.println(data);
42 class AltosFlightStatusTableModel extends AbstractTableModel {
43 private String[] columnNames = {"Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" };
44 private Object[] data = { 0, "idle", 0, 0 };
46 public int getColumnCount() { return columnNames.length; }
47 public int getRowCount() { return 2; }
48 public Object getValueAt(int row, int col) {
50 return columnNames[col];
54 public void setValueAt(Object value, int col) {
56 fireTableCellUpdated(1, col);
59 public void setValueAt(Object value, int row, int col) {
60 setValueAt(value, col);
63 public void set(AltosState state) {
64 setValueAt(String.format("%1.0f", state.height), 0);
65 setValueAt(state.data.state, 1);
66 setValueAt(state.data.rssi, 2);
67 double speed = state.baro_speed;
70 setValueAt(String.format("%1.0f", speed), 3);
74 class AltosFlightInfoTableModel extends AbstractTableModel {
75 private String[] columnNames = {"Field", "Value"};
81 public InfoLine(String n, String v) {
87 private ArrayList<InfoLine> rows = new ArrayList<InfoLine>();
89 public int getColumnCount() { return columnNames.length; }
90 public String getColumnName(int col) { return columnNames[col]; }
92 public int getRowCount() { return 20; }
94 public Object getValueAt(int row, int col) {
95 if (row >= rows.size())
98 return rows.get(row).name;
100 return rows.get(row).value;
104 int prev_num_rows = 0;
106 public void resetRow() {
109 public void addRow(String name, String value) {
110 if (current_row >= rows.size())
111 rows.add(current_row, new InfoLine(name, value));
113 rows.set(current_row, new InfoLine(name, value));
116 public void finish() {
117 if (current_row > prev_num_rows) {
118 fireTableRowsInserted(prev_num_rows, current_row - 1);
119 prev_num_rows = current_row;
121 fireTableDataChanged();
125 public class AltosUI extends JFrame {
126 private int channel = -1;
128 private AltosFlightStatusTableModel flightStatusModel;
129 private JTable flightStatus;
131 static final int info_columns = 3;
133 private AltosFlightInfoTableModel[] flightInfoModel;
134 private JTable[] flightInfo;
135 private AltosSerial serialLine;
140 private Font statusFont = new Font("SansSerif", Font.BOLD, 24);
141 private Font infoLabelFont = new Font("SansSerif", Font.PLAIN, 14);
142 private Font infoValueFont = new Font("Monospaced", Font.PLAIN, 14);
146 String[] statusNames = { "Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" };
147 Object[][] statusData = { { "0", "pad", "-50", "0" } };
149 vbox = Box.createVerticalBox();
152 flightStatusModel = new AltosFlightStatusTableModel();
153 flightStatus = new JTable(flightStatusModel);
154 flightStatus.setFont(statusFont);
155 TableColumnModel tcm = flightStatus.getColumnModel();
156 for (int i = 0; i < flightStatusModel.getColumnCount(); i++) {
157 DefaultTableCellRenderer r = new DefaultTableCellRenderer();
158 r.setFont(statusFont);
159 r.setHorizontalAlignment(SwingConstants.CENTER);
160 tcm.getColumn(i).setCellRenderer(r);
163 FontMetrics statusMetrics = flightStatus.getFontMetrics(statusFont);
164 int statusHeight = (statusMetrics.getHeight() + statusMetrics.getLeading()) * 15 / 10;
165 flightStatus.setRowHeight(statusHeight);
166 flightStatus.setShowGrid(false);
168 vbox.add(flightStatus);
170 hbox = Box.createHorizontalBox();
173 flightInfo = new JTable[3];
174 flightInfoModel = new AltosFlightInfoTableModel[3];
176 FontMetrics infoValueMetrics = flightStatus.getFontMetrics(infoValueFont);
177 int infoHeight = (infoValueMetrics.getHeight() + infoValueMetrics.getLeading()) * 20 / 10;
179 for (int i = 0; i < info_columns; i++) {
180 ibox[i] = Box.createVerticalBox();
181 flightInfoModel[i] = new AltosFlightInfoTableModel();
182 flightInfo[i] = new JTable(flightInfoModel[i]);
183 flightInfo[i].setFont(infoValueFont);
184 flightInfo[i].setRowHeight(infoHeight);
185 flightInfo[i].setShowGrid(true);
186 ibox[i].add(flightInfo[i].getTableHeader());
187 ibox[i].add(flightInfo[i]);
195 serialLine = new AltosSerial();
196 serialLine.monitor(new AltosUIMonitor());
197 int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
198 this.setSize(new Dimension (infoValueMetrics.charWidth('0') * 6 * 20,
199 statusHeight * 4 + infoHeight * 17));
201 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
202 addWindowListener(new WindowAdapter() {
204 public void windowClosing(WindowEvent e) {
210 public void info_reset() {
211 for (int i = 0; i < info_columns; i++)
212 flightInfoModel[i].resetRow();
215 public void info_add_row(int col, String name, String value) {
216 flightInfoModel[col].addRow(name, value);
219 public void info_add_row(int col, String name, String format, Object value) {
220 flightInfoModel[col].addRow(name, String.format(format, value));
223 public void info_add_row(int col, String name, String format, Object v1, Object v2) {
224 flightInfoModel[col].addRow(name, String.format(format, v1, v2));
227 public void info_add_row(int col, String name, String format, Object v1, Object v2, Object v3) {
228 flightInfoModel[col].addRow(name, String.format(format, v1, v2, v3));
231 public void info_add_deg(int col, String name, double v, int pos, int neg) {
237 double deg = Math.floor(v);
238 double min = (v - deg) * 60;
240 flightInfoModel[col].addRow(name, String.format("%3.0f°%08.5f'", deg, min));
243 public void info_finish() {
244 for (int i = 0; i < info_columns; i++)
245 flightInfoModel[i].finish();
248 static final int MIN_PAD_SAMPLES = 10;
250 public void show(AltosState state) {
251 flightStatusModel.set(state);
254 if (state.npad >= MIN_PAD_SAMPLES)
255 info_add_row(0, "Ground state", "%s", "ready");
257 info_add_row(0, "Ground state", "wait (%d)",
258 MIN_PAD_SAMPLES - state.npad);
259 info_add_row(0, "Rocket state", "%s", state.data.state);
260 info_add_row(0, "Callsign", "%s", state.data.callsign);
261 info_add_row(0, "Rocket serial", "%6d", state.data.serial);
262 info_add_row(0, "Rocket flight", "%6d", state.data.flight);
264 info_add_row(0, "RSSI", "%6d dBm", state.data.rssi);
265 info_add_row(0, "Height", "%6.0f m", state.height);
266 info_add_row(0, "Max height", "%6.0f m", state.max_height);
267 info_add_row(0, "Acceleration", "%8.1f m/s²", state.acceleration);
268 info_add_row(0, "Max acceleration", "%8.1f m/s²", state.max_acceleration);
269 info_add_row(0, "Speed", "%8.1f m/s", state.ascent ? state.speed : state.baro_speed);
270 info_add_row(0, "Max Speed", "%8.1f m/s", state.max_speed);
271 info_add_row(0, "Temperature", "%9.2f °C", state.temperature);
272 info_add_row(0, "Battery", "%9.2f V", state.battery);
273 info_add_row(0, "Drogue", "%9.2f V", state.drogue_sense);
274 info_add_row(0, "Main", "%9.2f V", state.main_sense);
275 info_add_row(0, "Pad altitude", "%6.0f m", state.ground_altitude);
276 if (state.gps == null) {
277 info_add_row(1, "GPS", "not available");
279 if (state.data.gps.gps_locked)
280 info_add_row(1, "GPS", " locked");
281 else if (state.data.gps.gps_connected)
282 info_add_row(1, "GPS", " unlocked");
284 info_add_row(1, "GPS", " missing");
285 info_add_row(1, "Satellites", "%6d", state.data.gps.nsat);
286 info_add_deg(1, "Latitude", state.gps.lat, 'N', 'S');
287 info_add_deg(1, "Longitude", state.gps.lon, 'E', 'W');
288 info_add_row(1, "GPS altitude", "%6d", state.gps.alt);
289 info_add_row(1, "GPS height", "%6.0f", state.gps_height);
291 /* The SkyTraq GPS doesn't report these values */
293 info_add_row(1, "GPS ground speed", "%8.1f m/s %3d°",
294 state.gps.ground_speed,
296 info_add_row(1, "GPS climb rate", "%8.1f m/s",
297 state.gps.climb_rate);
298 info_add_row(1, "GPS error", "%6d m(h)%3d m(v)",
299 state.gps.h_error, state.gps.v_error);
301 info_add_row(1, "GPS hdop", "%8.1f", state.gps.hdop);
303 if (state.npad > 0) {
304 if (state.from_pad != null) {
305 info_add_row(1, "Distance from pad", "%6.0f m", state.from_pad.distance);
306 info_add_row(1, "Direction from pad", "%6.0f°", state.from_pad.bearing);
308 info_add_row(1, "Distance from pad", "unknown");
309 info_add_row(1, "Direction from pad", "unknown");
311 info_add_deg(1, "Pad latitude", state.pad_lat, 'N', 'S');
312 info_add_deg(1, "Pad longitude", state.pad_lon, 'E', 'W');
313 info_add_row(1, "Pad GPS alt", "%6.0f m", state.pad_alt);
315 info_add_row(1, "GPS date", "%04d-%02d-%02d",
316 state.gps.gps_time.year,
317 state.gps.gps_time.month,
318 state.gps.gps_time.day);
319 info_add_row(1, "GPS time", " %02d:%02d:%02d",
320 state.gps.gps_time.hour,
321 state.gps.gps_time.minute,
322 state.gps.gps_time.second);
326 if (state.gps.cc_gps_sat == null)
327 info_add_row(2, "Satellites Visible", "%4d", 0);
329 info_add_row(2, "Satellites Visible", "%4d", state.gps.cc_gps_sat.length);
330 for (c = 0; c < state.gps.cc_gps_sat.length; c++) {
331 info_add_row(2, "Satellite id,C/N0",
333 state.gps.cc_gps_sat[c].svid,
334 state.gps.cc_gps_sat[c].c_n0);
341 /* User Preferences */
342 Preferences altosui_preferences = Preferences.userNodeForPackage(this.getClass());
345 private File logdir = null;
347 /* logdir preference name */
348 final static String logdirPreference = "LOGDIR";
350 /* Default logdir is ~/TeleMetrum */
351 final static String logdirName = "TeleMetrum";
353 /* Initialize logdir from preferences */
355 String logdir_string = altosui_preferences.get(logdirPreference, null);
356 if (logdir_string != null)
357 logdir = new File(logdir_string);
359 /* a hack -- make the file chooser tell us what the default directory
360 * would be and stick our logdir in a subdirectory of that.
362 logdir = new File(new JFileChooser().getCurrentDirectory(), logdirName);
365 private void set_logdir(File new_logdir) {
367 System.out.printf("Set logdir to %s\n", logdir.toString());
368 synchronized (altosui_preferences) {
369 altosui_preferences.put(logdirPreference, logdir.getPath());
371 altosui_preferences.flush();
372 } catch (BackingStoreException ee) {
373 JOptionPane.showMessageDialog(AltosUI.this,
374 altosui_preferences.absolutePath(),
375 "Cannot save prefernces",
376 JOptionPane.ERROR_MESSAGE);
381 private boolean check_dir(File dir) {
384 JOptionPane.showMessageDialog(AltosUI.this,
386 "Cannot create directory",
387 JOptionPane.ERROR_MESSAGE);
390 } else if (!dir.isDirectory()) {
391 JOptionPane.showMessageDialog(AltosUI.this,
393 "Is not a directory",
394 JOptionPane.ERROR_MESSAGE);
400 private void PickSerialDevice() {
401 java.util.Enumeration<CommPortIdentifier> port_list = CommPortIdentifier.getPortIdentifiers();
402 while (port_list.hasMoreElements()) {
403 CommPortIdentifier identifier = port_list.nextElement();
404 System.out.println("Serial port " + identifier.getName());
408 private void ConnectToDevice() {
409 JFileChooser device_chooser = new JFileChooser();
410 int returnVal = device_chooser.showOpenDialog(AltosUI.this);
412 if (returnVal == JFileChooser.APPROVE_OPTION) {
413 File file = device_chooser.getSelectedFile();
415 serialLine.connect(file.getCanonicalPath());
416 } catch (FileNotFoundException ee) {
417 JOptionPane.showMessageDialog(AltosUI.this,
419 "Cannot open serial port",
420 JOptionPane.ERROR_MESSAGE);
421 } catch (NoSuchPortException ee) {
422 JOptionPane.showMessageDialog(AltosUI.this,
424 "No such serial port",
425 JOptionPane.ERROR_MESSAGE);
426 } catch (PortInUseException ee) {
427 JOptionPane.showMessageDialog(AltosUI.this,
430 JOptionPane.ERROR_MESSAGE);
431 } catch (IOException ee) {
432 JOptionPane.showMessageDialog(AltosUI.this,
435 JOptionPane.ERROR_MESSAGE);
440 String readline(FileInputStream s) throws IOException {
444 while ((c = s.read()) != -1) {
449 line = line + (char) c;
455 * Open an existing telemetry file and replay it in realtime
458 class ReplayThread extends Thread {
459 FileInputStream replay;
462 ReplayThread(FileInputStream in, String name) {
467 /* Run the replay in a separate thread
468 * so that the UI can update
472 AltosState state = null;
474 while ((line = readline(replay)) != null) {
476 AltosTelemetry t = new AltosTelemetry(line);
477 state = new AltosState(t, state);
480 /* Make it run in realtime after the rocket leaves the pad */
482 if (state.state > AltosTelemetry.ao_flight_pad)
483 Thread.sleep((int) (state.time_change * 1000));
484 } catch (InterruptedException e) {}
485 } catch (ParseException pp) {
486 JOptionPane.showMessageDialog(AltosUI.this,
489 JOptionPane.ERROR_MESSAGE);
493 } catch (IOException ee) {
494 JOptionPane.showMessageDialog(AltosUI.this,
497 JOptionPane.ERROR_MESSAGE);
501 } catch (IOException e) {}
507 * Replay a flight from telemetry data
509 private void Replay() {
510 JFileChooser logfile_chooser = new JFileChooser();
512 logfile_chooser.setDialogTitle("Select Telemetry File");
513 logfile_chooser.setFileFilter(new FileNameExtensionFilter("Telemetry file", "telem"));
514 logfile_chooser.setCurrentDirectory(logdir);
515 int returnVal = logfile_chooser.showOpenDialog(AltosUI.this);
517 if (returnVal == JFileChooser.APPROVE_OPTION) {
518 File file = logfile_chooser.getSelectedFile();
520 System.out.println("No file selected?");
521 String filename = file.getName();
523 FileInputStream replay = new FileInputStream(file);
524 ReplayThread thread = new ReplayThread(replay, filename);
526 } catch (FileNotFoundException ee) {
527 JOptionPane.showMessageDialog(AltosUI.this,
529 "Cannot open serial port",
530 JOptionPane.ERROR_MESSAGE);
535 /* Connect to TeleMetrum, either directly or through
536 * a TeleDongle over the packet link
538 private void SaveFlightData() {
541 /* Configure the log directory. This is where all telemetry and eeprom files
542 * will be written to, and where replay will look for telemetry files
544 private void ConfigureLog() {
545 JFileChooser logdir_chooser = new JFileChooser();
547 logdir_chooser.setDialogTitle("Configure Data Logging Directory");
548 logdir_chooser.setCurrentDirectory(logdir.getParentFile());
549 logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
551 if (logdir_chooser.showDialog(AltosUI.this, "Select Directory") == JFileChooser.APPROVE_OPTION) {
552 File dir = logdir_chooser.getSelectedFile();
558 /* Create the AltosUI menus
560 private void createMenu() {
561 JMenuBar menubar = new JMenuBar();
564 JRadioButtonMenuItem radioitem;
568 menu = new JMenu("File");
569 menu.setMnemonic(KeyEvent.VK_F);
572 item = new JMenuItem("Quit",KeyEvent.VK_Q);
573 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
574 ActionEvent.CTRL_MASK));
575 item.addActionListener(new ActionListener() {
576 public void actionPerformed(ActionEvent e) {
585 menu = new JMenu("Device");
586 menu.setMnemonic(KeyEvent.VK_D);
589 item = new JMenuItem("Connect to Device",KeyEvent.VK_C);
590 item.addActionListener(new ActionListener() {
591 public void actionPerformed(ActionEvent e) {
597 item = new JMenuItem("Disconnect from Device",KeyEvent.VK_D);
598 item.addActionListener(new ActionListener() {
599 public void actionPerformed(ActionEvent e) {
607 item = new JMenuItem("Save Flight Data",KeyEvent.VK_S);
608 item.addActionListener(new ActionListener() {
609 public void actionPerformed(ActionEvent e) {
615 item = new JMenuItem("Replay",KeyEvent.VK_R);
616 item.addActionListener(new ActionListener() {
617 public void actionPerformed(ActionEvent e) {
625 menu = new JMenu("Log");
626 menu.setMnemonic(KeyEvent.VK_L);
629 item = new JMenuItem("New Log",KeyEvent.VK_N);
630 item.addActionListener(new ActionListener() {
631 public void actionPerformed(ActionEvent e) {
636 item = new JMenuItem("Configure Log",KeyEvent.VK_C);
637 item.addActionListener(new ActionListener() {
638 public void actionPerformed(ActionEvent e) {
646 menu = new JMenu("Voice", true);
647 menu.setMnemonic(KeyEvent.VK_V);
650 radioitem = new JRadioButtonMenuItem("Enable Voice");
651 radioitem.addActionListener(new ActionListener() {
652 public void actionPerformed(ActionEvent e) {
660 menu = new JMenu("Channel", true);
661 menu.setMnemonic(KeyEvent.VK_C);
663 ButtonGroup group = new ButtonGroup();
665 for (int c = 0; c <= 9; c++) {
666 radioitem = new JRadioButtonMenuItem(String.format("Channel %1d (%7.3fMHz)", c,
669 radioitem.setActionCommand(String.format("%d", c));
670 radioitem.addActionListener(new ActionListener() {
671 public void actionPerformed(ActionEvent e) {
672 System.out.println("Command: " + e.getActionCommand() + " param: " +
677 group.add(radioitem);
681 this.setJMenuBar(menubar);
684 public static void main(final String[] args) {
685 AltosDevice[] devices = AltosDeviceLinux.list();
686 for (int i = 0; i < devices.length; i++)
687 System.out.printf("Model: %s Serial: %d Tty: %s\n",
688 devices[i].product, devices[i].serial, devices[i].tty);
689 AltosUI altosui = new AltosUI();
690 altosui.setVisible(true);