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 Runnable, 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;
71 int_ref radio_channel;
72 int_ref radio_calibration;
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.set_radio();
114 serial_line.printf("p\nE 0\n");
115 serial_line.flush_input();
119 void stop_serial() throws InterruptedException {
122 serial_started = false;
124 serial_line.printf("~");
125 serial_line.flush_output();
129 void get_data() throws InterruptedException, TimeoutException {
132 serial_line.printf("c s\nv\n");
134 String line = serial_line.get_reply(5000);
136 throw new TimeoutException();
137 get_int(line, "serial-number", serial);
138 get_int(line, "Main deploy:", main_deploy);
139 get_int(line, "Apogee delay:", apogee_delay);
140 get_int(line, "Radio channel:", radio_channel);
141 get_int(line, "Radio cal:", radio_calibration);
142 get_string(line, "Callsign:", callsign);
143 get_string(line,"software-version", version);
144 get_string(line,"product", product);
146 /* signals the end of the version info */
147 if (line.startsWith("software-version"))
155 void init_ui () throws InterruptedException, TimeoutException {
156 config_ui = new AltosConfigUI(owner, remote);
157 config_ui.addActionListener(this);
162 JOptionPane.showMessageDialog(owner,
163 String.format("Connection to \"%s\" failed",
164 device.toShortString()),
166 JOptionPane.ERROR_MESSAGE);
169 } catch (InterruptedException ie) {
175 void set_ui() throws InterruptedException, TimeoutException {
176 if (serial_line != null)
178 config_ui.set_serial(serial.get());
179 config_ui.set_product(product.get());
180 config_ui.set_version(version.get());
181 config_ui.set_main_deploy(main_deploy.get());
182 config_ui.set_apogee_delay(apogee_delay.get());
183 config_ui.set_radio_channel(radio_channel.get());
184 config_ui.set_radio_calibration(radio_calibration.get());
185 config_ui.set_callsign(callsign.get());
186 config_ui.set_clean();
193 main_deploy.set(config_ui.main_deploy());
194 apogee_delay.set(config_ui.apogee_delay());
195 radio_channel.set(config_ui.radio_channel());
196 radio_calibration.set(config_ui.radio_calibration());
197 callsign.set(config_ui.callsign());
200 serial_line.printf("c m %d\n", main_deploy.get());
201 serial_line.printf("c d %d\n", apogee_delay.get());
203 serial_line.printf("c r %d\n", radio_channel.get());
204 serial_line.printf("c f %d\n", radio_calibration.get());
206 serial_line.printf("c c %s\n", callsign.get());
207 serial_line.printf("c w\n");
208 } catch (InterruptedException ie) {
212 } catch (InterruptedException ie) {
217 public void actionPerformed(ActionEvent e) {
218 String cmd = e.getActionCommand();
220 if (cmd.equals("Save")) {
223 } else if (cmd.equals("Reset")) {
225 } else if (cmd.equals("Reboot")) {
226 if (serial_line != null) {
228 serial_line.printf("r eboot\n");
229 serial_line.flush_output();
233 } else if (cmd.equals("Close")) {
234 if (serial_line != null)
237 } catch (InterruptedException ie) {
239 } catch (TimeoutException te) {
247 config_ui.make_visible();
248 } catch (InterruptedException ie) {
250 } catch (TimeoutException te) {
255 public AltosConfig(JFrame given_owner) {
258 serial = new int_ref(0);
259 main_deploy = new int_ref(250);
260 apogee_delay = new int_ref(0);
261 radio_channel = new int_ref(0);
262 radio_calibration = new int_ref(1186611);
263 callsign = new string_ref("N0CALL");
264 version = new string_ref("unknown");
265 product = new string_ref("unknown");
267 device = AltosDeviceDialog.show(owner, AltosDevice.product_any);
268 if (device != null) {
270 serial_line = new AltosSerial(device);
271 if (!device.matchProduct(AltosDevice.product_telemetrum))
273 config_thread = new Thread(this);
274 config_thread.start();
275 } catch (FileNotFoundException ee) {
276 JOptionPane.showMessageDialog(owner,
277 String.format("Cannot open device \"%s\"",
278 device.toShortString()),
279 "Cannot open target device",
280 JOptionPane.ERROR_MESSAGE);
281 } catch (AltosSerialInUseException si) {
282 JOptionPane.showMessageDialog(owner,
283 String.format("Device \"%s\" already in use",
284 device.toShortString()),
286 JOptionPane.ERROR_MESSAGE);
287 } catch (IOException ee) {
288 JOptionPane.showMessageDialog(owner,
289 device.toShortString(),
290 ee.getLocalizedMessage(),
291 JOptionPane.ERROR_MESSAGE);