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