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;
85 AltosConfigUI config_ui;
86 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_radio_calibration(radio_calibration.get());
149 config_ui.set_radio_frequency(frequency());
150 config_ui.set_flight_log_max_enabled(stored_flight.get() < 0);
151 config_ui.set_flight_log_max_limit(log_limit());
152 config_ui.set_flight_log_max(flight_log_max.get());
153 config_ui.set_ignite_mode(ignite_mode.get());
154 config_ui.set_pad_orientation(pad_orientation.get());
155 config_ui.set_callsign(callsign.get());
156 config_ui.set_clean();
157 config_ui.make_visible();
160 void process_line(String line) {
165 if (line.equals("done")) {
166 if (serial_line != null)
170 get_int(line, "serial-number", serial);
171 get_int(line, "log-format", log_format);
172 get_int(line, "Main deploy:", main_deploy);
173 get_int(line, "Apogee delay:", apogee_delay);
174 get_int(line, "Radio channel:", radio_channel);
175 get_int(line, "Radio cal:", radio_calibration);
176 get_int(line, "Max flight log:", flight_log_max);
177 get_int(line, "Ignite mode:", ignite_mode);
178 get_int(line, "Pad orientation:", pad_orientation);
179 get_int(line, "Radio setting:", radio_setting);
180 get_int(line, "Storage size:", storage_size);
181 get_int(line, "Storage erase unit:", storage_erase_unit);
182 get_int(line, "flight", stored_flight);
183 get_string(line, "Callsign:", callsign);
184 get_string(line,"software-version", version);
185 get_string(line,"product", product);
188 final static int serial_mode_read = 0;
189 final static int serial_mode_save = 1;
190 final static int serial_mode_reboot = 2;
192 class SerialData implements Runnable {
196 void process_line(String line) {
197 config.process_line(line);
199 void callback(String in_line) {
200 final String line = in_line;
201 Runnable r = new Runnable() {
206 SwingUtilities.invokeLater(r);
211 config.start_serial();
212 stored_flight.set(-1);
213 config.serial_line.printf("c s\nf\nl\nv\n");
216 String line = config.serial_line.get_reply(5000);
220 if (line.startsWith("software-version"))
222 } catch (Exception e) {
226 } catch (InterruptedException ie) {
227 } catch (TimeoutException te) {
231 } catch (InterruptedException ie) {
239 double frequency = frequency();
240 boolean has_setting = radio_setting.get() > 0;
242 serial_line.printf("c m %d\n", main_deploy.get());
243 serial_line.printf("c d %d\n", apogee_delay.get());
245 serial_line.printf("c f %d\n", radio_calibration.get());
246 serial_line.set_radio_frequency(frequency,
248 radio_calibration.get());
250 serial_line.stop_remote();
251 serial_line.set_radio_frequency(frequency);
252 AltosPreferences.set_frequency(device.getSerial(), frequency);
253 serial_line.start_remote();
255 serial_line.printf("c c %s\n", callsign.get());
256 if (flight_log_max.get() != 0)
257 serial_line.printf("c l %d\n", flight_log_max.get());
258 if (ignite_mode.get() >= 0)
259 serial_line.printf("c i %d\n", ignite_mode.get());
260 if (pad_orientation.get() >= 0)
261 serial_line.printf("c o %d\n", pad_orientation.get());
262 serial_line.printf("c w\n");
263 } catch (InterruptedException ie) {
264 } catch (TimeoutException te) {
268 } catch (InterruptedException ie) {
276 serial_line.printf("r eboot\n");
277 serial_line.flush_output();
278 } catch (InterruptedException ie) {
279 } catch (TimeoutException te) {
283 } catch (InterruptedException ie) {
290 switch (serial_mode) {
291 case serial_mode_save:
293 /* fall through ... */
294 case serial_mode_read:
297 case serial_mode_reboot:
303 public SerialData(AltosConfig in_config, int in_serial_mode) {
305 serial_mode = in_serial_mode;
309 void run_serial_thread(int serial_mode) {
310 SerialData sd = new SerialData(this, serial_mode);
311 Thread st = new Thread(sd);
315 void init_ui () throws InterruptedException, TimeoutException {
316 config_ui = new AltosConfigUI(owner, remote);
317 config_ui.addActionListener(this);
318 serial_line.set_frame(owner);
325 JOptionPane.showMessageDialog(owner,
326 String.format("Connection to \"%s\" failed",
327 device.toShortString()),
329 JOptionPane.ERROR_MESSAGE);
330 config_ui.setVisible(false);
333 void set_ui() throws InterruptedException, TimeoutException {
334 if (serial_line != null)
335 run_serial_thread(serial_mode_read);
341 return AltosConvert.radio_to_frequency(radio_setting.get(),
342 radio_calibration.get(),
343 radio_channel.get());
346 void set_frequency(double freq) {
347 int setting = radio_setting.get();
350 radio_setting.set(AltosConvert.radio_frequency_to_setting(freq,
351 radio_calibration.get()));
352 radio_channel.set(0);
354 radio_channel.set(AltosConvert.radio_frequency_to_channel(freq));
360 /* bounds check stuff */
361 if (config_ui.flight_log_max() > log_limit()) {
362 JOptionPane.showMessageDialog(owner,
363 String.format("Requested flight log, %dk, is larger than the available space, %dk.\n",
364 config_ui.flight_log_max(),
366 "Maximum Flight Log Too Large",
367 JOptionPane.ERROR_MESSAGE);
371 main_deploy.set(config_ui.main_deploy());
372 apogee_delay.set(config_ui.apogee_delay());
373 radio_calibration.set(config_ui.radio_calibration());
374 set_frequency(config_ui.radio_frequency());
375 flight_log_max.set(config_ui.flight_log_max());
376 if (ignite_mode.get() >= 0)
377 ignite_mode.set(config_ui.ignite_mode());
378 if (pad_orientation.get() >= 0)
379 pad_orientation.set(config_ui.pad_orientation());
380 callsign.set(config_ui.callsign());
381 run_serial_thread(serial_mode_save);
384 public void actionPerformed(ActionEvent e) {
385 String cmd = e.getActionCommand();
387 if (cmd.equals("Save")) {
389 } else if (cmd.equals("Reset")) {
391 } else if (cmd.equals("Reboot")) {
392 if (serial_line != null)
393 run_serial_thread(serial_mode_reboot);
394 } else if (cmd.equals("Close")) {
395 if (serial_line != null)
398 } catch (InterruptedException ie) {
400 } catch (TimeoutException te) {
405 public AltosConfig(JFrame given_owner) {
408 serial = new int_ref(0);
409 log_format = new int_ref(Altos.AO_LOG_FORMAT_UNKNOWN);
410 main_deploy = new int_ref(250);
411 apogee_delay = new int_ref(0);
412 radio_channel = new int_ref(0);
413 radio_setting = new int_ref(0);
414 radio_calibration = new int_ref(1186611);
415 flight_log_max = new int_ref(0);
416 ignite_mode = new int_ref(-1);
417 pad_orientation = new int_ref(-1);
418 storage_size = new int_ref(-1);
419 storage_erase_unit = new int_ref(-1);
420 stored_flight = new int_ref(-1);
421 callsign = new string_ref("N0CALL");
422 version = new string_ref("unknown");
423 product = new string_ref("unknown");
425 device = AltosDeviceDialog.show(owner, Altos.product_any);
426 if (device != null) {
428 serial_line = new AltosSerial(device);
430 if (!device.matchProduct(Altos.product_telemetrum))
433 } catch (InterruptedException ie) {
435 } catch (TimeoutException te) {
438 } catch (FileNotFoundException ee) {
439 JOptionPane.showMessageDialog(owner,
440 String.format("Cannot open device \"%s\"",
441 device.toShortString()),
442 "Cannot open target device",
443 JOptionPane.ERROR_MESSAGE);
444 } catch (AltosSerialInUseException si) {
445 JOptionPane.showMessageDialog(owner,
446 String.format("Device \"%s\" already in use",
447 device.toShortString()),
449 JOptionPane.ERROR_MESSAGE);
450 } catch (IOException ee) {
451 JOptionPane.showMessageDialog(owner,
452 device.toShortString(),
453 ee.getLocalizedMessage(),
454 JOptionPane.ERROR_MESSAGE);