altosui: Move AltosGreatCircle.java to altoslib
[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 import org.altusmetrum.AltosLib.*;
31
32 import libaltosJNI.*;
33
34 public class AltosConfig implements ActionListener {
35
36         class int_ref {
37                 int     value;
38
39                 public int get() {
40                         return value;
41                 }
42                 public void set(int i) {
43                         value = i;
44                 }
45                 public int_ref(int i) {
46                         value = i;
47                 }
48         }
49
50         class string_ref {
51                 String  value;
52
53                 public String get() {
54                         return value;
55                 }
56                 public void set(String i) {
57                         value = i;
58                 }
59                 public string_ref(String i) {
60                         value = i;
61                 }
62         }
63
64         JFrame          owner;
65         AltosDevice     device;
66         AltosSerial     serial_line;
67         boolean         remote;
68         AltosConfigData remote_config_data;
69         double          remote_frequency;
70         int_ref         serial;
71         int_ref         log_format;
72         int_ref         main_deploy;
73         int_ref         apogee_delay;
74         int_ref         radio_channel;
75         int_ref         radio_calibration;
76         int_ref         flight_log_max;
77         int_ref         ignite_mode;
78         int_ref         pad_orientation;
79         int_ref         radio_setting;
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                 get_int(line, "Radio enable:", radio_enable);
198                 get_int(line, "Storage size:", storage_size);
199                 get_int(line, "Storage erase unit:", storage_erase_unit);
200                 get_int(line, "flight", stored_flight);
201                 get_string(line, "Callsign:", callsign);
202                 get_string(line,"software-version", version);
203                 get_string(line,"product", product);
204         }
205
206         final static int        serial_mode_read = 0;
207         final static int        serial_mode_save = 1;
208         final static int        serial_mode_reboot = 2;
209
210         class SerialData implements Runnable {
211                 AltosConfig     config;
212                 int             serial_mode;
213
214                 void process_line(String line) {
215                         config.process_line(line);
216                 }
217                 void callback(String in_line) {
218                         final String line = in_line;
219                         Runnable r = new Runnable() {
220                                         public void run() {
221                                                 process_line(line);
222                                         }
223                                 };
224                         SwingUtilities.invokeLater(r);
225                 }
226
227                 void reset_data() {
228                         serial.set(0);
229                         log_format.set(Altos.AO_LOG_FORMAT_UNKNOWN);
230                         main_deploy.set(250);
231                         apogee_delay.set(0);
232                         radio_channel.set(0);
233                         radio_setting.set(0);
234                         radio_calibration.set(1186611);
235                         radio_enable.set(-1);
236                         flight_log_max.set(0);
237                         ignite_mode.set(-1);
238                         pad_orientation.set(-1);
239                         storage_size.set(-1);
240                         storage_erase_unit.set(-1);
241                         stored_flight.set(-1);
242                         callsign.set("N0CALL");
243                         version.set("unknown");
244                         product.set("unknown");
245                 }
246
247                 void get_data() {
248                         try {
249                                 config.start_serial();
250                                 reset_data();
251
252                                 config.serial_line.printf("c s\nf\nl\nv\n");
253                                 for (;;) {
254                                         try {
255                                                 String line = config.serial_line.get_reply(5000);
256                                                 if (line == null)
257                                                         stop_serial();
258                                                 callback(line);
259                                                 if (line.startsWith("software-version"))
260                                                         break;
261                                         } catch (Exception e) {
262                                                 break;
263                                         }
264                                 }
265                         } catch (InterruptedException ie) {
266                         } catch (TimeoutException te) {
267                         } finally {
268                                 try {
269                                         stop_serial();
270                                 } catch (InterruptedException ie) {
271                                 }
272                         }
273                         callback("all finished");
274                 }
275
276                 void save_data() {
277                         try {
278                                 double frequency = frequency();
279                                 boolean has_setting = radio_setting.get() > 0;
280                                 start_serial();
281                                 serial_line.printf("c m %d\n", main_deploy.get());
282                                 serial_line.printf("c d %d\n", apogee_delay.get());
283                                 if (!remote)
284                                         serial_line.printf("c f %d\n", radio_calibration.get());
285                                 serial_line.set_radio_frequency(frequency,
286                                                                 has_setting,
287                                                                 radio_calibration.get());
288                                 if (remote) {
289                                         serial_line.stop_remote();
290                                         serial_line.set_radio_frequency(frequency);
291                                         AltosUIPreferences.set_frequency(device.getSerial(), frequency);
292                                         serial_line.start_remote();
293                                 }
294                                 serial_line.printf("c c %s\n", callsign.get());
295                                 if (flight_log_max.get() != 0)
296                                         serial_line.printf("c l %d\n", flight_log_max.get());
297                                 if (radio_enable.get() >= 0)
298                                         serial_line.printf("c e %d\n", radio_enable.get());
299                                 if (ignite_mode.get() >= 0)
300                                         serial_line.printf("c i %d\n", ignite_mode.get());
301                                 if (pad_orientation.get() >= 0)
302                                         serial_line.printf("c o %d\n", pad_orientation.get());
303                                 serial_line.printf("c w\n");
304                         } catch (InterruptedException ie) {
305                         } catch (TimeoutException te) {
306                         } finally {
307                                 try {
308                                         stop_serial();
309                                 } catch (InterruptedException ie) {
310                                 }
311                         }
312                 }
313
314                 void reboot() {
315                         try {
316                                 start_serial();
317                                 serial_line.printf("r eboot\n");
318                                 serial_line.flush_output();
319                         } catch (InterruptedException ie) {
320                         } catch (TimeoutException te) {
321                         } finally {
322                                 try {
323                                         stop_serial();
324                                 } catch (InterruptedException ie) {
325                                 }
326                                 serial_line.close();
327                         }
328                 }
329
330                 public void run () {
331                         switch (serial_mode) {
332                         case serial_mode_save:
333                                 save_data();
334                                 /* fall through ... */
335                         case serial_mode_read:
336                                 get_data();
337                                 break;
338                         case serial_mode_reboot:
339                                 reboot();
340                                 break;
341                         }
342                 }
343
344                 public SerialData(AltosConfig in_config, int in_serial_mode) {
345                         config = in_config;
346                         serial_mode = in_serial_mode;
347                 }
348         }
349
350         void run_serial_thread(int serial_mode) {
351                 SerialData      sd = new SerialData(this, serial_mode);
352                 Thread          st = new Thread(sd);
353                 st.start();
354         }
355
356         void init_ui () throws InterruptedException, TimeoutException {
357                 config_ui = new AltosConfigUI(owner, remote);
358                 config_ui.addActionListener(this);
359                 serial_line.set_frame(owner);
360                 set_ui();
361         }
362
363         void abort() {
364                 serial_line.close();
365                 serial_line = null;
366                 JOptionPane.showMessageDialog(owner,
367                                               String.format("Connection to \"%s\" failed",
368                                                             device.toShortString()),
369                                               "Connection Failed",
370                                               JOptionPane.ERROR_MESSAGE);
371                 config_ui.setVisible(false);
372         }
373
374         void set_ui() throws InterruptedException, TimeoutException {
375                 if (serial_line != null)
376                         run_serial_thread(serial_mode_read);
377                 else
378                         update_ui();
379         }
380
381         double frequency() {
382                 return AltosConvert.radio_to_frequency(radio_setting.get(),
383                                                        radio_calibration.get(),
384                                                        radio_channel.get());
385         }
386
387         void set_frequency(double freq) {
388                 int     setting = radio_setting.get();
389
390                 if (setting > 0) {
391                         radio_setting.set(AltosConvert.radio_frequency_to_setting(freq,
392                                                                                   radio_calibration.get()));
393                         radio_channel.set(0);
394                 } else {
395                         radio_channel.set(AltosConvert.radio_frequency_to_channel(freq));
396                 }
397         }
398
399         void save_data() {
400
401                 /* bounds check stuff */
402                 if (config_ui.flight_log_max() > log_limit()) {
403                         JOptionPane.showMessageDialog(owner,
404                                                       String.format("Requested flight log, %dk, is larger than the available space, %dk.\n",
405                                                                     config_ui.flight_log_max(),
406                                                                     log_limit()),
407                                                       "Maximum Flight Log Too Large",
408                                                       JOptionPane.ERROR_MESSAGE);
409                         return;
410                 }
411
412                 main_deploy.set(config_ui.main_deploy());
413                 apogee_delay.set(config_ui.apogee_delay());
414                 radio_calibration.set(config_ui.radio_calibration());
415                 set_frequency(config_ui.radio_frequency());
416                 flight_log_max.set(config_ui.flight_log_max());
417                 if (radio_enable.get() >= 0)
418                         radio_enable.set(config_ui.radio_enable());
419                 if (ignite_mode.get() >= 0)
420                         ignite_mode.set(config_ui.ignite_mode());
421                 if (pad_orientation.get() >= 0)
422                         pad_orientation.set(config_ui.pad_orientation());
423                 callsign.set(config_ui.callsign());
424                 run_serial_thread(serial_mode_save);
425         }
426
427         public void actionPerformed(ActionEvent e) {
428                 String  cmd = e.getActionCommand();
429                 try {
430                         if (cmd.equals("Save")) {
431                                 save_data();
432                         } else if (cmd.equals("Reset")) {
433                                 set_ui();
434                         } else if (cmd.equals("Reboot")) {
435                                 if (serial_line != null)
436                                         run_serial_thread(serial_mode_reboot);
437                         } else if (cmd.equals("Close")) {
438                                 if (serial_line != null)
439                                         serial_line.close();
440                         }
441                 } catch (InterruptedException ie) {
442                         abort();
443                 } catch (TimeoutException te) {
444                         abort();
445                 }
446         }
447
448         public AltosConfig(JFrame given_owner) {
449                 owner = given_owner;
450
451                 serial = new int_ref(0);
452                 log_format = new int_ref(Altos.AO_LOG_FORMAT_UNKNOWN);
453                 main_deploy = new int_ref(250);
454                 apogee_delay = new int_ref(0);
455                 radio_channel = new int_ref(0);
456                 radio_setting = new int_ref(0);
457                 radio_calibration = new int_ref(1186611);
458                 radio_enable = new int_ref(-1);
459                 flight_log_max = new int_ref(0);
460                 ignite_mode = new int_ref(-1);
461                 pad_orientation = new int_ref(-1);
462                 storage_size = new int_ref(-1);
463                 storage_erase_unit = new int_ref(-1);
464                 stored_flight = new int_ref(-1);
465                 callsign = new string_ref("N0CALL");
466                 version = new string_ref("unknown");
467                 product = new string_ref("unknown");
468
469                 device = AltosDeviceDialog.show(owner, Altos.product_any);
470                 if (device != null) {
471                         try {
472                                 serial_line = new AltosSerial(device);
473                                 try {
474                                         if (!device.matchProduct(Altos.product_telemetrum))
475                                                 remote = true;
476                                         init_ui();
477                                 } catch (InterruptedException ie) {
478                                         abort();
479                                 } catch (TimeoutException te) {
480                                         abort();
481                                 }
482                         } catch (FileNotFoundException ee) {
483                                 JOptionPane.showMessageDialog(owner,
484                                                               ee.getMessage(),
485                                                               "Cannot open target device",
486                                                               JOptionPane.ERROR_MESSAGE);
487                         } catch (AltosSerialInUseException si) {
488                                 JOptionPane.showMessageDialog(owner,
489                                                               String.format("Device \"%s\" already in use",
490                                                                             device.toShortString()),
491                                                               "Device in use",
492                                                               JOptionPane.ERROR_MESSAGE);
493                         } catch (IOException ee) {
494                                 JOptionPane.showMessageDialog(owner,
495                                                               device.toShortString(),
496                                                               ee.getLocalizedMessage(),
497                                                               JOptionPane.ERROR_MESSAGE);
498                         }
499                 }
500         }
501 }