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