47de6156dd005003ee5ea8b9b741a36ecf6d5fb3
[fw/altos] / 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.*;
30
31 import libaltosJNI.*;
32
33 public class AltosConfig implements ActionListener {
34
35         class int_ref {
36                 int     value;
37
38                 public int get() {
39                         return value;
40                 }
41                 public void set(int i) {
42                         value = i;
43                 }
44                 public int_ref(int i) {
45                         value = i;
46                 }
47         }
48
49         class string_ref {
50                 String  value;
51
52                 public String get() {
53                         return value;
54                 }
55                 public void set(String i) {
56                         value = i;
57                 }
58                 public string_ref(String i) {
59                         value = i;
60                 }
61         }
62
63         JFrame          owner;
64         AltosDevice     device;
65         AltosSerial     serial_line;
66         boolean         remote;
67         int_ref         serial;
68         int_ref         main_deploy;
69         int_ref         apogee_delay;
70         int_ref         radio_channel;
71         int_ref         radio_calibration;
72         int_ref         flight_log_max;
73         string_ref      version;
74         string_ref      product;
75         string_ref      callsign;
76         AltosConfigUI   config_ui;
77         boolean         serial_started;
78
79         boolean get_int(String line, String label, int_ref x) {
80                 if (line.startsWith(label)) {
81                         try {
82                                 String tail = line.substring(label.length()).trim();
83                                 String[] tokens = tail.split("\\s+");
84                                 if (tokens.length > 0) {
85                                         int     i = Integer.parseInt(tokens[0]);
86                                         x.set(i);
87                                         return true;
88                                 }
89                         } catch (NumberFormatException ne) {
90                         }
91                 }
92                 return false;
93         }
94
95         boolean get_string(String line, String label, string_ref s) {
96                 if (line.startsWith(label)) {
97                         String  quoted = line.substring(label.length()).trim();
98
99                         if (quoted.startsWith("\""))
100                                 quoted = quoted.substring(1);
101                         if (quoted.endsWith("\""))
102                                 quoted = quoted.substring(0,quoted.length()-1);
103                         s.set(quoted);
104                         return true;
105                 } else {
106                         return false;
107                 }
108         }
109
110         void start_serial() throws InterruptedException {
111                 serial_started = true;
112                 if (remote) {
113                         serial_line.set_radio();
114                         serial_line.printf("p\nE 0\n");
115                         serial_line.flush_input();
116                 }
117         }
118
119         void stop_serial() throws InterruptedException {
120                 if (!serial_started)
121                         return;
122                 serial_started = false;
123                 if (remote) {
124                         serial_line.printf("~");
125                         serial_line.flush_output();
126                 }
127         }
128
129         void get_data() throws InterruptedException, TimeoutException {
130                 try {
131                         start_serial();
132                         serial_line.printf("c s\nv\n");
133                         for (;;) {
134                                 String line = serial_line.get_reply(5000);
135                                 if (line == null)
136                                         throw new TimeoutException();
137                                 get_int(line, "serial-number", serial);
138                                 get_int(line, "Main deploy:", main_deploy);
139                                 get_int(line, "Apogee delay:", apogee_delay);
140                                 get_int(line, "Radio channel:", radio_channel);
141                                 get_int(line, "Radio cal:", radio_calibration);
142                                 get_int(line, "Max flight log:", flight_log_max);
143                                 get_string(line, "Callsign:", callsign);
144                                 get_string(line,"software-version", version);
145                                 get_string(line,"product", product);
146
147                                 /* signals the end of the version info */
148                                 if (line.startsWith("software-version"))
149                                         break;
150                         }
151                 } finally {
152                         stop_serial();
153                 }
154         }
155
156         void init_ui () throws InterruptedException, TimeoutException {
157                 config_ui = new AltosConfigUI(owner, remote);
158                 config_ui.addActionListener(this);
159                 set_ui();
160         }
161
162         void abort() {
163                 JOptionPane.showMessageDialog(owner,
164                                               String.format("Connection to \"%s\" failed",
165                                                             device.toShortString()),
166                                               "Connection Failed",
167                                               JOptionPane.ERROR_MESSAGE);
168                 try {
169                         stop_serial();
170                 } catch (InterruptedException ie) {
171                 }
172                 serial_line.close();
173                 serial_line = null;
174         }
175
176         void set_ui() throws InterruptedException, TimeoutException {
177                 if (serial_line != null)
178                         get_data();
179                 config_ui.set_serial(serial.get());
180                 config_ui.set_product(product.get());
181                 config_ui.set_version(version.get());
182                 config_ui.set_main_deploy(main_deploy.get());
183                 config_ui.set_apogee_delay(apogee_delay.get());
184                 config_ui.set_radio_channel(radio_channel.get());
185                 config_ui.set_radio_calibration(radio_calibration.get());
186                 config_ui.set_flight_log_max(flight_log_max.get());
187                 config_ui.set_callsign(callsign.get());
188                 config_ui.set_clean();
189         }
190
191         void run_dialog() {
192         }
193
194         void save_data() {
195                 main_deploy.set(config_ui.main_deploy());
196                 apogee_delay.set(config_ui.apogee_delay());
197                 radio_channel.set(config_ui.radio_channel());
198                 radio_calibration.set(config_ui.radio_calibration());
199                 flight_log_max.set(config_ui.flight_log_max());
200                 callsign.set(config_ui.callsign());
201                 try {
202                         start_serial();
203                         serial_line.printf("c m %d\n", main_deploy.get());
204                         serial_line.printf("c d %d\n", apogee_delay.get());
205                         if (!remote) {
206                                 serial_line.printf("c r %d\n", radio_channel.get());
207                                 serial_line.printf("c f %d\n", radio_calibration.get());
208                         }
209                         serial_line.printf("c c %s\n", callsign.get());
210                         if (flight_log_max.get() != 0)
211                                 serial_line.printf("c l %d\n", flight_log_max.get());
212                         serial_line.printf("c w\n");
213                 } catch (InterruptedException ie) {
214                 } finally {
215                         try {
216                                 stop_serial();
217                         } catch (InterruptedException ie) {
218                         }
219                 }
220         }
221
222         public void actionPerformed(ActionEvent e) {
223                 String  cmd = e.getActionCommand();
224                 try {
225                         if (cmd.equals("Save")) {
226                                 save_data();
227                                 set_ui();
228                         } else if (cmd.equals("Reset")) {
229                                 set_ui();
230                         } else if (cmd.equals("Reboot")) {
231                                 if (serial_line != null) {
232                                         start_serial();
233                                         serial_line.printf("r eboot\n");
234                                         serial_line.flush_output();
235                                         stop_serial();
236                                         serial_line.close();
237                                 }
238                         } else if (cmd.equals("Close")) {
239                                 if (serial_line != null)
240                                         serial_line.close();
241                         }
242                 } catch (InterruptedException ie) {
243                         abort();
244                 } catch (TimeoutException te) {
245                         abort();
246                 }
247         }
248
249         public AltosConfig(JFrame given_owner) {
250                 owner = given_owner;
251
252                 serial = new int_ref(0);
253                 main_deploy = new int_ref(250);
254                 apogee_delay = new int_ref(0);
255                 radio_channel = new int_ref(0);
256                 radio_calibration = new int_ref(1186611);
257                 flight_log_max = new int_ref(0);
258                 callsign = new string_ref("N0CALL");
259                 version = new string_ref("unknown");
260                 product = new string_ref("unknown");
261
262                 device = AltosDeviceDialog.show(owner, AltosDevice.product_any);
263                 if (device != null) {
264                         try {
265                                 serial_line = new AltosSerial(device);
266                                 if (!device.matchProduct(AltosDevice.product_telemetrum))
267                                         remote = true;
268                                 try {
269                                         init_ui();
270                                         config_ui.make_visible();
271                                 } catch (InterruptedException ie) {
272                                         abort();
273                                 } catch (TimeoutException te) {
274                                         abort();
275                                 }
276                         } catch (FileNotFoundException ee) {
277                                 JOptionPane.showMessageDialog(owner,
278                                                               String.format("Cannot open device \"%s\"",
279                                                                             device.toShortString()),
280                                                               "Cannot open target device",
281                                                               JOptionPane.ERROR_MESSAGE);
282                         } catch (AltosSerialInUseException si) {
283                                 JOptionPane.showMessageDialog(owner,
284                                                               String.format("Device \"%s\" already in use",
285                                                                             device.toShortString()),
286                                                               "Device in use",
287                                                               JOptionPane.ERROR_MESSAGE);
288                         } catch (IOException ee) {
289                                 JOptionPane.showMessageDialog(owner,
290                                                               device.toShortString(),
291                                                               ee.getLocalizedMessage(),
292                                                               JOptionPane.ERROR_MESSAGE);
293                         }
294                 }
295         }
296 }