89eaac156b17d34f5c3b39d02191316536b7357e
[fw/altos] / ao-tools / altosui / AltosUI.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package altosui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import java.io.*;
24 import java.util.*;
25 import java.text.*;
26 import gnu.io.CommPortIdentifier;
27
28 import altosui.AltosSerial;
29 import altosui.AltosSerialMonitor;
30
31 class AltosUIMonitor implements AltosSerialMonitor {
32         public void data(String data) {
33                 System.out.println(data);
34         }
35 }
36
37 public class AltosUI extends JFrame {
38         private int channel = -1;
39
40         private JTable flightStatus;
41         private JTable flightInfo;
42         private AltosSerial serialLine;
43
44         public AltosUI() {
45
46                 String[] statusNames = { "Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" };
47                 Object[][] statusData = { { "0", "pad", "-50", "0" } };
48
49                 flightStatus = new JTable(statusData, statusNames);
50
51                 flightStatus.setShowGrid(false);
52
53                 this.add(flightStatus);
54
55                 setTitle("AltOS");
56
57                 createMenu();
58
59                 serialLine = new AltosSerial();
60                 serialLine.monitor(new AltosUIMonitor());
61                 int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
62                 this.setSize(new Dimension (dpi * 5, dpi * 4));
63                 this.validate();
64                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
65                 addWindowListener(new WindowAdapter() {
66                         @Override
67                         public void windowClosing(WindowEvent e) {
68                                 System.exit(0);
69                         }
70                 });
71         }
72
73         final JFileChooser deviceChooser = new JFileChooser();
74
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());
80                 }
81         }
82
83         private void ConnectToDevice() {
84                 PickSerialDevice();
85                 int returnVal = deviceChooser.showOpenDialog(AltosUI.this);
86
87                 if (returnVal == JFileChooser.APPROVE_OPTION) {
88                         File file = deviceChooser.getSelectedFile();
89                         try {
90                                 serialLine.open(file);
91                         } catch (FileNotFoundException ee) {
92                                 JOptionPane.showMessageDialog(AltosUI.this,
93                                                               file.getName(),
94                                                               "Cannot open serial port",
95                                                               JOptionPane.ERROR_MESSAGE);
96                         }
97                 }
98         }
99
100         String readline(FileInputStream s) throws IOException {
101                 int c;
102                 String  line = "";
103
104                 while ((c = s.read()) != -1) {
105                         if (c == '\r')
106                                 continue;
107                         if (c == '\n')
108                                 return line;
109                         line = line + (char) c;
110                 }
111                 return null;
112         }
113
114         private void Replay() {
115 //              int returnVal = deviceChooser.showOpenDialog(AltosUI.this);
116
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";
121                         try {
122 //                              FileInputStream replay = new FileInputStream(file);
123                                 FileInputStream replay = new FileInputStream(filename);
124                                 String  line;
125
126                                 try {
127                                         while ((line = readline(replay)) != null) {
128                                                 try {
129                                                         AltosTelemetry  t = new AltosTelemetry(line);
130                                                         System.out.println ("Version " + t.version + t.callsign);
131                                                 } catch (ParseException pp) {
132                                                         JOptionPane.showMessageDialog(AltosUI.this,
133                                                                                       line,
134                                                                                       "error parsing",
135                                                                                       JOptionPane.ERROR_MESSAGE);
136                                                         break;
137                                                 }
138                                         }
139                                 } catch (IOException ee) {
140                                         JOptionPane.showMessageDialog(AltosUI.this,
141                                                                       filename,
142                                                                       "error reading",
143                                                                       JOptionPane.ERROR_MESSAGE);
144                                 } finally {
145                                         try {
146                                                 replay.close();
147                                         } catch (IOException e) {}
148                                 }
149                         } catch (FileNotFoundException ee) {
150                                 JOptionPane.showMessageDialog(AltosUI.this,
151                                                               filename,
152                                                               "Cannot open serial port",
153                                                               JOptionPane.ERROR_MESSAGE);
154                         }
155                 }
156         }
157
158         private void SaveFlightData() {
159         }
160
161         private void createMenu() {
162                 JMenuBar menubar = new JMenuBar();
163                 JMenu menu;
164                 JMenuItem item;
165                 JRadioButtonMenuItem radioitem;
166
167                 // File menu
168                 {
169                         menu = new JMenu("File");
170                         menu.setMnemonic(KeyEvent.VK_F);
171                         menubar.add(menu);
172
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) {
178                                                 System.exit(0);
179                                         }
180                                 });
181                         menu.add(item);
182                 }
183
184                 // Device menu
185                 {
186                         menu = new JMenu("Device");
187                         menu.setMnemonic(KeyEvent.VK_D);
188                         menubar.add(menu);
189
190                         item = new JMenuItem("Connect to Device",KeyEvent.VK_C);
191                         item.addActionListener(new ActionListener() {
192                                         public void actionPerformed(ActionEvent e) {
193                                                 ConnectToDevice();
194                                         }
195                                 });
196                         menu.add(item);
197
198                         item = new JMenuItem("Disconnect from Device",KeyEvent.VK_D);
199                         item.addActionListener(new ActionListener() {
200                                         public void actionPerformed(ActionEvent e) {
201                                                 serialLine.close();
202                                         }
203                                 });
204                         menu.add(item);
205
206                         menu.addSeparator();
207
208                         item = new JMenuItem("Save Flight Data",KeyEvent.VK_S);
209                         item.addActionListener(new ActionListener() {
210                                         public void actionPerformed(ActionEvent e) {
211                                                 SaveFlightData();
212                                         }
213                                 });
214                         menu.add(item);
215
216                         item = new JMenuItem("Replay",KeyEvent.VK_R);
217                         item.addActionListener(new ActionListener() {
218                                         public void actionPerformed(ActionEvent e) {
219                                                 Replay();
220                                         }
221                                 });
222                         menu.add(item);
223                 }
224                 // Log menu
225                 {
226                         menu = new JMenu("Log");
227                         menu.setMnemonic(KeyEvent.VK_L);
228                         menubar.add(menu);
229
230                         item = new JMenuItem("New Log",KeyEvent.VK_N);
231                         item.addActionListener(new ActionListener() {
232                                         public void actionPerformed(ActionEvent e) {
233                                         }
234                                 });
235                         menu.add(item);
236
237                         item = new JMenuItem("Configure Log",KeyEvent.VK_C);
238                         item.addActionListener(new ActionListener() {
239                                         public void actionPerformed(ActionEvent e) {
240                                         }
241                                 });
242                         menu.add(item);
243                 }
244                 // Voice menu
245                 {
246                         menu = new JMenu("Voice", true);
247                         menu.setMnemonic(KeyEvent.VK_V);
248                         menubar.add(menu);
249
250                         radioitem = new JRadioButtonMenuItem("Enable Voice");
251                         radioitem.addActionListener(new ActionListener() {
252                                         public void actionPerformed(ActionEvent e) {
253                                         }
254                                 });
255                         menu.add(radioitem);
256                 }
257
258                 // Channel menu
259                 {
260                         menu = new JMenu("Channel", true);
261                         menu.setMnemonic(KeyEvent.VK_C);
262                         menubar.add(menu);
263                         ButtonGroup group = new ButtonGroup();
264
265                         for (int c = 0; c <= 9; c++) {
266                                 radioitem = new JRadioButtonMenuItem(String.format("Channel %1d (%7.3fMHz)", c,
267                                                                                    434.550 + c * 0.1),
268                                                                      c == 0);
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: " +
273                                                                            e.paramString());
274                                                 }
275                                         });
276                                 menu.add(radioitem);
277                                 group.add(radioitem);
278                         }
279                 }
280
281                 this.setJMenuBar(menubar);
282
283         }
284         public static void main(final String[] args) {
285                 AltosUI altosui = new AltosUI();
286                 altosui.setVisible(true);
287         }
288 }