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