2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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.
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.
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.
21 import java.awt.event.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.*;
33 public class AltosConfig implements ActionListener {
41 public void set(int i) {
44 public int_ref(int i) {
55 public void set(String i) {
58 public string_ref(String i) {
65 AltosSerial serial_line;
67 AltosConfigData remote_config_data;
68 double remote_frequency;
73 int_ref radio_channel;
74 int_ref radio_calibration;
75 int_ref flight_log_max;
77 int_ref pad_orientation;
78 int_ref radio_setting;
80 int_ref storage_erase_unit;
81 int_ref stored_flight;
86 AltosConfigUI config_ui;
87 boolean serial_started;
90 boolean get_int(String line, String label, int_ref x) {
91 if (line.startsWith(label)) {
93 String tail = line.substring(label.length()).trim();
94 String[] tokens = tail.split("\\s+");
95 if (tokens.length > 0) {
96 int i = Integer.parseInt(tokens[0]);
100 } catch (NumberFormatException ne) {
106 boolean get_string(String line, String label, string_ref s) {
107 if (line.startsWith(label)) {
108 String quoted = line.substring(label.length()).trim();
110 if (quoted.startsWith("\""))
111 quoted = quoted.substring(1);
112 if (quoted.endsWith("\""))
113 quoted = quoted.substring(0,quoted.length()-1);
121 void start_serial() throws InterruptedException, TimeoutException {
122 serial_started = true;
124 serial_line.start_remote();
127 void stop_serial() throws InterruptedException {
130 serial_started = false;
132 serial_line.stop_remote();
136 if (storage_size.get() > 0 && storage_erase_unit.get() > 0) {
137 int log_limit = storage_size.get() - storage_erase_unit.get();
139 return log_limit / 1024;
145 config_ui.set_serial(serial.get());
146 config_ui.set_product(product.get());
147 config_ui.set_version(version.get());
148 config_ui.set_main_deploy(main_deploy.get());
149 config_ui.set_apogee_delay(apogee_delay.get());
150 config_ui.set_radio_calibration(radio_calibration.get());
151 config_ui.set_radio_frequency(frequency());
152 boolean max_enabled = true;
153 switch (log_format.get()) {
154 case Altos.AO_LOG_FORMAT_TINY:
158 if (stored_flight.get() >= 0)
162 config_ui.set_flight_log_max_enabled(max_enabled);
163 config_ui.set_radio_enable(radio_enable.get());
164 config_ui.set_flight_log_max_limit(log_limit());
165 config_ui.set_flight_log_max(flight_log_max.get());
166 config_ui.set_ignite_mode(ignite_mode.get());
167 config_ui.set_pad_orientation(pad_orientation.get());
168 config_ui.set_callsign(callsign.get());
169 config_ui.set_clean();
172 config_ui.make_visible();
176 void process_line(String line) {
181 if (line.equals("all finished")) {
182 if (serial_line != null)
186 get_int(line, "serial-number", serial);
187 get_int(line, "log-format", log_format);
188 get_int(line, "Main deploy:", main_deploy);
189 get_int(line, "Apogee delay:", apogee_delay);
190 get_int(line, "Radio channel:", radio_channel);
191 get_int(line, "Radio cal:", radio_calibration);
192 get_int(line, "Max flight log:", flight_log_max);
193 get_int(line, "Ignite mode:", ignite_mode);
194 get_int(line, "Pad orientation:", pad_orientation);
195 get_int(line, "Radio setting:", radio_setting);
196 get_int(line, "Radio enable:", radio_enable);
197 get_int(line, "Storage size:", storage_size);
198 get_int(line, "Storage erase unit:", storage_erase_unit);
199 get_int(line, "flight", stored_flight);
200 get_string(line, "Callsign:", callsign);
201 get_string(line,"software-version", version);
202 get_string(line,"product", product);
205 final static int serial_mode_read = 0;
206 final static int serial_mode_save = 1;
207 final static int serial_mode_reboot = 2;
209 class SerialData implements Runnable {
213 void process_line(String line) {
214 config.process_line(line);
216 void callback(String in_line) {
217 final String line = in_line;
218 Runnable r = new Runnable() {
223 SwingUtilities.invokeLater(r);
228 log_format.set(Altos.AO_LOG_FORMAT_UNKNOWN);
229 main_deploy.set(250);
231 radio_channel.set(0);
232 radio_setting.set(0);
233 radio_calibration.set(1186611);
234 radio_enable.set(-1);
235 flight_log_max.set(0);
237 pad_orientation.set(-1);
238 storage_size.set(-1);
239 storage_erase_unit.set(-1);
240 stored_flight.set(-1);
241 callsign.set("N0CALL");
242 version.set("unknown");
243 product.set("unknown");
248 config.start_serial();
251 config.serial_line.printf("c s\nf\nl\nv\n");
254 String line = config.serial_line.get_reply(5000);
258 if (line.startsWith("software-version"))
260 } catch (Exception e) {
264 } catch (InterruptedException ie) {
265 } catch (TimeoutException te) {
269 } catch (InterruptedException ie) {
272 callback("all finished");
277 double frequency = frequency();
278 boolean has_setting = radio_setting.get() > 0;
280 serial_line.printf("c m %d\n", main_deploy.get());
281 serial_line.printf("c d %d\n", apogee_delay.get());
283 serial_line.printf("c f %d\n", radio_calibration.get());
284 serial_line.set_radio_frequency(frequency,
286 radio_calibration.get());
288 serial_line.stop_remote();
289 serial_line.set_radio_frequency(frequency);
290 AltosPreferences.set_frequency(device.getSerial(), frequency);
291 serial_line.start_remote();
293 serial_line.printf("c c %s\n", callsign.get());
294 if (flight_log_max.get() != 0)
295 serial_line.printf("c l %d\n", flight_log_max.get());
296 if (radio_enable.get() >= 0)
297 serial_line.printf("c e %d\n", radio_enable.get());
298 if (ignite_mode.get() >= 0)
299 serial_line.printf("c i %d\n", ignite_mode.get());
300 if (pad_orientation.get() >= 0)
301 serial_line.printf("c o %d\n", pad_orientation.get());
302 serial_line.printf("c w\n");
303 } catch (InterruptedException ie) {
304 } catch (TimeoutException te) {
308 } catch (InterruptedException ie) {
316 serial_line.printf("r eboot\n");
317 serial_line.flush_output();
318 } catch (InterruptedException ie) {
319 } catch (TimeoutException te) {
323 } catch (InterruptedException ie) {
330 switch (serial_mode) {
331 case serial_mode_save:
333 /* fall through ... */
334 case serial_mode_read:
337 case serial_mode_reboot:
343 public SerialData(AltosConfig in_config, int in_serial_mode) {
345 serial_mode = in_serial_mode;
349 void run_serial_thread(int serial_mode) {
350 SerialData sd = new SerialData(this, serial_mode);
351 Thread st = new Thread(sd);
355 void init_ui () throws InterruptedException, TimeoutException {
356 config_ui = new AltosConfigUI(owner, remote);
357 config_ui.addActionListener(this);
358 serial_line.set_frame(owner);
365 JOptionPane.showMessageDialog(owner,
366 String.format("Connection to \"%s\" failed",
367 device.toShortString()),
369 JOptionPane.ERROR_MESSAGE);
370 config_ui.setVisible(false);
373 void set_ui() throws InterruptedException, TimeoutException {
374 if (serial_line != null)
375 run_serial_thread(serial_mode_read);
381 return AltosConvert.radio_to_frequency(radio_setting.get(),
382 radio_calibration.get(),
383 radio_channel.get());
386 void set_frequency(double freq) {
387 int setting = radio_setting.get();
390 radio_setting.set(AltosConvert.radio_frequency_to_setting(freq,
391 radio_calibration.get()));
392 radio_channel.set(0);
394 radio_channel.set(AltosConvert.radio_frequency_to_channel(freq));
400 /* bounds check stuff */
401 if (config_ui.flight_log_max() > log_limit()) {
402 JOptionPane.showMessageDialog(owner,
403 String.format("Requested flight log, %dk, is larger than the available space, %dk.\n",
404 config_ui.flight_log_max(),
406 "Maximum Flight Log Too Large",
407 JOptionPane.ERROR_MESSAGE);
411 main_deploy.set(config_ui.main_deploy());
412 apogee_delay.set(config_ui.apogee_delay());
413 radio_calibration.set(config_ui.radio_calibration());
414 set_frequency(config_ui.radio_frequency());
415 flight_log_max.set(config_ui.flight_log_max());
416 if (radio_enable.get() >= 0)
417 radio_enable.set(config_ui.radio_enable());
418 if (ignite_mode.get() >= 0)
419 ignite_mode.set(config_ui.ignite_mode());
420 if (pad_orientation.get() >= 0)
421 pad_orientation.set(config_ui.pad_orientation());
422 callsign.set(config_ui.callsign());
423 run_serial_thread(serial_mode_save);
426 public void actionPerformed(ActionEvent e) {
427 String cmd = e.getActionCommand();
429 if (cmd.equals("Save")) {
431 } else if (cmd.equals("Reset")) {
433 } else if (cmd.equals("Reboot")) {
434 if (serial_line != null)
435 run_serial_thread(serial_mode_reboot);
436 } else if (cmd.equals("Close")) {
437 if (serial_line != null)
440 } catch (InterruptedException ie) {
442 } catch (TimeoutException te) {
447 public AltosConfig(JFrame given_owner) {
450 serial = new int_ref(0);
451 log_format = new int_ref(Altos.AO_LOG_FORMAT_UNKNOWN);
452 main_deploy = new int_ref(250);
453 apogee_delay = new int_ref(0);
454 radio_channel = new int_ref(0);
455 radio_setting = new int_ref(0);
456 radio_calibration = new int_ref(1186611);
457 radio_enable = new int_ref(-1);
458 flight_log_max = new int_ref(0);
459 ignite_mode = new int_ref(-1);
460 pad_orientation = new int_ref(-1);
461 storage_size = new int_ref(-1);
462 storage_erase_unit = new int_ref(-1);
463 stored_flight = new int_ref(-1);
464 callsign = new string_ref("N0CALL");
465 version = new string_ref("unknown");
466 product = new string_ref("unknown");
468 device = AltosDeviceDialog.show(owner, Altos.product_any);
469 if (device != null) {
471 serial_line = new AltosSerial(device);
473 if (!device.matchProduct(Altos.product_telemetrum))
476 } catch (InterruptedException ie) {
478 } catch (TimeoutException te) {
481 } catch (FileNotFoundException ee) {
482 JOptionPane.showMessageDialog(owner,
483 String.format("Cannot open device \"%s\"",
484 device.toShortString()),
485 "Cannot open target device",
486 JOptionPane.ERROR_MESSAGE);
487 } catch (AltosSerialInUseException si) {
488 JOptionPane.showMessageDialog(owner,
489 String.format("Device \"%s\" already in use",
490 device.toShortString()),
492 JOptionPane.ERROR_MESSAGE);
493 } catch (IOException ee) {
494 JOptionPane.showMessageDialog(owner,
495 device.toShortString(),
496 ee.getLocalizedMessage(),
497 JOptionPane.ERROR_MESSAGE);