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