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;
72 int_ref radio_channel;
73 int_ref radio_calibration;
74 int_ref flight_log_max;
76 int_ref pad_orientation;
77 int_ref radio_setting;
81 AltosConfigUI config_ui;
82 boolean serial_started;
84 boolean get_int(String line, String label, int_ref x) {
85 if (line.startsWith(label)) {
87 String tail = line.substring(label.length()).trim();
88 String[] tokens = tail.split("\\s+");
89 if (tokens.length > 0) {
90 int i = Integer.parseInt(tokens[0]);
94 } catch (NumberFormatException ne) {
100 boolean get_string(String line, String label, string_ref s) {
101 if (line.startsWith(label)) {
102 String quoted = line.substring(label.length()).trim();
104 if (quoted.startsWith("\""))
105 quoted = quoted.substring(1);
106 if (quoted.endsWith("\""))
107 quoted = quoted.substring(0,quoted.length()-1);
115 void start_serial() throws InterruptedException, TimeoutException {
116 serial_started = true;
118 serial_line.start_remote();
121 void stop_serial() throws InterruptedException {
124 serial_started = false;
126 serial_line.stop_remote();
130 config_ui.set_serial(serial.get());
131 config_ui.set_product(product.get());
132 config_ui.set_version(version.get());
133 config_ui.set_main_deploy(main_deploy.get());
134 config_ui.set_apogee_delay(apogee_delay.get());
135 config_ui.set_radio_frequency(frequency());
136 config_ui.set_radio_calibration(radio_calibration.get());
137 config_ui.set_flight_log_max(flight_log_max.get());
138 config_ui.set_ignite_mode(ignite_mode.get());
139 config_ui.set_pad_orientation(pad_orientation.get());
140 config_ui.set_callsign(callsign.get());
141 config_ui.set_radio_setting(radio_setting.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, "Main deploy:", main_deploy);
158 get_int(line, "Apogee delay:", apogee_delay);
159 get_int(line, "Radio channel:", radio_channel);
160 get_int(line, "Radio cal:", radio_calibration);
161 get_int(line, "Max flight log:", flight_log_max);
162 get_int(line, "Ignite mode:", ignite_mode);
163 get_int(line, "Pad orientation:", pad_orientation);
164 get_int(line, "Radio setting:", radio_setting);
165 get_string(line, "Callsign:", callsign);
166 get_string(line,"software-version", version);
167 get_string(line,"product", product);
170 final static int serial_mode_read = 0;
171 final static int serial_mode_save = 1;
172 final static int serial_mode_reboot = 2;
174 class SerialData implements Runnable {
178 void process_line(String line) {
179 config.process_line(line);
181 void callback(String in_line) {
182 final String line = in_line;
183 Runnable r = new Runnable() {
188 SwingUtilities.invokeLater(r);
193 config.start_serial();
194 config.serial_line.printf("c s\nv\n");
197 String line = config.serial_line.get_reply(5000);
201 if (line.startsWith("software-version"))
203 } catch (Exception e) {
207 } catch (InterruptedException ie) {
208 } catch (TimeoutException te) {
212 } catch (InterruptedException ie) {
220 double frequency = frequency();
221 boolean has_setting = radio_setting.get() != 0;
223 serial_line.printf("c m %d\n", main_deploy.get());
224 serial_line.printf("c d %d\n", apogee_delay.get());
225 serial_line.set_radio_frequency(frequency,
227 radio_calibration.get());
229 serial_line.stop_remote();
230 serial_line.set_radio_frequency(frequency,
232 radio_calibration.get());
233 AltosPreferences.set_frequency(device.getSerial(), frequency);
234 serial_line.start_remote();
237 serial_line.printf("c f %d\n", radio_calibration.get());
238 serial_line.printf("c c %s\n", callsign.get());
239 if (flight_log_max.get() != 0)
240 serial_line.printf("c l %d\n", flight_log_max.get());
241 if (ignite_mode.get() >= 0)
242 serial_line.printf("c i %d\n", ignite_mode.get());
243 if (pad_orientation.get() >= 0)
244 serial_line.printf("c o %d\n", pad_orientation.get());
245 serial_line.printf("c w\n");
246 } catch (InterruptedException ie) {
247 } catch (TimeoutException te) {
251 } catch (InterruptedException ie) {
259 serial_line.printf("r eboot\n");
260 serial_line.flush_output();
261 } catch (InterruptedException ie) {
262 } catch (TimeoutException te) {
266 } catch (InterruptedException ie) {
273 switch (serial_mode) {
274 case serial_mode_save:
276 /* fall through ... */
277 case serial_mode_read:
280 case serial_mode_reboot:
286 public SerialData(AltosConfig in_config, int in_serial_mode) {
288 serial_mode = in_serial_mode;
292 void run_serial_thread(int serial_mode) {
293 SerialData sd = new SerialData(this, serial_mode);
294 Thread st = new Thread(sd);
298 void init_ui () throws InterruptedException, TimeoutException {
299 config_ui = new AltosConfigUI(owner, remote);
300 config_ui.addActionListener(this);
301 serial_line.set_frame(owner);
308 JOptionPane.showMessageDialog(owner,
309 String.format("Connection to \"%s\" failed",
310 device.toShortString()),
312 JOptionPane.ERROR_MESSAGE);
313 config_ui.setVisible(false);
316 void set_ui() throws InterruptedException, TimeoutException {
317 if (serial_line != null)
318 run_serial_thread(serial_mode_read);
324 int setting = radio_setting.get();
327 return AltosConvert.radio_setting_to_frequency(setting, radio_calibration.get());
329 return AltosConvert.radio_channel_to_frequency(radio_channel.get());
332 void set_frequency(double freq) {
333 int setting = radio_setting.get();
336 radio_setting.set(AltosConvert.radio_frequency_to_setting(freq,
337 radio_calibration.get()));
338 radio_channel.set(0);
340 radio_channel.set(AltosConvert.radio_frequency_to_channel(freq));
345 main_deploy.set(config_ui.main_deploy());
346 apogee_delay.set(config_ui.apogee_delay());
347 radio_calibration.set(config_ui.radio_calibration());
348 set_frequency(config_ui.radio_frequency());
349 flight_log_max.set(config_ui.flight_log_max());
350 ignite_mode.set(config_ui.ignite_mode());
351 pad_orientation.set(config_ui.pad_orientation());
352 callsign.set(config_ui.callsign());
353 run_serial_thread(serial_mode_save);
356 public void actionPerformed(ActionEvent e) {
357 String cmd = e.getActionCommand();
359 if (cmd.equals("Save")) {
361 } else if (cmd.equals("Reset")) {
363 } else if (cmd.equals("Reboot")) {
364 if (serial_line != null)
365 run_serial_thread(serial_mode_reboot);
366 } else if (cmd.equals("Close")) {
367 if (serial_line != null)
370 } catch (InterruptedException ie) {
372 } catch (TimeoutException te) {
377 public AltosConfig(JFrame given_owner) {
380 serial = new int_ref(0);
381 main_deploy = new int_ref(250);
382 apogee_delay = new int_ref(0);
383 radio_channel = new int_ref(0);
384 radio_setting = new int_ref(0);
385 radio_calibration = new int_ref(1186611);
386 flight_log_max = new int_ref(0);
387 ignite_mode = new int_ref(-1);
388 pad_orientation = new int_ref(-1);
389 callsign = new string_ref("N0CALL");
390 version = new string_ref("unknown");
391 product = new string_ref("unknown");
393 device = AltosDeviceDialog.show(owner, Altos.product_any);
394 if (device != null) {
396 serial_line = new AltosSerial(device);
398 if (!device.matchProduct(Altos.product_telemetrum))
401 } catch (InterruptedException ie) {
403 } catch (TimeoutException te) {
406 } catch (FileNotFoundException ee) {
407 JOptionPane.showMessageDialog(owner,
408 String.format("Cannot open device \"%s\"",
409 device.toShortString()),
410 "Cannot open target device",
411 JOptionPane.ERROR_MESSAGE);
412 } catch (AltosSerialInUseException si) {
413 JOptionPane.showMessageDialog(owner,
414 String.format("Device \"%s\" already in use",
415 device.toShortString()),
417 JOptionPane.ERROR_MESSAGE);
418 } catch (IOException ee) {
419 JOptionPane.showMessageDialog(owner,
420 device.toShortString(),
421 ee.getLocalizedMessage(),
422 JOptionPane.ERROR_MESSAGE);