altosui: Radio channel gets set to zero by altos when frequency is set
[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         log_format;
71         int_ref         main_deploy;
72         int_ref         apogee_delay;
73         int_ref         radio_channel;
74         int_ref         radio_calibration;
75         int_ref         flight_log_max;
76         int_ref         ignite_mode;
77         int_ref         pad_orientation;
78         int_ref         radio_setting;
79         int_ref         radio_frequency;
80         int_ref         storage_size;
81         int_ref         storage_erase_unit;
82         int_ref         stored_flight;
83         int_ref         radio_enable;
84         string_ref      version;
85         string_ref      product;
86         string_ref      callsign;
87         AltosConfigUI   config_ui;
88         boolean         serial_started;
89         boolean         made_visible;
90
91         boolean get_int(String line, String label, int_ref x) {
92                 if (line.startsWith(label)) {
93                         try {
94                                 String tail = line.substring(label.length()).trim();
95                                 String[] tokens = tail.split("\\s+");
96                                 if (tokens.length > 0) {
97                                         int     i = Integer.parseInt(tokens[0]);
98                                         x.set(i);
99                                         return true;
100                                 }
101                         } catch (NumberFormatException ne) {
102                         }
103                 }
104                 return false;
105         }
106
107         boolean get_string(String line, String label, string_ref s) {
108                 if (line.startsWith(label)) {
109                         String  quoted = line.substring(label.length()).trim();
110
111                         if (quoted.startsWith("\""))
112                                 quoted = quoted.substring(1);
113                         if (quoted.endsWith("\""))
114                                 quoted = quoted.substring(0,quoted.length()-1);
115                         s.set(quoted);
116                         return true;
117                 } else {
118                         return false;
119                 }
120         }
121
122         void start_serial() throws InterruptedException, TimeoutException {
123                 serial_started = true;
124                 if (remote)
125                         serial_line.start_remote();
126         }
127
128         void stop_serial() throws InterruptedException {
129                 if (!serial_started)
130                         return;
131                 serial_started = false;
132                 if (remote)
133                         serial_line.stop_remote();
134         }
135
136         int log_limit() {
137                 if (storage_size.get() > 0 && storage_erase_unit.get() > 0) {
138                         int     log_limit = storage_size.get() - storage_erase_unit.get();
139                         if (log_limit > 0)
140                                 return log_limit / 1024;
141                 }
142                 return 1024;
143         }
144
145         void update_ui() {
146                 config_ui.set_serial(serial.get());
147                 config_ui.set_product(product.get());
148                 config_ui.set_version(version.get());
149                 config_ui.set_main_deploy(main_deploy.get());
150                 config_ui.set_apogee_delay(apogee_delay.get());
151                 config_ui.set_radio_calibration(radio_calibration.get());
152                 config_ui.set_radio_frequency(frequency());
153                 boolean max_enabled = true;
154                 switch (log_format.get()) {
155                 case Altos.AO_LOG_FORMAT_TINY:
156                         max_enabled = false;
157                         break;
158                 default:
159                         if (stored_flight.get() >= 0)
160                                 max_enabled = false;
161                         break;
162                 }
163                 config_ui.set_flight_log_max_enabled(max_enabled);
164                 config_ui.set_radio_enable(radio_enable.get());
165                 config_ui.set_flight_log_max_limit(log_limit());
166                 config_ui.set_flight_log_max(flight_log_max.get());
167                 config_ui.set_ignite_mode(ignite_mode.get());
168                 config_ui.set_pad_orientation(pad_orientation.get());
169                 config_ui.set_callsign(callsign.get());
170                 config_ui.set_clean();
171                 if (!made_visible) {
172                         made_visible = true;
173                         config_ui.make_visible();
174                 }
175         }
176
177         void process_line(String line) {
178                 if (line == null) {
179                         abort();
180                         return;
181                 }
182                 if (line.equals("all finished")) {
183                         if (serial_line != null)
184                                 update_ui();
185                         return;
186                 }
187                 get_int(line, "serial-number", serial);
188                 get_int(line, "log-format", log_format);
189                 get_int(line, "Main deploy:", main_deploy);
190                 get_int(line, "Apogee delay:", apogee_delay);
191                 get_int(line, "Radio channel:", radio_channel);
192                 get_int(line, "Radio cal:", radio_calibration);
193                 get_int(line, "Max flight log:", flight_log_max);
194                 get_int(line, "Ignite mode:", ignite_mode);
195                 get_int(line, "Pad orientation:", pad_orientation);
196                 get_int(line, "Radio setting:", radio_setting);
197                 if (get_int(line, "Frequency:", radio_frequency))
198                         if (radio_frequency.get() < 0)
199                                 radio_frequency.set(434550);
200                 get_int(line, "Radio enable:", radio_enable);
201                 get_int(line, "Storage size:", storage_size);
202                 get_int(line, "Storage erase unit:", storage_erase_unit);
203                 get_int(line, "flight", stored_flight);
204                 get_string(line, "Callsign:", callsign);
205                 get_string(line,"software-version", version);
206                 get_string(line,"product", product);
207         }
208
209         final static int        serial_mode_read = 0;
210         final static int        serial_mode_save = 1;
211         final static int        serial_mode_reboot = 2;
212
213         class SerialData implements Runnable {
214                 AltosConfig     config;
215                 int             serial_mode;
216
217                 void process_line(String line) {
218                         config.process_line(line);
219                 }
220                 void callback(String in_line) {
221                         final String line = in_line;
222                         Runnable r = new Runnable() {
223                                         public void run() {
224                                                 process_line(line);
225                                         }
226                                 };
227                         SwingUtilities.invokeLater(r);
228                 }
229
230                 void reset_data() {
231                         serial.set(0);
232                         log_format.set(Altos.AO_LOG_FORMAT_UNKNOWN);
233                         main_deploy.set(250);
234                         apogee_delay.set(0);
235                         radio_channel.set(0);
236                         radio_setting.set(0);
237                         radio_frequency.set(0);
238                         radio_calibration.set(1186611);
239                         radio_enable.set(-1);
240                         flight_log_max.set(0);
241                         ignite_mode.set(-1);
242                         pad_orientation.set(-1);
243                         storage_size.set(-1);
244                         storage_erase_unit.set(-1);
245                         stored_flight.set(-1);
246                         callsign.set("N0CALL");
247                         version.set("unknown");
248                         product.set("unknown");
249                 }
250
251                 void get_data() {
252                         try {
253                                 config.start_serial();
254                                 reset_data();
255
256                                 config.serial_line.printf("c s\nf\nl\nv\n");
257                                 for (;;) {
258                                         try {
259                                                 String line = config.serial_line.get_reply(5000);
260                                                 if (line == null)
261                                                         stop_serial();
262                                                 callback(line);
263                                                 if (line.startsWith("software-version"))
264                                                         break;
265                                         } catch (Exception e) {
266                                                 break;
267                                         }
268                                 }
269                         } catch (InterruptedException ie) {
270                         } catch (TimeoutException te) {
271                         } finally {
272                                 try {
273                                         stop_serial();
274                                 } catch (InterruptedException ie) {
275                                 }
276                         }
277                         callback("all finished");
278                 }
279
280                 void save_data() {
281                         try {
282                                 double frequency = frequency();
283                                 boolean has_frequency = radio_frequency.get() > 0;
284                                 boolean has_setting = radio_setting.get() > 0;
285                                 start_serial();
286                                 serial_line.printf("c m %d\n", main_deploy.get());
287                                 serial_line.printf("c d %d\n", apogee_delay.get());
288                                 if (!remote)
289                                         serial_line.printf("c f %d\n", radio_calibration.get());
290                                 serial_line.set_radio_frequency(frequency,
291                                                                 has_frequency,
292                                                                 has_setting,
293                                                                 radio_calibration.get());
294                                 if (remote) {
295                                         serial_line.stop_remote();
296                                         serial_line.set_radio_frequency(frequency);
297                                         AltosPreferences.set_frequency(device.getSerial(), frequency);
298                                         serial_line.start_remote();
299                                 }
300                                 serial_line.printf("c c %s\n", callsign.get());
301                                 if (flight_log_max.get() != 0)
302                                         serial_line.printf("c l %d\n", flight_log_max.get());
303                                 if (radio_enable.get() >= 0)
304                                         serial_line.printf("c e %d\n", radio_enable.get());
305                                 if (ignite_mode.get() >= 0)
306                                         serial_line.printf("c i %d\n", ignite_mode.get());
307                                 if (pad_orientation.get() >= 0)
308                                         serial_line.printf("c o %d\n", pad_orientation.get());
309                                 serial_line.printf("c w\n");
310                         } catch (InterruptedException ie) {
311                         } catch (TimeoutException te) {
312                         } finally {
313                                 try {
314                                         stop_serial();
315                                 } catch (InterruptedException ie) {
316                                 }
317                         }
318                 }
319
320                 void reboot() {
321                         try {
322                                 start_serial();
323                                 serial_line.printf("r eboot\n");
324                                 serial_line.flush_output();
325                         } catch (InterruptedException ie) {
326                         } catch (TimeoutException te) {
327                         } finally {
328                                 try {
329                                         stop_serial();
330                                 } catch (InterruptedException ie) {
331                                 }
332                                 serial_line.close();
333                         }
334                 }
335
336                 public void run () {
337                         switch (serial_mode) {
338                         case serial_mode_save:
339                                 save_data();
340                                 /* fall through ... */
341                         case serial_mode_read:
342                                 get_data();
343                                 break;
344                         case serial_mode_reboot:
345                                 reboot();
346                                 break;
347                         }
348                 }
349
350                 public SerialData(AltosConfig in_config, int in_serial_mode) {
351                         config = in_config;
352                         serial_mode = in_serial_mode;
353                 }
354         }
355
356         void run_serial_thread(int serial_mode) {
357                 SerialData      sd = new SerialData(this, serial_mode);
358                 Thread          st = new Thread(sd);
359                 st.start();
360         }
361
362         void init_ui () throws InterruptedException, TimeoutException {
363                 config_ui = new AltosConfigUI(owner, remote);
364                 config_ui.addActionListener(this);
365                 serial_line.set_frame(owner);
366                 set_ui();
367         }
368
369         void abort() {
370                 serial_line.close();
371                 serial_line = null;
372                 JOptionPane.showMessageDialog(owner,
373                                               String.format("Connection to \"%s\" failed",
374                                                             device.toShortString()),
375                                               "Connection Failed",
376                                               JOptionPane.ERROR_MESSAGE);
377                 config_ui.setVisible(false);
378         }
379
380         void set_ui() throws InterruptedException, TimeoutException {
381                 if (serial_line != null)
382                         run_serial_thread(serial_mode_read);
383                 else
384                         update_ui();
385         }
386
387         double frequency() {
388                 return AltosConvert.radio_to_frequency(radio_frequency.get(),
389                                                        radio_setting.get(),
390                                                        radio_calibration.get(),
391                                                        radio_channel.get());
392         }
393
394         void set_frequency(double freq) {
395                 int     frequency = radio_frequency.get();
396                 int     setting = radio_setting.get();
397
398                 if (frequency > 0) {
399                         radio_frequency.set((int) Math.floor (freq * 1000 + 0.5));
400                         radio_channel.set(0);
401                 } else if (setting > 0) {
402                         radio_setting.set(AltosConvert.radio_frequency_to_setting(freq,
403                                                                                   radio_calibration.get()));
404                         radio_channel.set(0);
405                 } else {
406                         radio_channel.set(AltosConvert.radio_frequency_to_channel(freq));
407                 }
408         }
409
410         void save_data() {
411
412                 /* bounds check stuff */
413                 if (config_ui.flight_log_max() > log_limit()) {
414                         JOptionPane.showMessageDialog(owner,
415                                                       String.format("Requested flight log, %dk, is larger than the available space, %dk.\n",
416                                                                     config_ui.flight_log_max(),
417                                                                     log_limit()),
418                                                       "Maximum Flight Log Too Large",
419                                                       JOptionPane.ERROR_MESSAGE);
420                         return;
421                 }
422
423                 main_deploy.set(config_ui.main_deploy());
424                 apogee_delay.set(config_ui.apogee_delay());
425                 radio_calibration.set(config_ui.radio_calibration());
426                 set_frequency(config_ui.radio_frequency());
427                 flight_log_max.set(config_ui.flight_log_max());
428                 if (radio_enable.get() >= 0)
429                         radio_enable.set(config_ui.radio_enable());
430                 if (ignite_mode.get() >= 0)
431                         ignite_mode.set(config_ui.ignite_mode());
432                 if (pad_orientation.get() >= 0)
433                         pad_orientation.set(config_ui.pad_orientation());
434                 callsign.set(config_ui.callsign());
435                 run_serial_thread(serial_mode_save);
436         }
437
438         public void actionPerformed(ActionEvent e) {
439                 String  cmd = e.getActionCommand();
440                 try {
441                         if (cmd.equals("Save")) {
442                                 save_data();
443                         } else if (cmd.equals("Reset")) {
444                                 set_ui();
445                         } else if (cmd.equals("Reboot")) {
446                                 if (serial_line != null)
447                                         run_serial_thread(serial_mode_reboot);
448                         } else if (cmd.equals("Close")) {
449                                 if (serial_line != null)
450                                         serial_line.close();
451                         }
452                 } catch (InterruptedException ie) {
453                         abort();
454                 } catch (TimeoutException te) {
455                         abort();
456                 }
457         }
458
459         public AltosConfig(JFrame given_owner) {
460                 owner = given_owner;
461
462                 serial = new int_ref(0);
463                 log_format = new int_ref(Altos.AO_LOG_FORMAT_UNKNOWN);
464                 main_deploy = new int_ref(250);
465                 apogee_delay = new int_ref(0);
466                 radio_channel = new int_ref(0);
467                 radio_setting = new int_ref(0);
468                 radio_frequency = new int_ref(0);
469                 radio_calibration = new int_ref(1186611);
470                 radio_enable = new int_ref(-1);
471                 flight_log_max = new int_ref(0);
472                 ignite_mode = new int_ref(-1);
473                 pad_orientation = new int_ref(-1);
474                 storage_size = new int_ref(-1);
475                 storage_erase_unit = new int_ref(-1);
476                 stored_flight = new int_ref(-1);
477                 callsign = new string_ref("N0CALL");
478                 version = new string_ref("unknown");
479                 product = new string_ref("unknown");
480
481                 device = AltosDeviceDialog.show(owner, Altos.product_any);
482                 if (device != null) {
483                         try {
484                                 serial_line = new AltosSerial(device);
485                                 try {
486                                         if (!device.matchProduct(Altos.product_telemetrum))
487                                                 remote = true;
488                                         init_ui();
489                                 } catch (InterruptedException ie) {
490                                         abort();
491                                 } catch (TimeoutException te) {
492                                         abort();
493                                 }
494                         } catch (FileNotFoundException ee) {
495                                 JOptionPane.showMessageDialog(owner,
496                                                               ee.getMessage(),
497                                                               "Cannot open target device",
498                                                               JOptionPane.ERROR_MESSAGE);
499                         } catch (AltosSerialInUseException si) {
500                                 JOptionPane.showMessageDialog(owner,
501                                                               String.format("Device \"%s\" already in use",
502                                                                             device.toShortString()),
503                                                               "Device in use",
504                                                               JOptionPane.ERROR_MESSAGE);
505                         } catch (IOException ee) {
506                                 JOptionPane.showMessageDialog(owner,
507                                                               device.toShortString(),
508                                                               ee.getLocalizedMessage(),
509                                                               JOptionPane.ERROR_MESSAGE);
510                         }
511                 }
512         }
513 }