Merge remote-tracking branch 'mjb/altosui_mjb'
[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
26 public class AltosConfig implements ActionListener {
27
28         class int_ref {
29                 int     value;
30
31                 public int get() {
32                         return value;
33                 }
34                 public void set(int i) {
35                         value = i;
36                 }
37                 public int_ref(int i) {
38                         value = i;
39                 }
40         }
41
42         class string_ref {
43                 String  value;
44
45                 public String get() {
46                         return value;
47                 }
48                 public void set(String i) {
49                         value = i;
50                 }
51                 public string_ref(String i) {
52                         value = i;
53                 }
54         }
55
56         JFrame          owner;
57         AltosDevice     device;
58         AltosSerial     serial_line;
59         boolean         remote;
60         AltosConfigData remote_config_data;
61         double          remote_frequency;
62         int_ref         serial;
63         int_ref         log_format;
64         int_ref         main_deploy;
65         int_ref         apogee_delay;
66         int_ref         apogee_lockout;
67         int_ref         radio_channel;
68         int_ref         radio_calibration;
69         int_ref         flight_log_max;
70         int_ref         ignite_mode;
71         int_ref         pad_orientation;
72         int_ref         radio_setting;
73         int_ref         radio_frequency;
74         int_ref         storage_size;
75         int_ref         storage_erase_unit;
76         int_ref         stored_flight;
77         int_ref         radio_enable;
78         string_ref      version;
79         string_ref      product;
80         string_ref      callsign;
81         AltosConfigUI   config_ui;
82         boolean         serial_started;
83         boolean         made_visible;
84
85         boolean get_int(String line, String label, int_ref x) {
86                 if (line.startsWith(label)) {
87                         try {
88                                 String tail = line.substring(label.length()).trim();
89                                 String[] tokens = tail.split("\\s+");
90                                 if (tokens.length > 0) {
91                                         int     i = Integer.parseInt(tokens[0]);
92                                         x.set(i);
93                                         return true;
94                                 }
95                         } catch (NumberFormatException ne) {
96                         }
97                 }
98                 return false;
99         }
100
101         boolean get_string(String line, String label, string_ref s) {
102                 if (line.startsWith(label)) {
103                         String  quoted = line.substring(label.length()).trim();
104
105                         if (quoted.startsWith("\""))
106                                 quoted = quoted.substring(1);
107                         if (quoted.endsWith("\""))
108                                 quoted = quoted.substring(0,quoted.length()-1);
109                         s.set(quoted);
110                         return true;
111                 } else {
112                         return false;
113                 }
114         }
115
116         void start_serial() throws InterruptedException, TimeoutException {
117                 serial_started = true;
118                 if (remote)
119                         serial_line.start_remote();
120         }
121
122         void stop_serial() throws InterruptedException {
123                 if (!serial_started)
124                         return;
125                 serial_started = false;
126                 if (remote)
127                         serial_line.stop_remote();
128         }
129
130         int log_limit() {
131                 if (storage_size.get() > 0 && storage_erase_unit.get() > 0) {
132                         int     log_limit = storage_size.get() - storage_erase_unit.get();
133                         if (log_limit > 0)
134                                 return log_limit / 1024;
135                 }
136                 return 1024;
137         }
138
139         void update_ui() {
140                 config_ui.set_serial(serial.get());
141                 config_ui.set_product(product.get());
142                 config_ui.set_version(version.get());
143                 config_ui.set_main_deploy(main_deploy.get());
144                 config_ui.set_apogee_delay(apogee_delay.get());
145                 config_ui.set_apogee_lockout(apogee_lockout.get());
146                 config_ui.set_radio_calibration(radio_calibration.get());
147                 config_ui.set_radio_frequency(frequency());
148                 boolean max_enabled = true;
149                 switch (log_format.get()) {
150                 case Altos.AO_LOG_FORMAT_TINY:
151                         max_enabled = false;
152                         break;
153                 default:
154                         if (stored_flight.get() >= 0)
155                                 max_enabled = false;
156                         break;
157                 }
158                 config_ui.set_flight_log_max_enabled(max_enabled);
159                 config_ui.set_radio_enable(radio_enable.get());
160                 config_ui.set_flight_log_max_limit(log_limit());
161                 config_ui.set_flight_log_max(flight_log_max.get());
162                 config_ui.set_ignite_mode(ignite_mode.get());
163                 config_ui.set_pad_orientation(pad_orientation.get());
164                 config_ui.set_callsign(callsign.get());
165                 config_ui.set_clean();
166                 if (!made_visible) {
167                         made_visible = true;
168                         config_ui.make_visible();
169                 }
170         }
171
172         void process_line(String line) {
173                 if (line == null) {
174                         abort();
175                         return;
176                 }
177                 if (line.equals("all finished")) {
178                         if (serial_line != null)
179                                 update_ui();
180                         return;
181                 }
182                 get_int(line, "serial-number", serial);
183                 get_int(line, "log-format", log_format);
184                 get_int(line, "Main deploy:", main_deploy);
185                 get_int(line, "Apogee delay:", apogee_delay);
186                 get_int(line, "Apogee lockout:", apogee_lockout);
187                 get_int(line, "Radio channel:", radio_channel);
188                 get_int(line, "Radio cal:", radio_calibration);
189                 get_int(line, "Max flight log:", flight_log_max);
190                 get_int(line, "Ignite mode:", ignite_mode);
191                 get_int(line, "Pad orientation:", pad_orientation);
192                 get_int(line, "Radio setting:", radio_setting);
193                 if (get_int(line, "Frequency:", radio_frequency))
194                         if (radio_frequency.get() < 0)
195                                 radio_frequency.set(434550);
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 reset_data() {
227                         serial.set(0);
228                         log_format.set(Altos.AO_LOG_FORMAT_UNKNOWN);
229                         main_deploy.set(250);
230                         apogee_delay.set(0);
231                         apogee_lockout.set(0);
232                         radio_channel.set(0);
233                         radio_setting.set(0);
234                         radio_frequency.set(0);
235                         radio_calibration.set(1186611);
236                         radio_enable.set(-1);
237                         flight_log_max.set(0);
238                         ignite_mode.set(-1);
239                         pad_orientation.set(-1);
240                         storage_size.set(-1);
241                         storage_erase_unit.set(-1);
242                         stored_flight.set(-1);
243                         callsign.set("N0CALL");
244                         version.set("unknown");
245                         product.set("unknown");
246                 }
247
248                 void get_data() {
249                         try {
250                                 config.start_serial();
251                                 reset_data();
252
253                                 config.serial_line.printf("c s\nf\nl\nv\n");
254                                 for (;;) {
255                                         try {
256                                                 String line = config.serial_line.get_reply(5000);
257                                                 if (line == null)
258                                                         stop_serial();
259                                                 callback(line);
260                                                 if (line.startsWith("software-version"))
261                                                         break;
262                                         } catch (Exception e) {
263                                                 break;
264                                         }
265                                 }
266                         } catch (InterruptedException ie) {
267                         } catch (TimeoutException te) {
268                         } finally {
269                                 try {
270                                         stop_serial();
271                                 } catch (InterruptedException ie) {
272                                 }
273                         }
274                         callback("all finished");
275                 }
276
277                 void save_data() {
278                         try {
279                                 double frequency = frequency();
280                                 boolean has_frequency = radio_frequency.get() > 0;
281                                 boolean has_setting = radio_setting.get() > 0;
282                                 start_serial();
283                                 serial_line.printf("c m %d\n", main_deploy.get());
284                                 serial_line.printf("c d %d\n", apogee_delay.get());
285                                 serial_line.printf("c L %d\n", apogee_lockout.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                                         AltosUIPreferences.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                         radio_channel.set(0);
399                 } else if (setting > 0) {
400                         radio_setting.set(AltosConvert.radio_frequency_to_setting(freq,
401                                                                                   radio_calibration.get()));
402                         radio_channel.set(0);
403                 } else {
404                         radio_channel.set(AltosConvert.radio_frequency_to_channel(freq));
405                 }
406         }
407
408         void save_data() {
409
410                 /* bounds check stuff */
411                 if (config_ui.flight_log_max() > log_limit()) {
412                         JOptionPane.showMessageDialog(owner,
413                                                       String.format("Requested flight log, %dk, is larger than the available space, %dk.\n",
414                                                                     config_ui.flight_log_max(),
415                                                                     log_limit()),
416                                                       "Maximum Flight Log Too Large",
417                                                       JOptionPane.ERROR_MESSAGE);
418                         return;
419                 }
420
421                 main_deploy.set(config_ui.main_deploy());
422                 apogee_delay.set(config_ui.apogee_delay());
423                 apogee_lockout.set(config_ui.apogee_lockout());
424                 radio_calibration.set(config_ui.radio_calibration());
425                 set_frequency(config_ui.radio_frequency());
426                 flight_log_max.set(config_ui.flight_log_max());
427                 if (radio_enable.get() >= 0)
428                         radio_enable.set(config_ui.radio_enable());
429                 if (ignite_mode.get() >= 0)
430                         ignite_mode.set(config_ui.ignite_mode());
431                 if (pad_orientation.get() >= 0)
432                         pad_orientation.set(config_ui.pad_orientation());
433                 callsign.set(config_ui.callsign());
434                 run_serial_thread(serial_mode_save);
435         }
436
437         public void actionPerformed(ActionEvent e) {
438                 String  cmd = e.getActionCommand();
439                 try {
440                         if (cmd.equals("Save")) {
441                                 save_data();
442                         } else if (cmd.equals("Reset")) {
443                                 set_ui();
444                         } else if (cmd.equals("Reboot")) {
445                                 if (serial_line != null)
446                                         run_serial_thread(serial_mode_reboot);
447                         } else if (cmd.equals("Close")) {
448                                 if (serial_line != null)
449                                         serial_line.close();
450                         }
451                 } catch (InterruptedException ie) {
452                         abort();
453                 } catch (TimeoutException te) {
454                         abort();
455                 }
456         }
457
458         public AltosConfig(JFrame given_owner) {
459                 owner = given_owner;
460
461                 serial = new int_ref(0);
462                 log_format = new int_ref(Altos.AO_LOG_FORMAT_UNKNOWN);
463                 main_deploy = new int_ref(250);
464                 apogee_delay = new int_ref(0);
465                 apogee_lockout = new int_ref(0);
466                 radio_channel = new int_ref(0);
467                 radio_setting = new int_ref(0);
468                 radio_frequency = new int_ref(0);
469                 radio_calibration = new int_ref(1186611);
470                 radio_enable = new int_ref(-1);
471                 flight_log_max = new int_ref(0);
472                 ignite_mode = new int_ref(-1);
473                 pad_orientation = new int_ref(-1);
474                 storage_size = new int_ref(-1);
475                 storage_erase_unit = new int_ref(-1);
476                 stored_flight = new int_ref(-1);
477                 callsign = new string_ref("N0CALL");
478                 version = new string_ref("unknown");
479                 product = new string_ref("unknown");
480
481                 device = AltosDeviceDialog.show(owner, Altos.product_any);
482                 if (device != null) {
483                         try {
484                                 serial_line = new AltosSerial(device);
485                                 try {
486                                         if (!device.matchProduct(Altos.product_altimeter))
487                                                 remote = true;
488                                         init_ui();
489                                 } catch (InterruptedException ie) {
490                                         abort();
491                                 } catch (TimeoutException te) {
492                                         abort();
493                                 }
494                         } catch (FileNotFoundException ee) {
495                                 JOptionPane.showMessageDialog(owner,
496                                                               ee.getMessage(),
497                                                               "Cannot open target device",
498                                                               JOptionPane.ERROR_MESSAGE);
499                         } catch (AltosSerialInUseException si) {
500                                 JOptionPane.showMessageDialog(owner,
501                                                               String.format("Device \"%s\" already in use",
502                                                                             device.toShortString()),
503                                                               "Device in use",
504                                                               JOptionPane.ERROR_MESSAGE);
505                         }
506                 }
507         }
508 }