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;
70 int_ref radio_channel;
71 int_ref radio_calibration;
72 int_ref flight_log_max;
76 AltosConfigUI config_ui;
77 boolean serial_started;
79 boolean get_int(String line, String label, int_ref x) {
80 if (line.startsWith(label)) {
82 String tail = line.substring(label.length()).trim();
83 String[] tokens = tail.split("\\s+");
84 if (tokens.length > 0) {
85 int i = Integer.parseInt(tokens[0]);
89 } catch (NumberFormatException ne) {
95 boolean get_string(String line, String label, string_ref s) {
96 if (line.startsWith(label)) {
97 String quoted = line.substring(label.length()).trim();
99 if (quoted.startsWith("\""))
100 quoted = quoted.substring(1);
101 if (quoted.endsWith("\""))
102 quoted = quoted.substring(0,quoted.length()-1);
110 void start_serial() throws InterruptedException {
111 serial_started = true;
113 serial_line.start_remote();
116 void stop_serial() throws InterruptedException {
119 serial_started = false;
121 serial_line.stop_remote();
125 config_ui.set_serial(serial.get());
126 config_ui.set_product(product.get());
127 config_ui.set_version(version.get());
128 config_ui.set_main_deploy(main_deploy.get());
129 config_ui.set_apogee_delay(apogee_delay.get());
130 config_ui.set_radio_channel(radio_channel.get());
131 config_ui.set_radio_calibration(radio_calibration.get());
132 config_ui.set_flight_log_max(flight_log_max.get());
133 config_ui.set_callsign(callsign.get());
134 config_ui.set_clean();
135 config_ui.make_visible();
138 void process_line(String line) {
140 System.out.printf("timeout\n");
144 if (line.equals("done")) {
145 System.out.printf("done\n");
146 if (serial_line != null)
150 get_int(line, "serial-number", serial);
151 get_int(line, "Main deploy:", main_deploy);
152 get_int(line, "Apogee delay:", apogee_delay);
153 get_int(line, "Radio channel:", radio_channel);
154 get_int(line, "Radio cal:", radio_calibration);
155 get_int(line, "Max flight log:", flight_log_max);
156 get_string(line, "Callsign:", callsign);
157 get_string(line,"software-version", version);
158 get_string(line,"product", product);
161 final static int serial_mode_read = 0;
162 final static int serial_mode_save = 1;
163 final static int serial_mode_reboot = 2;
165 class SerialData implements Runnable {
169 void process_line(String line) {
170 config.process_line(line);
172 void callback(String in_line) {
173 final String line = in_line;
174 Runnable r = new Runnable() {
179 SwingUtilities.invokeLater(r);
184 config.start_serial();
185 config.serial_line.printf("c s\nv\n");
188 String line = config.serial_line.get_reply(5000);
192 if (line.startsWith("software-version"))
194 } catch (Exception e) {
198 } catch (InterruptedException ie) {
202 } catch (InterruptedException ie) {
211 serial_line.printf("c m %d\n", main_deploy.get());
212 serial_line.printf("c d %d\n", apogee_delay.get());
214 serial_line.printf("c r %d\n", radio_channel.get());
215 serial_line.printf("c f %d\n", radio_calibration.get());
217 serial_line.printf("c c %s\n", callsign.get());
218 if (flight_log_max.get() != 0)
219 serial_line.printf("c l %d\n", flight_log_max.get());
220 serial_line.printf("c w\n");
221 } catch (InterruptedException ie) {
225 } catch (InterruptedException ie) {
233 serial_line.printf("r eboot\n");
234 serial_line.flush_output();
235 } catch (InterruptedException ie) {
239 } catch (InterruptedException ie) {
246 switch (serial_mode) {
247 case serial_mode_save:
249 /* fall through ... */
250 case serial_mode_read:
253 case serial_mode_reboot:
259 public SerialData(AltosConfig in_config, int in_serial_mode) {
261 serial_mode = in_serial_mode;
265 void run_serial_thread(int serial_mode) {
266 SerialData sd = new SerialData(this, serial_mode);
267 Thread st = new Thread(sd);
271 void init_ui () throws InterruptedException, TimeoutException {
272 config_ui = new AltosConfigUI(owner, remote);
273 config_ui.addActionListener(this);
274 serial_line.set_frame(owner);
281 JOptionPane.showMessageDialog(owner,
282 String.format("Connection to \"%s\" failed",
283 device.toShortString()),
285 JOptionPane.ERROR_MESSAGE);
286 config_ui.setVisible(false);
289 void set_ui() throws InterruptedException, TimeoutException {
290 if (serial_line != null)
291 run_serial_thread(serial_mode_read);
297 main_deploy.set(config_ui.main_deploy());
298 apogee_delay.set(config_ui.apogee_delay());
299 radio_channel.set(config_ui.radio_channel());
300 radio_calibration.set(config_ui.radio_calibration());
301 flight_log_max.set(config_ui.flight_log_max());
302 callsign.set(config_ui.callsign());
303 run_serial_thread(serial_mode_save);
306 public void actionPerformed(ActionEvent e) {
307 String cmd = e.getActionCommand();
309 if (cmd.equals("Save")) {
311 } else if (cmd.equals("Reset")) {
313 } else if (cmd.equals("Reboot")) {
314 if (serial_line != null)
315 run_serial_thread(serial_mode_reboot);
316 } else if (cmd.equals("Close")) {
317 if (serial_line != null)
320 } catch (InterruptedException ie) {
322 } catch (TimeoutException te) {
327 public AltosConfig(JFrame given_owner) {
330 serial = new int_ref(0);
331 main_deploy = new int_ref(250);
332 apogee_delay = new int_ref(0);
333 radio_channel = new int_ref(0);
334 radio_calibration = new int_ref(1186611);
335 flight_log_max = new int_ref(0);
336 callsign = new string_ref("N0CALL");
337 version = new string_ref("unknown");
338 product = new string_ref("unknown");
340 device = AltosDeviceDialog.show(owner, AltosDevice.product_any);
341 if (device != null) {
343 serial_line = new AltosSerial(device);
344 if (!device.matchProduct(AltosDevice.product_telemetrum))
348 } catch (InterruptedException ie) {
350 } catch (TimeoutException te) {
353 } catch (FileNotFoundException ee) {
354 JOptionPane.showMessageDialog(owner,
355 String.format("Cannot open device \"%s\"",
356 device.toShortString()),
357 "Cannot open target device",
358 JOptionPane.ERROR_MESSAGE);
359 } catch (AltosSerialInUseException si) {
360 JOptionPane.showMessageDialog(owner,
361 String.format("Device \"%s\" already in use",
362 device.toShortString()),
364 JOptionPane.ERROR_MESSAGE);
365 } catch (IOException ee) {
366 JOptionPane.showMessageDialog(owner,
367 device.toShortString(),
368 ee.getLocalizedMessage(),
369 JOptionPane.ERROR_MESSAGE);