telegps: use new eeprom reading code
[fw/altos] / telegps / TeleGPS.java
1 /*
2  * Copyright © 2014 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.telegps;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import javax.swing.*;
24 import java.io.*;
25 import java.util.concurrent.*;
26 import java.util.*;
27 import java.text.*;
28 import org.altusmetrum.altoslib_11.*;
29 import org.altusmetrum.altosuilib_11.*;
30
31 public class TeleGPS
32         extends AltosUIFrame
33         implements AltosFlightDisplay, AltosFontListener, AltosUnitsListener, ActionListener
34 {
35
36         static String[] telegps_icon_names = {
37                 "/altusmetrum-telegps-16.png",
38                 "/altusmetrum-telegps-32.png",
39                 "/altusmetrum-telegps-48.png",
40                 "/altusmetrum-telegps-64.png",
41                 "/altusmetrum-telegps-128.png",
42                 "/altusmetrum-telegps-256.png"
43         };
44
45         static { set_icon_names(telegps_icon_names); }
46
47         static AltosVoice       voice;
48
49         static AltosVoice voice() {
50                 if (voice == null)
51                         voice = new AltosVoice();
52                 return voice;
53         }
54
55         AltosFlightReader       reader;
56         TeleGPSDisplayThread    thread;
57         boolean                 idle_mode;
58
59         JMenuBar                menu_bar;
60
61         JMenu                   file_menu;
62         JMenu                   monitor_menu;
63         JMenu                   device_menu;
64         AltosUIFreqList         frequencies;
65         ActionListener          frequency_listener;
66         AltosUIRateList         rates;
67         ActionListener          rate_listener;
68
69         Container               bag;
70
71         TeleGPSStatus           telegps_status;
72         TeleGPSStatusUpdate     status_update;
73
74         JTabbedPane             pane;
75
76         AltosUIMap              map;
77         TeleGPSInfo             gps_info;
78         TeleGPSState            gps_state;
79         AltosInfoTable          info_table;
80
81         LinkedList<AltosFlightDisplay>  displays;
82
83         /* File menu */
84         final static String     new_command = "new";
85         final static String     graph_command = "graph";
86         final static String     export_command = "export";
87         final static String     load_maps_command = "loadmaps";
88         final static String     preferences_command = "preferences";
89         final static String     close_command = "close";
90         final static String     exit_command = "exit";
91
92         static final String[][] file_menu_entries = new String[][] {
93                 { "New Window",         new_command },
94                 { "Graph Data",         graph_command },
95                 { "Export Data",        export_command },
96                 { "Load Maps",          load_maps_command },
97                 { "Preferences",        preferences_command },
98                 { "Close",              close_command },
99                 { "Exit",               exit_command },
100         };
101
102         /* Monitor menu */
103         final static String     connect_command = "connect";
104         final static String     disconnect_command = "disconnect";
105         final static String     scan_command = "scan";
106
107         static final String[][] monitor_menu_entries = new String[][] {
108                 { "Connect Device",     connect_command },
109                 { "Disconnect",         disconnect_command },
110                 { "Scan Channels",      scan_command },
111         };
112
113         /* Device menu */
114         final static String     download_command = "download";
115         final static String     configure_command = "configure";
116         final static String     flash_command = "flash";
117
118         static final String[][] device_menu_entries = new String[][] {
119                 { "Download Data",      download_command },
120                 { "Configure Device",   configure_command },
121                 { "Flash Device",       flash_command },
122         };
123
124         void stop_display() {
125                 if (thread != null && thread.isAlive()) {
126                         thread.interrupt();
127                         try {
128                                 thread.join();
129                         } catch (InterruptedException ie) {}
130                 }
131                 thread = null;
132         }
133
134         public void reset() {
135                 for (AltosFlightDisplay display : displays)
136                         display.reset();
137         }
138
139         public void font_size_changed(int font_size) {
140                 for (AltosFlightDisplay display : displays)
141                         display.font_size_changed(font_size);
142         }
143
144         public void units_changed(boolean imperial_units) {
145                 for (AltosFlightDisplay display : displays)
146                         display.units_changed(imperial_units);
147         }
148
149         public void show(AltosState state, AltosListenerState listener_state) {
150                 try {
151                         status_update.saved_state = state;
152                         status_update.saved_listener_state = listener_state;
153
154                         if (state == null)
155                                 state = new AltosState();
156
157                         int i = 0;
158                         for (AltosFlightDisplay display : displays) {
159                                 display.show(state, listener_state);
160                                 i++;
161                         }
162                 } catch (Exception ex) {
163                         System.out.printf("Exception %s\n", ex.toString());
164                         for (StackTraceElement e : ex.getStackTrace())
165                                 System.out.printf("%s\n", e.toString());
166                 }
167         }
168
169         void new_window() {
170                 new TeleGPS();
171         }
172
173         void preferences() {
174                 new TeleGPSPreferences(this, voice());
175         }
176
177         void load_maps() {
178                 new AltosUIMapPreload(this);
179         }
180
181         void disconnect() {
182                 setTitle("TeleGPS");
183                 stop_display();
184                 telegps_status.stop();
185
186                 telegps_status.disable_receive();
187                 disable_frequency_menu();
188                 disable_rate_menu();
189         }
190
191         void connect_flight(AltosDevice device) {
192                 try {
193                         AltosFlightReader       reader = new AltosTelemetryReader(new AltosSerial(device));
194                         set_reader(reader, device, false);
195                 } catch (FileNotFoundException ee) {
196                         JOptionPane.showMessageDialog(this,
197                                                       ee.getMessage(),
198                                                       String.format ("Cannot open %s", device.toShortString()),
199                                                       JOptionPane.ERROR_MESSAGE);
200                 } catch (AltosSerialInUseException si) {
201                         JOptionPane.showMessageDialog(this,
202                                                       String.format("Device \"%s\" already in use",
203                                                                     device.toShortString()),
204                                                       "Device in use",
205                                                       JOptionPane.ERROR_MESSAGE);
206                 } catch (IOException ee) {
207                         JOptionPane.showMessageDialog(this,
208                                                       String.format ("Unknown I/O error on %s", device.toShortString()),
209                                                       "Unknown I/O error",
210                                                       JOptionPane.ERROR_MESSAGE);
211                 } catch (TimeoutException te) {
212                         JOptionPane.showMessageDialog(this,
213                                                       String.format ("Timeout on %s", device.toShortString()),
214                                                       "Timeout error",
215                                                       JOptionPane.ERROR_MESSAGE);
216                 } catch (InterruptedException ie) {
217                         JOptionPane.showMessageDialog(this,
218                                                       String.format("Interrupted %s", device.toShortString()),
219                                                       "Interrupted exception",
220                                                       JOptionPane.ERROR_MESSAGE);
221                 }
222         }
223
224         void connect_idle(AltosDevice device) {
225                 try {
226                         AltosFlightReader       reader = new AltosIdleReader(new AltosSerial(device), false);
227                         set_reader(reader, device, true);
228                 } catch (FileNotFoundException ee) {
229                         JOptionPane.showMessageDialog(this,
230                                                       ee.getMessage(),
231                                                       String.format ("Cannot open %s", device.toShortString()),
232                                                       JOptionPane.ERROR_MESSAGE);
233                 } catch (AltosSerialInUseException si) {
234                         JOptionPane.showMessageDialog(this,
235                                                       String.format("Device \"%s\" already in use",
236                                                                     device.toShortString()),
237                                                       "Device in use",
238                                                       JOptionPane.ERROR_MESSAGE);
239                 } catch (IOException ee) {
240                         JOptionPane.showMessageDialog(this,
241                                                       String.format ("Unknown I/O error on %s", device.toShortString()),
242                                                       "Unknown I/O error",
243                                                       JOptionPane.ERROR_MESSAGE);
244                 } catch (TimeoutException te) {
245                         JOptionPane.showMessageDialog(this,
246                                                       String.format ("Timeout on %s", device.toShortString()),
247                                                       "Timeout error",
248                                                       JOptionPane.ERROR_MESSAGE);
249                 } catch (InterruptedException ie) {
250                         JOptionPane.showMessageDialog(this,
251                                                       String.format("Interrupted %s", device.toShortString()),
252                                                       "Interrupted exception",
253                                                       JOptionPane.ERROR_MESSAGE);
254                 }
255         }
256
257         void connect(AltosDevice device) {
258                 if (reader != null)
259                         disconnect();
260                 if (device.matchProduct(AltosLib.product_basestation))
261                         connect_flight(device);
262                 else
263                         connect_idle(device);
264         }
265
266         void connect() {
267                 AltosDevice     device = AltosDeviceUIDialog.show(this,
268                                                                   AltosLib.product_any);
269                 if (device == null)
270                         return;
271                 connect(device);
272         }
273
274         public void scan_device_selected(AltosDevice device) {
275                 connect(device);
276         }
277
278         void scan() {
279                 new AltosScanUI(this, false);
280         }
281
282         void download(){
283                 new AltosEepromManage(this, AltosLib.product_telegps);
284         }
285
286         void configure() {
287                 new TeleGPSConfig(this);
288         }
289
290         void export() {
291                 AltosDataChooser chooser;
292                 chooser = new AltosDataChooser(this);
293                 AltosStateIterable states = chooser.runDialog();
294                 if (states == null)
295                         return;
296                 new AltosCSVUI(this, states, chooser.file());
297         }
298
299         void graph() {
300                 AltosDataChooser chooser;
301                 chooser = new AltosDataChooser(this);
302                 AltosStateIterable states = chooser.runDialog();
303                 if (states == null)
304                         return;
305                 try {
306                         new TeleGPSGraphUI(states, chooser.file());
307                 } catch (InterruptedException ie) {
308                 } catch (IOException ie) {
309                 }
310         }
311
312         void flash() {
313                 AltosFlashUI.show(this);
314         }
315
316         public void actionPerformed(ActionEvent ev) {
317
318                 /* File menu */
319                 if (new_command.equals(ev.getActionCommand())) {
320                         new_window();
321                         return;
322                 }
323                 if (preferences_command.equals(ev.getActionCommand())) {
324                         preferences();
325                         return;
326                 }
327                 if (load_maps_command.equals(ev.getActionCommand())) {
328                         load_maps();
329                         return;
330                 }
331                 if (close_command.equals(ev.getActionCommand())) {
332                         close();
333                         return;
334                 }
335                 if (exit_command.equals(ev.getActionCommand()))
336                         System.exit(0);
337
338                 /* Monitor menu */
339                 if (connect_command.equals(ev.getActionCommand())) {
340                         connect();
341                         return;
342                 }
343                 if (disconnect_command.equals(ev.getActionCommand())) {
344                         disconnect();
345                         return;
346                 }
347                 if (scan_command.equals(ev.getActionCommand())) {
348                         scan();
349                         return;
350                 }
351
352                 /* Device menu */
353                 if (download_command.equals(ev.getActionCommand())) {
354                         download();
355                         return;
356                 }
357                 if (configure_command.equals(ev.getActionCommand())) {
358                         configure();
359                         return;
360                 }
361                 if (export_command.equals(ev.getActionCommand())) {
362                         export();
363                         return;
364                 }
365                 if (graph_command.equals(ev.getActionCommand())) {
366                         graph();
367                         return;
368                 }
369                 if (flash_command.equals(ev.getActionCommand())) {
370                         flash();
371                         return;
372                 }
373         }
374
375         void enable_frequency_menu(int serial, final AltosFlightReader reader) {
376
377                 if (frequency_listener != null)
378                         disable_frequency_menu();
379
380                 frequency_listener = new ActionListener() {
381                                 public void actionPerformed(ActionEvent e) {
382                                         double frequency = frequencies.frequency();
383                                         try {
384                                                 reader.set_frequency(frequency);
385                                         } catch (TimeoutException te) {
386                                         } catch (InterruptedException ie) {
387                                         }
388                                         reader.save_frequency();
389                                 }
390                         };
391
392                 frequencies.addActionListener(frequency_listener);
393                 frequencies.set_product("Monitor");
394                 frequencies.set_serial(serial);
395                 frequencies.set_frequency(AltosUIPreferences.frequency(serial));
396                 frequencies.setEnabled(true);
397
398         }
399
400         void disable_frequency_menu() {
401                 if (frequency_listener != null) {
402                         frequencies.removeActionListener(frequency_listener);
403                         frequencies.setEnabled(false);
404                         frequency_listener = null;
405                 }
406
407         }
408
409         void enable_rate_menu(int serial, final AltosFlightReader reader) {
410
411                 if (rate_listener != null)
412                         disable_rate_menu();
413
414                 rate_listener = new ActionListener() {
415                                 public void actionPerformed(ActionEvent e) {
416                                         int rate = rates.rate();
417                                         try {
418                                                 reader.set_telemetry_rate(rate);
419                                         } catch (TimeoutException te) {
420                                         } catch (InterruptedException ie) {
421                                         }
422                                         reader.save_telemetry_rate();
423                                 }
424                         };
425
426                 rates.addActionListener(rate_listener);
427                 rates.set_product("Monitor");
428                 rates.set_serial(serial);
429                 rates.set_rate(AltosUIPreferences.telemetry_rate(serial));
430                 rates.setEnabled(reader.supports_telemetry_rate(AltosLib.ao_telemetry_rate_2400));
431         }
432
433         void disable_rate_menu() {
434                 if (rate_listener != null) {
435                         rates.removeActionListener(rate_listener);
436                         rates.setEnabled(false);
437                         rate_listener = null;
438                 }
439
440         }
441
442         public void set_reader(AltosFlightReader reader, AltosDevice device, boolean idle_mode) {
443                 this.idle_mode = idle_mode;
444                 status_update = new TeleGPSStatusUpdate(telegps_status);
445
446                 telegps_status.start(status_update);
447
448                 setTitle(String.format("TeleGPS %s", reader.name));
449                 thread = new TeleGPSDisplayThread(this, voice(), this, reader);
450                 thread.start();
451
452                 if (device != null) {
453                         if (idle_mode) {
454                                 disable_frequency_menu();
455                                 disable_rate_menu();
456                         } else {
457                                 enable_frequency_menu(device.getSerial(), reader);
458                                 enable_rate_menu(device.getSerial(), reader);
459                         }
460                 }
461         }
462
463         static int      number_of_windows;
464
465         static public void add_window() {
466                 ++number_of_windows;
467         }
468
469         static public void subtract_window() {
470                 --number_of_windows;
471                 if (number_of_windows == 0)
472                         System.exit(0);
473         }
474
475         private void close() {
476                 disconnect();
477                 AltosUIPreferences.unregister_font_listener(this);
478                 AltosPreferences.unregister_units_listener(this);
479                 setVisible(false);
480                 dispose();
481                 subtract_window();
482         }
483
484         private void add_menu(JMenu menu, String label, String action) {
485                 JMenuItem       item = new JMenuItem(label);
486                 menu.add(item);
487                 item.addActionListener(this);
488                 item.setActionCommand(action);
489         }
490
491
492         private JMenu make_menu(String label, String[][] items) {
493                 JMenu   menu = new JMenu(label);
494                 for (int i = 0; i < items.length; i++) {
495                         if (MAC_OS_X) {
496                                 if (items[i][1].equals("exit"))
497                                         continue;
498                                 if (items[i][1].equals("preferences"))
499                                         continue;
500                         }
501                         add_menu(menu, items[i][0], items[i][1]);
502                 }
503                 menu_bar.add(menu);
504                 return menu;
505         }
506
507         /* OSXAdapter interfaces */
508         public void macosx_file_handler(String path) {
509                 process_graph(new File(path));
510         }
511
512         public void macosx_quit_handler() {
513                 System.exit(0);
514         }
515
516         public void macosx_preferences_handler() {
517                 preferences();
518         }
519
520         public TeleGPS() {
521
522                 AltosUIPreferences.set_component(this);
523
524                 register_for_macosx_events();
525
526                 reader = null;
527
528                 bag = getContentPane();
529                 bag.setLayout(new GridBagLayout());
530
531                 setTitle("TeleGPS");
532
533                 menu_bar = new JMenuBar();
534                 setJMenuBar(menu_bar);
535
536                 file_menu = make_menu("File", file_menu_entries);
537                 monitor_menu = make_menu("Monitor", monitor_menu_entries);
538                 device_menu = make_menu("Device", device_menu_entries);
539
540                 set_inset(3);
541                 frequencies = new AltosUIFreqList();
542                 frequencies.setEnabled(false);
543                 bag.add(frequencies, constraints (0, 1));
544
545                 rates = new AltosUIRateList();
546                 rates.setEnabled(false);
547                 bag.add(rates, constraints(1, 1));
548                 next_row();
549                 set_inset(0);
550
551                 displays = new LinkedList<AltosFlightDisplay>();
552
553                 int serial = -1;
554
555                 /* TeleGPS status is always visible */
556                 telegps_status = new TeleGPSStatus();
557                 bag.add(telegps_status, constraints(0, 3, GridBagConstraints.HORIZONTAL));
558                 next_row();
559
560                 displays.add(telegps_status);
561
562
563                 /* The rest of the window uses a tabbed pane to
564                  * show one of the alternate data views
565                  */
566                 pane = new JTabbedPane();
567
568                 /* Make the tabbed pane use the rest of the window space */
569                 bag.add(pane, constraints(0, 3, GridBagConstraints.BOTH));
570
571                 map = new AltosUIMap();
572                 pane.add(map.getName(), map);
573                 displays.add(map);
574
575                 gps_info = new TeleGPSInfo();
576                 pane.add(gps_info.getName(), gps_info);
577                 displays.add(gps_info);
578
579                 gps_state = new TeleGPSState();
580                 pane.add(gps_state.getName(), gps_state);
581                 displays.add(gps_state);
582
583                 info_table = new AltosInfoTable();
584                 pane.add("Table", info_table);
585                 displays.add(info_table);
586
587                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
588
589                 AltosUIPreferences.register_font_listener(this);
590                 AltosPreferences.register_units_listener(this);
591
592                 addWindowListener(new WindowAdapter() {
593                                 @Override
594                                 public void windowClosing(WindowEvent e) {
595                                         close();
596                                 }
597                         });
598
599                 pack();
600                 setVisible(true);
601
602                 add_window();
603         }
604
605         public TeleGPS(AltosFlightReader reader, boolean idle_mode) {
606                 this();
607                 set_reader(reader, null, idle_mode);
608         }
609
610         public TeleGPS(AltosDevice device) {
611                 this();
612                 connect(device);
613         }
614
615         static AltosStateIterable record_iterable(File file) {
616                 FileInputStream in;
617                 if (file.getName().endsWith("telem")) {
618                         try {
619                                 in = new FileInputStream(file);
620                                 return new AltosTelemetryFile(in);
621                         } catch (Exception e) {
622                                 System.out.printf("Failed to open file '%s'\n", file);
623                         }
624                 } else {
625
626                         try {
627                                 AltosEepromFile f = new AltosEepromFile(new FileReader(file));
628                                 return f;
629                         } catch (Exception e) {
630                                 System.out.printf("Failed to open file '%s'\n", file);
631                         }
632                 }
633                 return null;
634         }
635
636         static AltosReplayReader replay_file(File file) {
637                 AltosStateIterable states = record_iterable(file);
638                 if (states == null)
639                         return null;
640                 return new AltosReplayReader(states.iterator(), file);
641         }
642
643         static boolean process_graph(File file) {
644                 AltosStateIterable states = record_iterable(file);
645                 if (states == null)
646                         return false;
647                 try {
648                         new TeleGPSGraphUI(states, file);
649                 } catch (Exception e) {
650                         return false;
651                 }
652                 return true;
653         }
654
655         static boolean process_replay(File file) {
656                 AltosReplayReader new_reader = replay_file(file);
657                 if (new_reader == null)
658                         return false;
659
660                 new TeleGPS(new_reader, true);
661                 return true;
662         }
663
664         static final int process_none = 0;
665         static final int process_csv = 1;
666         static final int process_kml = 2;
667         static final int process_graph = 3;
668         static final int process_replay = 4;
669         static final int process_summary = 5;
670         static final int process_cat = 6;
671
672         public static boolean load_library(Frame frame) {
673                 if (!AltosUILib.load_library()) {
674                         JOptionPane.showMessageDialog(frame,
675                                                       String.format("No AltOS library in \"%s\"",
676                                                                     System.getProperty("java.library.path","<undefined>")),
677                                                       "Cannot load device access library",
678                                                       JOptionPane.ERROR_MESSAGE);
679                         return false;
680                 }
681                 return true;
682         }
683
684         public static void help(int code) {
685                 System.out.printf("Usage: altosui [OPTION]... [FILE]...\n");
686                 System.out.printf("  Options:\n");
687                 System.out.printf("    --replay <filename>\t\trelive the glory of past flights \n");
688                 System.out.printf("    --graph <filename>\t\tgraph a flight\n");
689                 System.out.printf("    --csv\tgenerate comma separated output for spreadsheets, etc\n");
690                 System.out.printf("    --kml\tgenerate KML output for use with Google Earth\n");
691                 System.exit(code);
692         }
693
694         public static void main(String[] args) {
695                 int     errors = 0;
696
697                 load_library(null);
698                 try {
699                         UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
700                 } catch (Exception e) {
701                 }
702
703                 boolean any_created = false;
704
705
706                 /* Handle batch-mode */
707                 int process = process_none;
708                 for (int i = 0; i < args.length; i++) {
709                         if (args[i].equals("--help"))
710                                 help(0);
711                         else if (args[i].equals("--replay"))
712                                 process = process_replay;
713                         else if (args[i].equals("--kml"))
714                                 process = process_kml;
715                         else if (args[i].equals("--csv"))
716                                 process = process_csv;
717                         else if (args[i].equals("--graph"))
718                                 process = process_graph;
719                         else if (args[i].equals("--summary"))
720                                 process = process_summary;
721                         else if (args[i].equals("--cat"))
722                                 process = process_cat;
723                         else if (args[i].startsWith("--"))
724                                 help(1);
725                         else {
726                                 File file = new File(args[i]);
727                                 switch (process) {
728                                 case process_none:
729                                 case process_graph:
730                                         if (!process_graph(file))
731                                                 ++errors;
732                                         break;
733                                 case process_replay:
734                                         if (!process_replay(file))
735                                                 ++errors;
736                                         any_created = true;
737                                         break;
738                                 case process_kml:
739                                         ++errors;
740                                         break;
741                                 case process_csv:
742                                         ++errors;
743                                         break;
744                                 case process_summary:
745                                         ++errors;
746                                         break;
747                                 case process_cat:
748                                         ++errors;
749                                 }
750                         }
751                 }
752                 if (errors != 0)
753                         System.exit(errors);
754                 if (number_of_windows == 0) {
755                         java.util.List<AltosDevice> devices = AltosUSBDevice.list(AltosLib.product_basestation);
756                         if (devices != null)
757                                 for (AltosDevice device : devices) {
758                                         new TeleGPS(device);
759                                         any_created = true;
760                                 }
761                         if (number_of_windows == 0)
762                                 new TeleGPS();
763                 }
764         }
765 }