altosui: Show only supported telemetry version
[fw/altos] / altosui / AltosConfig.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 AltosConfig implements ActionListener {
34
35         class int_ref {
36                 int     value;
37
38                 public int get() {
39                         return value;
40                 }
41                 public void set(int i) {
42                         value = i;
43                 }
44                 public int_ref(int i) {
45                         value = i;
46                 }
47         }
48
49         class string_ref {
50                 String  value;
51
52                 public String get() {
53                         return value;
54                 }
55                 public void set(String i) {
56                         value = i;
57                 }
58                 public string_ref(String i) {
59                         value = i;
60                 }
61         }
62
63         JFrame          owner;
64         AltosDevice     device;
65         AltosSerial     serial_line;
66         boolean         remote;
67         AltosConfigData remote_config_data;
68         double          remote_frequency;
69         int_ref         serial;
70         int_ref         log_format;
71         int_ref         main_deploy;
72         int_ref         apogee_delay;
73         int_ref         radio_channel;
74         int_ref         radio_calibration;
75         int_ref         flight_log_max;
76         int_ref         ignite_mode;
77         int_ref         pad_orientation;
78         int_ref         radio_setting;
79         int_ref         radio_frequency;
80         int_ref         storage_size;
81         int_ref         storage_erase_unit;
82         int_ref         stored_flight;
83         int_ref         radio_enable;
84         string_ref      version;
85         string_ref      product;
86         string_ref      callsign;
87         AltosConfigUI   config_ui;
88         boolean         serial_started;
89         boolean         made_visible;
90
91         boolean get_int(String line, String label, int_ref x) {
92                 if (line.startsWith(label)) {
93                         try {
94                                 String tail = line.substring(label.length()).trim();
95                                 String[] tokens = tail.split("\\s+");
96                                 if (tokens.length > 0) {
97                                         int     i = Integer.parseInt(tokens[0]);
98                                         x.set(i);
99                                         return true;
100                                 }
101                         } catch (NumberFormatException ne) {
102                         }
103                 }
104                 return false;
105         }
106
107         boolean get_string(String line, String label, string_ref s) {
108                 if (line.startsWith(label)) {
109                         String  quoted = line.substring(label.length()).trim();
110
111                         if (quoted.startsWith("\""))
112                                 quoted = quoted.substring(1);
113                         if (quoted.endsWith("\""))
114                                 quoted = quoted.substring(0,quoted.length()-1);
115                         s.set(quoted);
116                         return true;
117                 } else {
118                         return false;
119                 }
120         }
121
122         void start_serial() throws InterruptedException, TimeoutException {
123                 serial_started = true;
124                 if (remote)
125                         serial_line.start_remote();
126         }
127
128         void stop_serial() throws InterruptedException {
129                 if (!serial_started)
130                         return;
131                 serial_started = false;
132                 if (remote)
133                         serial_line.stop_remote();
134         }
135
136         int log_limit() {
137                 if (storage_size.get() > 0 && storage_erase_unit.get() > 0) {
138                         int     log_limit = storage_size.get() - storage_erase_unit.get();
139                         if (log_limit > 0)
140                                 return log_limit / 1024;
141                 }
142                 return 1024;
143         }
144
145         void update_ui() {
146                 config_ui.set_serial(serial.get());
147                 config_ui.set_product(product.get());
148                 config_ui.set_version(version.get());
149                 config_ui.set_main_deploy(main_deploy.get());
150                 config_ui.set_apogee_delay(apogee_delay.get());
151                 config_ui.set_radio_calibration(radio_calibration.get());
152                 config_ui.set_radio_frequency(frequency());
153                 boolean max_enabled = true;
154                 switch (log_format.get()) {
155                 case Altos.AO_LOG_FORMAT_TINY:
156                         max_enabled = false;
157                         break;
158                 default:
159                         if (stored_flight.get() >= 0)
160                                 max_enabled = false;
161                         break;
162                 }
163                 config_ui.set_flight_log_max_enabled(max_enabled);
164                 config_ui.set_radio_enable(radio_enable.get());
165                 config_ui.set_flight_log_max_limit(log_limit());
166                 config_ui.set_flight_log_max(flight_log_max.get());
167                 config_ui.set_ignite_mode(ignite_mode.get());
168                 config_ui.set_pad_orientation(pad_orientation.get());
169                 config_ui.set_callsign(callsign.get());
170                 config_ui.set_clean();
171                 if (!made_visible) {
172                         made_visible = true;
173                         config_ui.make_visible();
174                 }
175         }
176
177         void process_line(String line) {
178                 if (line == null) {
179                         abort();
180                         return;
181                 }
182                 if (line.equals("all finished")) {
183                         if (serial_line != null)
184                                 update_ui();
185                         return;
186                 }
187                 get_int(line, "serial-number", serial);
188                 get_int(line, "log-format", log_format);
189                 get_int(line, "Main deploy:", main_deploy);
190                 get_int(line, "Apogee delay:", apogee_delay);
191                 get_int(line, "Radio channel:", radio_channel);
192                 get_int(line, "Radio cal:", radio_calibration);
193                 get_int(line, "Max flight log:", flight_log_max);
194                 get_int(line, "Ignite mode:", ignite_mode);
195                 get_int(line, "Pad orientation:", pad_orientation);
196                 get_int(line, "Radio setting:", radio_setting);
197                 get_int(line, "Frequency:", radio_frequency);
198                 get_int(line, "Radio enable:", radio_enable);
199                 get_int(line, "Storage size:", storage_size);
200                 get_int(line, "Storage erase unit:", storage_erase_unit);
201                 get_int(line, "flight", stored_flight);
202                 get_string(line, "Callsign:", callsign);
203                 get_string(line,"software-version", version);
204                 get_string(line,"product", product);
205         }
206
207         final static int        serial_mode_read = 0;
208         final static int        serial_mode_save = 1;
209         final static int        serial_mode_reboot = 2;
210
211         class SerialData implements Runnable {
212                 AltosConfig     config;
213                 int             serial_mode;
214
215                 void process_line(String line) {
216                         config.process_line(line);
217                 }
218                 void callback(String in_line) {
219                         final String line = in_line;
220                         Runnable r = new Runnable() {
221                                         public void run() {
222                                                 process_line(line);
223                                         }
224                                 };
225                         SwingUtilities.invokeLater(r);
226                 }
227
228                 void reset_data() {
229                         serial.set(0);
230                         log_format.set(Altos.AO_LOG_FORMAT_UNKNOWN);
231                         main_deploy.set(250);
232                         apogee_delay.set(0);
233                         radio_channel.set(0);
234                         radio_setting.set(0);
235                         radio_frequency.set(0);
236                         radio_calibration.set(1186611);
237                         radio_enable.set(-1);
238                         flight_log_max.set(0);
239                         ignite_mode.set(-1);
240                         pad_orientation.set(-1);
241                         storage_size.set(-1);
242                         storage_erase_unit.set(-1);
243                         stored_flight.set(-1);
244                         callsign.set("N0CALL");
245                         version.set("unknown");
246                         product.set("unknown");
247                 }
248
249                 void get_data() {
250                         try {
251                                 config.start_serial();
252                                 reset_data();
253
254                                 config.serial_line.printf("c s\nf\nl\nv\n");
255                                 for (;;) {
256                                         try {
257                                                 String line = config.serial_line.get_reply(5000);
258                                                 if (line == null)
259                                                         stop_serial();
260                                                 callback(line);
261                                                 if (line.startsWith("software-version"))
262                                                         break;
263                                         } catch (Exception e) {
264                                                 break;
265                                         }
266                                 }
267                         } catch (InterruptedException ie) {
268                         } catch (TimeoutException te) {
269                         } finally {
270                                 try {
271                                         stop_serial();
272                                 } catch (InterruptedException ie) {
273                                 }
274                         }
275                         callback("all finished");
276                 }
277
278                 void save_data() {
279                         try {
280                                 double frequency = frequency();
281                                 boolean has_frequency = radio_frequency.get() > 0;
282                                 boolean has_setting = radio_setting.get() > 0;
283                                 start_serial();
284                                 serial_line.printf("c m %d\n", main_deploy.get());
285                                 serial_line.printf("c d %d\n", apogee_delay.get());
286                                 if (!remote)
287                                         serial_line.printf("c f %d\n", radio_calibration.get());
288                                 serial_line.set_radio_frequency(frequency,
289                                                                 has_frequency,
290                                                                 has_setting,
291                                                                 radio_calibration.get());
292                                 if (remote) {
293                                         serial_line.stop_remote();
294                                         serial_line.set_radio_frequency(frequency);
295                                         AltosPreferences.set_frequency(device.getSerial(), frequency);
296                                         serial_line.start_remote();
297                                 }
298                                 serial_line.printf("c c %s\n", callsign.get());
299                                 if (flight_log_max.get() != 0)
300                                         serial_line.printf("c l %d\n", flight_log_max.get());
301                                 if (radio_enable.get() >= 0)
302                                         serial_line.printf("c e %d\n", radio_enable.get());
303                                 if (ignite_mode.get() >= 0)
304                                         serial_line.printf("c i %d\n", ignite_mode.get());
305                                 if (pad_orientation.get() >= 0)
306                                         serial_line.printf("c o %d\n", pad_orientation.get());
307                                 serial_line.printf("c w\n");
308                         } catch (InterruptedException ie) {
309                         } catch (TimeoutException te) {
310                         } finally {
311                                 try {
312                                         stop_serial();
313                                 } catch (InterruptedException ie) {
314                                 }
315                         }
316                 }
317
318                 void reboot() {
319                         try {
320                                 start_serial();
321                                 serial_line.printf("r eboot\n");
322                                 serial_line.flush_output();
323                         } catch (InterruptedException ie) {
324                         } catch (TimeoutException te) {
325                         } finally {
326                                 try {
327                                         stop_serial();
328                                 } catch (InterruptedException ie) {
329                                 }
330                                 serial_line.close();
331                         }
332                 }
333
334                 public void run () {
335                         switch (serial_mode) {
336                         case serial_mode_save:
337                                 save_data();
338                                 /* fall through ... */
339                         case serial_mode_read:
340                                 get_data();
341                                 break;
342                         case serial_mode_reboot:
343                                 reboot();
344                                 break;
345                         }
346                 }
347
348                 public SerialData(AltosConfig in_config, int in_serial_mode) {
349                         config = in_config;
350                         serial_mode = in_serial_mode;
351                 }
352         }
353
354         void run_serial_thread(int serial_mode) {
355                 SerialData      sd = new SerialData(this, serial_mode);
356                 Thread          st = new Thread(sd);
357                 st.start();
358         }
359
360         void init_ui () throws InterruptedException, TimeoutException {
361                 config_ui = new AltosConfigUI(owner, remote);
362                 config_ui.addActionListener(this);
363                 serial_line.set_frame(owner);
364                 set_ui();
365         }
366
367         void abort() {
368                 serial_line.close();
369                 serial_line = null;
370                 JOptionPane.showMessageDialog(owner,
371                                               String.format("Connection to \"%s\" failed",
372                                                             device.toShortString()),
373                                               "Connection Failed",
374                                               JOptionPane.ERROR_MESSAGE);
375                 config_ui.setVisible(false);
376         }
377
378         void set_ui() throws InterruptedException, TimeoutException {
379                 if (serial_line != null)
380                         run_serial_thread(serial_mode_read);
381                 else
382                         update_ui();
383         }
384
385         double frequency() {
386                 return AltosConvert.radio_to_frequency(radio_frequency.get(),
387                                                        radio_setting.get(),
388                                                        radio_calibration.get(),
389                                                        radio_channel.get());
390         }
391
392         void set_frequency(double freq) {
393                 int     frequency = radio_frequency.get();
394                 int     setting = radio_setting.get();
395
396                 if (frequency > 0) {
397                         radio_frequency.set((int) Math.floor (freq * 1000 + 0.5));
398                 } else if (setting > 0) {
399                         radio_setting.set(AltosConvert.radio_frequency_to_setting(freq,
400                                                                                   radio_calibration.get()));
401                         radio_channel.set(0);
402                 } else {
403                         radio_channel.set(AltosConvert.radio_frequency_to_channel(freq));
404                 }
405         }
406
407         void save_data() {
408
409                 /* bounds check stuff */
410                 if (config_ui.flight_log_max() > log_limit()) {
411                         JOptionPane.showMessageDialog(owner,
412                                                       String.format("Requested flight log, %dk, is larger than the available space, %dk.\n",
413                                                                     config_ui.flight_log_max(),
414                                                                     log_limit()),
415                                                       "Maximum Flight Log Too Large",
416                                                       JOptionPane.ERROR_MESSAGE);
417                         return;
418                 }
419
420                 main_deploy.set(config_ui.main_deploy());
421                 apogee_delay.set(config_ui.apogee_delay());
422                 radio_calibration.set(config_ui.radio_calibration());
423                 set_frequency(config_ui.radio_frequency());
424                 flight_log_max.set(config_ui.flight_log_max());
425                 if (radio_enable.get() >= 0)
426                         radio_enable.set(config_ui.radio_enable());
427                 if (ignite_mode.get() >= 0)
428                         ignite_mode.set(config_ui.ignite_mode());
429                 if (pad_orientation.get() >= 0)
430                         pad_orientation.set(config_ui.pad_orientation());
431                 callsign.set(config_ui.callsign());
432                 run_serial_thread(serial_mode_save);
433         }
434
435         public void actionPerformed(ActionEvent e) {
436                 String  cmd = e.getActionCommand();
437                 try {
438                         if (cmd.equals("Save")) {
439                                 save_data();
440                         } else if (cmd.equals("Reset")) {
441                                 set_ui();
442                         } else if (cmd.equals("Reboot")) {
443                                 if (serial_line != null)
444                                         run_serial_thread(serial_mode_reboot);
445                         } else if (cmd.equals("Close")) {
446                                 if (serial_line != null)
447                                         serial_line.close();
448                         }
449                 } catch (InterruptedException ie) {
450                         abort();
451                 } catch (TimeoutException te) {
452                         abort();
453                 }
454         }
455
456         public AltosConfig(JFrame given_owner) {
457                 owner = given_owner;
458
459                 serial = new int_ref(0);
460                 log_format = new int_ref(Altos.AO_LOG_FORMAT_UNKNOWN);
461                 main_deploy = new int_ref(250);
462                 apogee_delay = new int_ref(0);
463                 radio_channel = new int_ref(0);
464                 radio_setting = new int_ref(0);
465                 radio_frequency = new int_ref(0);
466                 radio_calibration = new int_ref(1186611);
467                 radio_enable = new int_ref(-1);
468                 flight_log_max = new int_ref(0);
469                 ignite_mode = new int_ref(-1);
470                 pad_orientation = new int_ref(-1);
471                 storage_size = new int_ref(-1);
472                 storage_erase_unit = new int_ref(-1);
473                 stored_flight = new int_ref(-1);
474                 callsign = new string_ref("N0CALL");
475                 version = new string_ref("unknown");
476                 product = new string_ref("unknown");
477
478                 device = AltosDeviceDialog.show(owner, Altos.product_any);
479                 if (device != null) {
480                         try {
481                                 serial_line = new AltosSerial(device);
482                                 try {
483                                         if (!device.matchProduct(Altos.product_telemetrum))
484                                                 remote = true;
485                                         init_ui();
486                                 } catch (InterruptedException ie) {
487                                         abort();
488                                 } catch (TimeoutException te) {
489                                         abort();
490                                 }
491                         } catch (FileNotFoundException ee) {
492                                 JOptionPane.showMessageDialog(owner,
493                                                               ee.getMessage(),
494                                                               "Cannot open target device",
495                                                               JOptionPane.ERROR_MESSAGE);
496                         } catch (AltosSerialInUseException si) {
497                                 JOptionPane.showMessageDialog(owner,
498                                                               String.format("Device \"%s\" already in use",
499                                                                             device.toShortString()),
500                                                               "Device in use",
501                                                               JOptionPane.ERROR_MESSAGE);
502                         } catch (IOException ee) {
503                                 JOptionPane.showMessageDialog(owner,
504                                                               device.toShortString(),
505                                                               ee.getLocalizedMessage(),
506                                                               JOptionPane.ERROR_MESSAGE);
507                         }
508                 }
509         }
510 }