Merge branch 'buttonbox' of git://git.gag.com/fw/altos into buttonbox
[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 libaltosJNI.*;
32
33 public class AltosConfig implements Runnable, 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         Thread          config_thread;
68         int_ref         serial;
69         int_ref         main_deploy;
70         int_ref         apogee_delay;
71         int_ref         radio_channel;
72         int_ref         radio_calibration;
73         string_ref      version;
74         string_ref      product;
75         string_ref      callsign;
76         AltosConfigUI   config_ui;
77
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                 if (remote) {
112                         serial_line.set_channel(AltosPreferences.channel(device.getSerial()));
113                         serial_line.set_callsign(AltosPreferences.callsign());
114                         serial_line.printf("p\n");
115                         serial_line.flush_input();
116                 }
117         }
118
119         void stop_serial() throws InterruptedException {
120                 if (remote) {
121                         serial_line.printf("~");
122                         serial_line.flush_output();
123                 }
124         }
125
126         void get_data() throws InterruptedException {
127                 try {
128                         start_serial();
129                         serial_line.printf("c s\nv\n");
130                         for (;;) {
131                                 String line = serial_line.get_reply();
132                                 get_int(line, "serial-number", serial);
133                                 get_int(line, "Main deploy:", main_deploy);
134                                 get_int(line, "Apogee delay:", apogee_delay);
135                                 get_int(line, "Radio channel:", radio_channel);
136                                 get_int(line, "Radio cal:", radio_calibration);
137                                 get_string(line, "Callsign:", callsign);
138                                 get_string(line,"software-version", version);
139                                 get_string(line,"product", product);
140
141                                 /* signals the end of the version info */
142                                 if (line.startsWith("software-version"))
143                                         break;
144                         }
145                 } finally {
146                         stop_serial();
147                 }
148         }
149
150         void init_ui () {
151                 config_ui = new AltosConfigUI(owner);
152                 config_ui.addActionListener(this);
153                 set_ui();
154         }
155
156         void set_ui() {
157                 try {
158                         if (serial_line != null)
159                                 get_data();
160                         config_ui.set_serial(serial.get());
161                         config_ui.set_product(product.get());
162                         config_ui.set_version(version.get());
163                         config_ui.set_main_deploy(main_deploy.get());
164                         config_ui.set_apogee_delay(apogee_delay.get());
165                         config_ui.set_radio_channel(radio_channel.get());
166                         config_ui.set_radio_calibration(radio_calibration.get());
167                         config_ui.set_callsign(callsign.get());
168                         config_ui.set_clean();
169                 } catch (InterruptedException ie) {
170                 }
171         }
172
173         void run_dialog() {
174         }
175
176         void save_data() {
177                 main_deploy.set(config_ui.main_deploy());
178                 apogee_delay.set(config_ui.apogee_delay());
179                 radio_channel.set(config_ui.radio_channel());
180                 radio_calibration.set(config_ui.radio_calibration());
181                 callsign.set(config_ui.callsign());
182                 try {
183                         start_serial();
184                         serial_line.printf("c m %d\n", main_deploy.get());
185                         serial_line.printf("c d %d\n", apogee_delay.get());
186                         serial_line.printf("c r %d\n", radio_channel.get());
187                         serial_line.printf("c f %d\n", radio_calibration.get());
188                         serial_line.printf("c c %s\n", callsign.get());
189                         serial_line.printf("c w\n");
190                 } catch (InterruptedException ie) {
191                 } finally {
192                         try {
193                                 stop_serial();
194                         } catch (InterruptedException ie) {
195                         }
196                 }
197         }
198
199         public void actionPerformed(ActionEvent e) {
200                 String  cmd = e.getActionCommand();
201                 if (cmd.equals("Save")) {
202                         save_data();
203                         set_ui();
204                 } else if (cmd.equals("Reset")) {
205                         set_ui();
206                 } else if (cmd.equals("Reboot")) {
207                         if (serial_line != null) {
208                                 try {
209                                         start_serial();
210                                         serial_line.printf("r eboot\n");
211                                 } catch (InterruptedException ie) {
212                                 } finally {
213                                         try {
214                                                 stop_serial();
215                                         } catch (InterruptedException ie) {
216                                         }
217                                 }
218                                 serial_line.close();
219                         }
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                 if (device != null) {
249                         try {
250                                 serial_line = new AltosSerial(device);
251                                 if (!device.matchProduct(AltosDevice.product_telemetrum))
252                                         remote = true;
253                                 config_thread = new Thread(this);
254                                 config_thread.start();
255                         } catch (FileNotFoundException ee) {
256                                 JOptionPane.showMessageDialog(owner,
257                                                               String.format("Cannot open device \"%s\"",
258                                                                             device.getPath()),
259                                                               "Cannot open target device",
260                                                               JOptionPane.ERROR_MESSAGE);
261                         } catch (AltosSerialInUseException si) {
262                                 JOptionPane.showMessageDialog(owner,
263                                                               String.format("Device \"%s\" already in use",
264                                                                             device.getPath()),
265                                                               "Device in use",
266                                                               JOptionPane.ERROR_MESSAGE);
267                         } catch (IOException ee) {
268                                 JOptionPane.showMessageDialog(owner,
269                                                               device.getPath(),
270                                                               ee.getLocalizedMessage(),
271                                                               JOptionPane.ERROR_MESSAGE);
272                         }
273                 }
274         }
275 }