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;
79 int_ref radio_frequency;
81 int_ref storage_erase_unit;
82 int_ref stored_flight;
87 AltosConfigUI config_ui;
88 boolean serial_started;
91 boolean get_int(String line, String label, int_ref x) {
92 if (line.startsWith(label)) {
94 String tail = line.substring(label.length()).trim();
95 String[] tokens = tail.split("\\s+");
96 if (tokens.length > 0) {
97 int i = Integer.parseInt(tokens[0]);
101 } catch (NumberFormatException ne) {
107 boolean get_string(String line, String label, string_ref s) {
108 if (line.startsWith(label)) {
109 String quoted = line.substring(label.length()).trim();
111 if (quoted.startsWith("\""))
112 quoted = quoted.substring(1);
113 if (quoted.endsWith("\""))
114 quoted = quoted.substring(0,quoted.length()-1);
122 void start_serial() throws InterruptedException, TimeoutException {
123 serial_started = true;
125 serial_line.start_remote();
128 void stop_serial() throws InterruptedException {
131 serial_started = false;
133 serial_line.stop_remote();
137 if (storage_size.get() > 0 && storage_erase_unit.get() > 0) {
138 int log_limit = storage_size.get() - storage_erase_unit.get();
140 return log_limit / 1024;
146 config_ui.set_serial(serial.get());
147 config_ui.set_product(product.get());
148 config_ui.set_version(version.get());
149 config_ui.set_main_deploy(main_deploy.get());
150 config_ui.set_apogee_delay(apogee_delay.get());
151 config_ui.set_radio_calibration(radio_calibration.get());
152 config_ui.set_radio_frequency(frequency());
153 boolean max_enabled = true;
154 switch (log_format.get()) {
155 case Altos.AO_LOG_FORMAT_TINY:
159 if (stored_flight.get() >= 0)
163 config_ui.set_flight_log_max_enabled(max_enabled);
164 config_ui.set_radio_enable(radio_enable.get());
165 config_ui.set_flight_log_max_limit(log_limit());
166 config_ui.set_flight_log_max(flight_log_max.get());
167 config_ui.set_ignite_mode(ignite_mode.get());
168 config_ui.set_pad_orientation(pad_orientation.get());
169 config_ui.set_callsign(callsign.get());
170 config_ui.set_clean();
173 config_ui.make_visible();
177 void process_line(String line) {
182 if (line.equals("all finished")) {
183 if (serial_line != null)
187 get_int(line, "serial-number", serial);
188 get_int(line, "log-format", log_format);
189 get_int(line, "Main deploy:", main_deploy);
190 get_int(line, "Apogee delay:", apogee_delay);
191 get_int(line, "Radio channel:", radio_channel);
192 get_int(line, "Radio cal:", radio_calibration);
193 get_int(line, "Max flight log:", flight_log_max);
194 get_int(line, "Ignite mode:", ignite_mode);
195 get_int(line, "Pad orientation:", pad_orientation);
196 get_int(line, "Radio setting:", radio_setting);
197 if (get_int(line, "Frequency:", radio_frequency))
198 if (radio_frequency.get() < 0)
199 radio_frequency.set(434550);
200 get_int(line, "Radio enable:", radio_enable);
201 get_int(line, "Storage size:", storage_size);
202 get_int(line, "Storage erase unit:", storage_erase_unit);
203 get_int(line, "flight", stored_flight);
204 get_string(line, "Callsign:", callsign);
205 get_string(line,"software-version", version);
206 get_string(line,"product", product);
209 final static int serial_mode_read = 0;
210 final static int serial_mode_save = 1;
211 final static int serial_mode_reboot = 2;
213 class SerialData implements Runnable {
217 void process_line(String line) {
218 config.process_line(line);
220 void callback(String in_line) {
221 final String line = in_line;
222 Runnable r = new Runnable() {
227 SwingUtilities.invokeLater(r);
232 log_format.set(Altos.AO_LOG_FORMAT_UNKNOWN);
233 main_deploy.set(250);
235 radio_channel.set(0);
236 radio_setting.set(0);
237 radio_frequency.set(0);
238 radio_calibration.set(1186611);
239 radio_enable.set(-1);
240 flight_log_max.set(0);
242 pad_orientation.set(-1);
243 storage_size.set(-1);
244 storage_erase_unit.set(-1);
245 stored_flight.set(-1);
246 callsign.set("N0CALL");
247 version.set("unknown");
248 product.set("unknown");
253 config.start_serial();
256 config.serial_line.printf("c s\nf\nl\nv\n");
259 String line = config.serial_line.get_reply(5000);
263 if (line.startsWith("software-version"))
265 } catch (Exception e) {
269 } catch (InterruptedException ie) {
270 } catch (TimeoutException te) {
274 } catch (InterruptedException ie) {
277 callback("all finished");
282 double frequency = frequency();
283 boolean has_frequency = radio_frequency.get() > 0;
284 boolean has_setting = radio_setting.get() > 0;
286 serial_line.printf("c m %d\n", main_deploy.get());
287 serial_line.printf("c d %d\n", apogee_delay.get());
289 serial_line.printf("c f %d\n", radio_calibration.get());
290 serial_line.set_radio_frequency(frequency,
293 radio_calibration.get());
295 serial_line.stop_remote();
296 serial_line.set_radio_frequency(frequency);
297 AltosPreferences.set_frequency(device.getSerial(), frequency);
298 serial_line.start_remote();
300 serial_line.printf("c c %s\n", callsign.get());
301 if (flight_log_max.get() != 0)
302 serial_line.printf("c l %d\n", flight_log_max.get());
303 if (radio_enable.get() >= 0)
304 serial_line.printf("c e %d\n", radio_enable.get());
305 if (ignite_mode.get() >= 0)
306 serial_line.printf("c i %d\n", ignite_mode.get());
307 if (pad_orientation.get() >= 0)
308 serial_line.printf("c o %d\n", pad_orientation.get());
309 serial_line.printf("c w\n");
310 } catch (InterruptedException ie) {
311 } catch (TimeoutException te) {
315 } catch (InterruptedException ie) {
323 serial_line.printf("r eboot\n");
324 serial_line.flush_output();
325 } catch (InterruptedException ie) {
326 } catch (TimeoutException te) {
330 } catch (InterruptedException ie) {
337 switch (serial_mode) {
338 case serial_mode_save:
340 /* fall through ... */
341 case serial_mode_read:
344 case serial_mode_reboot:
350 public SerialData(AltosConfig in_config, int in_serial_mode) {
352 serial_mode = in_serial_mode;
356 void run_serial_thread(int serial_mode) {
357 SerialData sd = new SerialData(this, serial_mode);
358 Thread st = new Thread(sd);
362 void init_ui () throws InterruptedException, TimeoutException {
363 config_ui = new AltosConfigUI(owner, remote);
364 config_ui.addActionListener(this);
365 serial_line.set_frame(owner);
372 JOptionPane.showMessageDialog(owner,
373 String.format("Connection to \"%s\" failed",
374 device.toShortString()),
376 JOptionPane.ERROR_MESSAGE);
377 config_ui.setVisible(false);
380 void set_ui() throws InterruptedException, TimeoutException {
381 if (serial_line != null)
382 run_serial_thread(serial_mode_read);
388 return AltosConvert.radio_to_frequency(radio_frequency.get(),
390 radio_calibration.get(),
391 radio_channel.get());
394 void set_frequency(double freq) {
395 int frequency = radio_frequency.get();
396 int setting = radio_setting.get();
399 radio_frequency.set((int) Math.floor (freq * 1000 + 0.5));
400 radio_channel.set(0);
401 } else if (setting > 0) {
402 radio_setting.set(AltosConvert.radio_frequency_to_setting(freq,
403 radio_calibration.get()));
404 radio_channel.set(0);
406 radio_channel.set(AltosConvert.radio_frequency_to_channel(freq));
412 /* bounds check stuff */
413 if (config_ui.flight_log_max() > log_limit()) {
414 JOptionPane.showMessageDialog(owner,
415 String.format("Requested flight log, %dk, is larger than the available space, %dk.\n",
416 config_ui.flight_log_max(),
418 "Maximum Flight Log Too Large",
419 JOptionPane.ERROR_MESSAGE);
423 main_deploy.set(config_ui.main_deploy());
424 apogee_delay.set(config_ui.apogee_delay());
425 radio_calibration.set(config_ui.radio_calibration());
426 set_frequency(config_ui.radio_frequency());
427 flight_log_max.set(config_ui.flight_log_max());
428 if (radio_enable.get() >= 0)
429 radio_enable.set(config_ui.radio_enable());
430 if (ignite_mode.get() >= 0)
431 ignite_mode.set(config_ui.ignite_mode());
432 if (pad_orientation.get() >= 0)
433 pad_orientation.set(config_ui.pad_orientation());
434 callsign.set(config_ui.callsign());
435 run_serial_thread(serial_mode_save);
438 public void actionPerformed(ActionEvent e) {
439 String cmd = e.getActionCommand();
441 if (cmd.equals("Save")) {
443 } else if (cmd.equals("Reset")) {
445 } else if (cmd.equals("Reboot")) {
446 if (serial_line != null)
447 run_serial_thread(serial_mode_reboot);
448 } else if (cmd.equals("Close")) {
449 if (serial_line != null)
452 } catch (InterruptedException ie) {
454 } catch (TimeoutException te) {
459 public AltosConfig(JFrame given_owner) {
462 serial = new int_ref(0);
463 log_format = new int_ref(Altos.AO_LOG_FORMAT_UNKNOWN);
464 main_deploy = new int_ref(250);
465 apogee_delay = 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");
481 device = AltosDeviceDialog.show(owner, Altos.product_any);
482 if (device != null) {
484 serial_line = new AltosSerial(device);
486 if (!device.matchProduct(Altos.product_telemetrum))
489 } catch (InterruptedException ie) {
491 } catch (TimeoutException te) {
494 } catch (FileNotFoundException ee) {
495 JOptionPane.showMessageDialog(owner,
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()),
504 JOptionPane.ERROR_MESSAGE);
505 } catch (IOException ee) {
506 JOptionPane.showMessageDialog(owner,
507 device.toShortString(),
508 ee.getLocalizedMessage(),
509 JOptionPane.ERROR_MESSAGE);