4389e49b0c57ac431512a5544b3243441390afa0
[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_5.*;
25 import org.altusmetrum.altosuilib_3.*;
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                                         config.serial_line.printf("C\n ");
227                                         config.serial_line.flush_input();
228                                 }
229                         } catch (InterruptedException ie) {
230                         }
231                         /*
232                          * This makes sure the displayed frequency respects the limits that the
233                          * available firmware version might place on the actual frequency
234                          */
235                         config.set_frequency(AltosPreferences.frequency(serial.get()));
236                         config.set_telemetry_rate(AltosPreferences.telemetry_rate(serial.get()));
237                         config.process_line("all finished");
238                 }
239
240                 void save_data() {
241                         double frequency = frequency();
242                         if (frequency != 0)
243                                 AltosPreferences.set_frequency(serial.get(),
244                                                                frequency);
245                         AltosPreferences.set_telemetry_rate(serial.get(),
246                                                             telemetry_rate());
247                 }
248
249                 public void run () {
250                         switch (serial_mode) {
251                         case serial_mode_save:
252                                 save_data();
253                                 /* fall through ... */
254                         case serial_mode_read:
255                                 get_data();
256                                 break;
257                         }
258                 }
259
260                 public SerialData(AltosConfigTD in_config, int in_serial_mode) {
261                         config = in_config;
262                         serial_mode = in_serial_mode;
263                 }
264         }
265
266         void run_serial_thread(int serial_mode) {
267                 SerialData      sd = new SerialData(this, serial_mode);
268                 Thread          st = new Thread(sd);
269                 st.start();
270         }
271
272         void init_ui () throws InterruptedException, TimeoutException {
273                 config_ui = new AltosConfigTDUI(owner);
274                 config_ui.addActionListener(this);
275                 serial_line.set_frame(owner);
276                 set_ui();
277         }
278
279         void abort() {
280                 serial_line.close();
281                 serial_line = null;
282                 JOptionPane.showMessageDialog(owner,
283                                               String.format("Connection to \"%s\" failed",
284                                                             device.toShortString()),
285                                               "Connection Failed",
286                                               JOptionPane.ERROR_MESSAGE);
287                 config_ui.setVisible(false);
288         }
289
290         void set_ui() throws InterruptedException, TimeoutException {
291                 if (serial_line != null)
292                         run_serial_thread(serial_mode_read);
293                 else
294                         update_ui();
295         }
296
297         void save_data() {
298                 double  freq = config_ui.radio_frequency();
299                 set_frequency(freq);
300                 int telemetry_rate = config_ui.telemetry_rate();
301                 set_telemetry_rate(telemetry_rate);
302                 run_serial_thread(serial_mode_save);
303         }
304
305         public void actionPerformed(ActionEvent e) {
306                 String  cmd = e.getActionCommand();
307                 try {
308                         if (cmd.equals("Save")) {
309                                 save_data();
310                         } else if (cmd.equals("Reset")) {
311                                 set_ui();
312                         } else if (cmd.equals("Reboot")) {
313                                 if (serial_line != null)
314                                         run_serial_thread(serial_mode_reboot);
315                         } else if (cmd.equals("Close")) {
316                                 if (serial_line != null)
317                                         serial_line.close();
318                         }
319                 } catch (InterruptedException ie) {
320                         abort();
321                 } catch (TimeoutException te) {
322                         abort();
323                 }
324         }
325
326         public AltosConfigTD(JFrame given_owner) {
327                 owner = given_owner;
328
329                 serial = new int_ref(0);
330                 radio_channel = new int_ref(0);
331                 radio_setting = new int_ref(0);
332                 radio_frequency = new int_ref(0);
333                 radio_calibration = new int_ref(1186611);
334                 telemetry_rate = new int_ref(AltosLib.ao_telemetry_rate_38400);
335                 config_version = new string_ref("0.0");
336                 version = new string_ref("unknown");
337                 product = new string_ref("unknown");
338
339                 device = AltosDeviceUIDialog.show(owner, Altos.product_basestation);
340                 if (device != null) {
341                         try {
342                                 serial_line = new AltosSerial(device);
343                                 try {
344                                         init_ui();
345                                 } catch (InterruptedException ie) {
346                                         abort();
347                                 } catch (TimeoutException te) {
348                                         abort();
349                                 }
350                         } catch (FileNotFoundException ee) {
351                                 JOptionPane.showMessageDialog(owner,
352                                                               ee.getMessage(),
353                                                               "Cannot open target device",
354                                                               JOptionPane.ERROR_MESSAGE);
355                         } catch (AltosSerialInUseException si) {
356                                 JOptionPane.showMessageDialog(owner,
357                                                               String.format("Device \"%s\" already in use",
358                                                                             device.toShortString()),
359                                                               "Device in use",
360                                                               JOptionPane.ERROR_MESSAGE);
361                         }
362                 }
363         }
364 }