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;
80 int_ref storage_erase_unit;
84 AltosConfigUI config_ui;
85 boolean serial_started;
87 boolean get_int(String line, String label, int_ref x) {
88 if (line.startsWith(label)) {
90 String tail = line.substring(label.length()).trim();
91 String[] tokens = tail.split("\\s+");
92 if (tokens.length > 0) {
93 int i = Integer.parseInt(tokens[0]);
97 } catch (NumberFormatException ne) {
103 boolean get_string(String line, String label, string_ref s) {
104 if (line.startsWith(label)) {
105 String quoted = line.substring(label.length()).trim();
107 if (quoted.startsWith("\""))
108 quoted = quoted.substring(1);
109 if (quoted.endsWith("\""))
110 quoted = quoted.substring(0,quoted.length()-1);
118 void start_serial() throws InterruptedException, TimeoutException {
119 serial_started = true;
121 serial_line.start_remote();
124 void stop_serial() throws InterruptedException {
127 serial_started = false;
129 serial_line.stop_remote();
133 if (storage_size.get() > 0 && storage_erase_unit.get() > 0) {
134 int log_limit = storage_size.get() - storage_erase_unit.get();
136 return log_limit / 1024;
142 config_ui.set_serial(serial.get());
143 config_ui.set_product(product.get());
144 config_ui.set_version(version.get());
145 config_ui.set_main_deploy(main_deploy.get());
146 config_ui.set_apogee_delay(apogee_delay.get());
147 config_ui.set_radio_calibration(radio_calibration.get());
148 config_ui.set_radio_frequency(frequency());
149 config_ui.set_flight_log_max_limit(log_limit());
150 config_ui.set_flight_log_max(flight_log_max.get());
151 config_ui.set_ignite_mode(ignite_mode.get());
152 config_ui.set_pad_orientation(pad_orientation.get());
153 config_ui.set_callsign(callsign.get());
154 config_ui.set_clean();
155 config_ui.make_visible();
158 void process_line(String line) {
163 if (line.equals("done")) {
164 if (serial_line != null)
168 get_int(line, "serial-number", serial);
169 get_int(line, "log-format", log_format);
170 get_int(line, "Main deploy:", main_deploy);
171 get_int(line, "Apogee delay:", apogee_delay);
172 get_int(line, "Radio channel:", radio_channel);
173 get_int(line, "Radio cal:", radio_calibration);
174 get_int(line, "Max flight log:", flight_log_max);
175 get_int(line, "Ignite mode:", ignite_mode);
176 get_int(line, "Pad orientation:", pad_orientation);
177 get_int(line, "Radio setting:", radio_setting);
178 get_int(line, "Storage size:", storage_size);
179 get_int(line, "Storage erase unit:", storage_erase_unit);
180 get_string(line, "Callsign:", callsign);
181 get_string(line,"software-version", version);
182 get_string(line,"product", product);
185 final static int serial_mode_read = 0;
186 final static int serial_mode_save = 1;
187 final static int serial_mode_reboot = 2;
189 class SerialData implements Runnable {
193 void process_line(String line) {
194 config.process_line(line);
196 void callback(String in_line) {
197 final String line = in_line;
198 Runnable r = new Runnable() {
203 SwingUtilities.invokeLater(r);
208 config.start_serial();
209 config.serial_line.printf("c s\nf\nv\n");
212 String line = config.serial_line.get_reply(5000);
216 if (line.startsWith("software-version"))
218 } catch (Exception e) {
222 } catch (InterruptedException ie) {
223 } catch (TimeoutException te) {
227 } catch (InterruptedException ie) {
235 double frequency = frequency();
236 boolean has_setting = radio_setting.get() > 0;
238 serial_line.printf("c m %d\n", main_deploy.get());
239 serial_line.printf("c d %d\n", apogee_delay.get());
241 serial_line.printf("c f %d\n", radio_calibration.get());
242 serial_line.set_radio_frequency(frequency,
244 radio_calibration.get());
246 serial_line.stop_remote();
247 serial_line.set_radio_frequency(frequency);
248 AltosPreferences.set_frequency(device.getSerial(), frequency);
249 serial_line.start_remote();
251 serial_line.printf("c c %s\n", callsign.get());
252 if (flight_log_max.get() != 0)
253 serial_line.printf("c l %d\n", flight_log_max.get());
254 if (ignite_mode.get() >= 0)
255 serial_line.printf("c i %d\n", ignite_mode.get());
256 if (pad_orientation.get() >= 0)
257 serial_line.printf("c o %d\n", pad_orientation.get());
258 serial_line.printf("c w\n");
259 } catch (InterruptedException ie) {
260 } catch (TimeoutException te) {
264 } catch (InterruptedException ie) {
272 serial_line.printf("r eboot\n");
273 serial_line.flush_output();
274 } catch (InterruptedException ie) {
275 } catch (TimeoutException te) {
279 } catch (InterruptedException ie) {
286 switch (serial_mode) {
287 case serial_mode_save:
289 /* fall through ... */
290 case serial_mode_read:
293 case serial_mode_reboot:
299 public SerialData(AltosConfig in_config, int in_serial_mode) {
301 serial_mode = in_serial_mode;
305 void run_serial_thread(int serial_mode) {
306 SerialData sd = new SerialData(this, serial_mode);
307 Thread st = new Thread(sd);
311 void init_ui () throws InterruptedException, TimeoutException {
312 config_ui = new AltosConfigUI(owner, remote);
313 config_ui.addActionListener(this);
314 serial_line.set_frame(owner);
321 JOptionPane.showMessageDialog(owner,
322 String.format("Connection to \"%s\" failed",
323 device.toShortString()),
325 JOptionPane.ERROR_MESSAGE);
326 config_ui.setVisible(false);
329 void set_ui() throws InterruptedException, TimeoutException {
330 if (serial_line != null)
331 run_serial_thread(serial_mode_read);
337 return AltosConvert.radio_to_frequency(radio_setting.get(),
338 radio_calibration.get(),
339 radio_channel.get());
342 void set_frequency(double freq) {
343 int setting = radio_setting.get();
346 radio_setting.set(AltosConvert.radio_frequency_to_setting(freq,
347 radio_calibration.get()));
348 radio_channel.set(0);
350 radio_channel.set(AltosConvert.radio_frequency_to_channel(freq));
356 /* bounds check stuff */
357 if (config_ui.flight_log_max() > log_limit()) {
358 JOptionPane.showMessageDialog(owner,
359 String.format("Requested flight log, %dk, is larger than the available space, %dk.\n",
360 config_ui.flight_log_max(),
362 "Maximum Flight Log Too Large",
363 JOptionPane.ERROR_MESSAGE);
367 main_deploy.set(config_ui.main_deploy());
368 apogee_delay.set(config_ui.apogee_delay());
369 radio_calibration.set(config_ui.radio_calibration());
370 set_frequency(config_ui.radio_frequency());
371 flight_log_max.set(config_ui.flight_log_max());
372 if (ignite_mode.get() >= 0)
373 ignite_mode.set(config_ui.ignite_mode());
374 if (pad_orientation.get() >= 0)
375 pad_orientation.set(config_ui.pad_orientation());
376 callsign.set(config_ui.callsign());
377 run_serial_thread(serial_mode_save);
380 public void actionPerformed(ActionEvent e) {
381 String cmd = e.getActionCommand();
383 if (cmd.equals("Save")) {
385 } else if (cmd.equals("Reset")) {
387 } else if (cmd.equals("Reboot")) {
388 if (serial_line != null)
389 run_serial_thread(serial_mode_reboot);
390 } else if (cmd.equals("Close")) {
391 if (serial_line != null)
394 } catch (InterruptedException ie) {
396 } catch (TimeoutException te) {
401 public AltosConfig(JFrame given_owner) {
404 serial = new int_ref(0);
405 log_format = new int_ref(Altos.AO_LOG_FORMAT_UNKNOWN);
406 main_deploy = new int_ref(250);
407 apogee_delay = new int_ref(0);
408 radio_channel = new int_ref(0);
409 radio_setting = new int_ref(0);
410 radio_calibration = new int_ref(1186611);
411 flight_log_max = new int_ref(0);
412 ignite_mode = new int_ref(-1);
413 pad_orientation = new int_ref(-1);
414 storage_size = new int_ref(-1);
415 storage_erase_unit = new int_ref(-1);
416 callsign = new string_ref("N0CALL");
417 version = new string_ref("unknown");
418 product = new string_ref("unknown");
420 device = AltosDeviceDialog.show(owner, Altos.product_any);
421 if (device != null) {
423 serial_line = new AltosSerial(device);
425 if (!device.matchProduct(Altos.product_telemetrum))
428 } catch (InterruptedException ie) {
430 } catch (TimeoutException te) {
433 } catch (FileNotFoundException ee) {
434 JOptionPane.showMessageDialog(owner,
435 String.format("Cannot open device \"%s\"",
436 device.toShortString()),
437 "Cannot open target device",
438 JOptionPane.ERROR_MESSAGE);
439 } catch (AltosSerialInUseException si) {
440 JOptionPane.showMessageDialog(owner,
441 String.format("Device \"%s\" already in use",
442 device.toShortString()),
444 JOptionPane.ERROR_MESSAGE);
445 } catch (IOException ee) {
446 JOptionPane.showMessageDialog(owner,
447 device.toShortString(),
448 ee.getLocalizedMessage(),
449 JOptionPane.ERROR_MESSAGE);