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;
74 int_ref pad_orientation;
78 AltosConfigUI config_ui;
79 boolean serial_started;
81 boolean get_int(String line, String label, int_ref x) {
82 if (line.startsWith(label)) {
84 String tail = line.substring(label.length()).trim();
85 String[] tokens = tail.split("\\s+");
86 if (tokens.length > 0) {
87 int i = Integer.parseInt(tokens[0]);
91 } catch (NumberFormatException ne) {
97 boolean get_string(String line, String label, string_ref s) {
98 if (line.startsWith(label)) {
99 String quoted = line.substring(label.length()).trim();
101 if (quoted.startsWith("\""))
102 quoted = quoted.substring(1);
103 if (quoted.endsWith("\""))
104 quoted = quoted.substring(0,quoted.length()-1);
112 void start_serial() throws InterruptedException {
113 serial_started = true;
115 serial_line.start_remote();
118 void stop_serial() throws InterruptedException {
121 serial_started = false;
123 serial_line.stop_remote();
127 config_ui.set_serial(serial.get());
128 config_ui.set_product(product.get());
129 config_ui.set_version(version.get());
130 config_ui.set_main_deploy(main_deploy.get());
131 config_ui.set_apogee_delay(apogee_delay.get());
132 config_ui.set_radio_channel(radio_channel.get());
133 config_ui.set_radio_calibration(radio_calibration.get());
134 config_ui.set_flight_log_max(flight_log_max.get());
135 config_ui.set_ignite_mode(ignite_mode.get());
136 config_ui.set_pad_orientation(pad_orientation.get());
137 config_ui.set_callsign(callsign.get());
138 config_ui.set_clean();
139 config_ui.make_visible();
142 void process_line(String line) {
147 if (line.equals("done")) {
148 if (serial_line != null)
152 get_int(line, "serial-number", serial);
153 get_int(line, "Main deploy:", main_deploy);
154 get_int(line, "Apogee delay:", apogee_delay);
155 get_int(line, "Radio channel:", radio_channel);
156 get_int(line, "Radio cal:", radio_calibration);
157 get_int(line, "Max flight log:", flight_log_max);
158 get_int(line, "Ignite mode:", ignite_mode);
159 get_int(line, "Pad orientation:", pad_orientation);
160 get_string(line, "Callsign:", callsign);
161 get_string(line,"software-version", version);
162 get_string(line,"product", product);
165 final static int serial_mode_read = 0;
166 final static int serial_mode_save = 1;
167 final static int serial_mode_reboot = 2;
169 class SerialData implements Runnable {
173 void process_line(String line) {
174 config.process_line(line);
176 void callback(String in_line) {
177 final String line = in_line;
178 Runnable r = new Runnable() {
183 SwingUtilities.invokeLater(r);
188 config.start_serial();
189 config.serial_line.printf("c s\nv\n");
192 String line = config.serial_line.get_reply(5000);
196 if (line.startsWith("software-version"))
198 } catch (Exception e) {
202 } catch (InterruptedException ie) {
206 } catch (InterruptedException ie) {
216 serial_line.printf("c m %d\n", main_deploy.get());
217 serial_line.printf("c d %d\n", apogee_delay.get());
218 channel = radio_channel.get();
219 serial_line.printf("c r %d\n", channel);
221 serial_line.stop_remote();
222 serial_line.set_channel(channel);
223 AltosPreferences.set_channel(device.getSerial(), channel);
224 serial_line.start_remote();
227 serial_line.printf("c f %d\n", radio_calibration.get());
228 serial_line.printf("c c %s\n", callsign.get());
229 if (flight_log_max.get() != 0)
230 serial_line.printf("c l %d\n", flight_log_max.get());
231 if (ignite_mode.get() >= 0)
232 serial_line.printf("c i %d\n", ignite_mode.get());
233 if (pad_orientation.get() >= 0)
234 serial_line.printf("c o %d\n", pad_orientation.get());
235 serial_line.printf("c w\n");
236 } catch (InterruptedException ie) {
240 } catch (InterruptedException ie) {
248 serial_line.printf("r eboot\n");
249 serial_line.flush_output();
250 } catch (InterruptedException ie) {
254 } catch (InterruptedException ie) {
261 switch (serial_mode) {
262 case serial_mode_save:
264 /* fall through ... */
265 case serial_mode_read:
268 case serial_mode_reboot:
274 public SerialData(AltosConfig in_config, int in_serial_mode) {
276 serial_mode = in_serial_mode;
280 void run_serial_thread(int serial_mode) {
281 SerialData sd = new SerialData(this, serial_mode);
282 Thread st = new Thread(sd);
286 void init_ui () throws InterruptedException, TimeoutException {
287 config_ui = new AltosConfigUI(owner, remote);
288 config_ui.addActionListener(this);
289 serial_line.set_frame(owner);
296 JOptionPane.showMessageDialog(owner,
297 String.format("Connection to \"%s\" failed",
298 device.toShortString()),
300 JOptionPane.ERROR_MESSAGE);
301 config_ui.setVisible(false);
304 void set_ui() throws InterruptedException, TimeoutException {
305 if (serial_line != null)
306 run_serial_thread(serial_mode_read);
312 main_deploy.set(config_ui.main_deploy());
313 apogee_delay.set(config_ui.apogee_delay());
314 radio_channel.set(config_ui.radio_channel());
315 radio_calibration.set(config_ui.radio_calibration());
316 flight_log_max.set(config_ui.flight_log_max());
317 ignite_mode.set(config_ui.ignite_mode());
318 pad_orientation.set(config_ui.pad_orientation());
319 callsign.set(config_ui.callsign());
320 run_serial_thread(serial_mode_save);
323 public void actionPerformed(ActionEvent e) {
324 String cmd = e.getActionCommand();
326 if (cmd.equals("Save")) {
328 } else if (cmd.equals("Reset")) {
330 } else if (cmd.equals("Reboot")) {
331 if (serial_line != null)
332 run_serial_thread(serial_mode_reboot);
333 } else if (cmd.equals("Close")) {
334 if (serial_line != null)
337 } catch (InterruptedException ie) {
339 } catch (TimeoutException te) {
344 public AltosConfig(JFrame given_owner) {
347 serial = new int_ref(0);
348 main_deploy = new int_ref(250);
349 apogee_delay = new int_ref(0);
350 radio_channel = new int_ref(0);
351 radio_calibration = new int_ref(1186611);
352 flight_log_max = new int_ref(0);
353 ignite_mode = new int_ref(-1);
354 pad_orientation = new int_ref(-1);
355 callsign = new string_ref("N0CALL");
356 version = new string_ref("unknown");
357 product = new string_ref("unknown");
359 device = AltosDeviceDialog.show(owner, Altos.product_any);
360 if (device != null) {
362 serial_line = new AltosSerial(device);
363 if (!device.matchProduct(Altos.product_telemetrum))
367 } catch (InterruptedException ie) {
369 } catch (TimeoutException te) {
372 } catch (FileNotFoundException ee) {
373 JOptionPane.showMessageDialog(owner,
374 String.format("Cannot open device \"%s\"",
375 device.toShortString()),
376 "Cannot open target device",
377 JOptionPane.ERROR_MESSAGE);
378 } catch (AltosSerialInUseException si) {
379 JOptionPane.showMessageDialog(owner,
380 String.format("Device \"%s\" already in use",
381 device.toShortString()),
383 JOptionPane.ERROR_MESSAGE);
384 } catch (IOException ee) {
385 JOptionPane.showMessageDialog(owner,
386 device.toShortString(),
387 ee.getLocalizedMessage(),
388 JOptionPane.ERROR_MESSAGE);