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.
20 import java.awt.event.*;
23 import java.util.concurrent.*;
24 import org.altusmetrum.AltosLib.*;
27 public class AltosConfig implements ActionListener {
35 public void set(int i) {
38 public int_ref(int i) {
49 public void set(String i) {
52 public string_ref(String i) {
59 AltosSerial serial_line;
61 AltosConfigData remote_config_data;
62 double remote_frequency;
67 int_ref apogee_lockout;
68 int_ref radio_channel;
69 int_ref radio_calibration;
70 int_ref flight_log_max;
72 int_ref pad_orientation;
73 int_ref radio_setting;
74 int_ref radio_frequency;
76 int_ref storage_erase_unit;
77 int_ref stored_flight;
84 AltosConfigUI config_ui;
85 boolean serial_started;
88 boolean get_int(String line, String label, int_ref x) {
89 if (line.startsWith(label)) {
91 String tail = line.substring(label.length()).trim();
92 String[] tokens = tail.split("\\s+");
93 if (tokens.length > 0) {
94 int i = Integer.parseInt(tokens[0]);
98 } catch (NumberFormatException ne) {
104 boolean get_string(String line, String label, string_ref s) {
105 if (line.startsWith(label)) {
106 String quoted = line.substring(label.length()).trim();
108 if (quoted.startsWith("\""))
109 quoted = quoted.substring(1);
110 if (quoted.endsWith("\""))
111 quoted = quoted.substring(0,quoted.length()-1);
119 void start_serial() throws InterruptedException, TimeoutException {
120 serial_started = true;
122 serial_line.start_remote();
125 void stop_serial() throws InterruptedException {
128 serial_started = false;
130 serial_line.stop_remote();
134 if (storage_size.get() > 0 && storage_erase_unit.get() > 0) {
135 int log_limit = storage_size.get() - storage_erase_unit.get();
137 return log_limit / 1024;
143 config_ui.set_serial(serial.get());
144 config_ui.set_product(product.get());
145 config_ui.set_version(version.get());
146 config_ui.set_main_deploy(main_deploy.get());
147 config_ui.set_apogee_delay(apogee_delay.get());
148 config_ui.set_apogee_lockout(apogee_lockout.get());
149 config_ui.set_radio_calibration(radio_calibration.get());
150 config_ui.set_radio_frequency(frequency());
151 boolean max_enabled = true;
152 switch (log_format.get()) {
153 case Altos.AO_LOG_FORMAT_TINY:
157 if (stored_flight.get() >= 0)
161 config_ui.set_flight_log_max_enabled(max_enabled);
162 config_ui.set_radio_enable(radio_enable.get());
163 config_ui.set_flight_log_max_limit(log_limit());
164 config_ui.set_flight_log_max(flight_log_max.get());
165 config_ui.set_ignite_mode(ignite_mode.get());
166 config_ui.set_pad_orientation(pad_orientation.get());
167 config_ui.set_callsign(callsign.get());
168 config_ui.set_pyros(pyros);
169 config_ui.set_has_pyro(npyro.get() > 0);
170 config_ui.set_clean();
173 config_ui.make_visible();
179 void process_line(String line) {
184 if (line.equals("all finished")) {
185 if (serial_line != null)
189 if (pyro < npyro.get()) {
191 pyros = new AltosPyro[npyro.get()];
194 pyros[pyro] = new AltosPyro(pyro, line);
195 } catch (ParseException e) {
196 System.out.printf ("pyro parse failed %s\n", line);
201 get_int(line, "serial-number", serial);
202 get_int(line, "log-format", log_format);
203 get_int(line, "Main deploy:", main_deploy);
204 get_int(line, "Apogee delay:", apogee_delay);
205 get_int(line, "Apogee lockout:", apogee_lockout);
206 get_int(line, "Radio channel:", radio_channel);
207 get_int(line, "Radio cal:", radio_calibration);
208 get_int(line, "Max flight log:", flight_log_max);
209 get_int(line, "Ignite mode:", ignite_mode);
210 get_int(line, "Pad orientation:", pad_orientation);
211 get_int(line, "Radio setting:", radio_setting);
212 if (get_int(line, "Frequency:", radio_frequency))
213 if (radio_frequency.get() < 0)
214 radio_frequency.set(434550);
215 get_int(line, "Radio enable:", radio_enable);
216 get_int(line, "Storage size:", storage_size);
217 get_int(line, "Storage erase unit:", storage_erase_unit);
218 get_int(line, "flight", stored_flight);
219 get_string(line, "Callsign:", callsign);
220 get_string(line,"software-version", version);
221 get_string(line,"product", product);
222 get_int(line, "Pyro-count:", npyro);
225 final static int serial_mode_read = 0;
226 final static int serial_mode_save = 1;
227 final static int serial_mode_reboot = 2;
229 class SerialData implements Runnable {
233 void process_line(String line) {
234 config.process_line(line);
236 void callback(String in_line) {
237 final String line = in_line;
238 Runnable r = new Runnable() {
243 SwingUtilities.invokeLater(r);
248 log_format.set(Altos.AO_LOG_FORMAT_UNKNOWN);
249 main_deploy.set(250);
251 apogee_lockout.set(0);
252 radio_channel.set(0);
253 radio_setting.set(0);
254 radio_frequency.set(0);
255 radio_calibration.set(1186611);
256 radio_enable.set(-1);
257 flight_log_max.set(0);
259 pad_orientation.set(-1);
260 storage_size.set(-1);
261 storage_erase_unit.set(-1);
262 stored_flight.set(-1);
263 callsign.set("N0CALL");
264 version.set("unknown");
265 product.set("unknown");
272 config.start_serial();
275 config.serial_line.printf("c s\nf\nl\nv\n");
278 String line = config.serial_line.get_reply(5000);
282 if (line.startsWith("software-version"))
284 } catch (Exception e) {
288 } catch (InterruptedException ie) {
289 } catch (TimeoutException te) {
293 } catch (InterruptedException ie) {
296 callback("all finished");
301 double frequency = frequency();
302 boolean has_frequency = radio_frequency.get() > 0;
303 boolean has_setting = radio_setting.get() > 0;
305 serial_line.printf("c m %d\n", main_deploy.get());
306 serial_line.printf("c d %d\n", apogee_delay.get());
307 serial_line.printf("c L %d\n", apogee_lockout.get());
309 serial_line.printf("c f %d\n", radio_calibration.get());
310 serial_line.set_radio_frequency(frequency,
313 radio_calibration.get());
315 serial_line.stop_remote();
316 serial_line.set_radio_frequency(frequency);
317 AltosUIPreferences.set_frequency(device.getSerial(), frequency);
318 serial_line.start_remote();
320 serial_line.printf("c c %s\n", callsign.get());
321 if (flight_log_max.get() != 0)
322 serial_line.printf("c l %d\n", flight_log_max.get());
323 if (radio_enable.get() >= 0)
324 serial_line.printf("c e %d\n", radio_enable.get());
325 if (ignite_mode.get() >= 0)
326 serial_line.printf("c i %d\n", ignite_mode.get());
327 if (pad_orientation.get() >= 0)
328 serial_line.printf("c o %d\n", pad_orientation.get());
329 if (pyros.length > 0) {
330 for (int p = 0; p < pyros.length; p++) {
331 serial_line.printf("c P %s\n",
332 pyros[p].toString());
335 serial_line.printf("c w\n");
336 } catch (InterruptedException ie) {
337 } catch (TimeoutException te) {
341 } catch (InterruptedException ie) {
349 serial_line.printf("r eboot\n");
350 serial_line.flush_output();
351 } catch (InterruptedException ie) {
352 } catch (TimeoutException te) {
356 } catch (InterruptedException ie) {
363 switch (serial_mode) {
364 case serial_mode_save:
366 /* fall through ... */
367 case serial_mode_read:
370 case serial_mode_reboot:
376 public SerialData(AltosConfig in_config, int in_serial_mode) {
378 serial_mode = in_serial_mode;
382 void run_serial_thread(int serial_mode) {
383 SerialData sd = new SerialData(this, serial_mode);
384 Thread st = new Thread(sd);
388 void init_ui () throws InterruptedException, TimeoutException {
389 config_ui = new AltosConfigUI(owner, remote);
390 config_ui.addActionListener(this);
391 serial_line.set_frame(owner);
396 if (serial_line != null) {
400 JOptionPane.showMessageDialog(owner,
401 String.format("Connection to \"%s\" failed",
402 device.toShortString()),
404 JOptionPane.ERROR_MESSAGE);
405 config_ui.setVisible(false);
408 void set_ui() throws InterruptedException, TimeoutException {
409 if (serial_line != null)
410 run_serial_thread(serial_mode_read);
416 return AltosConvert.radio_to_frequency(radio_frequency.get(),
418 radio_calibration.get(),
419 radio_channel.get());
422 void set_frequency(double freq) {
423 int frequency = radio_frequency.get();
424 int setting = radio_setting.get();
427 radio_frequency.set((int) Math.floor (freq * 1000 + 0.5));
428 radio_channel.set(0);
429 } else if (setting > 0) {
430 radio_setting.set(AltosConvert.radio_frequency_to_setting(freq,
431 radio_calibration.get()));
432 radio_channel.set(0);
434 radio_channel.set(AltosConvert.radio_frequency_to_channel(freq));
440 /* bounds check stuff */
441 if (config_ui.flight_log_max() > log_limit()) {
442 JOptionPane.showMessageDialog(owner,
443 String.format("Requested flight log, %dk, is larger than the available space, %dk.\n",
444 config_ui.flight_log_max(),
446 "Maximum Flight Log Too Large",
447 JOptionPane.ERROR_MESSAGE);
451 main_deploy.set(config_ui.main_deploy());
452 apogee_delay.set(config_ui.apogee_delay());
453 apogee_lockout.set(config_ui.apogee_lockout());
454 radio_calibration.set(config_ui.radio_calibration());
455 set_frequency(config_ui.radio_frequency());
456 flight_log_max.set(config_ui.flight_log_max());
457 if (radio_enable.get() >= 0)
458 radio_enable.set(config_ui.radio_enable());
459 if (ignite_mode.get() >= 0)
460 ignite_mode.set(config_ui.ignite_mode());
461 if (pad_orientation.get() >= 0)
462 pad_orientation.set(config_ui.pad_orientation());
463 callsign.set(config_ui.callsign());
464 if (npyro.get() > 0) {
465 pyros = config_ui.pyros();
467 run_serial_thread(serial_mode_save);
470 public void actionPerformed(ActionEvent e) {
471 String cmd = e.getActionCommand();
473 if (cmd.equals("Save")) {
475 } else if (cmd.equals("Reset")) {
477 } else if (cmd.equals("Reboot")) {
478 if (serial_line != null)
479 run_serial_thread(serial_mode_reboot);
480 } else if (cmd.equals("Close")) {
481 if (serial_line != null)
484 } catch (InterruptedException ie) {
486 } catch (TimeoutException te) {
491 public AltosConfig(JFrame given_owner) {
494 serial = new int_ref(0);
495 log_format = new int_ref(Altos.AO_LOG_FORMAT_UNKNOWN);
496 main_deploy = new int_ref(250);
497 apogee_delay = new int_ref(0);
498 apogee_lockout = new int_ref(0);
499 radio_channel = new int_ref(0);
500 radio_setting = new int_ref(0);
501 radio_frequency = new int_ref(0);
502 radio_calibration = new int_ref(1186611);
503 radio_enable = new int_ref(-1);
504 flight_log_max = new int_ref(0);
505 ignite_mode = new int_ref(-1);
506 pad_orientation = new int_ref(-1);
507 storage_size = new int_ref(-1);
508 storage_erase_unit = new int_ref(-1);
509 stored_flight = new int_ref(-1);
510 callsign = new string_ref("N0CALL");
511 version = new string_ref("unknown");
512 product = new string_ref("unknown");
513 npyro = new int_ref(0);
515 device = AltosDeviceDialog.show(owner, Altos.product_any);
516 if (device != null) {
518 serial_line = new AltosSerial(device);
520 if (device.matchProduct(Altos.product_basestation))
523 } catch (InterruptedException ie) {
525 } catch (TimeoutException te) {
528 } catch (FileNotFoundException ee) {
529 JOptionPane.showMessageDialog(owner,
531 "Cannot open target device",
532 JOptionPane.ERROR_MESSAGE);
533 } catch (AltosSerialInUseException si) {
534 JOptionPane.showMessageDialog(owner,
535 String.format("Device \"%s\" already in use",
536 device.toShortString()),
538 JOptionPane.ERROR_MESSAGE);