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;
89 AltosConfigUI config_ui;
92 boolean get_int(String line, String label, int_ref x) {
93 if (line.startsWith(label)) {
95 String tail = line.substring(label.length()).trim();
96 String[] tokens = tail.split("\\s+");
97 if (tokens.length > 0) {
98 int i = Integer.parseInt(tokens[0]);
102 } catch (NumberFormatException ne) {
108 boolean get_string(String line, String label, string_ref s) {
109 if (line.startsWith(label)) {
110 String quoted = line.substring(label.length()).trim();
112 if (quoted.startsWith("\""))
113 quoted = quoted.substring(1);
114 if (quoted.endsWith("\""))
115 quoted = quoted.substring(0,quoted.length()-1);
123 void start_serial() throws InterruptedException {
125 serial_line.printf("m 0\n");
126 serial_line.set_channel(AltosPreferences.channel());
127 serial_line.set_callsign(AltosPreferences.callsign());
128 serial_line.printf("p\n");
132 void stop_serial() throws InterruptedException {
134 serial_line.printf("~\n");
139 void get_data() throws InterruptedException {
142 serial_line.printf("c s\nv\n");
144 String line = serial_line.get_reply();
145 get_int(line, "serial-number", serial);
146 get_int(line, "Main deploy:", main_deploy);
147 get_int(line, "Apogee delay:", apogee_delay);
148 get_int(line, "Radio channel:", radio_channel);
149 get_string(line, "Callsign:", callsign);
150 get_string(line,"software-version", version);
151 get_string(line,"product", product);
153 /* signals the end of the version info */
154 if (line.startsWith("software-version"))
163 config_ui = new AltosConfigUI(owner);
164 config_ui.addActionListener(this);
170 if (serial_line != null)
172 config_ui.set_serial(serial.get());
173 config_ui.set_product(product.get());
174 config_ui.set_version(version.get());
175 config_ui.set_main_deploy(main_deploy.get());
176 config_ui.set_apogee_delay(apogee_delay.get());
177 config_ui.set_radio_channel(radio_channel.get());
178 config_ui.set_callsign(callsign.get());
179 config_ui.set_clean();
180 } catch (InterruptedException ie) {
188 main_deploy.set(config_ui.main_deploy());
189 apogee_delay.set(config_ui.apogee_delay());
190 radio_channel.set(config_ui.radio_channel());
191 callsign.set(config_ui.callsign());
194 serial_line.printf("c m %d\n", main_deploy.get());
195 serial_line.printf("c d %d\n", apogee_delay.get());
196 serial_line.printf("c r %d\n", radio_channel.get());
197 serial_line.printf("c c %s\n", callsign.get());
198 serial_line.printf("c w\n");
199 } catch (InterruptedException ie) {
203 } catch (InterruptedException ie) {
208 public void actionPerformed(ActionEvent e) {
209 String cmd = e.getActionCommand();
210 if (cmd.equals("save")) {
213 } else if (cmd.equals("reset")) {
215 } else if (cmd.equals("close")) {
216 if (serial_line != null)
224 config_ui.make_visible();
225 // } catch (InterruptedException ie) {
230 public AltosConfig(JFrame given_owner) {
233 serial = new int_ref(0);
234 main_deploy = new int_ref(250);
235 apogee_delay = new int_ref(0);
236 radio_channel = new int_ref(0);
237 callsign = new string_ref("N0CALL");
238 version = new string_ref("unknown");
239 product = new string_ref("unknown");
241 device = AltosDeviceDialog.show(owner, AltosDevice.Any);
242 serial_line = new AltosSerial();
243 if (device != null) {
245 serial_line.open(device);
246 if (!device.matchProduct(AltosDevice.TeleMetrum))
248 config_thread = new Thread(this);
249 config_thread.start();
250 } catch (FileNotFoundException ee) {
251 JOptionPane.showMessageDialog(owner,
252 String.format("Cannot open device \"%s\"",
254 "Cannot open target device",
255 JOptionPane.ERROR_MESSAGE);
256 } catch (IOException ee) {
257 JOptionPane.showMessageDialog(owner,
259 ee.getLocalizedMessage(),
260 JOptionPane.ERROR_MESSAGE);