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_calibration(radio_calibration.get());
136 config_ui.set_radio_frequency(frequency());
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_clean();
142 config_ui.make_visible();
145 void process_line(String line) {
150 if (line.equals("done")) {
151 if (serial_line != null)
155 get_int(line, "serial-number", serial);
156 get_int(line, "Main deploy:", main_deploy);
157 get_int(line, "Apogee delay:", apogee_delay);
158 get_int(line, "Radio channel:", radio_channel);
159 get_int(line, "Radio cal:", radio_calibration);
160 get_int(line, "Max flight log:", flight_log_max);
161 get_int(line, "Ignite mode:", ignite_mode);
162 get_int(line, "Pad orientation:", pad_orientation);
163 get_int(line, "Radio setting:", radio_setting);
164 get_string(line, "Callsign:", callsign);
165 get_string(line,"software-version", version);
166 get_string(line,"product", product);
169 final static int serial_mode_read = 0;
170 final static int serial_mode_save = 1;
171 final static int serial_mode_reboot = 2;
173 class SerialData implements Runnable {
177 void process_line(String line) {
178 config.process_line(line);
180 void callback(String in_line) {
181 final String line = in_line;
182 Runnable r = new Runnable() {
187 SwingUtilities.invokeLater(r);
192 config.start_serial();
193 config.serial_line.printf("c s\nv\n");
196 String line = config.serial_line.get_reply(5000);
200 if (line.startsWith("software-version"))
202 } catch (Exception e) {
206 } catch (InterruptedException ie) {
207 } catch (TimeoutException te) {
211 } catch (InterruptedException ie) {
219 double frequency = frequency();
220 boolean has_setting = radio_setting.get() > 0;
222 serial_line.printf("c m %d\n", main_deploy.get());
223 serial_line.printf("c d %d\n", apogee_delay.get());
225 serial_line.printf("c f %d\n", radio_calibration.get());
226 serial_line.set_radio_frequency(frequency,
228 radio_calibration.get());
230 serial_line.stop_remote();
231 serial_line.set_radio_frequency(frequency,
233 radio_calibration.get());
234 AltosPreferences.set_frequency(device.getSerial(), frequency);
235 serial_line.start_remote();
237 serial_line.printf("c c %s\n", callsign.get());
238 if (flight_log_max.get() != 0)
239 serial_line.printf("c l %d\n", flight_log_max.get());
240 if (ignite_mode.get() >= 0)
241 serial_line.printf("c i %d\n", ignite_mode.get());
242 if (pad_orientation.get() >= 0)
243 serial_line.printf("c o %d\n", pad_orientation.get());
244 serial_line.printf("c w\n");
245 } catch (InterruptedException ie) {
246 } catch (TimeoutException te) {
250 } catch (InterruptedException ie) {
258 serial_line.printf("r eboot\n");
259 serial_line.flush_output();
260 } catch (InterruptedException ie) {
261 } catch (TimeoutException te) {
265 } catch (InterruptedException ie) {
272 switch (serial_mode) {
273 case serial_mode_save:
275 /* fall through ... */
276 case serial_mode_read:
279 case serial_mode_reboot:
285 public SerialData(AltosConfig in_config, int in_serial_mode) {
287 serial_mode = in_serial_mode;
291 void run_serial_thread(int serial_mode) {
292 SerialData sd = new SerialData(this, serial_mode);
293 Thread st = new Thread(sd);
297 void init_ui () throws InterruptedException, TimeoutException {
298 config_ui = new AltosConfigUI(owner, remote);
299 config_ui.addActionListener(this);
300 serial_line.set_frame(owner);
307 JOptionPane.showMessageDialog(owner,
308 String.format("Connection to \"%s\" failed",
309 device.toShortString()),
311 JOptionPane.ERROR_MESSAGE);
312 config_ui.setVisible(false);
315 void set_ui() throws InterruptedException, TimeoutException {
316 if (serial_line != null)
317 run_serial_thread(serial_mode_read);
323 return AltosConvert.radio_to_frequency(radio_setting.get(),
324 radio_calibration.get(),
325 radio_channel.get());
328 void set_frequency(double freq) {
329 int setting = radio_setting.get();
332 radio_setting.set(AltosConvert.radio_frequency_to_setting(freq,
333 radio_calibration.get()));
334 radio_channel.set(0);
336 radio_channel.set(AltosConvert.radio_frequency_to_channel(freq));
341 main_deploy.set(config_ui.main_deploy());
342 apogee_delay.set(config_ui.apogee_delay());
343 radio_calibration.set(config_ui.radio_calibration());
344 set_frequency(config_ui.radio_frequency());
345 flight_log_max.set(config_ui.flight_log_max());
346 ignite_mode.set(config_ui.ignite_mode());
347 pad_orientation.set(config_ui.pad_orientation());
348 callsign.set(config_ui.callsign());
349 run_serial_thread(serial_mode_save);
352 public void actionPerformed(ActionEvent e) {
353 String cmd = e.getActionCommand();
355 if (cmd.equals("Save")) {
357 } else if (cmd.equals("Reset")) {
359 } else if (cmd.equals("Reboot")) {
360 if (serial_line != null)
361 run_serial_thread(serial_mode_reboot);
362 } else if (cmd.equals("Close")) {
363 if (serial_line != null)
366 } catch (InterruptedException ie) {
368 } catch (TimeoutException te) {
373 public AltosConfig(JFrame given_owner) {
376 serial = new int_ref(0);
377 main_deploy = new int_ref(250);
378 apogee_delay = new int_ref(0);
379 radio_channel = new int_ref(0);
380 radio_setting = new int_ref(0);
381 radio_calibration = new int_ref(1186611);
382 flight_log_max = new int_ref(0);
383 ignite_mode = new int_ref(-1);
384 pad_orientation = new int_ref(-1);
385 callsign = new string_ref("N0CALL");
386 version = new string_ref("unknown");
387 product = new string_ref("unknown");
389 device = AltosDeviceDialog.show(owner, Altos.product_any);
390 if (device != null) {
392 serial_line = new AltosSerial(device);
394 if (!device.matchProduct(Altos.product_telemetrum))
397 } catch (InterruptedException ie) {
399 } catch (TimeoutException te) {
402 } catch (FileNotFoundException ee) {
403 JOptionPane.showMessageDialog(owner,
404 String.format("Cannot open device \"%s\"",
405 device.toShortString()),
406 "Cannot open target device",
407 JOptionPane.ERROR_MESSAGE);
408 } catch (AltosSerialInUseException si) {
409 JOptionPane.showMessageDialog(owner,
410 String.format("Device \"%s\" already in use",
411 device.toShortString()),
413 JOptionPane.ERROR_MESSAGE);
414 } catch (IOException ee) {
415 JOptionPane.showMessageDialog(owner,
416 device.toShortString(),
417 ee.getLocalizedMessage(),
418 JOptionPane.ERROR_MESSAGE);