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;
75 AltosConfigUI config_ui;
76 boolean serial_started;
78 boolean get_int(String line, String label, int_ref x) {
79 if (line.startsWith(label)) {
81 String tail = line.substring(label.length()).trim();
82 String[] tokens = tail.split("\\s+");
83 if (tokens.length > 0) {
84 int i = Integer.parseInt(tokens[0]);
88 } catch (NumberFormatException ne) {
94 boolean get_string(String line, String label, string_ref s) {
95 if (line.startsWith(label)) {
96 String quoted = line.substring(label.length()).trim();
98 if (quoted.startsWith("\""))
99 quoted = quoted.substring(1);
100 if (quoted.endsWith("\""))
101 quoted = quoted.substring(0,quoted.length()-1);
109 void start_serial() throws InterruptedException {
110 serial_started = true;
112 serial_line.set_radio();
113 serial_line.printf("p\nE 0\n");
114 serial_line.flush_input();
118 void stop_serial() throws InterruptedException {
121 serial_started = false;
123 serial_line.printf("~");
124 serial_line.flush_output();
128 void get_data() throws InterruptedException, TimeoutException {
131 serial_line.printf("c s\nv\n");
133 String line = serial_line.get_reply(5000);
135 throw new TimeoutException();
136 get_int(line, "serial-number", serial);
137 get_int(line, "Main deploy:", main_deploy);
138 get_int(line, "Apogee delay:", apogee_delay);
139 get_int(line, "Radio channel:", radio_channel);
140 get_int(line, "Radio cal:", radio_calibration);
141 get_string(line, "Callsign:", callsign);
142 get_string(line,"software-version", version);
143 get_string(line,"product", product);
145 /* signals the end of the version info */
146 if (line.startsWith("software-version"))
154 void init_ui () throws InterruptedException, TimeoutException {
155 config_ui = new AltosConfigUI(owner, remote);
156 config_ui.addActionListener(this);
161 JOptionPane.showMessageDialog(owner,
162 String.format("Connection to \"%s\" failed",
163 device.toShortString()),
165 JOptionPane.ERROR_MESSAGE);
168 } catch (InterruptedException ie) {
174 void set_ui() throws InterruptedException, TimeoutException {
175 if (serial_line != null)
177 config_ui.set_serial(serial.get());
178 config_ui.set_product(product.get());
179 config_ui.set_version(version.get());
180 config_ui.set_main_deploy(main_deploy.get());
181 config_ui.set_apogee_delay(apogee_delay.get());
182 config_ui.set_radio_channel(radio_channel.get());
183 config_ui.set_radio_calibration(radio_calibration.get());
184 config_ui.set_callsign(callsign.get());
185 config_ui.set_clean();
192 main_deploy.set(config_ui.main_deploy());
193 apogee_delay.set(config_ui.apogee_delay());
194 radio_channel.set(config_ui.radio_channel());
195 radio_calibration.set(config_ui.radio_calibration());
196 callsign.set(config_ui.callsign());
199 serial_line.printf("c m %d\n", main_deploy.get());
200 serial_line.printf("c d %d\n", apogee_delay.get());
202 serial_line.printf("c r %d\n", radio_channel.get());
203 serial_line.printf("c f %d\n", radio_calibration.get());
205 serial_line.printf("c c %s\n", callsign.get());
206 serial_line.printf("c w\n");
207 } catch (InterruptedException ie) {
211 } catch (InterruptedException ie) {
216 public void actionPerformed(ActionEvent e) {
217 String cmd = e.getActionCommand();
219 if (cmd.equals("Save")) {
222 } else if (cmd.equals("Reset")) {
224 } else if (cmd.equals("Reboot")) {
225 if (serial_line != null) {
227 serial_line.printf("r eboot\n");
228 serial_line.flush_output();
232 } else if (cmd.equals("Close")) {
233 if (serial_line != null)
236 } catch (InterruptedException ie) {
238 } catch (TimeoutException te) {
243 public AltosConfig(JFrame given_owner) {
246 serial = new int_ref(0);
247 main_deploy = new int_ref(250);
248 apogee_delay = new int_ref(0);
249 radio_channel = new int_ref(0);
250 radio_calibration = new int_ref(1186611);
251 callsign = new string_ref("N0CALL");
252 version = new string_ref("unknown");
253 product = new string_ref("unknown");
255 device = AltosDeviceDialog.show(owner, AltosDevice.product_any);
256 if (device != null) {
258 serial_line = new AltosSerial(device);
259 if (!device.matchProduct(AltosDevice.product_telemetrum))
263 config_ui.make_visible();
264 } catch (InterruptedException ie) {
266 } catch (TimeoutException te) {
269 } catch (FileNotFoundException ee) {
270 JOptionPane.showMessageDialog(owner,
271 String.format("Cannot open device \"%s\"",
272 device.toShortString()),
273 "Cannot open target device",
274 JOptionPane.ERROR_MESSAGE);
275 } catch (AltosSerialInUseException si) {
276 JOptionPane.showMessageDialog(owner,
277 String.format("Device \"%s\" already in use",
278 device.toShortString()),
280 JOptionPane.ERROR_MESSAGE);
281 } catch (IOException ee) {
282 JOptionPane.showMessageDialog(owner,
283 device.toShortString(),
284 ee.getLocalizedMessage(),
285 JOptionPane.ERROR_MESSAGE);