altosui: Add EasyMini bits to fat distribution images. Update telemetrum.inf
[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 java.io.*;
24 import java.util.concurrent.*;
25 import org.altusmetrum.altoslib_2.*;
26 import org.altusmetrum.altosuilib_1.*;
27
28 public class AltosUI extends AltosUIFrame {
29         public AltosVoice voice = new AltosVoice();
30
31         public static boolean load_library(Frame frame) {
32                 if (!Altos.load_library()) {
33                         JOptionPane.showMessageDialog(frame,
34                                                       String.format("No AltOS library in \"%s\"",
35                                                                     System.getProperty("java.library.path","<undefined>")),
36                                                       "Cannot load device access library",
37                                                       JOptionPane.ERROR_MESSAGE);
38                         return false;
39                 }
40                 return true;
41         }
42
43         void telemetry_window(AltosDevice device) {
44                 try {
45                         AltosFlightReader reader = new AltosTelemetryReader(new AltosSerial(device));
46                         if (reader != null)
47                                 new AltosFlightUI(voice, reader, device.getSerial());
48                 } catch (FileNotFoundException ee) {
49                         JOptionPane.showMessageDialog(AltosUI.this,
50                                                       ee.getMessage(),
51                                                       String.format ("Cannot open %s", device.toShortString()),
52                                                       JOptionPane.ERROR_MESSAGE);
53                 } catch (AltosSerialInUseException si) {
54                         JOptionPane.showMessageDialog(AltosUI.this,
55                                                       String.format("Device \"%s\" already in use",
56                                                                     device.toShortString()),
57                                                       "Device in use",
58                                                       JOptionPane.ERROR_MESSAGE);
59                 } catch (IOException ee) {
60                         JOptionPane.showMessageDialog(AltosUI.this,
61                                                       String.format ("Unknown I/O error on %s", device.toShortString()),
62                                                       "Unknown I/O error",
63                                                       JOptionPane.ERROR_MESSAGE);
64                 } catch (TimeoutException te) {
65                         JOptionPane.showMessageDialog(this,
66                                                       String.format ("Timeout on %s", device.toShortString()),
67                                                       "Timeout error",
68                                                       JOptionPane.ERROR_MESSAGE);
69                 } catch (InterruptedException ie) {
70                         JOptionPane.showMessageDialog(this,
71                                                       String.format("Interrupted %s", device.toShortString()),
72                                                       "Interrupted exception",
73                                                       JOptionPane.ERROR_MESSAGE);
74                 }
75         }
76
77         Container       pane;
78         GridBagLayout   gridbag;
79
80         JButton addButton(int x, int y, String label) {
81                 GridBagConstraints      c;
82                 JButton                 b;
83
84                 c = new GridBagConstraints();
85                 c.gridx = x; c.gridy = y;
86                 c.fill = GridBagConstraints.BOTH;
87                 c.weightx = 1;
88                 c.weighty = 1;
89                 b = new JButton(label);
90
91                 //Dimension ps = b.getPreferredSize();
92
93                 gridbag.setConstraints(b, c);
94                 add(b, c);
95                 return b;
96         }
97
98         public AltosUI() {
99
100                 load_library(null);
101
102                 AltosUIPreferences.set_component(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.setToolTipText("Connect to TeleDongle and monitor telemetry");
117                 b = addButton(1, 0, "Save Flight Data");
118                 b.addActionListener(new ActionListener() {
119                                         public void actionPerformed(ActionEvent e) {
120                                                 SaveFlightData();
121                                         }
122                                 });
123                 b.setToolTipText("Download and/or delete flight data from an altimeter");
124                 b = addButton(2, 0, "Replay Flight");
125                 b.addActionListener(new ActionListener() {
126                                         public void actionPerformed(ActionEvent e) {
127                                                 Replay();
128                                         }
129                                 });
130                 b.setToolTipText("Watch an old flight in real-time");
131                 b = addButton(3, 0, "Graph Data");
132                 b.addActionListener(new ActionListener() {
133                                         public void actionPerformed(ActionEvent e) {
134                                                 GraphData();
135                                         }
136                                 });
137                 b.setToolTipText("Present flight data in a graph and table of statistics");
138                 b = addButton(4, 0, "Export Data");
139                 b.addActionListener(new ActionListener() {
140                                         public void actionPerformed(ActionEvent e) {
141                                                 ExportData();
142                                         }
143                                 });
144                 b.setToolTipText("Convert flight data for a spreadsheet or GoogleEarth");
145                 b = addButton(0, 1, "Configure Altimeter");
146                 b.addActionListener(new ActionListener() {
147                                         public void actionPerformed(ActionEvent e) {
148                                                 ConfigureTeleMetrum();
149                                         }
150                                 });
151                 b.setToolTipText("Set flight, storage and communication parameters");
152                 b = addButton(1, 1, "Configure AltosUI");
153                 b.addActionListener(new ActionListener() {
154                                 public void actionPerformed(ActionEvent e) {
155                                         ConfigureAltosUI();
156                                 }
157                         });
158                 b.setToolTipText("Global AltosUI settings");
159
160                 b = addButton(2, 1, "Configure Ground Station");
161                 b.addActionListener(new ActionListener() {
162                                 public void actionPerformed(ActionEvent e) {
163                                         ConfigureTeleDongle();
164                                 }
165                         });
166
167                 b = addButton(3, 1, "Flash Image");
168                 b.addActionListener(new ActionListener() {
169                                 public void actionPerformed(ActionEvent e) {
170                                         FlashImage();
171                                 }
172                         });
173                 b.setToolTipText("Replace the firmware in any AltusMetrum product");
174
175                 b = addButton(4, 1, "Fire Igniter");
176                 b.addActionListener(new ActionListener() {
177                                 public void actionPerformed(ActionEvent e) {
178                                         FireIgniter();
179                                 }
180                         });
181                 b.setToolTipText("Remote control of igniters for deployment testing");
182                 b = addButton(0, 2, "Scan Channels");
183                 b.addActionListener(new ActionListener() {
184                                 public void actionPerformed(ActionEvent e) {
185                                         ScanChannels();
186                                 }
187                         });
188                 b.setToolTipText("Find what channel an altimeter is sending telemetry on");
189                 b = addButton(1, 2, "Load Maps");
190                 b.addActionListener(new ActionListener() {
191                                 public void actionPerformed(ActionEvent e) {
192                                         LoadMaps();
193                                 }
194                         });
195                 b.setToolTipText("Download satellite images for off-line flight monitoring");
196                 b = addButton(2, 2, "Monitor Idle");
197                 b.addActionListener(new ActionListener() {
198                                 public void actionPerformed(ActionEvent e) {
199                                         IdleMonitor();
200                                 }
201                         });
202                 b.setToolTipText("Check flight readiness of altimeter in idle mode");
203
204 //              b = addButton(3, 2, "Launch Controller");
205 //              b.addActionListener(new ActionListener() {
206 //                              public void actionPerformed(ActionEvent e) {
207 //                                      LaunchController();
208 //                              }
209 //                      });
210
211                 b = addButton(4, 2, "Quit");
212                 b.addActionListener(new ActionListener() {
213                                 public void actionPerformed(ActionEvent e) {
214                                         System.exit(0);
215                                 }
216                         });
217                 b.setToolTipText("Close all active windows and terminate AltosUI");
218
219                 setTitle("AltOS");
220
221                 pane.doLayout();
222                 pane.validate();
223
224                 doLayout();
225                 validate();
226
227                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
228                 addWindowListener(new WindowAdapter() {
229                         @Override
230                         public void windowClosing(WindowEvent e) {
231                                 System.exit(0);
232                         }
233                 });
234
235                 setLocationByPlatform(false);
236                 
237                 /* Insets aren't set before the window is visible */
238                 setVisible(true);
239         }
240
241         private void ConnectToDevice() {
242                 AltosDevice     device = AltosDeviceUIDialog.show(AltosUI.this,
243                                                                 Altos.product_basestation);
244
245                 if (device != null)
246                         telemetry_window(device);
247         }
248
249         void ConfigureCallsign() {
250                 String  result;
251                 result = JOptionPane.showInputDialog(AltosUI.this,
252                                                      "Configure Callsign",
253                                                      AltosUIPreferences.callsign());
254                 if (result != null)
255                         AltosUIPreferences.set_callsign(result);
256         }
257
258         void ConfigureTeleMetrum() {
259                 new AltosConfig(AltosUI.this);
260         }
261
262         void ConfigureTeleDongle() {
263                 new AltosConfigTD(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                 Iterable<AltosState> states = chooser.runDialog();
294                 if (states != null) {
295                         AltosFlightReader reader = new AltosReplayReader(states.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                 AltosStateIterable states = chooser.runDialog();
316                 if (states == null)
317                         return;
318                 new AltosCSVUI(AltosUI.this, states, 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                 AltosStateIterable states = chooser.runDialog();
328                 if (states == null)
329                         return;
330                 try {
331                         new AltosGraphUI(states, chooser.file());
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 AltosStateIterable open_logfile(File file) {
349                 try {
350                         FileInputStream in;
351
352                         in = new FileInputStream(file);
353                         if (file.getName().endsWith("telem"))
354                                 return new AltosTelemetryFile(in);
355                         else
356                                 return new AltosEepromFile(in);
357                 } catch (FileNotFoundException fe) {
358                         System.out.printf("%s\n", fe.getMessage());
359                         return null;
360                 }
361         }
362
363         static AltosWriter open_csv(File file) {
364                 try {
365                         return new AltosCSV(file);
366                 } catch (FileNotFoundException fe) {
367                         System.out.printf("%s\n", fe.getMessage());
368                         return null;
369                 }
370         }
371
372         static AltosWriter open_kml(File file) {
373                 try {
374                         return new AltosKML(file);
375                 } catch (FileNotFoundException fe) {
376                         System.out.printf("%s\n", fe.getMessage());
377                         return null;
378                 }
379         }
380
381         static final int process_none = 0;
382         static final int process_csv = 1;
383         static final int process_kml = 2;
384         static final int process_graph = 3;
385         static final int process_replay = 4;
386         static final int process_summary = 5;
387         static final int process_cat = 6;
388
389         static boolean process_csv(File input) {
390                 AltosStateIterable states = open_logfile(input);
391                 if (states == null)
392                         return false;
393
394                 File output = Altos.replace_extension(input,".csv");
395                 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
396                 if (input.equals(output)) {
397                         System.out.printf("Not processing '%s'\n", input);
398                         return false;
399                 } else {
400                         AltosWriter writer = open_csv(output);
401                         if (writer == null)
402                                 return false;
403                         writer.write(states);
404                         writer.close();
405                 }
406                 return true;
407         }
408
409         static boolean process_kml(File input) {
410                 AltosStateIterable states = open_logfile(input);
411                 if (states == null)
412                         return false;
413
414                 File 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                         return false;
419                 } else {
420                         AltosWriter writer = open_kml(output);
421                         if (writer == null)
422                                 return false;
423                         writer.write(states);
424                         writer.close();
425                         return true;
426                 }
427         }
428
429         static AltosStateIterable record_iterable(File file) {
430                 FileInputStream in;
431                 try {
432                         in = new FileInputStream(file);
433                 } catch (Exception e) {
434                         System.out.printf("Failed to open file '%s'\n", file);
435                         return null;
436                 }
437                 if (file.getName().endsWith("telem"))
438                         return new AltosTelemetryFile(in);
439                 else
440                         return new AltosEepromFile(in);
441         }
442
443         static AltosReplayReader replay_file(File file) {
444                 AltosStateIterable states = record_iterable(file);
445                 if (states == null)
446                         return null;
447                 return new AltosReplayReader(states.iterator(), file);
448         }
449
450         static boolean process_replay(File file) {
451                 AltosReplayReader reader = replay_file(file);
452                 if (reader == null)
453                         return false;
454                 AltosFlightUI flight_ui = new AltosFlightUI(new AltosVoice(), reader);
455                 flight_ui.set_exit_on_close();
456                 return true;
457         }
458
459         static boolean process_graph(File file) {
460                 AltosStateIterable states = record_iterable(file);
461                 if (states == null)
462                         return false;
463                 try {
464                         new AltosGraphUI(states, file);
465                         return true;
466                 } catch (InterruptedException ie) {
467                 } catch (IOException ie) {
468                 }
469                 return false;
470         }
471         
472         static boolean process_summary(File file) {
473                 AltosStateIterable states = record_iterable(file);
474                 if (states == null)
475                         return false;
476                 try {
477                         AltosFlightStats stats = new AltosFlightStats(states);
478                         if (stats.serial > 0)
479                                 System.out.printf("Serial:       %5d\n", stats.serial);
480                         if (stats.flight > 0)
481                                 System.out.printf("Flight:       %5d\n", stats.flight);
482                         if (stats.year > 0)
483                                 System.out.printf("Date:    %04d-%02d-%02d\n",
484                                                   stats.year, stats.month, stats.day);
485                         if (stats.hour > 0)
486                                 System.out.printf("Time:      %02d:%02d:%02d UTC\n",
487                                                   stats.hour, stats.minute, stats.second);
488                         System.out.printf("Max height:  %6.0f m    %6.0f ft\n",
489                                           stats.max_height,
490                                           AltosConvert.meters_to_feet(stats.max_height));
491                         System.out.printf("Max speed:   %6.0f m/s  %6.0f ft/s  %6.4f Mach\n",
492                                           stats.max_speed,
493                                           AltosConvert.meters_to_feet(stats.max_speed),
494                                           AltosConvert.meters_to_mach(stats.max_speed));
495                         if (stats.max_acceleration != AltosLib.MISSING) {
496                                 System.out.printf("Max accel:   %6.0f m/s² %6.0f ft/s² %6.2f g\n",
497                                                   stats.max_acceleration,
498                                                   AltosConvert.meters_to_feet(stats.max_acceleration),
499                                                   AltosConvert.meters_to_g(stats.max_acceleration));
500                         }
501                         System.out.printf("Drogue rate: %6.0f m/s  %6.0f ft/s\n",
502                                           stats.state_speed[Altos.ao_flight_drogue],
503                                           AltosConvert.meters_to_feet(stats.state_speed[Altos.ao_flight_drogue]));
504                         System.out.printf("Main rate:   %6.0f m/s  %6.0f ft/s\n",
505                                           stats.state_speed[Altos.ao_flight_main],
506                                           AltosConvert.meters_to_feet(stats.state_speed[Altos.ao_flight_main]));
507                         System.out.printf("Flight time: %6.0f s\n",
508                                           stats.state_end[Altos.ao_flight_main] -
509                                           stats.state_start[Altos.ao_flight_boost]);
510                         return true;
511                 } catch (InterruptedException ie) {
512                 } catch (IOException ie) {
513                 }
514                 return false;
515         }
516
517         static boolean process_cat(File file) {
518                 try {
519                         AltosStateIterable eef = record_iterable(file);
520
521                         System.out.printf ("process cat\n");
522                         for (AltosState state : eef) {
523                                 System.out.printf ("tick %d state %d height %g\n",
524                                                    state.tick, state.state, state.height());
525                                 if ((state.set & AltosState.set_gps) != 0)
526                                         System.out.printf ("time %g lat %g lon %g alt %g\n",
527                                                            state.time_since_boost(),
528                                                            state.gps.lat,
529                                                            state.gps.lon,
530                                                            state.gps.alt);
531                         }
532
533                 } catch (Exception e) {
534                         System.out.printf("Failed to open file '%s'\n", file);
535                         return false;
536                 }
537                 return true;
538         }
539
540         public static void help(int code) {
541                 System.out.printf("Usage: altosui [OPTION]... [FILE]...\n");
542                 System.out.printf("  Options:\n");
543                 System.out.printf("    --fetchmaps <lat> <lon>\tpre-fetch maps for site map view\n");
544                 System.out.printf("    --replay <filename>\t\trelive the glory of past flights \n");
545                 System.out.printf("    --graph <filename>\t\tgraph a flight\n");
546                 System.out.printf("    --csv\tgenerate comma separated output for spreadsheets, etc\n");
547                 System.out.printf("    --kml\tgenerate KML output for use with Google Earth\n");
548                 System.exit(code);
549         }
550         
551         public static void main(final String[] args) {
552                 int     errors = 0;
553                 load_library(null);
554                 try {
555                         UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
556                 } catch (Exception e) {
557                 }
558                 /* Handle batch-mode */
559                 if (args.length == 0) {
560                         AltosUI altosui = new AltosUI();
561
562                         java.util.List<AltosDevice> devices = AltosUSBDevice.list(Altos.product_basestation);
563                         for (AltosDevice device : devices)
564                                 altosui.telemetry_window(device);
565                 } else {
566                         int process = process_none;
567                         for (int i = 0; i < args.length; i++) {
568                                 if (args[i].equals("--help"))
569                                         help(0);
570                                 else if (args[i].equals("--fetchmaps")) {
571                                         if (args.length < i + 3) {
572                                                 help(1);
573                                         } else {
574                                                 double lat = Double.parseDouble(args[i+1]);
575                                                 double lon = Double.parseDouble(args[i+2]);
576                                                 AltosSiteMap.prefetchMaps(lat, lon, 5, 5);
577                                                 i += 2;
578                                         }
579                                 } else if (args[i].equals("--replay"))
580                                         process = process_replay;
581                                 else if (args[i].equals("--kml"))
582                                         process = process_kml;
583                                 else if (args[i].equals("--csv"))
584                                         process = process_csv;
585                                 else if (args[i].equals("--graph"))
586                                         process = process_graph;
587                                 else if (args[i].equals("--summary"))
588                                         process = process_summary;
589                                 else if (args[i].equals("--cat"))
590                                         process = process_cat;
591                                 else if (args[i].startsWith("--"))
592                                         help(1);
593                                 else {
594                                         File file = new File(args[i]);
595                                         switch (process) {
596                                         case process_none:
597                                         case process_graph:
598                                                 if (!process_graph(file))
599                                                         ++errors;
600                                                 break;
601                                         case process_replay:
602                                                 if (!process_replay(file))
603                                                         ++errors;
604                                                 break;
605                                         case process_kml:
606                                                 if (!process_kml(file))
607                                                         ++errors;
608                                                 break;
609                                         case process_csv:
610                                                 if (!process_csv(file))
611                                                         ++errors;
612                                                 break;
613                                         case process_summary:
614                                                 if (!process_summary(file))
615                                                         ++errors;
616                                                 break;
617                                         case process_cat:
618                                                 if (!process_cat(file))
619                                                         ++errors;
620                                         }
621                                 }
622                         }
623                 }
624                 if (errors != 0)
625                         System.exit(errors);
626         }
627 }