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