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.*;
26 public class AltosConfig implements ActionListener {
34 public void set(int i) {
37 public int_ref(int i) {
48 public void set(String i) {
51 public string_ref(String i) {
58 AltosSerial serial_line;
60 AltosConfigData remote_config_data;
61 double remote_frequency;
66 int_ref apogee_lockout;
67 int_ref radio_channel;
68 int_ref radio_calibration;
69 int_ref flight_log_max;
71 int_ref pad_orientation;
72 int_ref radio_setting;
73 int_ref radio_frequency;
75 int_ref storage_erase_unit;
76 int_ref stored_flight;
81 AltosConfigUI config_ui;
82 boolean serial_started;
85 boolean get_int(String line, String label, int_ref x) {
86 if (line.startsWith(label)) {
88 String tail = line.substring(label.length()).trim();
89 String[] tokens = tail.split("\\s+");
90 if (tokens.length > 0) {
91 int i = Integer.parseInt(tokens[0]);
95 } catch (NumberFormatException ne) {
101 boolean get_string(String line, String label, string_ref s) {
102 if (line.startsWith(label)) {
103 String quoted = line.substring(label.length()).trim();
105 if (quoted.startsWith("\""))
106 quoted = quoted.substring(1);
107 if (quoted.endsWith("\""))
108 quoted = quoted.substring(0,quoted.length()-1);
116 void start_serial() throws InterruptedException, TimeoutException {
117 serial_started = true;
119 serial_line.start_remote();
122 void stop_serial() throws InterruptedException {
125 serial_started = false;
127 serial_line.stop_remote();
131 if (storage_size.get() > 0 && storage_erase_unit.get() > 0) {
132 int log_limit = storage_size.get() - storage_erase_unit.get();
134 return log_limit / 1024;
140 config_ui.set_serial(serial.get());
141 config_ui.set_product(product.get());
142 config_ui.set_version(version.get());
143 config_ui.set_main_deploy(main_deploy.get());
144 config_ui.set_apogee_delay(apogee_delay.get());
145 config_ui.set_apogee_lockout(apogee_lockout.get());
146 config_ui.set_radio_calibration(radio_calibration.get());
147 config_ui.set_radio_frequency(frequency());
148 boolean max_enabled = true;
149 switch (log_format.get()) {
150 case Altos.AO_LOG_FORMAT_TINY:
154 if (stored_flight.get() >= 0)
158 config_ui.set_flight_log_max_enabled(max_enabled);
159 config_ui.set_radio_enable(radio_enable.get());
160 config_ui.set_flight_log_max_limit(log_limit());
161 config_ui.set_flight_log_max(flight_log_max.get());
162 config_ui.set_ignite_mode(ignite_mode.get());
163 config_ui.set_pad_orientation(pad_orientation.get());
164 config_ui.set_callsign(callsign.get());
165 config_ui.set_clean();
168 config_ui.make_visible();
172 void process_line(String line) {
177 if (line.equals("all finished")) {
178 if (serial_line != null)
182 get_int(line, "serial-number", serial);
183 get_int(line, "log-format", log_format);
184 get_int(line, "Main deploy:", main_deploy);
185 get_int(line, "Apogee delay:", apogee_delay);
186 get_int(line, "Apogee lockout:", apogee_lockout);
187 get_int(line, "Radio channel:", radio_channel);
188 get_int(line, "Radio cal:", radio_calibration);
189 get_int(line, "Max flight log:", flight_log_max);
190 get_int(line, "Ignite mode:", ignite_mode);
191 get_int(line, "Pad orientation:", pad_orientation);
192 get_int(line, "Radio setting:", radio_setting);
193 if (get_int(line, "Frequency:", radio_frequency))
194 if (radio_frequency.get() < 0)
195 radio_frequency.set(434550);
196 get_int(line, "Radio enable:", radio_enable);
197 get_int(line, "Storage size:", storage_size);
198 get_int(line, "Storage erase unit:", storage_erase_unit);
199 get_int(line, "flight", stored_flight);
200 get_string(line, "Callsign:", callsign);
201 get_string(line,"software-version", version);
202 get_string(line,"product", product);
205 final static int serial_mode_read = 0;
206 final static int serial_mode_save = 1;
207 final static int serial_mode_reboot = 2;
209 class SerialData implements Runnable {
213 void process_line(String line) {
214 config.process_line(line);
216 void callback(String in_line) {
217 final String line = in_line;
218 Runnable r = new Runnable() {
223 SwingUtilities.invokeLater(r);
228 log_format.set(Altos.AO_LOG_FORMAT_UNKNOWN);
229 main_deploy.set(250);
231 apogee_lockout.set(0);
232 radio_channel.set(0);
233 radio_setting.set(0);
234 radio_frequency.set(0);
235 radio_calibration.set(1186611);
236 radio_enable.set(-1);
237 flight_log_max.set(0);
239 pad_orientation.set(-1);
240 storage_size.set(-1);
241 storage_erase_unit.set(-1);
242 stored_flight.set(-1);
243 callsign.set("N0CALL");
244 version.set("unknown");
245 product.set("unknown");
250 config.start_serial();
253 config.serial_line.printf("c s\nf\nl\nv\n");
256 String line = config.serial_line.get_reply(5000);
260 if (line.startsWith("software-version"))
262 } catch (Exception e) {
266 } catch (InterruptedException ie) {
267 } catch (TimeoutException te) {
271 } catch (InterruptedException ie) {
274 callback("all finished");
279 double frequency = frequency();
280 boolean has_frequency = radio_frequency.get() > 0;
281 boolean has_setting = radio_setting.get() > 0;
283 serial_line.printf("c m %d\n", main_deploy.get());
284 serial_line.printf("c d %d\n", apogee_delay.get());
285 serial_line.printf("c L %d\n", apogee_lockout.get());
287 serial_line.printf("c f %d\n", radio_calibration.get());
288 serial_line.set_radio_frequency(frequency,
291 radio_calibration.get());
293 serial_line.stop_remote();
294 serial_line.set_radio_frequency(frequency);
295 AltosUIPreferences.set_frequency(device.getSerial(), frequency);
296 serial_line.start_remote();
298 serial_line.printf("c c %s\n", callsign.get());
299 if (flight_log_max.get() != 0)
300 serial_line.printf("c l %d\n", flight_log_max.get());
301 if (radio_enable.get() >= 0)
302 serial_line.printf("c e %d\n", radio_enable.get());
303 if (ignite_mode.get() >= 0)
304 serial_line.printf("c i %d\n", ignite_mode.get());
305 if (pad_orientation.get() >= 0)
306 serial_line.printf("c o %d\n", pad_orientation.get());
307 serial_line.printf("c w\n");
308 } catch (InterruptedException ie) {
309 } catch (TimeoutException te) {
313 } catch (InterruptedException ie) {
321 serial_line.printf("r eboot\n");
322 serial_line.flush_output();
323 } catch (InterruptedException ie) {
324 } catch (TimeoutException te) {
328 } catch (InterruptedException ie) {
335 switch (serial_mode) {
336 case serial_mode_save:
338 /* fall through ... */
339 case serial_mode_read:
342 case serial_mode_reboot:
348 public SerialData(AltosConfig in_config, int in_serial_mode) {
350 serial_mode = in_serial_mode;
354 void run_serial_thread(int serial_mode) {
355 SerialData sd = new SerialData(this, serial_mode);
356 Thread st = new Thread(sd);
360 void init_ui () throws InterruptedException, TimeoutException {
361 config_ui = new AltosConfigUI(owner, remote);
362 config_ui.addActionListener(this);
363 serial_line.set_frame(owner);
370 JOptionPane.showMessageDialog(owner,
371 String.format("Connection to \"%s\" failed",
372 device.toShortString()),
374 JOptionPane.ERROR_MESSAGE);
375 config_ui.setVisible(false);
378 void set_ui() throws InterruptedException, TimeoutException {
379 if (serial_line != null)
380 run_serial_thread(serial_mode_read);
386 return AltosConvert.radio_to_frequency(radio_frequency.get(),
388 radio_calibration.get(),
389 radio_channel.get());
392 void set_frequency(double freq) {
393 int frequency = radio_frequency.get();
394 int setting = radio_setting.get();
397 radio_frequency.set((int) Math.floor (freq * 1000 + 0.5));
398 radio_channel.set(0);
399 } else if (setting > 0) {
400 radio_setting.set(AltosConvert.radio_frequency_to_setting(freq,
401 radio_calibration.get()));
402 radio_channel.set(0);
404 radio_channel.set(AltosConvert.radio_frequency_to_channel(freq));
410 /* bounds check stuff */
411 if (config_ui.flight_log_max() > log_limit()) {
412 JOptionPane.showMessageDialog(owner,
413 String.format("Requested flight log, %dk, is larger than the available space, %dk.\n",
414 config_ui.flight_log_max(),
416 "Maximum Flight Log Too Large",
417 JOptionPane.ERROR_MESSAGE);
421 main_deploy.set(config_ui.main_deploy());
422 apogee_delay.set(config_ui.apogee_delay());
423 apogee_lockout.set(config_ui.apogee_lockout());
424 radio_calibration.set(config_ui.radio_calibration());
425 set_frequency(config_ui.radio_frequency());
426 flight_log_max.set(config_ui.flight_log_max());
427 if (radio_enable.get() >= 0)
428 radio_enable.set(config_ui.radio_enable());
429 if (ignite_mode.get() >= 0)
430 ignite_mode.set(config_ui.ignite_mode());
431 if (pad_orientation.get() >= 0)
432 pad_orientation.set(config_ui.pad_orientation());
433 callsign.set(config_ui.callsign());
434 run_serial_thread(serial_mode_save);
437 public void actionPerformed(ActionEvent e) {
438 String cmd = e.getActionCommand();
440 if (cmd.equals("Save")) {
442 } else if (cmd.equals("Reset")) {
444 } else if (cmd.equals("Reboot")) {
445 if (serial_line != null)
446 run_serial_thread(serial_mode_reboot);
447 } else if (cmd.equals("Close")) {
448 if (serial_line != null)
451 } catch (InterruptedException ie) {
453 } catch (TimeoutException te) {
458 public AltosConfig(JFrame given_owner) {
461 serial = new int_ref(0);
462 log_format = new int_ref(Altos.AO_LOG_FORMAT_UNKNOWN);
463 main_deploy = new int_ref(250);
464 apogee_delay = new int_ref(0);
465 apogee_lockout = 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_altimeter))
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);