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.LinkedBlockingQueue;
32 import altosui.AltosSerial;
33 import altosui.AltosSerialMonitor;
34 import altosui.AltosRecord;
35 import altosui.AltosTelemetry;
36 import altosui.AltosState;
37 import altosui.AltosDeviceDialog;
38 import altosui.AltosPreferences;
39 import altosui.AltosLog;
40 import altosui.AltosVoice;
41 import altosui.AltosFlightStatusTableModel;
42 import altosui.AltosFlightInfoTableModel;
43 import altosui.AltosConfigUI;
47 public class AltosConfig implements Runnable, ActionListener {
55 public void set(int i) {
58 public int_ref(int i) {
69 public void set(String i) {
72 public string_ref(String i) {
79 AltosSerial serial_line;
85 int_ref radio_channel;
86 int_ref radio_calibration;
90 AltosConfigUI config_ui;
93 boolean get_int(String line, String label, int_ref x) {
94 if (line.startsWith(label)) {
96 String tail = line.substring(label.length()).trim();
97 String[] tokens = tail.split("\\s+");
98 if (tokens.length > 0) {
99 int i = Integer.parseInt(tokens[0]);
103 } catch (NumberFormatException ne) {
109 boolean get_string(String line, String label, string_ref s) {
110 if (line.startsWith(label)) {
111 String quoted = line.substring(label.length()).trim();
113 if (quoted.startsWith("\""))
114 quoted = quoted.substring(1);
115 if (quoted.endsWith("\""))
116 quoted = quoted.substring(0,quoted.length()-1);
124 void start_serial() throws InterruptedException {
126 serial_line.set_channel(AltosPreferences.channel(device.getSerial()));
127 serial_line.set_callsign(AltosPreferences.callsign());
128 serial_line.printf("p\n");
129 serial_line.flush_input();
133 void stop_serial() throws InterruptedException {
135 serial_line.printf("~");
136 serial_line.flush_output();
140 void get_data() throws InterruptedException {
143 serial_line.printf("c s\nv\n");
145 String line = serial_line.get_reply();
146 get_int(line, "serial-number", serial);
147 get_int(line, "Main deploy:", main_deploy);
148 get_int(line, "Apogee delay:", apogee_delay);
149 get_int(line, "Radio channel:", radio_channel);
150 get_int(line, "Radio cal:", radio_calibration);
151 get_string(line, "Callsign:", callsign);
152 get_string(line,"software-version", version);
153 get_string(line,"product", product);
155 /* signals the end of the version info */
156 if (line.startsWith("software-version"))
165 config_ui = new AltosConfigUI(owner);
166 config_ui.addActionListener(this);
172 if (serial_line != null)
174 config_ui.set_serial(serial.get());
175 config_ui.set_product(product.get());
176 config_ui.set_version(version.get());
177 config_ui.set_main_deploy(main_deploy.get());
178 config_ui.set_apogee_delay(apogee_delay.get());
179 config_ui.set_radio_channel(radio_channel.get());
180 config_ui.set_radio_calibration(radio_calibration.get());
181 config_ui.set_callsign(callsign.get());
182 config_ui.set_clean();
183 } catch (InterruptedException ie) {
191 main_deploy.set(config_ui.main_deploy());
192 apogee_delay.set(config_ui.apogee_delay());
193 radio_channel.set(config_ui.radio_channel());
194 radio_calibration.set(config_ui.radio_calibration());
195 callsign.set(config_ui.callsign());
198 serial_line.printf("c m %d\n", main_deploy.get());
199 serial_line.printf("c d %d\n", apogee_delay.get());
200 serial_line.printf("c r %d\n", radio_channel.get());
201 serial_line.printf("c f %d\n", radio_calibration.get());
202 serial_line.printf("c c %s\n", callsign.get());
203 serial_line.printf("c w\n");
204 } catch (InterruptedException ie) {
208 } catch (InterruptedException ie) {
213 public void actionPerformed(ActionEvent e) {
214 String cmd = e.getActionCommand();
215 if (cmd.equals("save")) {
218 } else if (cmd.equals("reset")) {
220 } else if (cmd.equals("close")) {
221 if (serial_line != null)
229 config_ui.make_visible();
230 // } catch (InterruptedException ie) {
235 public AltosConfig(JFrame given_owner) {
238 serial = new int_ref(0);
239 main_deploy = new int_ref(250);
240 apogee_delay = new int_ref(0);
241 radio_channel = new int_ref(0);
242 radio_calibration = new int_ref(1186611);
243 callsign = new string_ref("N0CALL");
244 version = new string_ref("unknown");
245 product = new string_ref("unknown");
247 device = AltosDeviceDialog.show(owner, AltosDevice.product_any);
248 serial_line = new AltosSerial();
249 if (device != null) {
251 serial_line.open(device);
252 if (!device.matchProduct(AltosDevice.product_telemetrum))
254 config_thread = new Thread(this);
255 config_thread.start();
256 } catch (FileNotFoundException ee) {
257 JOptionPane.showMessageDialog(owner,
258 String.format("Cannot open device \"%s\"",
260 "Cannot open target device",
261 JOptionPane.ERROR_MESSAGE);
262 } catch (IOException ee) {
263 JOptionPane.showMessageDialog(owner,
265 ee.getLocalizedMessage(),
266 JOptionPane.ERROR_MESSAGE);