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.*;
26 import gnu.io.CommPortIdentifier;
28 import altosui.AltosSerial;
29 import altosui.AltosSerialMonitor;
31 class AltosUIMonitor implements AltosSerialMonitor {
32 public void data(String data) {
33 System.out.println(data);
37 public class AltosUI extends JFrame {
38 private int channel = -1;
40 private JTable flightStatus;
41 private JTable flightInfo;
42 private AltosSerial serialLine;
46 String[] statusNames = { "Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" };
47 Object[][] statusData = { { "0", "pad", "-50", "0" } };
49 flightStatus = new JTable(statusData, statusNames);
51 flightStatus.setShowGrid(false);
53 this.add(flightStatus);
59 serialLine = new AltosSerial();
60 serialLine.monitor(new AltosUIMonitor());
61 int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
62 this.setSize(new Dimension (dpi * 5, dpi * 4));
64 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
65 addWindowListener(new WindowAdapter() {
67 public void windowClosing(WindowEvent e) {
73 final JFileChooser deviceChooser = new JFileChooser();
75 private void PickSerialDevice() {
76 java.util.Enumeration<CommPortIdentifier> port_list = CommPortIdentifier.getPortIdentifiers();
77 while (port_list.hasMoreElements()) {
78 CommPortIdentifier identifier = port_list.nextElement();
79 System.out.println("Serial port " + identifier.getName());
83 private void ConnectToDevice() {
85 int returnVal = deviceChooser.showOpenDialog(AltosUI.this);
87 if (returnVal == JFileChooser.APPROVE_OPTION) {
88 File file = deviceChooser.getSelectedFile();
90 serialLine.open(file);
91 } catch (FileNotFoundException ee) {
92 JOptionPane.showMessageDialog(AltosUI.this,
94 "Cannot open serial port",
95 JOptionPane.ERROR_MESSAGE);
100 String readline(FileInputStream s) throws IOException {
104 while ((c = s.read()) != -1) {
109 line = line + (char) c;
114 private void Replay() {
115 // int returnVal = deviceChooser.showOpenDialog(AltosUI.this);
117 /* if (returnVal == JFileChooser.APPROVE_OPTION) */ {
118 // File file = deviceChooser.getSelectedFile();
119 // String filename = file.getName();
120 String filename = "/home/keithp/src/cc1111/flights/2010-02-13-serial-051-flight-002.telem";
122 // FileInputStream replay = new FileInputStream(file);
123 FileInputStream replay = new FileInputStream(filename);
127 while ((line = readline(replay)) != null) {
129 AltosTelemetry t = new AltosTelemetry(line);
130 System.out.println ("Version " + t.version + t.callsign);
131 } catch (ParseException pp) {
132 JOptionPane.showMessageDialog(AltosUI.this,
135 JOptionPane.ERROR_MESSAGE);
139 } catch (IOException ee) {
140 JOptionPane.showMessageDialog(AltosUI.this,
143 JOptionPane.ERROR_MESSAGE);
147 } catch (IOException e) {}
149 } catch (FileNotFoundException ee) {
150 JOptionPane.showMessageDialog(AltosUI.this,
152 "Cannot open serial port",
153 JOptionPane.ERROR_MESSAGE);
158 private void SaveFlightData() {
161 private void createMenu() {
162 JMenuBar menubar = new JMenuBar();
165 JRadioButtonMenuItem radioitem;
169 menu = new JMenu("File");
170 menu.setMnemonic(KeyEvent.VK_F);
173 item = new JMenuItem("Quit",KeyEvent.VK_Q);
174 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
175 ActionEvent.CTRL_MASK));
176 item.addActionListener(new ActionListener() {
177 public void actionPerformed(ActionEvent e) {
186 menu = new JMenu("Device");
187 menu.setMnemonic(KeyEvent.VK_D);
190 item = new JMenuItem("Connect to Device",KeyEvent.VK_C);
191 item.addActionListener(new ActionListener() {
192 public void actionPerformed(ActionEvent e) {
198 item = new JMenuItem("Disconnect from Device",KeyEvent.VK_D);
199 item.addActionListener(new ActionListener() {
200 public void actionPerformed(ActionEvent e) {
208 item = new JMenuItem("Save Flight Data",KeyEvent.VK_S);
209 item.addActionListener(new ActionListener() {
210 public void actionPerformed(ActionEvent e) {
216 item = new JMenuItem("Replay",KeyEvent.VK_R);
217 item.addActionListener(new ActionListener() {
218 public void actionPerformed(ActionEvent e) {
226 menu = new JMenu("Log");
227 menu.setMnemonic(KeyEvent.VK_L);
230 item = new JMenuItem("New Log",KeyEvent.VK_N);
231 item.addActionListener(new ActionListener() {
232 public void actionPerformed(ActionEvent e) {
237 item = new JMenuItem("Configure Log",KeyEvent.VK_C);
238 item.addActionListener(new ActionListener() {
239 public void actionPerformed(ActionEvent e) {
246 menu = new JMenu("Voice", true);
247 menu.setMnemonic(KeyEvent.VK_V);
250 radioitem = new JRadioButtonMenuItem("Enable Voice");
251 radioitem.addActionListener(new ActionListener() {
252 public void actionPerformed(ActionEvent e) {
260 menu = new JMenu("Channel", true);
261 menu.setMnemonic(KeyEvent.VK_C);
263 ButtonGroup group = new ButtonGroup();
265 for (int c = 0; c <= 9; c++) {
266 radioitem = new JRadioButtonMenuItem(String.format("Channel %1d (%7.3fMHz)", c,
269 radioitem.setActionCommand(String.format("%d", c));
270 radioitem.addActionListener(new ActionListener() {
271 public void actionPerformed(ActionEvent e) {
272 System.out.println("Command: " + e.getActionCommand() + " param: " +
277 group.add(radioitem);
281 this.setJMenuBar(menubar);
284 public static void main(final String[] args) {
285 AltosUI altosui = new AltosUI();
286 altosui.setVisible(true);