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 apogee_lockout;
75 int_ref radio_channel;
76 int_ref radio_calibration;
77 int_ref flight_log_max;
79 int_ref pad_orientation;
80 int_ref radio_setting;
81 int_ref radio_frequency;
83 int_ref storage_erase_unit;
84 int_ref stored_flight;
89 AltosConfigUI config_ui;
90 boolean serial_started;
93 boolean get_int(String line, String label, int_ref x) {
94 if (line.startsWith(label)) {
96 String tail = line.substring(label.length()).trim();
97 String[] tokens = tail.split("\\s+");
98 if (tokens.length > 0) {
99 int i = Integer.parseInt(tokens[0]);
103 } catch (NumberFormatException ne) {
109 boolean get_string(String line, String label, string_ref s) {
110 if (line.startsWith(label)) {
111 String quoted = line.substring(label.length()).trim();
113 if (quoted.startsWith("\""))
114 quoted = quoted.substring(1);
115 if (quoted.endsWith("\""))
116 quoted = quoted.substring(0,quoted.length()-1);
124 void start_serial() throws InterruptedException, TimeoutException {
125 serial_started = true;
127 serial_line.start_remote();
130 void stop_serial() throws InterruptedException {
133 serial_started = false;
135 serial_line.stop_remote();
139 if (storage_size.get() > 0 && storage_erase_unit.get() > 0) {
140 int log_limit = storage_size.get() - storage_erase_unit.get();
142 return log_limit / 1024;
148 config_ui.set_serial(serial.get());
149 config_ui.set_product(product.get());
150 config_ui.set_version(version.get());
151 config_ui.set_main_deploy(main_deploy.get());
152 config_ui.set_apogee_delay(apogee_delay.get());
153 config_ui.set_apogee_lockout(apogee_lockout.get());
154 config_ui.set_radio_calibration(radio_calibration.get());
155 config_ui.set_radio_frequency(frequency());
156 boolean max_enabled = true;
157 switch (log_format.get()) {
158 case Altos.AO_LOG_FORMAT_TINY:
162 if (stored_flight.get() >= 0)
166 config_ui.set_flight_log_max_enabled(max_enabled);
167 config_ui.set_radio_enable(radio_enable.get());
168 config_ui.set_flight_log_max_limit(log_limit());
169 config_ui.set_flight_log_max(flight_log_max.get());
170 config_ui.set_ignite_mode(ignite_mode.get());
171 config_ui.set_pad_orientation(pad_orientation.get());
172 config_ui.set_callsign(callsign.get());
173 config_ui.set_clean();
176 config_ui.make_visible();
180 void process_line(String line) {
185 if (line.equals("all finished")) {
186 if (serial_line != null)
190 get_int(line, "serial-number", serial);
191 get_int(line, "log-format", log_format);
192 get_int(line, "Main deploy:", main_deploy);
193 get_int(line, "Apogee delay:", apogee_delay);
194 get_int(line, "Apogee lockout:", apogee_lockout);
195 get_int(line, "Radio channel:", radio_channel);
196 get_int(line, "Radio cal:", radio_calibration);
197 get_int(line, "Max flight log:", flight_log_max);
198 get_int(line, "Ignite mode:", ignite_mode);
199 get_int(line, "Pad orientation:", pad_orientation);
200 get_int(line, "Radio setting:", radio_setting);
201 if (get_int(line, "Frequency:", radio_frequency))
202 if (radio_frequency.get() < 0)
203 radio_frequency.set(434550);
204 get_int(line, "Radio enable:", radio_enable);
205 get_int(line, "Storage size:", storage_size);
206 get_int(line, "Storage erase unit:", storage_erase_unit);
207 get_int(line, "flight", stored_flight);
208 get_string(line, "Callsign:", callsign);
209 get_string(line,"software-version", version);
210 get_string(line,"product", product);
213 final static int serial_mode_read = 0;
214 final static int serial_mode_save = 1;
215 final static int serial_mode_reboot = 2;
217 class SerialData implements Runnable {
221 void process_line(String line) {
222 config.process_line(line);
224 void callback(String in_line) {
225 final String line = in_line;
226 Runnable r = new Runnable() {
231 SwingUtilities.invokeLater(r);
236 log_format.set(Altos.AO_LOG_FORMAT_UNKNOWN);
237 main_deploy.set(250);
239 apogee_lockout.set(0);
240 radio_channel.set(0);
241 radio_setting.set(0);
242 radio_frequency.set(0);
243 radio_calibration.set(1186611);
244 radio_enable.set(-1);
245 flight_log_max.set(0);
247 pad_orientation.set(-1);
248 storage_size.set(-1);
249 storage_erase_unit.set(-1);
250 stored_flight.set(-1);
251 callsign.set("N0CALL");
252 version.set("unknown");
253 product.set("unknown");
258 config.start_serial();
261 config.serial_line.printf("c s\nf\nl\nv\n");
264 String line = config.serial_line.get_reply(5000);
268 if (line.startsWith("software-version"))
270 } catch (Exception e) {
274 } catch (InterruptedException ie) {
275 } catch (TimeoutException te) {
279 } catch (InterruptedException ie) {
282 callback("all finished");
287 double frequency = frequency();
288 boolean has_frequency = radio_frequency.get() > 0;
289 boolean has_setting = radio_setting.get() > 0;
291 serial_line.printf("c m %d\n", main_deploy.get());
292 serial_line.printf("c d %d\n", apogee_delay.get());
293 serial_line.printf("c L %d\n", apogee_lockout.get());
295 serial_line.printf("c f %d\n", radio_calibration.get());
296 serial_line.set_radio_frequency(frequency,
299 radio_calibration.get());
301 serial_line.stop_remote();
302 serial_line.set_radio_frequency(frequency);
303 AltosUIPreferences.set_frequency(device.getSerial(), frequency);
304 serial_line.start_remote();
306 serial_line.printf("c c %s\n", callsign.get());
307 if (flight_log_max.get() != 0)
308 serial_line.printf("c l %d\n", flight_log_max.get());
309 if (radio_enable.get() >= 0)
310 serial_line.printf("c e %d\n", radio_enable.get());
311 if (ignite_mode.get() >= 0)
312 serial_line.printf("c i %d\n", ignite_mode.get());
313 if (pad_orientation.get() >= 0)
314 serial_line.printf("c o %d\n", pad_orientation.get());
315 serial_line.printf("c w\n");
316 } catch (InterruptedException ie) {
317 } catch (TimeoutException te) {
321 } catch (InterruptedException ie) {
329 serial_line.printf("r eboot\n");
330 serial_line.flush_output();
331 } catch (InterruptedException ie) {
332 } catch (TimeoutException te) {
336 } catch (InterruptedException ie) {
343 switch (serial_mode) {
344 case serial_mode_save:
346 /* fall through ... */
347 case serial_mode_read:
350 case serial_mode_reboot:
356 public SerialData(AltosConfig in_config, int in_serial_mode) {
358 serial_mode = in_serial_mode;
362 void run_serial_thread(int serial_mode) {
363 SerialData sd = new SerialData(this, serial_mode);
364 Thread st = new Thread(sd);
368 void init_ui () throws InterruptedException, TimeoutException {
369 config_ui = new AltosConfigUI(owner, remote);
370 config_ui.addActionListener(this);
371 serial_line.set_frame(owner);
378 JOptionPane.showMessageDialog(owner,
379 String.format("Connection to \"%s\" failed",
380 device.toShortString()),
382 JOptionPane.ERROR_MESSAGE);
383 config_ui.setVisible(false);
386 void set_ui() throws InterruptedException, TimeoutException {
387 if (serial_line != null)
388 run_serial_thread(serial_mode_read);
394 return AltosConvert.radio_to_frequency(radio_frequency.get(),
396 radio_calibration.get(),
397 radio_channel.get());
400 void set_frequency(double freq) {
401 int frequency = radio_frequency.get();
402 int setting = radio_setting.get();
405 radio_frequency.set((int) Math.floor (freq * 1000 + 0.5));
406 radio_channel.set(0);
407 } else if (setting > 0) {
408 radio_setting.set(AltosConvert.radio_frequency_to_setting(freq,
409 radio_calibration.get()));
410 radio_channel.set(0);
412 radio_channel.set(AltosConvert.radio_frequency_to_channel(freq));
418 /* bounds check stuff */
419 if (config_ui.flight_log_max() > log_limit()) {
420 JOptionPane.showMessageDialog(owner,
421 String.format("Requested flight log, %dk, is larger than the available space, %dk.\n",
422 config_ui.flight_log_max(),
424 "Maximum Flight Log Too Large",
425 JOptionPane.ERROR_MESSAGE);
429 main_deploy.set(config_ui.main_deploy());
430 apogee_delay.set(config_ui.apogee_delay());
431 apogee_lockout.set(config_ui.apogee_lockout());
432 radio_calibration.set(config_ui.radio_calibration());
433 set_frequency(config_ui.radio_frequency());
434 flight_log_max.set(config_ui.flight_log_max());
435 if (radio_enable.get() >= 0)
436 radio_enable.set(config_ui.radio_enable());
437 if (ignite_mode.get() >= 0)
438 ignite_mode.set(config_ui.ignite_mode());
439 if (pad_orientation.get() >= 0)
440 pad_orientation.set(config_ui.pad_orientation());
441 callsign.set(config_ui.callsign());
442 run_serial_thread(serial_mode_save);
445 public void actionPerformed(ActionEvent e) {
446 String cmd = e.getActionCommand();
448 if (cmd.equals("Save")) {
450 } else if (cmd.equals("Reset")) {
452 } else if (cmd.equals("Reboot")) {
453 if (serial_line != null)
454 run_serial_thread(serial_mode_reboot);
455 } else if (cmd.equals("Close")) {
456 if (serial_line != null)
459 } catch (InterruptedException ie) {
461 } catch (TimeoutException te) {
466 public AltosConfig(JFrame given_owner) {
469 serial = new int_ref(0);
470 log_format = new int_ref(Altos.AO_LOG_FORMAT_UNKNOWN);
471 main_deploy = new int_ref(250);
472 apogee_delay = new int_ref(0);
473 apogee_lockout = new int_ref(0);
474 radio_channel = new int_ref(0);
475 radio_setting = new int_ref(0);
476 radio_frequency = new int_ref(0);
477 radio_calibration = new int_ref(1186611);
478 radio_enable = new int_ref(-1);
479 flight_log_max = new int_ref(0);
480 ignite_mode = new int_ref(-1);
481 pad_orientation = new int_ref(-1);
482 storage_size = new int_ref(-1);
483 storage_erase_unit = new int_ref(-1);
484 stored_flight = new int_ref(-1);
485 callsign = new string_ref("N0CALL");
486 version = new string_ref("unknown");
487 product = new string_ref("unknown");
489 device = AltosDeviceDialog.show(owner, Altos.product_any);
490 if (device != null) {
492 serial_line = new AltosSerial(device);
494 if (!device.matchProduct(Altos.product_altimeter))
497 } catch (InterruptedException ie) {
499 } catch (TimeoutException te) {
502 } catch (FileNotFoundException ee) {
503 JOptionPane.showMessageDialog(owner,
505 "Cannot open target device",
506 JOptionPane.ERROR_MESSAGE);
507 } catch (AltosSerialInUseException si) {
508 JOptionPane.showMessageDialog(owner,
509 String.format("Device \"%s\" already in use",
510 device.toShortString()),
512 JOptionPane.ERROR_MESSAGE);
513 } catch (IOException ee) {
514 JOptionPane.showMessageDialog(owner,
515 device.toShortString(),
516 ee.getLocalizedMessage(),
517 JOptionPane.ERROR_MESSAGE);