altosui: Display current altitude in monitor idle Pad tab
[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         SerialData serial_data;
202         Thread serial_thread;
203
204         class SerialData implements Runnable {
205                 AltosConfigTD   config;
206                 int             serial_mode;
207
208                 void get_data() {
209                         try {
210                                 boolean been_there = false;
211                                 config.reset_data();
212
213                                 while (config.serial_line != null) {
214                                         config.serial_line.printf("c s\nf\nv\n");
215                                         while (config.serial_line != null) {
216                                                 try {
217                                                         String line = config.serial_line.get_reply(5000);
218                                                         config.process_line(line);
219                                                         if (line != null && line.startsWith("software-version"))
220                                                                 break;
221                                                 } catch (Exception e) {
222                                                         break;
223                                                 }
224                                         }
225                                         if (been_there)
226                                                 break;
227                                         if (!config_version.get().equals("0.0"))
228                                                 break;
229                                         been_there = true;
230                                         if (config != null && config.serial_line != null) {
231                                                 config.serial_line.printf("C\n ");
232                                                 config.serial_line.flush_input();
233                                         }
234                                 }
235                         } catch (InterruptedException ie) {
236                         }
237                         /*
238                          * This makes sure the displayed frequency respects the limits that the
239                          * available firmware version might place on the actual frequency
240                          */
241                         config.set_frequency(AltosPreferences.frequency(serial.get()));
242                         config.set_telemetry_rate(AltosPreferences.telemetry_rate(serial.get()));
243                         config.process_line("all finished");
244                 }
245
246                 void save_data() {
247                         double frequency = frequency();
248                         if (frequency != 0)
249                                 AltosPreferences.set_frequency(serial.get(),
250                                                                frequency);
251                         AltosPreferences.set_telemetry_rate(serial.get(),
252                                                             telemetry_rate());
253                 }
254
255                 public void run () {
256                         switch (serial_mode) {
257                         case serial_mode_save:
258                                 save_data();
259                                 /* fall through ... */
260                         case serial_mode_read:
261                                 get_data();
262                                 serial_thread = null;
263                                 break;
264                         }
265                 }
266
267                 public SerialData(AltosConfigTD in_config, int in_serial_mode) {
268                         config = in_config;
269                         serial_mode = in_serial_mode;
270                 }
271         }
272
273         void run_serial_thread(int serial_mode) {
274                 serial_data = new SerialData(this, serial_mode);
275                 serial_thread = new Thread(serial_data);
276                 serial_thread.start();
277         }
278
279         void abort_serial_thread() {
280                 if (serial_thread != null) {
281                         serial_thread.interrupt();
282                         serial_thread = null;
283                 }
284         }
285         void init_ui () throws InterruptedException, TimeoutException {
286                 config_ui = new AltosConfigTDUI(owner);
287                 config_ui.addActionListener(this);
288                 serial_line.set_frame(owner);
289                 set_ui();
290         }
291
292         void abort() {
293                 abort_serial_thread();
294                 if (serial_line != null) {
295                         serial_line.close();
296                         serial_line = null;
297                 }
298                 JOptionPane.showMessageDialog(owner,
299                                               String.format("Connection to \"%s\" failed",
300                                                             device.toShortString()),
301                                               "Connection Failed",
302                                               JOptionPane.ERROR_MESSAGE);
303                 config_ui.setVisible(false);
304         }
305
306         void set_ui() throws InterruptedException, TimeoutException {
307                 if (serial_line != null)
308                         run_serial_thread(serial_mode_read);
309                 else
310                         update_ui();
311         }
312
313         void save_data() {
314                 double  freq = config_ui.radio_frequency();
315                 set_frequency(freq);
316                 int telemetry_rate = config_ui.telemetry_rate();
317                 set_telemetry_rate(telemetry_rate);
318                 run_serial_thread(serial_mode_save);
319         }
320
321         public void actionPerformed(ActionEvent e) {
322                 String  cmd = e.getActionCommand();
323                 try {
324                         if (cmd.equals("Save")) {
325                                 save_data();
326                         } else if (cmd.equals("Reset")) {
327                                 set_ui();
328                         } else if (cmd.equals("Reboot")) {
329                                 if (serial_line != null)
330                                         run_serial_thread(serial_mode_reboot);
331                         } else if (cmd.equals("Close")) {
332                                 if (serial_line != null)
333                                         serial_line.close();
334                         }
335                 } catch (InterruptedException ie) {
336                         abort();
337                 } catch (TimeoutException te) {
338                         abort();
339                 }
340         }
341
342         public AltosConfigTD(JFrame given_owner) {
343                 owner = given_owner;
344
345                 serial = new int_ref(0);
346                 radio_channel = new int_ref(0);
347                 radio_setting = new int_ref(0);
348                 radio_frequency = new int_ref(0);
349                 radio_calibration = new int_ref(1186611);
350                 telemetry_rate = new int_ref(AltosLib.ao_telemetry_rate_38400);
351                 config_version = new string_ref("0.0");
352                 version = new string_ref("unknown");
353                 product = new string_ref("unknown");
354
355                 device = AltosDeviceUIDialog.show(owner, Altos.product_basestation);
356                 if (device != null) {
357                         try {
358                                 serial_line = new AltosSerial(device);
359                                 try {
360                                         init_ui();
361                                 } catch (InterruptedException ie) {
362                                         abort();
363                                 } catch (TimeoutException te) {
364                                         abort();
365                                 }
366                         } catch (FileNotFoundException ee) {
367                                 JOptionPane.showMessageDialog(owner,
368                                                               ee.getMessage(),
369                                                               "Cannot open target device",
370                                                               JOptionPane.ERROR_MESSAGE);
371                         } catch (AltosSerialInUseException si) {
372                                 JOptionPane.showMessageDialog(owner,
373                                                               String.format("Device \"%s\" already in use",
374                                                                             device.toShortString()),
375                                                               "Device in use",
376                                                               JOptionPane.ERROR_MESSAGE);
377                         }
378                 }
379         }
380 }