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.*;
30 import org.altusmetrum.AltosLib.*;
34 public class AltosConfig implements ActionListener {
42 public void set(int i) {
45 public int_ref(int i) {
56 public void set(String i) {
59 public string_ref(String i) {
66 AltosSerial serial_line;
68 AltosConfigData remote_config_data;
69 double remote_frequency;
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;
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 get_int(line, "Radio enable:", radio_enable);
198 get_int(line, "Storage size:", storage_size);
199 get_int(line, "Storage erase unit:", storage_erase_unit);
200 get_int(line, "flight", stored_flight);
201 get_string(line, "Callsign:", callsign);
202 get_string(line,"software-version", version);
203 get_string(line,"product", product);
206 final static int serial_mode_read = 0;
207 final static int serial_mode_save = 1;
208 final static int serial_mode_reboot = 2;
210 class SerialData implements Runnable {
214 void process_line(String line) {
215 config.process_line(line);
217 void callback(String in_line) {
218 final String line = in_line;
219 Runnable r = new Runnable() {
224 SwingUtilities.invokeLater(r);
229 log_format.set(Altos.AO_LOG_FORMAT_UNKNOWN);
230 main_deploy.set(250);
232 radio_channel.set(0);
233 radio_setting.set(0);
234 radio_calibration.set(1186611);
235 radio_enable.set(-1);
236 flight_log_max.set(0);
238 pad_orientation.set(-1);
239 storage_size.set(-1);
240 storage_erase_unit.set(-1);
241 stored_flight.set(-1);
242 callsign.set("N0CALL");
243 version.set("unknown");
244 product.set("unknown");
249 config.start_serial();
252 config.serial_line.printf("c s\nf\nl\nv\n");
255 String line = config.serial_line.get_reply(5000);
259 if (line.startsWith("software-version"))
261 } catch (Exception e) {
265 } catch (InterruptedException ie) {
266 } catch (TimeoutException te) {
270 } catch (InterruptedException ie) {
273 callback("all finished");
278 double frequency = frequency();
279 boolean has_setting = radio_setting.get() > 0;
281 serial_line.printf("c m %d\n", main_deploy.get());
282 serial_line.printf("c d %d\n", apogee_delay.get());
284 serial_line.printf("c f %d\n", radio_calibration.get());
285 serial_line.set_radio_frequency(frequency,
287 radio_calibration.get());
289 serial_line.stop_remote();
290 serial_line.set_radio_frequency(frequency);
291 AltosUIPreferences.set_frequency(device.getSerial(), frequency);
292 serial_line.start_remote();
294 serial_line.printf("c c %s\n", callsign.get());
295 if (flight_log_max.get() != 0)
296 serial_line.printf("c l %d\n", flight_log_max.get());
297 if (radio_enable.get() >= 0)
298 serial_line.printf("c e %d\n", radio_enable.get());
299 if (ignite_mode.get() >= 0)
300 serial_line.printf("c i %d\n", ignite_mode.get());
301 if (pad_orientation.get() >= 0)
302 serial_line.printf("c o %d\n", pad_orientation.get());
303 serial_line.printf("c w\n");
304 } catch (InterruptedException ie) {
305 } catch (TimeoutException te) {
309 } catch (InterruptedException ie) {
317 serial_line.printf("r eboot\n");
318 serial_line.flush_output();
319 } catch (InterruptedException ie) {
320 } catch (TimeoutException te) {
324 } catch (InterruptedException ie) {
331 switch (serial_mode) {
332 case serial_mode_save:
334 /* fall through ... */
335 case serial_mode_read:
338 case serial_mode_reboot:
344 public SerialData(AltosConfig in_config, int in_serial_mode) {
346 serial_mode = in_serial_mode;
350 void run_serial_thread(int serial_mode) {
351 SerialData sd = new SerialData(this, serial_mode);
352 Thread st = new Thread(sd);
356 void init_ui () throws InterruptedException, TimeoutException {
357 config_ui = new AltosConfigUI(owner, remote);
358 config_ui.addActionListener(this);
359 serial_line.set_frame(owner);
366 JOptionPane.showMessageDialog(owner,
367 String.format("Connection to \"%s\" failed",
368 device.toShortString()),
370 JOptionPane.ERROR_MESSAGE);
371 config_ui.setVisible(false);
374 void set_ui() throws InterruptedException, TimeoutException {
375 if (serial_line != null)
376 run_serial_thread(serial_mode_read);
382 return AltosConvert.radio_to_frequency(radio_setting.get(),
383 radio_calibration.get(),
384 radio_channel.get());
387 void set_frequency(double freq) {
388 int setting = radio_setting.get();
391 radio_setting.set(AltosConvert.radio_frequency_to_setting(freq,
392 radio_calibration.get()));
393 radio_channel.set(0);
395 radio_channel.set(AltosConvert.radio_frequency_to_channel(freq));
401 /* bounds check stuff */
402 if (config_ui.flight_log_max() > log_limit()) {
403 JOptionPane.showMessageDialog(owner,
404 String.format("Requested flight log, %dk, is larger than the available space, %dk.\n",
405 config_ui.flight_log_max(),
407 "Maximum Flight Log Too Large",
408 JOptionPane.ERROR_MESSAGE);
412 main_deploy.set(config_ui.main_deploy());
413 apogee_delay.set(config_ui.apogee_delay());
414 radio_calibration.set(config_ui.radio_calibration());
415 set_frequency(config_ui.radio_frequency());
416 flight_log_max.set(config_ui.flight_log_max());
417 if (radio_enable.get() >= 0)
418 radio_enable.set(config_ui.radio_enable());
419 if (ignite_mode.get() >= 0)
420 ignite_mode.set(config_ui.ignite_mode());
421 if (pad_orientation.get() >= 0)
422 pad_orientation.set(config_ui.pad_orientation());
423 callsign.set(config_ui.callsign());
424 run_serial_thread(serial_mode_save);
427 public void actionPerformed(ActionEvent e) {
428 String cmd = e.getActionCommand();
430 if (cmd.equals("Save")) {
432 } else if (cmd.equals("Reset")) {
434 } else if (cmd.equals("Reboot")) {
435 if (serial_line != null)
436 run_serial_thread(serial_mode_reboot);
437 } else if (cmd.equals("Close")) {
438 if (serial_line != null)
441 } catch (InterruptedException ie) {
443 } catch (TimeoutException te) {
448 public AltosConfig(JFrame given_owner) {
451 serial = new int_ref(0);
452 log_format = new int_ref(Altos.AO_LOG_FORMAT_UNKNOWN);
453 main_deploy = new int_ref(250);
454 apogee_delay = new int_ref(0);
455 radio_channel = new int_ref(0);
456 radio_setting = new int_ref(0);
457 radio_calibration = new int_ref(1186611);
458 radio_enable = new int_ref(-1);
459 flight_log_max = new int_ref(0);
460 ignite_mode = new int_ref(-1);
461 pad_orientation = new int_ref(-1);
462 storage_size = new int_ref(-1);
463 storage_erase_unit = new int_ref(-1);
464 stored_flight = new int_ref(-1);
465 callsign = new string_ref("N0CALL");
466 version = new string_ref("unknown");
467 product = new string_ref("unknown");
469 device = AltosDeviceDialog.show(owner, Altos.product_any);
470 if (device != null) {
472 serial_line = new AltosSerial(device);
474 if (!device.matchProduct(Altos.product_telemetrum))
477 } catch (InterruptedException ie) {
479 } catch (TimeoutException te) {
482 } catch (FileNotFoundException ee) {
483 JOptionPane.showMessageDialog(owner,
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);