AltosTelemetryReader: actually open serial port
[fw/altos] / ao-tools / altosui / AltosConfig.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package altosui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import java.io.*;
26 import java.util.*;
27 import java.text.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.LinkedBlockingQueue;
30
31 import altosui.Altos;
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;
44
45 import libaltosJNI.*;
46
47 public class AltosConfig implements Runnable, ActionListener {
48
49         class int_ref {
50                 int     value;
51
52                 public int get() {
53                         return value;
54                 }
55                 public void set(int i) {
56                         value = i;
57                 }
58                 public int_ref(int i) {
59                         value = i;
60                 }
61         }
62
63         class string_ref {
64                 String  value;
65
66                 public String get() {
67                         return value;
68                 }
69                 public void set(String i) {
70                         value = i;
71                 }
72                 public string_ref(String i) {
73                         value = i;
74                 }
75         }
76
77         JFrame          owner;
78         AltosDevice     device;
79         AltosSerial     serial_line;
80         boolean         remote;
81         Thread          config_thread;
82         int_ref         serial;
83         int_ref         main_deploy;
84         int_ref         apogee_delay;
85         int_ref         radio_channel;
86         int_ref         radio_calibration;
87         string_ref      version;
88         string_ref      product;
89         string_ref      callsign;
90         AltosConfigUI   config_ui;
91
92
93         boolean get_int(String line, String label, int_ref x) {
94                 if (line.startsWith(label)) {
95                         try {
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]);
100                                         x.set(i);
101                                         return true;
102                                 }
103                         } catch (NumberFormatException ne) {
104                         }
105                 }
106                 return false;
107         }
108
109         boolean get_string(String line, String label, string_ref s) {
110                 if (line.startsWith(label)) {
111                         String  quoted = line.substring(label.length()).trim();
112
113                         if (quoted.startsWith("\""))
114                                 quoted = quoted.substring(1);
115                         if (quoted.endsWith("\""))
116                                 quoted = quoted.substring(0,quoted.length()-1);
117                         s.set(quoted);
118                         return true;
119                 } else {
120                         return false;
121                 }
122         }
123
124         void start_serial() throws InterruptedException {
125                 if (remote) {
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();
130                 }
131         }
132
133         void stop_serial() throws InterruptedException {
134                 if (remote) {
135                         serial_line.printf("~");
136                         serial_line.flush_output();
137                 }
138         }
139
140         void get_data() throws InterruptedException {
141                 try {
142                         start_serial();
143                         serial_line.printf("c s\nv\n");
144                         for (;;) {
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);
154
155                                 /* signals the end of the version info */
156                                 if (line.startsWith("software-version"))
157                                         break;
158                         }
159                 } finally {
160                         stop_serial();
161                 }
162         }
163
164         void init_ui () {
165                 config_ui = new AltosConfigUI(owner);
166                 config_ui.addActionListener(this);
167                 set_ui();
168         }
169
170         void set_ui() {
171                 try {
172                         if (serial_line != null)
173                                 get_data();
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) {
184                 }
185         }
186
187         void run_dialog() {
188         }
189
190         void save_data() {
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());
196                 try {
197                         start_serial();
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) {
205                 } finally {
206                         try {
207                                 stop_serial();
208                         } catch (InterruptedException ie) {
209                         }
210                 }
211         }
212
213         public void actionPerformed(ActionEvent e) {
214                 String  cmd = e.getActionCommand();
215                 if (cmd.equals("save")) {
216                         save_data();
217                         set_ui();
218                 } else if (cmd.equals("reset")) {
219                         set_ui();
220                 } else if (cmd.equals("close")) {
221                         if (serial_line != null)
222                                 serial_line.close();
223                 }
224         }
225
226         public void run () {
227                 try {
228                         init_ui();
229                         config_ui.make_visible();
230 //              } catch (InterruptedException ie) {
231                 } finally {
232                 }
233         }
234
235         public AltosConfig(JFrame given_owner) {
236                 owner = given_owner;
237
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");
246
247                 device = AltosDeviceDialog.show(owner, AltosDevice.product_any);
248                 serial_line = new AltosSerial();
249                 if (device != null) {
250                         try {
251                                 serial_line.open(device);
252                                 if (!device.matchProduct(AltosDevice.product_telemetrum))
253                                         remote = true;
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\"",
259                                                                             device.getPath()),
260                                                               "Cannot open target device",
261                                                               JOptionPane.ERROR_MESSAGE);
262                         } catch (IOException ee) {
263                                 JOptionPane.showMessageDialog(owner,
264                                                               device.getPath(),
265                                                               ee.getLocalizedMessage(),
266                                                               JOptionPane.ERROR_MESSAGE);
267                         }
268                 }
269         }
270 }