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;
82 AltosConfigUI config_ui;
83 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 config_ui.set_serial(serial.get());
132 config_ui.set_product(product.get());
133 config_ui.set_version(version.get());
134 config_ui.set_main_deploy(main_deploy.get());
135 config_ui.set_apogee_delay(apogee_delay.get());
136 config_ui.set_radio_calibration(radio_calibration.get());
137 config_ui.set_radio_frequency(frequency());
138 config_ui.set_flight_log_max(flight_log_max.get());
139 config_ui.set_ignite_mode(ignite_mode.get());
140 config_ui.set_pad_orientation(pad_orientation.get());
141 config_ui.set_callsign(callsign.get());
142 config_ui.set_clean();
143 config_ui.make_visible();
146 void process_line(String line) {
151 if (line.equals("done")) {
152 if (serial_line != null)
156 get_int(line, "serial-number", serial);
157 get_int(line, "log-format", log_format);
158 get_int(line, "Main deploy:", main_deploy);
159 get_int(line, "Apogee delay:", apogee_delay);
160 get_int(line, "Radio channel:", radio_channel);
161 get_int(line, "Radio cal:", radio_calibration);
162 get_int(line, "Max flight log:", flight_log_max);
163 get_int(line, "Ignite mode:", ignite_mode);
164 get_int(line, "Pad orientation:", pad_orientation);
165 get_int(line, "Radio setting:", radio_setting);
166 get_string(line, "Callsign:", callsign);
167 get_string(line,"software-version", version);
168 get_string(line,"product", product);
171 final static int serial_mode_read = 0;
172 final static int serial_mode_save = 1;
173 final static int serial_mode_reboot = 2;
175 class SerialData implements Runnable {
179 void process_line(String line) {
180 config.process_line(line);
182 void callback(String in_line) {
183 final String line = in_line;
184 Runnable r = new Runnable() {
189 SwingUtilities.invokeLater(r);
194 config.start_serial();
195 config.serial_line.printf("c s\nv\n");
198 String line = config.serial_line.get_reply(5000);
202 if (line.startsWith("software-version"))
204 } catch (Exception e) {
208 } catch (InterruptedException ie) {
209 } catch (TimeoutException te) {
213 } catch (InterruptedException ie) {
221 double frequency = frequency();
222 boolean has_setting = radio_setting.get() > 0;
224 serial_line.printf("c m %d\n", main_deploy.get());
225 serial_line.printf("c d %d\n", apogee_delay.get());
227 serial_line.printf("c f %d\n", radio_calibration.get());
228 serial_line.set_radio_frequency(frequency,
230 radio_calibration.get());
232 serial_line.stop_remote();
233 serial_line.set_radio_frequency(frequency,
235 radio_calibration.get());
236 AltosPreferences.set_frequency(device.getSerial(), frequency);
237 serial_line.start_remote();
239 serial_line.printf("c c %s\n", callsign.get());
240 if (flight_log_max.get() != 0)
241 serial_line.printf("c l %d\n", flight_log_max.get());
242 if (ignite_mode.get() >= 0)
243 serial_line.printf("c i %d\n", ignite_mode.get());
244 if (pad_orientation.get() >= 0)
245 serial_line.printf("c o %d\n", pad_orientation.get());
246 serial_line.printf("c w\n");
247 } catch (InterruptedException ie) {
248 } catch (TimeoutException te) {
252 } catch (InterruptedException ie) {
260 serial_line.printf("r eboot\n");
261 serial_line.flush_output();
262 } catch (InterruptedException ie) {
263 } catch (TimeoutException te) {
267 } catch (InterruptedException ie) {
274 switch (serial_mode) {
275 case serial_mode_save:
277 /* fall through ... */
278 case serial_mode_read:
281 case serial_mode_reboot:
287 public SerialData(AltosConfig in_config, int in_serial_mode) {
289 serial_mode = in_serial_mode;
293 void run_serial_thread(int serial_mode) {
294 SerialData sd = new SerialData(this, serial_mode);
295 Thread st = new Thread(sd);
299 void init_ui () throws InterruptedException, TimeoutException {
300 config_ui = new AltosConfigUI(owner, remote);
301 config_ui.addActionListener(this);
302 serial_line.set_frame(owner);
309 JOptionPane.showMessageDialog(owner,
310 String.format("Connection to \"%s\" failed",
311 device.toShortString()),
313 JOptionPane.ERROR_MESSAGE);
314 config_ui.setVisible(false);
317 void set_ui() throws InterruptedException, TimeoutException {
318 if (serial_line != null)
319 run_serial_thread(serial_mode_read);
325 return AltosConvert.radio_to_frequency(radio_setting.get(),
326 radio_calibration.get(),
327 radio_channel.get());
330 void set_frequency(double freq) {
331 int setting = radio_setting.get();
334 radio_setting.set(AltosConvert.radio_frequency_to_setting(freq,
335 radio_calibration.get()));
336 radio_channel.set(0);
338 radio_channel.set(AltosConvert.radio_frequency_to_channel(freq));
343 main_deploy.set(config_ui.main_deploy());
344 apogee_delay.set(config_ui.apogee_delay());
345 radio_calibration.set(config_ui.radio_calibration());
346 set_frequency(config_ui.radio_frequency());
347 flight_log_max.set(config_ui.flight_log_max());
348 ignite_mode.set(config_ui.ignite_mode());
349 pad_orientation.set(config_ui.pad_orientation());
350 callsign.set(config_ui.callsign());
351 run_serial_thread(serial_mode_save);
354 public void actionPerformed(ActionEvent e) {
355 String cmd = e.getActionCommand();
357 if (cmd.equals("Save")) {
359 } else if (cmd.equals("Reset")) {
361 } else if (cmd.equals("Reboot")) {
362 if (serial_line != null)
363 run_serial_thread(serial_mode_reboot);
364 } else if (cmd.equals("Close")) {
365 if (serial_line != null)
368 } catch (InterruptedException ie) {
370 } catch (TimeoutException te) {
375 public AltosConfig(JFrame given_owner) {
378 serial = new int_ref(0);
379 log_format = new int_ref(Altos.AO_LOG_FORMAT_UNKNOWN);
380 main_deploy = new int_ref(250);
381 apogee_delay = new int_ref(0);
382 radio_channel = new int_ref(0);
383 radio_setting = new int_ref(0);
384 radio_calibration = new int_ref(1186611);
385 flight_log_max = new int_ref(0);
386 ignite_mode = new int_ref(-1);
387 pad_orientation = new int_ref(-1);
388 callsign = new string_ref("N0CALL");
389 version = new string_ref("unknown");
390 product = new string_ref("unknown");
392 device = AltosDeviceDialog.show(owner, Altos.product_any);
393 if (device != null) {
395 serial_line = new AltosSerial(device);
397 if (!device.matchProduct(Altos.product_telemetrum))
400 } catch (InterruptedException ie) {
402 } catch (TimeoutException te) {
405 } catch (FileNotFoundException ee) {
406 JOptionPane.showMessageDialog(owner,
407 String.format("Cannot open device \"%s\"",
408 device.toShortString()),
409 "Cannot open target device",
410 JOptionPane.ERROR_MESSAGE);
411 } catch (AltosSerialInUseException si) {
412 JOptionPane.showMessageDialog(owner,
413 String.format("Device \"%s\" already in use",
414 device.toShortString()),
416 JOptionPane.ERROR_MESSAGE);
417 } catch (IOException ee) {
418 JOptionPane.showMessageDialog(owner,
419 device.toShortString(),
420 ee.getLocalizedMessage(),
421 JOptionPane.ERROR_MESSAGE);