Merge remote branch 'origin/master' into new-packet-format
[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         string_ref      version;
87         string_ref      product;
88         string_ref      callsign;
89         AltosConfigUI   config_ui;
90
91
92         boolean get_int(String line, String label, int_ref x) {
93                 if (line.startsWith(label)) {
94                         try {
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]);
99                                         x.set(i);
100                                         return true;
101                                 }
102                         } catch (NumberFormatException ne) {
103                         }
104                 }
105                 return false;
106         }
107
108         boolean get_string(String line, String label, string_ref s) {
109                 if (line.startsWith(label)) {
110                         String  quoted = line.substring(label.length()).trim();
111
112                         if (quoted.startsWith("\""))
113                                 quoted = quoted.substring(1);
114                         if (quoted.endsWith("\""))
115                                 quoted = quoted.substring(0,quoted.length()-1);
116                         s.set(quoted);
117                         return true;
118                 } else {
119                         return false;
120                 }
121         }
122
123         void start_serial() throws InterruptedException {
124                 if (remote) {
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");
129                 }
130         }
131
132         void stop_serial() throws InterruptedException {
133                 if (remote) {
134                         serial_line.printf("~\n");
135                         serial_line.flush();
136                 }
137         }
138
139         void get_data() throws InterruptedException {
140                 try {
141                         start_serial();
142                         serial_line.printf("c s\nv\n");
143                         for (;;) {
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);
152
153                                 /* signals the end of the version info */
154                                 if (line.startsWith("software-version"))
155                                         break;
156                         }
157                 } finally {
158                         stop_serial();
159                 }
160         }
161
162         void init_ui () {
163                 config_ui = new AltosConfigUI(owner);
164                 config_ui.addActionListener(this);
165                 set_ui();
166         }
167
168         void set_ui() {
169                 try {
170                         if (serial_line != null)
171                                 get_data();
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) {
181                 }
182         }
183
184         void run_dialog() {
185         }
186
187         void save_data() {
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());
192                 try {
193                         start_serial();
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) {
200                 } finally {
201                         try {
202                                 stop_serial();
203                         } catch (InterruptedException ie) {
204                         }
205                 }
206         }
207
208         public void actionPerformed(ActionEvent e) {
209                 String  cmd = e.getActionCommand();
210                 if (cmd.equals("save")) {
211                         save_data();
212                         set_ui();
213                 } else if (cmd.equals("reset")) {
214                         set_ui();
215                 } else if (cmd.equals("close")) {
216                         if (serial_line != null)
217                                 serial_line.close();
218                 }
219         }
220
221         public void run () {
222                 try {
223                         init_ui();
224                         config_ui.make_visible();
225 //              } catch (InterruptedException ie) {
226                 } finally {
227                 }
228         }
229
230         public AltosConfig(JFrame given_owner) {
231                 owner = given_owner;
232
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");
240
241                 device = AltosDeviceDialog.show(owner, AltosDevice.Any);
242                 serial_line = new AltosSerial();
243                 if (device != null) {
244                         try {
245                                 serial_line.open(device);
246                                 if (!device.matchProduct(AltosDevice.TeleMetrum))
247                                         remote = true;
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\"",
253                                                                             device.getPath()),
254                                                               "Cannot open target device",
255                                                               JOptionPane.ERROR_MESSAGE);
256                         } catch (IOException ee) {
257                                 JOptionPane.showMessageDialog(owner,
258                                                               device.getPath(),
259                                                               ee.getLocalizedMessage(),
260                                                               JOptionPane.ERROR_MESSAGE);
261                         }
262                 }
263         }
264 }