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 radio_channel;
74 int_ref radio_calibration;
75 int_ref flight_log_max;
77 int_ref pad_orientation;
78 int_ref radio_setting;
80 int_ref storage_erase_unit;
81 int_ref stored_flight;
86 AltosConfigUI config_ui;
87 boolean serial_started;
89 boolean get_int(String line, String label, int_ref x) {
90 if (line.startsWith(label)) {
92 String tail = line.substring(label.length()).trim();
93 String[] tokens = tail.split("\\s+");
94 if (tokens.length > 0) {
95 int i = Integer.parseInt(tokens[0]);
99 } catch (NumberFormatException ne) {
105 boolean get_string(String line, String label, string_ref s) {
106 if (line.startsWith(label)) {
107 String quoted = line.substring(label.length()).trim();
109 if (quoted.startsWith("\""))
110 quoted = quoted.substring(1);
111 if (quoted.endsWith("\""))
112 quoted = quoted.substring(0,quoted.length()-1);
120 void start_serial() throws InterruptedException, TimeoutException {
121 serial_started = true;
123 serial_line.start_remote();
126 void stop_serial() throws InterruptedException {
129 serial_started = false;
131 serial_line.stop_remote();
135 if (storage_size.get() > 0 && storage_erase_unit.get() > 0) {
136 int log_limit = storage_size.get() - storage_erase_unit.get();
138 return log_limit / 1024;
144 config_ui.set_serial(serial.get());
145 config_ui.set_product(product.get());
146 config_ui.set_version(version.get());
147 config_ui.set_main_deploy(main_deploy.get());
148 config_ui.set_apogee_delay(apogee_delay.get());
149 config_ui.set_radio_calibration(radio_calibration.get());
150 config_ui.set_radio_frequency(frequency());
151 config_ui.set_flight_log_max_enabled(stored_flight.get() < 0);
152 config_ui.set_radio_enable(radio_enable.get());
153 config_ui.set_flight_log_max_limit(log_limit());
154 config_ui.set_flight_log_max(flight_log_max.get());
155 config_ui.set_ignite_mode(ignite_mode.get());
156 config_ui.set_pad_orientation(pad_orientation.get());
157 config_ui.set_callsign(callsign.get());
158 config_ui.set_clean();
159 config_ui.make_visible();
162 void process_line(String line) {
167 if (line.equals("done")) {
168 if (serial_line != null)
172 get_int(line, "serial-number", serial);
173 get_int(line, "log-format", log_format);
174 get_int(line, "Main deploy:", main_deploy);
175 get_int(line, "Apogee delay:", apogee_delay);
176 get_int(line, "Radio channel:", radio_channel);
177 get_int(line, "Radio cal:", radio_calibration);
178 get_int(line, "Max flight log:", flight_log_max);
179 get_int(line, "Ignite mode:", ignite_mode);
180 get_int(line, "Pad orientation:", pad_orientation);
181 get_int(line, "Radio setting:", radio_setting);
182 get_int(line, "Radio enable:", radio_enable);
183 get_int(line, "Storage size:", storage_size);
184 get_int(line, "Storage erase unit:", storage_erase_unit);
185 get_int(line, "flight", stored_flight);
186 get_string(line, "Callsign:", callsign);
187 get_string(line,"software-version", version);
188 get_string(line,"product", product);
191 final static int serial_mode_read = 0;
192 final static int serial_mode_save = 1;
193 final static int serial_mode_reboot = 2;
195 class SerialData implements Runnable {
199 void process_line(String line) {
200 config.process_line(line);
202 void callback(String in_line) {
203 final String line = in_line;
204 Runnable r = new Runnable() {
209 SwingUtilities.invokeLater(r);
214 config.start_serial();
215 stored_flight.set(-1);
216 config.serial_line.printf("c s\nf\nl\nv\n");
219 String line = config.serial_line.get_reply(5000);
223 if (line.startsWith("software-version"))
225 } catch (Exception e) {
229 } catch (InterruptedException ie) {
230 } catch (TimeoutException te) {
234 } catch (InterruptedException ie) {
242 double frequency = frequency();
243 boolean has_setting = radio_setting.get() > 0;
245 serial_line.printf("c m %d\n", main_deploy.get());
246 serial_line.printf("c d %d\n", apogee_delay.get());
248 serial_line.printf("c f %d\n", radio_calibration.get());
249 serial_line.set_radio_frequency(frequency,
251 radio_calibration.get());
253 serial_line.stop_remote();
254 serial_line.set_radio_frequency(frequency);
255 AltosPreferences.set_frequency(device.getSerial(), frequency);
256 serial_line.start_remote();
258 serial_line.printf("c c %s\n", callsign.get());
259 if (flight_log_max.get() != 0)
260 serial_line.printf("c l %d\n", flight_log_max.get());
261 if (radio_enable.get() >= 0)
262 serial_line.printf("c e %d\n", radio_enable.get());
263 if (ignite_mode.get() >= 0)
264 serial_line.printf("c i %d\n", ignite_mode.get());
265 if (pad_orientation.get() >= 0)
266 serial_line.printf("c o %d\n", pad_orientation.get());
267 serial_line.printf("c w\n");
268 } catch (InterruptedException ie) {
269 } catch (TimeoutException te) {
273 } catch (InterruptedException ie) {
281 serial_line.printf("r eboot\n");
282 serial_line.flush_output();
283 } catch (InterruptedException ie) {
284 } catch (TimeoutException te) {
288 } catch (InterruptedException ie) {
295 switch (serial_mode) {
296 case serial_mode_save:
298 /* fall through ... */
299 case serial_mode_read:
302 case serial_mode_reboot:
308 public SerialData(AltosConfig in_config, int in_serial_mode) {
310 serial_mode = in_serial_mode;
314 void run_serial_thread(int serial_mode) {
315 SerialData sd = new SerialData(this, serial_mode);
316 Thread st = new Thread(sd);
320 void init_ui () throws InterruptedException, TimeoutException {
321 config_ui = new AltosConfigUI(owner, remote);
322 config_ui.addActionListener(this);
323 serial_line.set_frame(owner);
330 JOptionPane.showMessageDialog(owner,
331 String.format("Connection to \"%s\" failed",
332 device.toShortString()),
334 JOptionPane.ERROR_MESSAGE);
335 config_ui.setVisible(false);
338 void set_ui() throws InterruptedException, TimeoutException {
339 if (serial_line != null)
340 run_serial_thread(serial_mode_read);
346 return AltosConvert.radio_to_frequency(radio_setting.get(),
347 radio_calibration.get(),
348 radio_channel.get());
351 void set_frequency(double freq) {
352 int setting = radio_setting.get();
355 radio_setting.set(AltosConvert.radio_frequency_to_setting(freq,
356 radio_calibration.get()));
357 radio_channel.set(0);
359 radio_channel.set(AltosConvert.radio_frequency_to_channel(freq));
365 /* bounds check stuff */
366 if (config_ui.flight_log_max() > log_limit()) {
367 JOptionPane.showMessageDialog(owner,
368 String.format("Requested flight log, %dk, is larger than the available space, %dk.\n",
369 config_ui.flight_log_max(),
371 "Maximum Flight Log Too Large",
372 JOptionPane.ERROR_MESSAGE);
376 main_deploy.set(config_ui.main_deploy());
377 apogee_delay.set(config_ui.apogee_delay());
378 radio_calibration.set(config_ui.radio_calibration());
379 set_frequency(config_ui.radio_frequency());
380 flight_log_max.set(config_ui.flight_log_max());
381 if (radio_enable.get() >= 0)
382 radio_enable.set(config_ui.radio_enable());
383 if (ignite_mode.get() >= 0)
384 ignite_mode.set(config_ui.ignite_mode());
385 if (pad_orientation.get() >= 0)
386 pad_orientation.set(config_ui.pad_orientation());
387 callsign.set(config_ui.callsign());
388 run_serial_thread(serial_mode_save);
391 public void actionPerformed(ActionEvent e) {
392 String cmd = e.getActionCommand();
394 if (cmd.equals("Save")) {
396 } else if (cmd.equals("Reset")) {
398 } else if (cmd.equals("Reboot")) {
399 if (serial_line != null)
400 run_serial_thread(serial_mode_reboot);
401 } else if (cmd.equals("Close")) {
402 if (serial_line != null)
405 } catch (InterruptedException ie) {
407 } catch (TimeoutException te) {
412 public AltosConfig(JFrame given_owner) {
415 serial = new int_ref(0);
416 log_format = new int_ref(Altos.AO_LOG_FORMAT_UNKNOWN);
417 main_deploy = new int_ref(250);
418 apogee_delay = new int_ref(0);
419 radio_channel = new int_ref(0);
420 radio_setting = new int_ref(0);
421 radio_calibration = new int_ref(1186611);
422 radio_enable = new int_ref(-1);
423 flight_log_max = new int_ref(0);
424 ignite_mode = new int_ref(-1);
425 pad_orientation = new int_ref(-1);
426 storage_size = new int_ref(-1);
427 storage_erase_unit = new int_ref(-1);
428 stored_flight = new int_ref(-1);
429 callsign = new string_ref("N0CALL");
430 version = new string_ref("unknown");
431 product = new string_ref("unknown");
433 device = AltosDeviceDialog.show(owner, Altos.product_any);
434 if (device != null) {
436 serial_line = new AltosSerial(device);
438 if (!device.matchProduct(Altos.product_telemetrum))
441 } catch (InterruptedException ie) {
443 } catch (TimeoutException te) {
446 } catch (FileNotFoundException ee) {
447 JOptionPane.showMessageDialog(owner,
448 String.format("Cannot open device \"%s\"",
449 device.toShortString()),
450 "Cannot open target device",
451 JOptionPane.ERROR_MESSAGE);
452 } catch (AltosSerialInUseException si) {
453 JOptionPane.showMessageDialog(owner,
454 String.format("Device \"%s\" already in use",
455 device.toShortString()),
457 JOptionPane.ERROR_MESSAGE);
458 } catch (IOException ee) {
459 JOptionPane.showMessageDialog(owner,
460 device.toShortString(),
461 ee.getLocalizedMessage(),
462 JOptionPane.ERROR_MESSAGE);