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