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 apogee_lockout;
74 int_ref radio_channel;
75 int_ref radio_calibration;
76 int_ref flight_log_max;
78 int_ref pad_orientation;
79 int_ref radio_setting;
80 int_ref radio_frequency;
82 int_ref storage_erase_unit;
83 int_ref stored_flight;
88 AltosConfigUI config_ui;
89 boolean serial_started;
92 boolean get_int(String line, String label, int_ref x) {
93 if (line.startsWith(label)) {
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]);
102 } catch (NumberFormatException ne) {
108 boolean get_string(String line, String label, string_ref s) {
109 if (line.startsWith(label)) {
110 String quoted = line.substring(label.length()).trim();
112 if (quoted.startsWith("\""))
113 quoted = quoted.substring(1);
114 if (quoted.endsWith("\""))
115 quoted = quoted.substring(0,quoted.length()-1);
123 void start_serial() throws InterruptedException, TimeoutException {
124 serial_started = true;
126 serial_line.start_remote();
129 void stop_serial() throws InterruptedException {
132 serial_started = false;
134 serial_line.stop_remote();
138 if (storage_size.get() > 0 && storage_erase_unit.get() > 0) {
139 int log_limit = storage_size.get() - storage_erase_unit.get();
141 return log_limit / 1024;
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:
161 if (stored_flight.get() >= 0)
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();
175 config_ui.make_visible();
179 void process_line(String line) {
184 if (line.equals("all finished")) {
185 if (serial_line != null)
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);
212 final static int serial_mode_read = 0;
213 final static int serial_mode_save = 1;
214 final static int serial_mode_reboot = 2;
216 class SerialData implements Runnable {
220 void process_line(String line) {
221 config.process_line(line);
223 void callback(String in_line) {
224 final String line = in_line;
225 Runnable r = new Runnable() {
230 SwingUtilities.invokeLater(r);
235 log_format.set(Altos.AO_LOG_FORMAT_UNKNOWN);
236 main_deploy.set(250);
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);
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");
257 config.start_serial();
260 config.serial_line.printf("c s\nf\nl\nv\n");
263 String line = config.serial_line.get_reply(5000);
267 if (line.startsWith("software-version"))
269 } catch (Exception e) {
273 } catch (InterruptedException ie) {
274 } catch (TimeoutException te) {
278 } catch (InterruptedException ie) {
281 callback("all finished");
286 double frequency = frequency();
287 boolean has_frequency = radio_frequency.get() > 0;
288 boolean has_setting = radio_setting.get() > 0;
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());
294 serial_line.printf("c f %d\n", radio_calibration.get());
295 serial_line.set_radio_frequency(frequency,
298 radio_calibration.get());
300 serial_line.stop_remote();
301 serial_line.set_radio_frequency(frequency);
302 AltosPreferences.set_frequency(device.getSerial(), frequency);
303 serial_line.start_remote();
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) {
320 } catch (InterruptedException ie) {
328 serial_line.printf("r eboot\n");
329 serial_line.flush_output();
330 } catch (InterruptedException ie) {
331 } catch (TimeoutException te) {
335 } catch (InterruptedException ie) {
342 switch (serial_mode) {
343 case serial_mode_save:
345 /* fall through ... */
346 case serial_mode_read:
349 case serial_mode_reboot:
355 public SerialData(AltosConfig in_config, int in_serial_mode) {
357 serial_mode = in_serial_mode;
361 void run_serial_thread(int serial_mode) {
362 SerialData sd = new SerialData(this, serial_mode);
363 Thread st = new Thread(sd);
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);
377 JOptionPane.showMessageDialog(owner,
378 String.format("Connection to \"%s\" failed",
379 device.toShortString()),
381 JOptionPane.ERROR_MESSAGE);
382 config_ui.setVisible(false);
385 void set_ui() throws InterruptedException, TimeoutException {
386 if (serial_line != null)
387 run_serial_thread(serial_mode_read);
393 return AltosConvert.radio_to_frequency(radio_frequency.get(),
395 radio_calibration.get(),
396 radio_channel.get());
399 void set_frequency(double freq) {
400 int frequency = radio_frequency.get();
401 int setting = radio_setting.get();
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);
411 radio_channel.set(AltosConvert.radio_frequency_to_channel(freq));
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(),
423 "Maximum Flight Log Too Large",
424 JOptionPane.ERROR_MESSAGE);
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);
444 public void actionPerformed(ActionEvent e) {
445 String cmd = e.getActionCommand();
447 if (cmd.equals("Save")) {
449 } else if (cmd.equals("Reset")) {
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)
458 } catch (InterruptedException ie) {
460 } catch (TimeoutException te) {
465 public AltosConfig(JFrame given_owner) {
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");
488 device = AltosDeviceDialog.show(owner, Altos.product_any);
489 if (device != null) {
491 serial_line = new AltosSerial(device);
493 if (!device.matchProduct(Altos.product_telemetrum))
496 } catch (InterruptedException ie) {
498 } catch (TimeoutException te) {
501 } catch (FileNotFoundException ee) {
502 JOptionPane.showMessageDialog(owner,
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()),
511 JOptionPane.ERROR_MESSAGE);
512 } catch (IOException ee) {
513 JOptionPane.showMessageDialog(owner,
514 device.toShortString(),
515 ee.getLocalizedMessage(),
516 JOptionPane.ERROR_MESSAGE);