a2816a3a6dc4cf7158e28d6273b3a7a298613f73
[fw/altos] / 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.*;
30
31 import libaltosJNI.*;
32
33 public class AltosUI extends AltosFrame {
34         public AltosVoice voice = new AltosVoice();
35
36         public static boolean load_library(Frame frame) {
37                 if (!Altos.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                                                       ee.getMessage(),
56                                                       "Cannot open target device",
57                                                       JOptionPane.ERROR_MESSAGE);
58                 } catch (AltosSerialInUseException si) {
59                         JOptionPane.showMessageDialog(AltosUI.this,
60                                                       String.format("Device \"%s\" already in use",
61                                                                     device.toShortString()),
62                                                       "Device in use",
63                                                       JOptionPane.ERROR_MESSAGE);
64                 } catch (IOException ee) {
65                         JOptionPane.showMessageDialog(AltosUI.this,
66                                                       device.toShortString(),
67                                                       "Unkonwn I/O error",
68                                                       JOptionPane.ERROR_MESSAGE);
69                 } catch (TimeoutException te) {
70                         JOptionPane.showMessageDialog(this,
71                                                       device.toShortString(),
72                                                       "Timeout error",
73                                                       JOptionPane.ERROR_MESSAGE);
74                 } catch (InterruptedException ie) {
75                         JOptionPane.showMessageDialog(this,
76                                                       device.toShortString(),
77                                                       "Interrupted exception",
78                                                       JOptionPane.ERROR_MESSAGE);
79                 }
80         }
81
82         Container       pane;
83         GridBagLayout   gridbag;
84
85         JButton addButton(int x, int y, String label) {
86                 GridBagConstraints      c;
87                 JButton                 b;
88
89                 c = new GridBagConstraints();
90                 c.gridx = x; c.gridy = y;
91                 c.fill = GridBagConstraints.BOTH;
92                 c.weightx = 1;
93                 c.weighty = 1;
94                 b = new JButton(label);
95
96                 Dimension ps = b.getPreferredSize();
97
98                 gridbag.setConstraints(b, c);
99                 add(b, c);
100                 return b;
101         }
102
103         public AltosUI() {
104
105                 load_library(null);
106
107                 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
108                 if (imgURL != null)
109                         setIconImage(new ImageIcon(imgURL).getImage());
110
111                 AltosUIPreferences.set_component(this);
112
113                 pane = getContentPane();
114                 gridbag = new GridBagLayout();
115                 pane.setLayout(gridbag);
116
117                 JButton b;
118
119                 b = addButton(0, 0, "Monitor Flight");
120                 b.addActionListener(new ActionListener() {
121                                         public void actionPerformed(ActionEvent e) {
122                                                 ConnectToDevice();
123                                         }
124                                 });
125                 b.setToolTipText("Connect to TeleDongle and monitor telemetry");
126                 b = addButton(1, 0, "Save Flight Data");
127                 b.addActionListener(new ActionListener() {
128                                         public void actionPerformed(ActionEvent e) {
129                                                 SaveFlightData();
130                                         }
131                                 });
132                 b.setToolTipText("Download and/or delete flight data from an altimeter");
133                 b = addButton(2, 0, "Replay Flight");
134                 b.addActionListener(new ActionListener() {
135                                         public void actionPerformed(ActionEvent e) {
136                                                 Replay();
137                                         }
138                                 });
139                 b.setToolTipText("Watch an old flight in real-time");
140                 b = addButton(3, 0, "Graph Data");
141                 b.addActionListener(new ActionListener() {
142                                         public void actionPerformed(ActionEvent e) {
143                                                 GraphData();
144                                         }
145                                 });
146                 b.setToolTipText("Present flight data in a graph and table of statistics");
147                 b = addButton(4, 0, "Export Data");
148                 b.addActionListener(new ActionListener() {
149                                         public void actionPerformed(ActionEvent e) {
150                                                 ExportData();
151                                         }
152                                 });
153                 b.setToolTipText("Convert flight data for a spreadsheet or GoogleEarth");
154                 b = addButton(0, 1, "Configure Altimeter");
155                 b.addActionListener(new ActionListener() {
156                                         public void actionPerformed(ActionEvent e) {
157                                                 ConfigureTeleMetrum();
158                                         }
159                                 });
160                 b.setToolTipText("Set flight, storage and communication parameters");
161                 b = addButton(1, 1, "Configure AltosUI");
162                 b.addActionListener(new ActionListener() {
163                                 public void actionPerformed(ActionEvent e) {
164                                         ConfigureAltosUI();
165                                 }
166                         });
167                 b.setToolTipText("Global AltosUI settings");
168
169                 b = addButton(2, 1, "Configure Ground Station");
170                 b.addActionListener(new ActionListener() {
171                                 public void actionPerformed(ActionEvent e) {
172                                         ConfigureTeleDongle();
173                                 }
174                         });
175
176                 b = addButton(3, 1, "Flash Image");
177                 b.addActionListener(new ActionListener() {
178                                 public void actionPerformed(ActionEvent e) {
179                                         FlashImage();
180                                 }
181                         });
182                 b.setToolTipText("Replace the firmware in any AltusMetrum product");
183
184                 b = addButton(4, 1, "Fire Igniter");
185                 b.addActionListener(new ActionListener() {
186                                 public void actionPerformed(ActionEvent e) {
187                                         FireIgniter();
188                                 }
189                         });
190                 b.setToolTipText("Remote control of igniters for deployment testing");
191                 b = addButton(0, 2, "Scan Channels");
192                 b.addActionListener(new ActionListener() {
193                                 public void actionPerformed(ActionEvent e) {
194                                         ScanChannels();
195                                 }
196                         });
197                 b.setToolTipText("Find what channel an altimeter is sending telemetry on");
198                 b = addButton(1, 2, "Load Maps");
199                 b.addActionListener(new ActionListener() {
200                                 public void actionPerformed(ActionEvent e) {
201                                         LoadMaps();
202                                 }
203                         });
204                 b.setToolTipText("Download satellite images for off-line flight monitoring");
205                 b = addButton(2, 2, "Monitor Idle");
206                 b.addActionListener(new ActionListener() {
207                                 public void actionPerformed(ActionEvent e) {
208                                         IdleMonitor();
209                                 }
210                         });
211                 b.setToolTipText("Check flight readiness of altimeter in idle mode");
212
213 //              b = addButton(3, 2, "Launch Controller");
214 //              b.addActionListener(new ActionListener() {
215 //                              public void actionPerformed(ActionEvent e) {
216 //                                      LaunchController();
217 //                              }
218 //                      });
219
220                 b = addButton(4, 2, "Quit");
221                 b.addActionListener(new ActionListener() {
222                                 public void actionPerformed(ActionEvent e) {
223                                         System.exit(0);
224                                 }
225                         });
226                 b.setToolTipText("Close all active windows and terminate AltosUI");
227
228                 setTitle("AltOS");
229
230                 pane.doLayout();
231                 pane.validate();
232
233                 doLayout();
234                 validate();
235
236                 setVisible(true);
237
238                 Insets i = getInsets();
239                 Dimension ps = rootPane.getPreferredSize();
240                 ps.width += i.left + i.right;
241                 ps.height += i.top + i.bottom;
242                 setPreferredSize(ps);
243                 setSize(ps);
244                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
245                 addWindowListener(new WindowAdapter() {
246                         @Override
247                         public void windowClosing(WindowEvent e) {
248                                 System.exit(0);
249                         }
250                 });
251         }
252
253         private void ConnectToDevice() {
254                 AltosDevice     device = AltosDeviceDialog.show(AltosUI.this,
255                                                                 Altos.product_basestation);
256
257                 if (device != null)
258                         telemetry_window(device);
259         }
260
261         void ConfigureCallsign() {
262                 String  result;
263                 result = JOptionPane.showInputDialog(AltosUI.this,
264                                                      "Configure Callsign",
265                                                      AltosUIPreferences.callsign());
266                 if (result != null)
267                         AltosUIPreferences.set_callsign(result);
268         }
269
270         void ConfigureTeleMetrum() {
271                 new AltosConfig(AltosUI.this);
272         }
273
274         void ConfigureTeleDongle() {
275                 new AltosConfigTD(AltosUI.this);
276         }
277
278         void FlashImage() {
279                 AltosFlashUI.show(AltosUI.this);
280         }
281
282         void FireIgniter() {
283                 new AltosIgniteUI(AltosUI.this);
284         }
285
286         void ScanChannels() {
287                 new AltosScanUI(AltosUI.this);
288         }
289
290         void LoadMaps() {
291                 new AltosSiteMapPreload(AltosUI.this);
292         }
293
294         void LaunchController() {
295                 new AltosLaunchUI(AltosUI.this);
296         }
297
298         /*
299          * Replay a flight from telemetry data
300          */
301         private void Replay() {
302                 AltosDataChooser chooser = new AltosDataChooser(
303                         AltosUI.this);
304
305                 AltosRecordIterable iterable = chooser.runDialog();
306                 if (iterable != null) {
307                         AltosFlightReader reader = new AltosReplayReader(iterable.iterator(),
308                                                                          chooser.file());
309                         new AltosFlightUI(voice, reader);
310                 }
311         }
312
313         /* Connect to TeleMetrum, either directly or through
314          * a TeleDongle over the packet link
315          */
316         private void SaveFlightData() {
317                 new AltosEepromManage(AltosUI.this);
318         }
319
320         /* Load a flight log file and write out a CSV file containing
321          * all of the data in standard units
322          */
323
324         private void ExportData() {
325                 AltosDataChooser chooser;
326                 chooser = new AltosDataChooser(this);
327                 AltosRecordIterable record_reader = chooser.runDialog();
328                 if (record_reader == null)
329                         return;
330                 new AltosCSVUI(AltosUI.this, record_reader, chooser.file());
331         }
332
333         /* Load a flight log CSV file and display a pretty graph.
334          */
335
336         private void GraphData() {
337                 AltosDataChooser chooser;
338                 chooser = new AltosDataChooser(this);
339                 AltosRecordIterable record_reader = chooser.runDialog();
340                 if (record_reader == null)
341                         return;
342                 try {
343                         new AltosGraphUI(record_reader, chooser.filename());
344                 } catch (InterruptedException ie) {
345                 } catch (IOException ie) {
346                 }
347         }
348
349         private void ConfigureAltosUI() {
350                 new AltosConfigureUI(AltosUI.this, voice);
351         }
352
353         private void IdleMonitor() {
354                 try {
355                         new AltosIdleMonitorUI(this);
356                 } catch (Exception e) {
357                 }
358         }
359
360         static AltosRecordIterable open_logfile(String filename) {
361                 File file = new File (filename);
362                 try {
363                         FileInputStream in;
364
365                         in = new FileInputStream(file);
366                         if (filename.endsWith("eeprom"))
367                                 return new AltosEepromIterable(in);
368                         else if (filename.endsWith("mega"))
369                                 return new AltosEepromMegaIterable(in);
370                         else
371                                 return new AltosTelemetryIterable(in);
372                 } catch (FileNotFoundException fe) {
373                         System.out.printf("%s\n", fe.getMessage());
374                         return null;
375                 }
376         }
377
378         static AltosWriter open_csv(String filename) {
379                 File file = new File (filename);
380                 try {
381                         return new AltosCSV(file);
382                 } catch (FileNotFoundException fe) {
383                         System.out.printf("%s\n", fe.getMessage());
384                         return null;
385                 }
386         }
387
388         static AltosWriter open_kml(String filename) {
389                 File file = new File (filename);
390                 try {
391                         return new AltosKML(file);
392                 } catch (FileNotFoundException fe) {
393                         System.out.printf("%s\n", fe.getMessage());
394                         return null;
395                 }
396         }
397
398         static final int process_none = 0;
399         static final int process_csv = 1;
400         static final int process_kml = 2;
401         static final int process_graph = 3;
402         static final int process_replay = 4;
403         static final int process_summary = 5;
404
405         static void process_csv(String input) {
406                 AltosRecordIterable iterable = open_logfile(input);
407                 if (iterable == null)
408                         return;
409
410                 String output = Altos.replace_extension(input,".csv");
411                 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
412                 if (input.equals(output)) {
413                         System.out.printf("Not processing '%s'\n", input);
414                 } else {
415                         AltosWriter writer = open_csv(output);
416                         if (writer == null)
417                                 return;
418                         writer.write(iterable);
419                         writer.close();
420                 }
421         }
422
423         static void process_kml(String input) {
424                 AltosRecordIterable iterable = open_logfile(input);
425                 if (iterable == null)
426                         return;
427
428                 String output = Altos.replace_extension(input,".kml");
429                 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
430                 if (input.equals(output)) {
431                         System.out.printf("Not processing '%s'\n", input);
432                 } else {
433                         AltosWriter writer = open_kml(output);
434                         if (writer == null)
435                                 return;
436                         writer.write(iterable);
437                         writer.close();
438                 }
439         }
440
441         static AltosRecordIterable record_iterable(File file) {
442                 FileInputStream in;
443                 try {
444                         in = new FileInputStream(file);
445                 } catch (Exception e) {
446                         System.out.printf("Failed to open file '%s'\n", file);
447                         return null;
448                 }
449                 AltosRecordIterable recs;
450                 AltosReplayReader reader;
451                 if (file.getName().endsWith("eeprom")) {
452                         recs = new AltosEepromIterable(in);
453                 } else {
454                         recs = new AltosTelemetryIterable(in);
455                 }
456                 return recs;
457         }
458
459         static AltosRecordIterable record_iterable_file(String filename) {
460                 return record_iterable (new File(filename));
461         }
462
463         static AltosReplayReader replay_file(String filename) {
464                 AltosRecordIterable recs = record_iterable_file(filename);
465                 if (recs == null)
466                         return null;
467                 return new AltosReplayReader(recs.iterator(), new File(filename));
468         }
469
470         static void process_replay(String filename) {
471                 AltosReplayReader reader = replay_file(filename);
472                 AltosFlightUI flight_ui = new AltosFlightUI(new AltosVoice(), reader);
473                 flight_ui.set_exit_on_close();
474         }
475
476         static void process_graph(String filename) {
477                 AltosRecordIterable recs = record_iterable_file(filename);
478                 if (recs == null)
479                         return;
480                 try {
481                         new AltosGraphUI(recs, filename);
482                 } catch (InterruptedException ie) {
483                 } catch (IOException ie) {
484                 }
485         }
486         
487         static void process_summary(String filename) {
488                 AltosRecordIterable iterable = record_iterable_file(filename);
489                 try {
490                         AltosFlightStats stats = new AltosFlightStats(iterable);
491                         if (stats.serial > 0)
492                                 System.out.printf("Serial:       %5d\n", stats.serial);
493                         if (stats.flight > 0)
494                                 System.out.printf("Flight:       %5d\n", stats.flight);
495                         if (stats.year > 0)
496                                 System.out.printf("Date:    %04d-%02d-%02d\n",
497                                                   stats.year, stats.month, stats.day);
498                         if (stats.hour > 0)
499                                 System.out.printf("Time:      %02d:%02d:%02d UTC\n",
500                                                   stats.hour, stats.minute, stats.second);
501                         System.out.printf("Max height:  %6.0f m    %6.0f ft\n",
502                                           stats.max_height,
503                                           AltosConvert.meters_to_feet(stats.max_height));
504                         System.out.printf("Max speed:   %6.0f m/s  %6.0f ft/s  %6.4f Mach\n",
505                                           stats.max_speed,
506                                           AltosConvert.meters_to_feet(stats.max_speed),
507                                           AltosConvert.meters_to_mach(stats.max_speed));
508                         if (stats.max_acceleration != AltosRecord.MISSING) {
509                                 System.out.printf("Max accel:   %6.0f m/s² %6.0f ft/s² %6.2f g\n",
510                                                   stats.max_acceleration,
511                                                   AltosConvert.meters_to_feet(stats.max_acceleration),
512                                                   AltosConvert.meters_to_g(stats.max_acceleration));
513                         }
514                         System.out.printf("Drogue rate: %6.0f m/s  %6.0f ft/s\n",
515                                           stats.state_baro_speed[Altos.ao_flight_drogue],
516                                           AltosConvert.meters_to_feet(stats.state_baro_speed[Altos.ao_flight_drogue]));
517                         System.out.printf("Main rate:   %6.0f m/s  %6.0f ft/s\n",
518                                           stats.state_baro_speed[Altos.ao_flight_main],
519                                           AltosConvert.meters_to_feet(stats.state_baro_speed[Altos.ao_flight_main]));
520                         System.out.printf("Flight time: %6.0f s\n",
521                                           stats.state_end[Altos.ao_flight_main] -
522                                           stats.state_start[Altos.ao_flight_boost]);
523                 } catch (InterruptedException ie) {
524                 } catch (IOException ie) {
525                 }
526         }
527
528         public static void help(int code) {
529                 System.out.printf("Usage: altosui [OPTION]... [FILE]...\n");
530                 System.out.printf("  Options:\n");
531                 System.out.printf("    --fetchmaps <lat> <lon>\tpre-fetch maps for site map view\n");
532                 System.out.printf("    --replay <filename>\t\trelive the glory of past flights \n");
533                 System.out.printf("    --graph <filename>\t\tgraph a flight\n");
534                 System.out.printf("    --csv\tgenerate comma separated output for spreadsheets, etc\n");
535                 System.out.printf("    --kml\tgenerate KML output for use with Google Earth\n");
536                 System.exit(code);
537         }
538         
539         public static void main(final String[] args) {
540                 try {
541                         UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
542                 } catch (Exception e) {
543                 }
544                 /* Handle batch-mode */
545                 if (args.length == 0) {
546                         AltosUI altosui = new AltosUI();
547                         altosui.setVisible(true);
548
549                         java.util.List<AltosDevice> devices = AltosUSBDevice.list(Altos.product_basestation);
550                         for (AltosDevice device : devices)
551                                 altosui.telemetry_window(device);
552                 } else {
553                         int process = process_none;
554                         for (int i = 0; i < args.length; i++) {
555                                 if (args[i].equals("--help"))
556                                         help(0);
557                                 else if (args[i].equals("--fetchmaps")) {
558                                         if (args.length < i + 3) {
559                                                 help(1);
560                                         } else {
561                                                 double lat = Double.parseDouble(args[i+1]);
562                                                 double lon = Double.parseDouble(args[i+2]);
563                                                 AltosSiteMap.prefetchMaps(lat, lon, 5, 5);
564                                                 i += 2;
565                                         }
566                                 } else if (args[i].equals("--replay"))
567                                         process = process_replay;
568                                 else if (args[i].equals("--kml"))
569                                         process = process_kml;
570                                 else if (args[i].equals("--csv"))
571                                         process = process_csv;
572                                 else if (args[i].equals("--graph"))
573                                         process = process_graph;
574                                 else if (args[i].equals("--summary"))
575                                         process = process_summary;
576                                 else if (args[i].startsWith("--"))
577                                         help(1);
578                                 else {
579                                         switch (process) {
580                                         case process_none:
581                                         case process_graph:
582                                                 process_graph(args[i]);
583                                                 break;
584                                         case process_replay:
585                                                 process_replay(args[i]);
586                                                 break;
587                                         case process_kml:
588                                                 process_kml(args[i]);
589                                                 break;
590                                         case process_csv:
591                                                 process_csv(args[i]);
592                                                 break;
593                                         case process_summary:
594                                                 process_summary(args[i]);
595                                                 break;
596                                         }
597                                 }
598                         }
599                 }
600         }
601 }