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