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