altosui: Must set radio calibration before radio setting
[fw/altos] / altosui / AltosConfig.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 AltosConfig 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         boolean         remote;
67         AltosConfigData remote_config_data;
68         double          remote_frequency;
69         int_ref         serial;
70         int_ref         main_deploy;
71         int_ref         apogee_delay;
72         int_ref         radio_channel;
73         int_ref         radio_calibration;
74         int_ref         flight_log_max;
75         int_ref         ignite_mode;
76         int_ref         pad_orientation;
77         int_ref         radio_setting;
78         string_ref      version;
79         string_ref      product;
80         string_ref      callsign;
81         AltosConfigUI   config_ui;
82         boolean         serial_started;
83
84         boolean get_int(String line, String label, int_ref x) {
85                 if (line.startsWith(label)) {
86                         try {
87                                 String tail = line.substring(label.length()).trim();
88                                 String[] tokens = tail.split("\\s+");
89                                 if (tokens.length > 0) {
90                                         int     i = Integer.parseInt(tokens[0]);
91                                         x.set(i);
92                                         return true;
93                                 }
94                         } catch (NumberFormatException ne) {
95                         }
96                 }
97                 return false;
98         }
99
100         boolean get_string(String line, String label, string_ref s) {
101                 if (line.startsWith(label)) {
102                         String  quoted = line.substring(label.length()).trim();
103
104                         if (quoted.startsWith("\""))
105                                 quoted = quoted.substring(1);
106                         if (quoted.endsWith("\""))
107                                 quoted = quoted.substring(0,quoted.length()-1);
108                         s.set(quoted);
109                         return true;
110                 } else {
111                         return false;
112                 }
113         }
114
115         void start_serial() throws InterruptedException, TimeoutException {
116                 serial_started = true;
117                 if (remote)
118                         serial_line.start_remote();
119         }
120
121         void stop_serial() throws InterruptedException {
122                 if (!serial_started)
123                         return;
124                 serial_started = false;
125                 if (remote)
126                         serial_line.stop_remote();
127         }
128
129         void update_ui() {
130                 config_ui.set_serial(serial.get());
131                 config_ui.set_product(product.get());
132                 config_ui.set_version(version.get());
133                 config_ui.set_main_deploy(main_deploy.get());
134                 config_ui.set_apogee_delay(apogee_delay.get());
135                 config_ui.set_radio_calibration(radio_calibration.get());
136                 config_ui.set_radio_frequency(frequency());
137                 config_ui.set_flight_log_max(flight_log_max.get());
138                 config_ui.set_ignite_mode(ignite_mode.get());
139                 config_ui.set_pad_orientation(pad_orientation.get());
140                 config_ui.set_callsign(callsign.get());
141                 config_ui.set_clean();
142                 config_ui.make_visible();
143         }
144
145         void process_line(String line) {
146                 if (line == null) {
147                         abort();
148                         return;
149                 }
150                 if (line.equals("done")) {
151                         if (serial_line != null)
152                                 update_ui();
153                         return;
154                 }
155                 get_int(line, "serial-number", serial);
156                 get_int(line, "Main deploy:", main_deploy);
157                 get_int(line, "Apogee delay:", apogee_delay);
158                 get_int(line, "Radio channel:", radio_channel);
159                 get_int(line, "Radio cal:", radio_calibration);
160                 get_int(line, "Max flight log:", flight_log_max);
161                 get_int(line, "Ignite mode:", ignite_mode);
162                 get_int(line, "Pad orientation:", pad_orientation);
163                 get_int(line, "Radio setting:", radio_setting);
164                 get_string(line, "Callsign:", callsign);
165                 get_string(line,"software-version", version);
166                 get_string(line,"product", product);
167         }
168
169         final static int        serial_mode_read = 0;
170         final static int        serial_mode_save = 1;
171         final static int        serial_mode_reboot = 2;
172
173         class SerialData implements Runnable {
174                 AltosConfig     config;
175                 int             serial_mode;
176
177                 void process_line(String line) {
178                         config.process_line(line);
179                 }
180                 void callback(String in_line) {
181                         final String line = in_line;
182                         Runnable r = new Runnable() {
183                                         public void run() {
184                                                 process_line(line);
185                                         }
186                                 };
187                         SwingUtilities.invokeLater(r);
188                 }
189
190                 void get_data() {
191                         try {
192                                 config.start_serial();
193                                 config.serial_line.printf("c s\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                         } catch (InterruptedException ie) {
207                         } catch (TimeoutException te) {
208                         } finally {
209                                 try {
210                                         stop_serial();
211                                 } catch (InterruptedException ie) {
212                                 }
213                         }
214                         callback("done");
215                 }
216
217                 void save_data() {
218                         try {
219                                 double frequency = frequency();
220                                 boolean has_setting = radio_setting.get() > 0;
221                                 start_serial();
222                                 serial_line.printf("c m %d\n", main_deploy.get());
223                                 serial_line.printf("c d %d\n", apogee_delay.get());
224                                 if (!remote)
225                                         serial_line.printf("c f %d\n", radio_calibration.get());
226                                 serial_line.set_radio_frequency(frequency,
227                                                                 has_setting,
228                                                                 radio_calibration.get());
229                                 if (remote) {
230                                         serial_line.stop_remote();
231                                         serial_line.set_radio_frequency(frequency,
232                                                                         has_setting,
233                                                                         radio_calibration.get());
234                                         AltosPreferences.set_frequency(device.getSerial(), frequency);
235                                         serial_line.start_remote();
236                                 }
237                                 serial_line.printf("c c %s\n", callsign.get());
238                                 if (flight_log_max.get() != 0)
239                                         serial_line.printf("c l %d\n", flight_log_max.get());
240                                 if (ignite_mode.get() >= 0)
241                                         serial_line.printf("c i %d\n", ignite_mode.get());
242                                 if (pad_orientation.get() >= 0)
243                                         serial_line.printf("c o %d\n", pad_orientation.get());
244                                 serial_line.printf("c w\n");
245                         } catch (InterruptedException ie) {
246                         } catch (TimeoutException te) {
247                         } finally {
248                                 try {
249                                         stop_serial();
250                                 } catch (InterruptedException ie) {
251                                 }
252                         }
253                 }
254
255                 void reboot() {
256                         try {
257                                 start_serial();
258                                 serial_line.printf("r eboot\n");
259                                 serial_line.flush_output();
260                         } catch (InterruptedException ie) {
261                         } catch (TimeoutException te) {
262                         } finally {
263                                 try {
264                                         stop_serial();
265                                 } catch (InterruptedException ie) {
266                                 }
267                                 serial_line.close();
268                         }
269                 }
270
271                 public void run () {
272                         switch (serial_mode) {
273                         case serial_mode_save:
274                                 save_data();
275                                 /* fall through ... */
276                         case serial_mode_read:
277                                 get_data();
278                                 break;
279                         case serial_mode_reboot:
280                                 reboot();
281                                 break;
282                         }
283                 }
284
285                 public SerialData(AltosConfig in_config, int in_serial_mode) {
286                         config = in_config;
287                         serial_mode = in_serial_mode;
288                 }
289         }
290
291         void run_serial_thread(int serial_mode) {
292                 SerialData      sd = new SerialData(this, serial_mode);
293                 Thread          st = new Thread(sd);
294                 st.start();
295         }
296
297         void init_ui () throws InterruptedException, TimeoutException {
298                 config_ui = new AltosConfigUI(owner, remote);
299                 config_ui.addActionListener(this);
300                 serial_line.set_frame(owner);
301                 set_ui();
302         }
303
304         void abort() {
305                 serial_line.close();
306                 serial_line = null;
307                 JOptionPane.showMessageDialog(owner,
308                                               String.format("Connection to \"%s\" failed",
309                                                             device.toShortString()),
310                                               "Connection Failed",
311                                               JOptionPane.ERROR_MESSAGE);
312                 config_ui.setVisible(false);
313         }
314
315         void set_ui() throws InterruptedException, TimeoutException {
316                 if (serial_line != null)
317                         run_serial_thread(serial_mode_read);
318                 else
319                         update_ui();
320         }
321
322         double frequency() {
323                 System.out.printf("setting %d channel %d calibration %d\n",
324                                   radio_setting.get(), radio_channel.get(), radio_calibration.get());
325                 return AltosConvert.radio_to_frequency(radio_setting.get(),
326                                                        radio_calibration.get(),
327                                                        radio_channel.get());
328         }
329
330         void set_frequency(double freq) {
331                 int     setting = radio_setting.get();
332
333                 if (setting > 0) {
334                         radio_setting.set(AltosConvert.radio_frequency_to_setting(freq,
335                                                                                   radio_calibration.get()));
336                         radio_channel.set(0);
337                 } else {
338                         radio_channel.set(AltosConvert.radio_frequency_to_channel(freq));
339                 }
340         }
341
342         void save_data() {
343                 main_deploy.set(config_ui.main_deploy());
344                 apogee_delay.set(config_ui.apogee_delay());
345                 radio_calibration.set(config_ui.radio_calibration());
346                 set_frequency(config_ui.radio_frequency());
347                 flight_log_max.set(config_ui.flight_log_max());
348                 ignite_mode.set(config_ui.ignite_mode());
349                 pad_orientation.set(config_ui.pad_orientation());
350                 callsign.set(config_ui.callsign());
351                 run_serial_thread(serial_mode_save);
352         }
353
354         public void actionPerformed(ActionEvent e) {
355                 String  cmd = e.getActionCommand();
356                 try {
357                         if (cmd.equals("Save")) {
358                                 save_data();
359                         } else if (cmd.equals("Reset")) {
360                                 set_ui();
361                         } else if (cmd.equals("Reboot")) {
362                                 if (serial_line != null)
363                                         run_serial_thread(serial_mode_reboot);
364                         } else if (cmd.equals("Close")) {
365                                 if (serial_line != null)
366                                         serial_line.close();
367                         }
368                 } catch (InterruptedException ie) {
369                         abort();
370                 } catch (TimeoutException te) {
371                         abort();
372                 }
373         }
374
375         public AltosConfig(JFrame given_owner) {
376                 owner = given_owner;
377
378                 serial = new int_ref(0);
379                 main_deploy = new int_ref(250);
380                 apogee_delay = new int_ref(0);
381                 radio_channel = new int_ref(0);
382                 radio_setting = new int_ref(0);
383                 radio_calibration = new int_ref(1186611);
384                 flight_log_max = new int_ref(0);
385                 ignite_mode = new int_ref(-1);
386                 pad_orientation = new int_ref(-1);
387                 callsign = new string_ref("N0CALL");
388                 version = new string_ref("unknown");
389                 product = new string_ref("unknown");
390
391                 device = AltosDeviceDialog.show(owner, Altos.product_any);
392                 if (device != null) {
393                         try {
394                                 serial_line = new AltosSerial(device);
395                                 try {
396                                         if (!device.matchProduct(Altos.product_telemetrum))
397                                                 remote = true;
398                                         init_ui();
399                                 } catch (InterruptedException ie) {
400                                         abort();
401                                 } catch (TimeoutException te) {
402                                         abort();
403                                 }
404                         } catch (FileNotFoundException ee) {
405                                 JOptionPane.showMessageDialog(owner,
406                                                               String.format("Cannot open device \"%s\"",
407                                                                             device.toShortString()),
408                                                               "Cannot open target device",
409                                                               JOptionPane.ERROR_MESSAGE);
410                         } catch (AltosSerialInUseException si) {
411                                 JOptionPane.showMessageDialog(owner,
412                                                               String.format("Device \"%s\" already in use",
413                                                                             device.toShortString()),
414                                                               "Device in use",
415                                                               JOptionPane.ERROR_MESSAGE);
416                         } catch (IOException ee) {
417                                 JOptionPane.showMessageDialog(owner,
418                                                               device.toShortString(),
419                                                               ee.getLocalizedMessage(),
420                                                               JOptionPane.ERROR_MESSAGE);
421                         }
422                 }
423         }
424 }