Update java library versions
[fw/altos] / altosui / AltosConfigTD.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.event.*;
21 import javax.swing.*;
22 import java.io.*;
23 import java.util.concurrent.*;
24 import org.altusmetrum.altoslib_9.*;
25 import org.altusmetrum.altosuilib_9.*;
26
27 public class AltosConfigTD implements ActionListener {
28
29         class int_ref {
30                 int     value;
31
32                 public int get() {
33                         return value;
34                 }
35                 public void set(int i) {
36                         value = i;
37                 }
38                 public int_ref(int i) {
39                         value = i;
40                 }
41         }
42
43         class string_ref {
44                 String  value;
45
46                 public String get() {
47                         return value;
48                 }
49                 public void set(String i) {
50                         value = i;
51                 }
52                 public string_ref(String i) {
53                         value = i;
54                 }
55         }
56
57         JFrame          owner;
58         AltosDevice     device;
59         AltosSerial     serial_line;
60         int_ref         serial;
61         int_ref         radio_channel;
62         int_ref         radio_calibration;
63         int_ref         radio_setting;
64         int_ref         radio_frequency;
65         int_ref         telemetry_rate;
66         string_ref      config_version;
67         string_ref      version;
68         string_ref      product;
69         AltosConfigTDUI config_ui;
70         boolean         made_visible;
71
72         boolean get_int(String line, String label, int_ref x) {
73                 if (line.startsWith(label)) {
74                         try {
75                                 String tail = line.substring(label.length()).trim();
76                                 String[] tokens = tail.split("\\s+");
77                                 if (tokens.length > 0) {
78                                         int     i = Integer.parseInt(tokens[0]);
79                                         x.set(i);
80                                         return true;
81                                 }
82                         } catch (NumberFormatException ne) {
83                         }
84                 }
85                 return false;
86         }
87
88         boolean get_string(String line, String label, string_ref s) {
89                 if (line.startsWith(label)) {
90                         String  quoted = line.substring(label.length()).trim();
91
92                         if (quoted.startsWith("\""))
93                                 quoted = quoted.substring(1);
94                         if (quoted.endsWith("\""))
95                                 quoted = quoted.substring(0,quoted.length()-1);
96                         s.set(quoted);
97                         return true;
98                 } else {
99                         return false;
100                 }
101         }
102
103         synchronized void update_ui() {
104                 config_ui.set_serial(serial.get());
105                 config_ui.set_product(product.get());
106                 config_ui.set_version(version.get());
107                 config_ui.set_radio_frequency(frequency());
108                 config_ui.set_radio_calibration(radio_calibration.get());
109                 config_ui.set_telemetry_rate(telemetry_rate.get());
110                 config_ui.set_clean();
111                 if (!made_visible) {
112                         made_visible = true;
113                         config_ui.make_visible();
114                 }
115         }
116
117         void finish_input(String line) {
118                 if (line == null) {
119                         abort();
120                         return;
121                 }
122                 if (line.equals("all finished")) {
123                         if (serial_line != null)
124                                 update_ui();
125                         return;
126                 }
127         }
128
129         synchronized void process_line(String line) {
130                 if (line == null || line.equals("all finished")) {
131                         final String last_line = line;
132                         Runnable r = new Runnable() {
133                                         public void run() {
134                                                 finish_input(last_line);
135                                         }
136                                 };
137                         SwingUtilities.invokeLater(r);
138                 } else {
139                         get_string(line, "Config version", config_version);
140                         get_int(line, "serial-number", serial);
141                         get_int(line, "Radio channel:", radio_channel);
142                         get_int(line, "Radio cal:", radio_calibration);
143                         get_int(line, "Frequency:", radio_frequency);
144                         get_int(line, "Radio setting:", radio_setting);
145                         get_int(line, "Telemetry rate:", telemetry_rate);
146                         get_string(line,"software-version", version);
147                         get_string(line,"product", product);
148                 }
149         }
150
151         synchronized void reset_data() {
152                 serial.set(0);
153                 radio_channel.set(0);
154                 radio_setting.set(0);
155                 radio_frequency.set(0);
156                 radio_calibration.set(1186611);
157                 telemetry_rate.set(Altos.ao_telemetry_rate_38400);
158                 config_version.set("0.0");
159                 version.set("unknown");
160                 product.set("unknown");
161         }
162
163         synchronized double frequency() {
164                 return AltosConvert.radio_to_frequency(radio_frequency.get(),
165                                                        radio_setting.get(),
166                                                        radio_calibration.get(),
167                                                        radio_channel.get());
168         }
169
170         synchronized void set_frequency(double freq) {
171                 int     frequency = radio_frequency.get();
172                 int     setting = radio_setting.get();
173
174                 if (frequency > 0) {
175                         radio_frequency.set((int) Math.floor (freq * 1000 + 0.5));
176                 } else if (setting > 0) {
177                         radio_setting.set(AltosConvert.radio_frequency_to_setting(freq,
178                                                                                   radio_calibration.get()));
179                         radio_channel.set(0);
180                 } else {
181                         radio_channel.set(AltosConvert.radio_frequency_to_channel(freq));
182                 }
183         }
184
185         synchronized int telemetry_rate() {
186                 return telemetry_rate.get();
187         }
188
189         synchronized void set_telemetry_rate(int new_telemetry_rate){
190                 int     rate = telemetry_rate.get();
191
192                 if (rate >= 0)
193                         telemetry_rate.set(new_telemetry_rate);
194         }
195
196         final static int        serial_mode_read = 0;
197         final static int        serial_mode_save = 1;
198         final static int        serial_mode_reboot = 2;
199
200         class SerialData implements Runnable {
201                 AltosConfigTD   config;
202                 int             serial_mode;
203
204                 void get_data() {
205                         try {
206                                 boolean been_there = false;
207                                 config.reset_data();
208
209                                 for (;;) {
210                                         config.serial_line.printf("c s\nf\nv\n");
211                                         for (;;) {
212                                                 try {
213                                                         String line = config.serial_line.get_reply(5000);
214                                                         config.process_line(line);
215                                                         if (line != null && line.startsWith("software-version"))
216                                                                 break;
217                                                 } catch (Exception e) {
218                                                         break;
219                                                 }
220                                         }
221                                         if (been_there)
222                                                 break;
223                                         if (!config_version.get().equals("0.0"))
224                                                 break;
225                                         been_there = true;
226                                         if (config != null && config.serial_line != null) {
227                                                 config.serial_line.printf("C\n ");
228                                                 config.serial_line.flush_input();
229                                         }
230                                 }
231                         } catch (InterruptedException ie) {
232                         }
233                         /*
234                          * This makes sure the displayed frequency respects the limits that the
235                          * available firmware version might place on the actual frequency
236                          */
237                         config.set_frequency(AltosPreferences.frequency(serial.get()));
238                         config.set_telemetry_rate(AltosPreferences.telemetry_rate(serial.get()));
239                         config.process_line("all finished");
240                 }
241
242                 void save_data() {
243                         double frequency = frequency();
244                         if (frequency != 0)
245                                 AltosPreferences.set_frequency(serial.get(),
246                                                                frequency);
247                         AltosPreferences.set_telemetry_rate(serial.get(),
248                                                             telemetry_rate());
249                 }
250
251                 public void run () {
252                         switch (serial_mode) {
253                         case serial_mode_save:
254                                 save_data();
255                                 /* fall through ... */
256                         case serial_mode_read:
257                                 get_data();
258                                 break;
259                         }
260                 }
261
262                 public SerialData(AltosConfigTD in_config, int in_serial_mode) {
263                         config = in_config;
264                         serial_mode = in_serial_mode;
265                 }
266         }
267
268         void run_serial_thread(int serial_mode) {
269                 SerialData      sd = new SerialData(this, serial_mode);
270                 Thread          st = new Thread(sd);
271                 st.start();
272         }
273
274         void init_ui () throws InterruptedException, TimeoutException {
275                 config_ui = new AltosConfigTDUI(owner);
276                 config_ui.addActionListener(this);
277                 serial_line.set_frame(owner);
278                 set_ui();
279         }
280
281         void abort() {
282                 if (serial_line != null) {
283                         serial_line.close();
284                         serial_line = null;
285                 }
286                 JOptionPane.showMessageDialog(owner,
287                                               String.format("Connection to \"%s\" failed",
288                                                             device.toShortString()),
289                                               "Connection Failed",
290                                               JOptionPane.ERROR_MESSAGE);
291                 config_ui.setVisible(false);
292         }
293
294         void set_ui() throws InterruptedException, TimeoutException {
295                 if (serial_line != null)
296                         run_serial_thread(serial_mode_read);
297                 else
298                         update_ui();
299         }
300
301         void save_data() {
302                 double  freq = config_ui.radio_frequency();
303                 set_frequency(freq);
304                 int telemetry_rate = config_ui.telemetry_rate();
305                 set_telemetry_rate(telemetry_rate);
306                 run_serial_thread(serial_mode_save);
307         }
308
309         public void actionPerformed(ActionEvent e) {
310                 String  cmd = e.getActionCommand();
311                 try {
312                         if (cmd.equals("Save")) {
313                                 save_data();
314                         } else if (cmd.equals("Reset")) {
315                                 set_ui();
316                         } else if (cmd.equals("Reboot")) {
317                                 if (serial_line != null)
318                                         run_serial_thread(serial_mode_reboot);
319                         } else if (cmd.equals("Close")) {
320                                 if (serial_line != null)
321                                         serial_line.close();
322                         }
323                 } catch (InterruptedException ie) {
324                         abort();
325                 } catch (TimeoutException te) {
326                         abort();
327                 }
328         }
329
330         public AltosConfigTD(JFrame given_owner) {
331                 owner = given_owner;
332
333                 serial = new int_ref(0);
334                 radio_channel = new int_ref(0);
335                 radio_setting = new int_ref(0);
336                 radio_frequency = new int_ref(0);
337                 radio_calibration = new int_ref(1186611);
338                 telemetry_rate = new int_ref(AltosLib.ao_telemetry_rate_38400);
339                 config_version = new string_ref("0.0");
340                 version = new string_ref("unknown");
341                 product = new string_ref("unknown");
342
343                 device = AltosDeviceUIDialog.show(owner, Altos.product_basestation);
344                 if (device != null) {
345                         try {
346                                 serial_line = new AltosSerial(device);
347                                 try {
348                                         init_ui();
349                                 } catch (InterruptedException ie) {
350                                         abort();
351                                 } catch (TimeoutException te) {
352                                         abort();
353                                 }
354                         } catch (FileNotFoundException ee) {
355                                 JOptionPane.showMessageDialog(owner,
356                                                               ee.getMessage(),
357                                                               "Cannot open target device",
358                                                               JOptionPane.ERROR_MESSAGE);
359                         } catch (AltosSerialInUseException si) {
360                                 JOptionPane.showMessageDialog(owner,
361                                                               String.format("Device \"%s\" already in use",
362                                                                             device.toShortString()),
363                                                               "Device in use",
364                                                               JOptionPane.ERROR_MESSAGE);
365                         }
366                 }
367         }
368 }