a2e416bad59308548b9099baa927e7e3817dff81
[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 javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import java.io.*;
26 import java.util.*;
27 import java.text.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.LinkedBlockingQueue;
30
31 import libaltosJNI.*;
32
33 public class AltosUI extends JFrame {
34         public AltosVoice voice = new AltosVoice();
35
36         public static boolean load_library(Frame frame) {
37                 if (!AltosDevice.load_library()) {
38                         JOptionPane.showMessageDialog(frame,
39                                                       String.format("No AltOS library in \"%s\"",
40                                                                     System.getProperty("java.library.path","<undefined>")),
41                                                       "Cannot load device access library",
42                                                       JOptionPane.ERROR_MESSAGE);
43                         return false;
44                 }
45                 return true;
46         }
47
48         void telemetry_window(AltosDevice device) {
49                 try {
50                         AltosFlightReader reader = new AltosTelemetryReader(device);
51                         if (reader != null)
52                                 new AltosFlightUI(voice, reader, device.getSerial());
53                 } catch (FileNotFoundException ee) {
54                         JOptionPane.showMessageDialog(AltosUI.this,
55                                                       String.format("Cannot open device \"%s\"",
56                                                                     device.getPath()),
57                                                       "Cannot open target device",
58                                                       JOptionPane.ERROR_MESSAGE);
59                 } catch (AltosSerialInUseException si) {
60                         JOptionPane.showMessageDialog(AltosUI.this,
61                                                       String.format("Device \"%s\" already in use",
62                                                                     device.getPath()),
63                                                       "Device in use",
64                                                       JOptionPane.ERROR_MESSAGE);
65                 } catch (IOException ee) {
66                         JOptionPane.showMessageDialog(AltosUI.this,
67                                                       device.getPath(),
68                                                       "Unkonwn I/O error",
69                                                       JOptionPane.ERROR_MESSAGE);
70                 }
71         }
72
73         Container       pane;
74         GridBagLayout   gridbag;
75
76         JButton addButton(int x, int y, String label) {
77                 GridBagConstraints      c;
78                 JButton                 b;
79
80                 c = new GridBagConstraints();
81                 c.gridx = x; c.gridy = y;
82                 c.fill = GridBagConstraints.BOTH;
83                 c.weightx = 1;
84                 c.weighty = 1;
85                 b = new JButton(label);
86
87                 Dimension ps = b.getPreferredSize();
88
89                 gridbag.setConstraints(b, c);
90                 add(b, c);
91                 return b;
92         }
93
94         public AltosUI() {
95
96                 load_library(null);
97
98                 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
99                 if (imgURL != null)
100                         setIconImage(new ImageIcon(imgURL).getImage());
101
102                 AltosPreferences.init(this);
103
104                 pane = getContentPane();
105                 gridbag = new GridBagLayout();
106                 pane.setLayout(gridbag);
107
108                 JButton b;
109
110                 b = addButton(0, 0, "Monitor Flight");
111                 b.addActionListener(new ActionListener() {
112                                         public void actionPerformed(ActionEvent e) {
113                                                 ConnectToDevice();
114                                         }
115                                 });
116                 b = addButton(1, 0, "Save Flight Data");
117                 b.addActionListener(new ActionListener() {
118                                         public void actionPerformed(ActionEvent e) {
119                                                 SaveFlightData();
120                                         }
121                                 });
122                 b = addButton(2, 0, "Replay Flight");
123                 b.addActionListener(new ActionListener() {
124                                         public void actionPerformed(ActionEvent e) {
125                                                 Replay();
126                                         }
127                                 });
128                 b = addButton(0, 1, "Graph Data");
129                 b.addActionListener(new ActionListener() {
130                                         public void actionPerformed(ActionEvent e) {
131                                                 GraphData();
132                                         }
133                                 });
134                 b = addButton(1, 1, "Export Data");
135                 b.addActionListener(new ActionListener() {
136                                         public void actionPerformed(ActionEvent e) {
137                                                 ExportData();
138                                         }
139                                 });
140                 b = addButton(2, 1, "Configure TeleMetrum");
141                 b.addActionListener(new ActionListener() {
142                                         public void actionPerformed(ActionEvent e) {
143                                                 ConfigureTeleMetrum();
144                                         }
145                                 });
146
147                 setTitle("AltOS");
148
149                 createMenu();
150
151                 pane.doLayout();
152                 pane.validate();
153
154                 doLayout();
155                 validate();
156
157                 setVisible(true);
158
159                 Insets i = getInsets();
160                 Dimension ps = rootPane.getPreferredSize();
161                 ps.width += i.left + i.right;
162                 ps.height += i.top + i.bottom;
163                 setPreferredSize(ps);
164                 setSize(ps);
165                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
166                 addWindowListener(new WindowAdapter() {
167                         @Override
168                         public void windowClosing(WindowEvent e) {
169                                 System.exit(0);
170                         }
171                 });
172         }
173
174         private void ConnectToDevice() {
175                 AltosDevice     device = AltosDeviceDialog.show(AltosUI.this,
176                                                                 AltosDevice.product_basestation);
177
178                 if (device != null)
179                         telemetry_window(device);
180         }
181
182         void ConfigureCallsign() {
183                 String  result;
184                 result = JOptionPane.showInputDialog(AltosUI.this,
185                                                      "Configure Callsign",
186                                                      AltosPreferences.callsign());
187                 if (result != null)
188                         AltosPreferences.set_callsign(result);
189         }
190
191         void ConfigureTeleMetrum() {
192                 new AltosConfig(AltosUI.this);
193         }
194
195         void FlashImage() {
196                 new AltosFlashUI(AltosUI.this);
197         }
198
199         /*
200          * Replay a flight from telemetry data
201          */
202         private void Replay() {
203                 AltosLogfileChooser chooser = new AltosLogfileChooser(
204                         AltosUI.this);
205                 AltosRecordIterable iterable = chooser.runDialog();
206                 if (iterable != null) {
207                         AltosFlightReader reader = new AltosReplayReader(iterable.iterator(),
208                                                                          chooser.filename());
209                         new AltosFlightUI(voice, reader);
210                 }
211         }
212
213         /* Connect to TeleMetrum, either directly or through
214          * a TeleDongle over the packet link
215          */
216         private void SaveFlightData() {
217                 new AltosEepromDownload(AltosUI.this);
218         }
219
220         /* Load a flight log file and write out a CSV file containing
221          * all of the data in standard units
222          */
223
224         private void ExportData() {
225                 new AltosCSVUI(AltosUI.this);
226         }
227
228         /* Load a flight log CSV file and display a pretty graph.
229          */
230
231         private void GraphData() {
232                 new AltosGraphUI(AltosUI.this);
233         }
234
235         /* Create the AltosUI menus
236          */
237         private void createMenu() {
238                 JMenuBar menubar = new JMenuBar();
239                 JMenu menu;
240                 JMenuItem item;
241                 JRadioButtonMenuItem radioitem;
242
243                 // File menu
244                 {
245                         menu = new JMenu("File");
246                         menu.setMnemonic(KeyEvent.VK_F);
247                         menubar.add(menu);
248
249                         item = new JMenuItem("Flash Image",KeyEvent.VK_I);
250                         item.addActionListener(new ActionListener() {
251                                         public void actionPerformed(ActionEvent e) {
252                                                 FlashImage();
253                                         }
254                                 });
255                         menu.add(item);
256
257                         item = new JMenuItem("Export Data",KeyEvent.VK_E);
258                         item.addActionListener(new ActionListener() {
259                                         public void actionPerformed(ActionEvent e) {
260                                                 ExportData();
261                                         }
262                                 });
263                         menu.add(item);
264
265                         item = new JMenuItem("Graph Data",KeyEvent.VK_G);
266                         item.addActionListener(new ActionListener() {
267                                         public void actionPerformed(ActionEvent e) {
268                                                 GraphData();
269                                         }
270                                 });
271                         menu.add(item);
272
273                         item = new JMenuItem("Quit",KeyEvent.VK_Q);
274                         item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
275                                                                    ActionEvent.CTRL_MASK));
276                         item.addActionListener(new ActionListener() {
277                                         public void actionPerformed(ActionEvent e) {
278                                                 System.out.printf("exiting\n");
279                                                 System.exit(0);
280                                         }
281                                 });
282                         menu.add(item);
283                 }
284
285                 // Device menu
286                 if (false) {
287                         menu = new JMenu("Device");
288                         menu.setMnemonic(KeyEvent.VK_D);
289                         menubar.add(menu);
290
291                         item = new JMenuItem("Connect to Device",KeyEvent.VK_C);
292                         item.addActionListener(new ActionListener() {
293                                         public void actionPerformed(ActionEvent e) {
294                                                 ConnectToDevice();
295                                         }
296                                 });
297                         menu.add(item);
298
299                         menu.addSeparator();
300
301                         item = new JMenuItem("Set Callsign",KeyEvent.VK_S);
302                         item.addActionListener(new ActionListener() {
303                                         public void actionPerformed(ActionEvent e) {
304                                                 ConfigureCallsign();
305                                         }
306                                 });
307
308                         menu.add(item);
309
310                         item = new JMenuItem("Configure TeleMetrum device",KeyEvent.VK_T);
311                         item.addActionListener(new ActionListener() {
312                                         public void actionPerformed(ActionEvent e) {
313                                                 ConfigureTeleMetrum();
314                                         }
315                                 });
316
317                         menu.add(item);
318                 }
319                 // Log menu
320                 {
321                         menu = new JMenu("Log");
322                         menu.setMnemonic(KeyEvent.VK_L);
323                         menubar.add(menu);
324
325                         item = new JMenuItem("New Log",KeyEvent.VK_N);
326                         item.addActionListener(new ActionListener() {
327                                         public void actionPerformed(ActionEvent e) {
328                                         }
329                                 });
330                         menu.add(item);
331
332                         item = new JMenuItem("Configure Log",KeyEvent.VK_C);
333                         item.addActionListener(new ActionListener() {
334                                         public void actionPerformed(ActionEvent e) {
335                                                 AltosPreferences.ConfigureLog();
336                                         }
337                                 });
338                         menu.add(item);
339                 }
340                 // Voice menu
341                 {
342                         menu = new JMenu("Voice", true);
343                         menu.setMnemonic(KeyEvent.VK_V);
344                         menubar.add(menu);
345
346                         radioitem = new JRadioButtonMenuItem("Enable Voice", AltosPreferences.voice());
347                         radioitem.addActionListener(new ActionListener() {
348                                         public void actionPerformed(ActionEvent e) {
349                                                 JRadioButtonMenuItem item = (JRadioButtonMenuItem) e.getSource();
350                                                 boolean enabled = item.isSelected();
351                                                 AltosPreferences.set_voice(enabled);
352                                                 if (enabled)
353                                                         voice.speak_always("Enable voice.");
354                                                 else
355                                                         voice.speak_always("Disable voice.");
356                                         }
357                                 });
358                         menu.add(radioitem);
359                         item = new JMenuItem("Test Voice",KeyEvent.VK_T);
360                         item.addActionListener(new ActionListener() {
361                                         public void actionPerformed(ActionEvent e) {
362                                                 voice.speak("That's one small step for man; one giant leap for mankind.");
363                                         }
364                                 });
365                         menu.add(item);
366                 }
367                 this.setJMenuBar(menubar);
368         }
369
370         static AltosRecordIterable open_logfile(String filename) {
371                 File file = new File (filename);
372                 try {
373                         FileInputStream in;
374
375                         in = new FileInputStream(file);
376                         if (filename.endsWith("eeprom"))
377                                 return new AltosEepromIterable(in);
378                         else
379                                 return new AltosTelemetryIterable(in);
380                 } catch (FileNotFoundException fe) {
381                         System.out.printf("Cannot open '%s'\n", filename);
382                         return null;
383                 }
384         }
385
386         static AltosWriter open_csv(String filename) {
387                 File file = new File (filename);
388                 try {
389                         return new AltosCSV(file);
390                 } catch (FileNotFoundException fe) {
391                         System.out.printf("Cannot open '%s'\n", filename);
392                         return null;
393                 }
394         }
395
396         static AltosWriter open_kml(String filename) {
397                 File file = new File (filename);
398                 try {
399                         return new AltosKML(file);
400                 } catch (FileNotFoundException fe) {
401                         System.out.printf("Cannot open '%s'\n", filename);
402                         return null;
403                 }
404         }
405
406         static final int process_csv = 1;
407         static final int process_kml = 2;
408
409         static void process_file(String input, int process) {
410                 AltosRecordIterable iterable = open_logfile(input);
411                 if (iterable == null)
412                         return;
413                 if (process == 0)
414                         process = process_csv;
415                 if ((process & process_csv) != 0) {
416                         String output = Altos.replace_extension(input,".csv");
417                         System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
418                         if (input.equals(output)) {
419                                 System.out.printf("Not processing '%s'\n", input);
420                         } else {
421                                 AltosWriter writer = open_csv(output);
422                                 if (writer != null) {
423                                         writer.write(iterable);
424                                         writer.close();
425                                 }
426                         }
427                 }
428                 if ((process & process_kml) != 0) {
429                         String output = Altos.replace_extension(input,".kml");
430                         System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
431                         if (input.equals(output)) {
432                                 System.out.printf("Not processing '%s'\n", input);
433                         } else {
434                                 AltosWriter writer = open_kml(output);
435                                 if (writer == null)
436                                         return;
437                                 writer.write(iterable);
438                                 writer.close();
439                         }
440                 }
441         }
442
443         public static void main(final String[] args) {
444                 int     process = 0;
445                 /* Handle batch-mode */
446                 if (args.length == 2 && args[0].equals("--replay")) {
447                         String filename = args[1];
448                         FileInputStream in;
449                         try {
450                                 in = new FileInputStream(filename);
451                         } catch (Exception e) {
452                                 System.out.printf("Failed to open file '%s'\n", filename);
453                                 return;
454                         }
455                         AltosRecordIterable recs;
456                         AltosReplayReader reader;
457                         if (filename.endsWith("eeprom")) {
458                                 recs = new AltosEepromIterable(in);
459                         } else {
460                                 recs = new AltosTelemetryIterable(in);
461                         }
462                         reader = new AltosReplayReader(recs.iterator(), filename);
463                         AltosFlightUI flight_ui = new AltosFlightUI(new AltosVoice(), reader);
464                         flight_ui.set_exit_on_close();
465                         return;
466                 } else if (args.length > 0) {
467                         for (int i = 0; i < args.length; i++) {
468                                 if (args[i].equals("--kml"))
469                                         process |= process_kml;
470                                 else if (args[i].equals("--csv"))
471                                         process |= process_csv;
472                                 else
473                                         process_file(args[i], process);
474                         }
475                 } else {
476                         AltosUI altosui = new AltosUI();
477                         altosui.setVisible(true);
478
479                         AltosDevice[] devices = AltosDevice.list(AltosDevice.product_basestation);
480                         for (int i = 0; i < devices.length; i++)
481                                 altosui.telemetry_window(devices[i]);
482                 }
483         }
484 }