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