de1b582ef80d6130d491ad6ac2bf5c7108720001
[fw/altos] / altosui / AltosConfigUI.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.event.*;
24 import org.altusmetrum.altoslib_5.*;
25 import org.altusmetrum.altosuilib_3.*;
26
27 public class AltosConfigUI
28         extends AltosUIDialog
29         implements ActionListener, ItemListener, DocumentListener, AltosConfigValues, AltosUnitsListener
30 {
31
32         Container               pane;
33         JLabel                  product_label;
34         JLabel                  version_label;
35         JLabel                  serial_label;
36         JLabel                  main_deploy_label;
37         JLabel                  apogee_delay_label;
38         JLabel                  apogee_lockout_label;
39         JLabel                  frequency_label;
40         JLabel                  radio_calibration_label;
41         JLabel                  radio_frequency_label;
42         JLabel                  radio_enable_label;
43         JLabel                  aprs_interval_label;
44         JLabel                  aprs_ssid_label;
45         JLabel                  flight_log_max_label;
46         JLabel                  ignite_mode_label;
47         JLabel                  pad_orientation_label;
48         JLabel                  callsign_label;
49         JLabel                  beep_label;
50         JLabel                  tracker_motion_label;
51         JLabel                  tracker_interval_label;
52
53         public boolean          dirty;
54
55         JFrame                  owner;
56         JLabel                  product_value;
57         JLabel                  version_value;
58         JLabel                  serial_value;
59         JComboBox<String>       main_deploy_value;
60         JComboBox<String>       apogee_delay_value;
61         JComboBox<String>       apogee_lockout_value;
62         AltosFreqList           radio_frequency_value;
63         JTextField              radio_calibration_value;
64         JRadioButton            radio_enable_value;
65         JComboBox<String>       aprs_interval_value;
66         JComboBox<Integer>      aprs_ssid_value;
67         JComboBox<String>       flight_log_max_value;
68         JComboBox<String>       ignite_mode_value;
69         JComboBox<String>       pad_orientation_value;
70         JTextField              callsign_value;
71         JComboBox<String>       beep_value;
72         JComboBox<String>       tracker_motion_value;
73         JComboBox<String>       tracker_interval_value;
74
75         JButton                 pyro;
76
77         JButton                 save;
78         JButton                 reset;
79         JButton                 reboot;
80         JButton                 close;
81
82         AltosPyro[]             pyros;
83         double                  pyro_firing_time;
84
85         ActionListener          listener;
86
87         static String[]         main_deploy_values_m = {
88                 "100", "150", "200", "250", "300", "350",
89                 "400", "450", "500"
90         };
91
92         static String[]         main_deploy_values_ft = {
93                 "250", "500", "750", "1000", "1250", "1500",
94                 "1750", "2000"
95         };
96
97         static String[]         apogee_delay_values = {
98                 "0", "1", "2", "3", "4", "5"
99         };
100
101         static String[]         apogee_lockout_values = {
102                 "0", "5", "10", "15", "20"
103         };
104
105         static String[]         ignite_mode_values = {
106                 "Dual Deploy",
107                 "Redundant Apogee",
108                 "Redundant Main",
109         };
110
111         static String[]         aprs_interval_values = {
112                 "Disabled",
113                 "2",
114                 "5",
115                 "10"
116         };
117
118         static Integer[]        aprs_ssid_values = {
119                 0, 1, 2 ,3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
120         };
121
122         static String[]         beep_values = {
123                 "3750",
124                 "4000",
125                 "4250",
126         };
127
128         static String[]         pad_orientation_values = {
129                 "Antenna Up",
130                 "Antenna Down",
131         };
132
133         static String[]         tracker_motion_values_m = {
134                 "2",
135                 "5",
136                 "10",
137                 "25",
138         };
139
140         static String[]         tracker_motion_values_ft = {
141                 "5",
142                 "20",
143                 "50",
144                 "100"
145         };
146
147         static String[]         tracker_interval_values = {
148                 "1",
149                 "2",
150                 "5",
151                 "10"
152         };
153
154         /* A window listener to catch closing events and tell the config code */
155         class ConfigListener extends WindowAdapter {
156                 AltosConfigUI   ui;
157
158                 public ConfigListener(AltosConfigUI this_ui) {
159                         ui = this_ui;
160                 }
161
162                 public void windowClosing(WindowEvent e) {
163                         ui.actionPerformed(new ActionEvent(e.getSource(),
164                                                            ActionEvent.ACTION_PERFORMED,
165                                                            "Close"));
166                 }
167         }
168
169         boolean is_telemini_v1() {
170                 String  product = product_value.getText();
171                 return product != null && product.startsWith("TeleMini-v1");
172         }
173
174         boolean is_telemini() {
175                 String  product = product_value.getText();
176                 return product != null && product.startsWith("TeleMini");
177         }
178
179         boolean is_easymini() {
180                 String  product = product_value.getText();
181                 return product != null && product.startsWith("EasyMini");
182         }
183
184         boolean is_telemetrum() {
185                 String  product = product_value.getText();
186                 return product != null && product.startsWith("TeleMetrum");
187         }
188
189         void set_radio_calibration_tool_tip() {
190                 if (radio_calibration_value.isEnabled())
191                         radio_calibration_value.setToolTipText("Tune radio output to match desired frequency");
192                 else
193                         radio_calibration_value.setToolTipText("Cannot tune radio while connected over packet mode");
194         }
195
196         void set_radio_enable_tool_tip() {
197                 if (radio_enable_value.isEnabled())
198                         radio_enable_value.setToolTipText("Enable/Disable telemetry and RDF transmissions");
199                 else
200                         radio_enable_value.setToolTipText("Firmware version does not support disabling radio");
201         }
202
203         void set_aprs_interval_tool_tip() {
204                 if (aprs_interval_value.isEnabled())
205                         aprs_interval_value.setToolTipText("Enable APRS and set the interval between APRS reports");
206                 else
207                         aprs_interval_value.setToolTipText("Hardware doesn't support APRS");
208         }
209
210         void set_aprs_ssid_tool_tip() {
211                 if (aprs_ssid_value.isEnabled())
212                         aprs_interval_value.setToolTipText("Set the APRS SSID (secondary station identifier)");
213                 else if (aprs_interval_value.isEnabled())
214                         aprs_interval_value.setToolTipText("Software version doesn't support setting the APRS SSID");
215                 else
216                         aprs_interval_value.setToolTipText("Hardware doesn't support APRS");
217         }
218
219         void set_flight_log_max_tool_tip() {
220                 if (flight_log_max_value.isEnabled())
221                         flight_log_max_value.setToolTipText("Size reserved for each flight log (in kB)");
222                 else {
223                         if (is_telemini_v1())
224                                 flight_log_max_value.setToolTipText("TeleMini-v1 stores only one flight");
225                         else
226                                 flight_log_max_value.setToolTipText("Cannot set max value with flight logs in memory");
227                 }
228         }
229
230         void set_ignite_mode_tool_tip() {
231                 if (ignite_mode_value.isEnabled())
232                         ignite_mode_value.setToolTipText("Select when igniters will be fired");
233                 else
234                         ignite_mode_value.setToolTipText("Older firmware could not select ignite mode");
235         }
236
237         void set_pad_orientation_tool_tip() {
238                 if (pad_orientation_value.isEnabled())
239                         pad_orientation_value.setToolTipText("How will the computer be mounted in the airframe");
240                 else {
241                         if (is_telemetrum())
242                                 pad_orientation_value.setToolTipText("Older TeleMetrum firmware must fly antenna forward");
243                         else if (is_telemini() || is_easymini())
244                                 pad_orientation_value.setToolTipText("TeleMini and EasyMini don't care how they are mounted");
245                         else
246                                 pad_orientation_value.setToolTipText("Can't select orientation");
247                 }
248         }
249
250         void set_beep_tool_tip() {
251                 if (beep_value.isEnabled())
252                         beep_value.setToolTipText("What frequency the beeper will sound at");
253                 else
254                         beep_value.setToolTipText("Older firmware could not select beeper frequency");
255         }
256
257         /* Build the UI using a grid bag */
258         public AltosConfigUI(JFrame in_owner, boolean remote) {
259                 super (in_owner, "Configure Flight Computer", false);
260
261                 owner = in_owner;
262                 GridBagConstraints c;
263                 int row = 0;
264
265                 Insets il = new Insets(4,4,4,4);
266                 Insets ir = new Insets(4,4,4,4);
267
268                 pane = getContentPane();
269                 pane.setLayout(new GridBagLayout());
270
271                 /* Product */
272                 c = new GridBagConstraints();
273                 c.gridx = 0; c.gridy = row;
274                 c.gridwidth = 4;
275                 c.fill = GridBagConstraints.NONE;
276                 c.anchor = GridBagConstraints.LINE_START;
277                 c.insets = il;
278                 product_label = new JLabel("Product:");
279                 pane.add(product_label, c);
280
281                 c = new GridBagConstraints();
282                 c.gridx = 4; c.gridy = row;
283                 c.gridwidth = 4;
284                 c.fill = GridBagConstraints.HORIZONTAL;
285                 c.weightx = 1;
286                 c.anchor = GridBagConstraints.LINE_START;
287                 c.insets = ir;
288                 product_value = new JLabel("");
289                 pane.add(product_value, c);
290                 row++;
291
292                 /* Version */
293                 c = new GridBagConstraints();
294                 c.gridx = 0; c.gridy = row;
295                 c.gridwidth = 4;
296                 c.fill = GridBagConstraints.NONE;
297                 c.anchor = GridBagConstraints.LINE_START;
298                 c.insets = il;
299                 c.ipady = 5;
300                 version_label = new JLabel("Software version:");
301                 pane.add(version_label, c);
302
303                 c = new GridBagConstraints();
304                 c.gridx = 4; c.gridy = row;
305                 c.gridwidth = 4;
306                 c.fill = GridBagConstraints.HORIZONTAL;
307                 c.weightx = 1;
308                 c.anchor = GridBagConstraints.LINE_START;
309                 c.insets = ir;
310                 c.ipady = 5;
311                 version_value = new JLabel("");
312                 pane.add(version_value, c);
313                 row++;
314
315                 /* Serial */
316                 c = new GridBagConstraints();
317                 c.gridx = 0; c.gridy = row;
318                 c.gridwidth = 4;
319                 c.fill = GridBagConstraints.NONE;
320                 c.anchor = GridBagConstraints.LINE_START;
321                 c.insets = il;
322                 c.ipady = 5;
323                 serial_label = new JLabel("Serial:");
324                 pane.add(serial_label, c);
325
326                 c = new GridBagConstraints();
327                 c.gridx = 4; c.gridy = row;
328                 c.gridwidth = 4;
329                 c.fill = GridBagConstraints.HORIZONTAL;
330                 c.weightx = 1;
331                 c.anchor = GridBagConstraints.LINE_START;
332                 c.insets = ir;
333                 c.ipady = 5;
334                 serial_value = new JLabel("");
335                 pane.add(serial_value, c);
336                 row++;
337
338                 /* Main deploy */
339                 c = new GridBagConstraints();
340                 c.gridx = 0; c.gridy = row;
341                 c.gridwidth = 4;
342                 c.fill = GridBagConstraints.NONE;
343                 c.anchor = GridBagConstraints.LINE_START;
344                 c.insets = il;
345                 c.ipady = 5;
346                 main_deploy_label = new JLabel(get_main_deploy_label());
347                 pane.add(main_deploy_label, c);
348
349                 c = new GridBagConstraints();
350                 c.gridx = 4; c.gridy = row;
351                 c.gridwidth = 4;
352                 c.fill = GridBagConstraints.HORIZONTAL;
353                 c.weightx = 1;
354                 c.anchor = GridBagConstraints.LINE_START;
355                 c.insets = ir;
356                 c.ipady = 5;
357                 main_deploy_value = new JComboBox<String>(main_deploy_values());
358                 main_deploy_value.setEditable(true);
359                 main_deploy_value.addItemListener(this);
360                 pane.add(main_deploy_value, c);
361                 main_deploy_value.setToolTipText("Height above pad altitude to fire main charge");
362                 row++;
363
364                 /* Apogee delay */
365                 c = new GridBagConstraints();
366                 c.gridx = 0; c.gridy = row;
367                 c.gridwidth = 4;
368                 c.fill = GridBagConstraints.NONE;
369                 c.anchor = GridBagConstraints.LINE_START;
370                 c.insets = il;
371                 c.ipady = 5;
372                 apogee_delay_label = new JLabel("Apogee Delay(s):");
373                 pane.add(apogee_delay_label, c);
374
375                 c = new GridBagConstraints();
376                 c.gridx = 4; c.gridy = row;
377                 c.gridwidth = 4;
378                 c.fill = GridBagConstraints.HORIZONTAL;
379                 c.weightx = 1;
380                 c.anchor = GridBagConstraints.LINE_START;
381                 c.insets = ir;
382                 c.ipady = 5;
383                 apogee_delay_value = new JComboBox<String>(apogee_delay_values);
384                 apogee_delay_value.setEditable(true);
385                 apogee_delay_value.addItemListener(this);
386                 pane.add(apogee_delay_value, c);
387                 apogee_delay_value.setToolTipText("Delay after apogee before charge fires");
388                 row++;
389
390                 /* Apogee lockout */
391                 c = new GridBagConstraints();
392                 c.gridx = 0; c.gridy = row;
393                 c.gridwidth = 4;
394                 c.fill = GridBagConstraints.NONE;
395                 c.anchor = GridBagConstraints.LINE_START;
396                 c.insets = il;
397                 c.ipady = 5;
398                 apogee_lockout_label = new JLabel("Apogee Lockout(s):");
399                 pane.add(apogee_lockout_label, c);
400
401                 c = new GridBagConstraints();
402                 c.gridx = 4; c.gridy = row;
403                 c.gridwidth = 4;
404                 c.fill = GridBagConstraints.HORIZONTAL;
405                 c.weightx = 1;
406                 c.anchor = GridBagConstraints.LINE_START;
407                 c.insets = ir;
408                 c.ipady = 5;
409                 apogee_lockout_value = new JComboBox<String>(apogee_lockout_values);
410                 apogee_lockout_value.setEditable(true);
411                 apogee_lockout_value.addItemListener(this);
412                 pane.add(apogee_lockout_value, c);
413                 apogee_lockout_value.setToolTipText("Time after boost while apogee detection is locked out");
414                 row++;
415
416                 /* Frequency */
417                 c = new GridBagConstraints();
418                 c.gridx = 0; c.gridy = row;
419                 c.gridwidth = 4;
420                 c.fill = GridBagConstraints.NONE;
421                 c.anchor = GridBagConstraints.LINE_START;
422                 c.insets = il;
423                 c.ipady = 5;
424                 radio_frequency_label = new JLabel("Frequency:");
425                 pane.add(radio_frequency_label, c);
426
427                 c = new GridBagConstraints();
428                 c.gridx = 4; c.gridy = row;
429                 c.gridwidth = 4;
430                 c.fill = GridBagConstraints.HORIZONTAL;
431                 c.weightx = 1;
432                 c.anchor = GridBagConstraints.LINE_START;
433                 c.insets = ir;
434                 c.ipady = 5;
435                 radio_frequency_value = new AltosFreqList();
436                 radio_frequency_value.addItemListener(this);
437
438                 JMenuBar menu_bar = new JMenuBar();
439                 menu_bar.add(radio_frequency_value);
440                 pane.add(menu_bar, c);
441                 radio_frequency_value.setToolTipText("Telemetry, RDF and packet frequency");
442                 row++;
443
444                 /* Radio Calibration */
445                 c = new GridBagConstraints();
446                 c.gridx = 0; c.gridy = row;
447                 c.gridwidth = 4;
448                 c.fill = GridBagConstraints.NONE;
449                 c.anchor = GridBagConstraints.LINE_START;
450                 c.insets = il;
451                 c.ipady = 5;
452                 radio_calibration_label = new JLabel("RF Calibration:");
453                 pane.add(radio_calibration_label, c);
454
455                 c = new GridBagConstraints();
456                 c.gridx = 4; c.gridy = row;
457                 c.gridwidth = 4;
458                 c.fill = GridBagConstraints.HORIZONTAL;
459                 c.weightx = 1;
460                 c.anchor = GridBagConstraints.LINE_START;
461                 c.insets = ir;
462                 c.ipady = 5;
463                 radio_calibration_value = new JTextField(String.format("%d", 1186611));
464                 radio_calibration_value.getDocument().addDocumentListener(this);
465                 if (remote)
466                         radio_calibration_value.setEnabled(false);
467                 pane.add(radio_calibration_value, c);
468                 set_radio_calibration_tool_tip();
469                 row++;
470
471                 /* Radio Enable */
472                 c = new GridBagConstraints();
473                 c.gridx = 0; c.gridy = row;
474                 c.gridwidth = 4;
475                 c.fill = GridBagConstraints.NONE;
476                 c.anchor = GridBagConstraints.LINE_START;
477                 c.insets = il;
478                 c.ipady = 5;
479                 radio_enable_label = new JLabel("Telemetry/RDF/APRS Enable:");
480                 pane.add(radio_enable_label, c);
481
482                 c = new GridBagConstraints();
483                 c.gridx = 4; c.gridy = row;
484                 c.gridwidth = 4;
485                 c.fill = GridBagConstraints.HORIZONTAL;
486                 c.weightx = 1;
487                 c.anchor = GridBagConstraints.LINE_START;
488                 c.insets = ir;
489                 c.ipady = 5;
490                 radio_enable_value = new JRadioButton("Enabled");
491                 radio_enable_value.addItemListener(this);
492                 pane.add(radio_enable_value, c);
493                 set_radio_enable_tool_tip();
494                 row++;
495
496                 /* APRS interval */
497                 c = new GridBagConstraints();
498                 c.gridx = 0; c.gridy = row;
499                 c.gridwidth = 4;
500                 c.fill = GridBagConstraints.NONE;
501                 c.anchor = GridBagConstraints.LINE_START;
502                 c.insets = il;
503                 c.ipady = 5;
504                 aprs_interval_label = new JLabel("APRS Interval(s):");
505                 pane.add(aprs_interval_label, c);
506
507                 c = new GridBagConstraints();
508                 c.gridx = 4; c.gridy = row;
509                 c.gridwidth = 4;
510                 c.fill = GridBagConstraints.HORIZONTAL;
511                 c.weightx = 1;
512                 c.anchor = GridBagConstraints.LINE_START;
513                 c.insets = ir;
514                 c.ipady = 5;
515                 aprs_interval_value = new JComboBox<String>(aprs_interval_values);
516                 aprs_interval_value.setEditable(true);
517                 aprs_interval_value.addItemListener(this);
518                 pane.add(aprs_interval_value, c);
519                 set_aprs_interval_tool_tip();
520                 row++;
521
522                 /* APRS SSID */
523                 c = new GridBagConstraints();
524                 c.gridx = 0; c.gridy = row;
525                 c.gridwidth = 4;
526                 c.fill = GridBagConstraints.NONE;
527                 c.anchor = GridBagConstraints.LINE_START;
528                 c.insets = il;
529                 c.ipady = 5;
530                 aprs_ssid_label = new JLabel("APRS SSID:");
531                 pane.add(aprs_ssid_label, c);
532
533                 c = new GridBagConstraints();
534                 c.gridx = 4; c.gridy = row;
535                 c.gridwidth = 4;
536                 c.fill = GridBagConstraints.HORIZONTAL;
537                 c.weightx = 1;
538                 c.anchor = GridBagConstraints.LINE_START;
539                 c.insets = ir;
540                 c.ipady = 5;
541                 aprs_ssid_value = new JComboBox<Integer>(aprs_ssid_values);
542                 aprs_ssid_value.setEditable(false);
543                 aprs_ssid_value.addItemListener(this);
544                 aprs_ssid_value.setMaximumRowCount(aprs_ssid_values.length);
545                 pane.add(aprs_ssid_value, c);
546                 set_aprs_ssid_tool_tip();
547                 row++;
548
549                 /* Callsign */
550                 c = new GridBagConstraints();
551                 c.gridx = 0; c.gridy = row;
552                 c.gridwidth = 4;
553                 c.fill = GridBagConstraints.NONE;
554                 c.anchor = GridBagConstraints.LINE_START;
555                 c.insets = il;
556                 c.ipady = 5;
557                 callsign_label = new JLabel("Callsign:");
558                 pane.add(callsign_label, c);
559
560                 c = new GridBagConstraints();
561                 c.gridx = 4; c.gridy = row;
562                 c.gridwidth = 4;
563                 c.fill = GridBagConstraints.HORIZONTAL;
564                 c.weightx = 1;
565                 c.anchor = GridBagConstraints.LINE_START;
566                 c.insets = ir;
567                 c.ipady = 5;
568                 callsign_value = new JTextField(AltosUIPreferences.callsign());
569                 callsign_value.getDocument().addDocumentListener(this);
570                 pane.add(callsign_value, c);
571                 callsign_value.setToolTipText("Callsign reported in telemetry data");
572                 row++;
573
574                 /* Flight log max */
575                 c = new GridBagConstraints();
576                 c.gridx = 0; c.gridy = row;
577                 c.gridwidth = 4;
578                 c.fill = GridBagConstraints.NONE;
579                 c.anchor = GridBagConstraints.LINE_START;
580                 c.insets = il;
581                 c.ipady = 5;
582                 flight_log_max_label = new JLabel("Maximum Flight Log Size (kB):");
583                 pane.add(flight_log_max_label, c);
584
585                 c = new GridBagConstraints();
586                 c.gridx = 4; c.gridy = row;
587                 c.gridwidth = 4;
588                 c.fill = GridBagConstraints.HORIZONTAL;
589                 c.weightx = 1;
590                 c.anchor = GridBagConstraints.LINE_START;
591                 c.insets = ir;
592                 c.ipady = 5;
593                 flight_log_max_value = new JComboBox<String>();
594                 flight_log_max_value.setEditable(true);
595                 flight_log_max_value.addItemListener(this);
596                 pane.add(flight_log_max_value, c);
597                 set_flight_log_max_tool_tip();
598                 row++;
599
600                 /* Ignite mode */
601                 c = new GridBagConstraints();
602                 c.gridx = 0; c.gridy = row;
603                 c.gridwidth = 4;
604                 c.fill = GridBagConstraints.NONE;
605                 c.anchor = GridBagConstraints.LINE_START;
606                 c.insets = il;
607                 c.ipady = 5;
608                 ignite_mode_label = new JLabel("Igniter Firing Mode:");
609                 pane.add(ignite_mode_label, c);
610
611                 c = new GridBagConstraints();
612                 c.gridx = 4; c.gridy = row;
613                 c.gridwidth = 4;
614                 c.fill = GridBagConstraints.HORIZONTAL;
615                 c.weightx = 1;
616                 c.anchor = GridBagConstraints.LINE_START;
617                 c.insets = ir;
618                 c.ipady = 5;
619                 ignite_mode_value = new JComboBox<String>(ignite_mode_values);
620                 ignite_mode_value.setEditable(false);
621                 ignite_mode_value.addItemListener(this);
622                 pane.add(ignite_mode_value, c);
623                 set_ignite_mode_tool_tip();
624                 row++;
625
626                 /* Pad orientation */
627                 c = new GridBagConstraints();
628                 c.gridx = 0; c.gridy = row;
629                 c.gridwidth = 4;
630                 c.fill = GridBagConstraints.NONE;
631                 c.anchor = GridBagConstraints.LINE_START;
632                 c.insets = il;
633                 c.ipady = 5;
634                 pad_orientation_label = new JLabel("Pad Orientation:");
635                 pane.add(pad_orientation_label, c);
636
637                 c = new GridBagConstraints();
638                 c.gridx = 4; c.gridy = row;
639                 c.gridwidth = 4;
640                 c.fill = GridBagConstraints.HORIZONTAL;
641                 c.weightx = 1;
642                 c.anchor = GridBagConstraints.LINE_START;
643                 c.insets = ir;
644                 c.ipady = 5;
645                 pad_orientation_value = new JComboBox<String>(pad_orientation_values);
646                 pad_orientation_value.setEditable(false);
647                 pad_orientation_value.addItemListener(this);
648                 pane.add(pad_orientation_value, c);
649                 set_pad_orientation_tool_tip();
650                 row++;
651
652                 /* Beeper */
653                 c = new GridBagConstraints();
654                 c.gridx = 0; c.gridy = row;
655                 c.gridwidth = 4;
656                 c.fill = GridBagConstraints.NONE;
657                 c.anchor = GridBagConstraints.LINE_START;
658                 c.insets = il;
659                 c.ipady = 5;
660                 beep_label = new JLabel("Beeper Frequency:");
661                 pane.add(beep_label, c);
662
663                 c = new GridBagConstraints();
664                 c.gridx = 4; c.gridy = row;
665                 c.gridwidth = 4;
666                 c.fill = GridBagConstraints.HORIZONTAL;
667                 c.weightx = 1;
668                 c.anchor = GridBagConstraints.LINE_START;
669                 c.insets = ir;
670                 c.ipady = 5;
671                 beep_value = new JComboBox<String>(beep_values);
672                 beep_value.setEditable(true);
673                 beep_value.addItemListener(this);
674                 pane.add(beep_value, c);
675                 set_beep_tool_tip();
676                 row++;
677
678                 /* Tracker triger horiz distances */
679                 c = new GridBagConstraints();
680                 c.gridx = 0; c.gridy = row;
681                 c.gridwidth = 4;
682                 c.fill = GridBagConstraints.NONE;
683                 c.anchor = GridBagConstraints.LINE_START;
684                 c.insets = il;
685                 c.ipady = 5;
686                 tracker_motion_label = new JLabel(get_tracker_motion_label());
687                 pane.add(tracker_motion_label, c);
688
689                 c = new GridBagConstraints();
690                 c.gridx = 4; c.gridy = row;
691                 c.gridwidth = 4;
692                 c.fill = GridBagConstraints.HORIZONTAL;
693                 c.weightx = 1;
694                 c.anchor = GridBagConstraints.LINE_START;
695                 c.insets = ir;
696                 c.ipady = 5;
697                 tracker_motion_value = new JComboBox<String>(tracker_motion_values());
698                 tracker_motion_value.setEditable(true);
699                 tracker_motion_value.addItemListener(this);
700                 pane.add(tracker_motion_value, c);
701                 row++;
702
703                 /* Tracker triger vert distances */
704                 c = new GridBagConstraints();
705                 c.gridx = 0; c.gridy = row;
706                 c.gridwidth = 4;
707                 c.fill = GridBagConstraints.NONE;
708                 c.anchor = GridBagConstraints.LINE_START;
709                 c.insets = il;
710                 c.ipady = 5;
711                 tracker_interval_label = new JLabel("Position Reporting Interval(s):");
712                 pane.add(tracker_interval_label, c);
713
714                 c = new GridBagConstraints();
715                 c.gridx = 4; c.gridy = row;
716                 c.gridwidth = 4;
717                 c.fill = GridBagConstraints.HORIZONTAL;
718                 c.weightx = 1;
719                 c.anchor = GridBagConstraints.LINE_START;
720                 c.insets = ir;
721                 c.ipady = 5;
722                 tracker_interval_value = new JComboBox<String>(tracker_interval_values);
723                 tracker_interval_value.setEditable(true);
724                 tracker_interval_value.addItemListener(this);
725                 pane.add(tracker_interval_value, c);
726                 set_tracker_tool_tip();
727                 row++;
728
729                 /* Pyro channels */
730                 c = new GridBagConstraints();
731                 c.gridx = 4; c.gridy = row;
732                 c.gridwidth = 4;
733                 c.fill = GridBagConstraints.HORIZONTAL;
734                 c.anchor = GridBagConstraints.LINE_START;
735                 c.insets = il;
736                 c.ipady = 5;
737                 pyro = new JButton("Configure Pyro Channels");
738                 pane.add(pyro, c);
739                 pyro.addActionListener(this);
740                 pyro.setActionCommand("Pyro");
741                 row++;
742
743                 /* Buttons */
744                 c = new GridBagConstraints();
745                 c.gridx = 0; c.gridy = row;
746                 c.gridwidth = 2;
747                 c.fill = GridBagConstraints.NONE;
748                 c.anchor = GridBagConstraints.LINE_START;
749                 c.insets = il;
750                 save = new JButton("Save");
751                 pane.add(save, c);
752                 save.addActionListener(this);
753                 save.setActionCommand("Save");
754
755                 c = new GridBagConstraints();
756                 c.gridx = 2; c.gridy = row;
757                 c.gridwidth = 2;
758                 c.fill = GridBagConstraints.NONE;
759                 c.anchor = GridBagConstraints.CENTER;
760                 c.insets = il;
761                 reset = new JButton("Reset");
762                 pane.add(reset, c);
763                 reset.addActionListener(this);
764                 reset.setActionCommand("Reset");
765
766                 c = new GridBagConstraints();
767                 c.gridx = 4; c.gridy = row;
768                 c.gridwidth = 2;
769                 c.fill = GridBagConstraints.NONE;
770                 c.anchor = GridBagConstraints.CENTER;
771                 c.insets = il;
772                 reboot = new JButton("Reboot");
773                 pane.add(reboot, c);
774                 reboot.addActionListener(this);
775                 reboot.setActionCommand("Reboot");
776
777                 c = new GridBagConstraints();
778                 c.gridx = 6; c.gridy = row;
779                 c.gridwidth = 2;
780                 c.fill = GridBagConstraints.NONE;
781                 c.anchor = GridBagConstraints.LINE_END;
782                 c.insets = il;
783                 close = new JButton("Close");
784                 pane.add(close, c);
785                 close.addActionListener(this);
786                 close.setActionCommand("Close");
787
788                 addWindowListener(new ConfigListener(this));
789                 AltosPreferences.register_units_listener(this);
790         }
791
792         /* Once the initial values are set, the config code will show the dialog */
793         public void make_visible() {
794                 pack();
795                 setLocationRelativeTo(owner);
796                 setVisible(true);
797         }
798
799         /* If any values have been changed, confirm before closing */
800         public boolean check_dirty(String operation) {
801                 if (dirty) {
802                         Object[] options = { String.format("%s anyway", operation), "Keep editing" };
803                         int i;
804                         i = JOptionPane.showOptionDialog(this,
805                                                          String.format("Configuration modified. %s anyway?", operation),
806                                                          "Configuration Modified",
807                                                          JOptionPane.DEFAULT_OPTION,
808                                                          JOptionPane.WARNING_MESSAGE,
809                                                          null, options, options[1]);
810                         if (i != 0)
811                                 return false;
812                 }
813                 return true;
814         }
815
816         void set_dirty() {
817                 dirty = true;
818                 save.setEnabled(true);
819         }
820
821         public void set_clean() {
822                 dirty = false;
823                 save.setEnabled(false);
824         }
825
826         AltosConfigPyroUI       pyro_ui;
827
828         public void dispose() {
829                 if (pyro_ui != null)
830                         pyro_ui.dispose();
831                 AltosPreferences.unregister_units_listener(this);
832                 super.dispose();
833         }
834
835         /* Listen for events from our buttons */
836         public void actionPerformed(ActionEvent e) {
837                 String  cmd = e.getActionCommand();
838
839                 if (cmd.equals("Pyro")) {
840                         if (pyro_ui == null && pyros != null)
841                                 pyro_ui = new AltosConfigPyroUI(this, pyros, pyro_firing_time);
842                         if (pyro_ui != null)
843                                 pyro_ui.make_visible();
844                         return;
845                 }
846
847                 if (cmd.equals("Close") || cmd.equals("Reboot"))
848                         if (!check_dirty(cmd))
849                                 return;
850                 listener.actionPerformed(e);
851                 if (cmd.equals("Close") || cmd.equals("Reboot")) {
852                         setVisible(false);
853                         dispose();
854                 }
855                 set_clean();
856         }
857
858         /* ItemListener interface method */
859         public void itemStateChanged(ItemEvent e) {
860                 set_dirty();
861         }
862
863         /* DocumentListener interface methods */
864         public void changedUpdate(DocumentEvent e) {
865                 set_dirty();
866         }
867
868         public void insertUpdate(DocumentEvent e) {
869                 set_dirty();
870         }
871
872         public void removeUpdate(DocumentEvent e) {
873                 set_dirty();
874         }
875
876         /* Let the config code hook on a listener */
877         public void addActionListener(ActionListener l) {
878                 listener = l;
879         }
880
881         /* set and get all of the dialog values */
882         public void set_product(String product) {
883                 radio_frequency_value.set_product(product);
884                 product_value.setText(product);
885                 set_pad_orientation_tool_tip();
886                 set_flight_log_max_tool_tip();
887         }
888
889         public void set_version(String version) {
890                 version_value.setText(version);
891         }
892
893         public void set_serial(int serial) {
894                 radio_frequency_value.set_serial(serial);
895                 serial_value.setText(String.format("%d", serial));
896         }
897
898         public void set_main_deploy(int new_main_deploy) {
899                 main_deploy_value.setSelectedItem(AltosConvert.height.say(new_main_deploy));
900                 main_deploy_value.setEnabled(new_main_deploy >= 0);
901         }
902
903         public int main_deploy() {
904                 return (int) (AltosConvert.height.parse(main_deploy_value.getSelectedItem().toString()) + 0.5);
905         }
906
907         String get_main_deploy_label() {
908                 return String.format("Main Deploy Altitude(%s):", AltosConvert.height.show_units());
909         }
910
911         String[] main_deploy_values() {
912                 if (AltosConvert.imperial_units)
913                         return main_deploy_values_ft;
914                 else
915                         return main_deploy_values_m;
916         }
917
918         void set_main_deploy_values() {
919                 String[]        v = main_deploy_values();
920                 while (main_deploy_value.getItemCount() > 0)
921                         main_deploy_value.removeItemAt(0);
922                 for (int i = 0; i < v.length; i++)
923                         main_deploy_value.addItem(v[i]);
924                 main_deploy_value.setMaximumRowCount(v.length);
925         }
926
927         public void units_changed(boolean imperial_units) {
928                 boolean was_dirty = dirty;
929
930                 String v = main_deploy_value.getSelectedItem().toString();
931                 main_deploy_label.setText(get_main_deploy_label());
932                 set_main_deploy_values();
933                 int m = (int) (AltosConvert.height.parse(v, !imperial_units) + 0.5);
934                 set_main_deploy(m);
935
936                 if (tracker_motion_value.isEnabled()) {
937                         String motion = tracker_motion_value.getSelectedItem().toString();
938                         tracker_motion_label.setText(get_tracker_motion_label());
939                         set_tracker_motion_values();
940                         set_tracker_motion((int) (AltosConvert.height.parse(motion, !imperial_units) + 0.5));
941                 }
942
943                 if (!was_dirty)
944                         set_clean();
945         }
946
947         public void set_apogee_delay(int new_apogee_delay) {
948                 apogee_delay_value.setSelectedItem(Integer.toString(new_apogee_delay));
949                 apogee_delay_value.setEnabled(new_apogee_delay >= 0);
950         }
951
952         private int parse_int(String name, String s, boolean split) throws AltosConfigDataException {
953                 String v = s;
954                 if (split)
955                         v = s.split("\\s+")[0];
956                 try {
957                         return Integer.parseInt(v);
958                 } catch (NumberFormatException ne) {
959                         throw new AltosConfigDataException("Invalid %s \"%s\"", name, s);
960                 }
961         }
962
963         public int apogee_delay() throws AltosConfigDataException {
964                 return parse_int("apogee delay", apogee_delay_value.getSelectedItem().toString(), false);
965         }
966
967         public void set_apogee_lockout(int new_apogee_lockout) {
968                 apogee_lockout_value.setSelectedItem(Integer.toString(new_apogee_lockout));
969                 apogee_lockout_value.setEnabled(new_apogee_lockout >= 0);
970         }
971
972         public int apogee_lockout() throws AltosConfigDataException {
973                 return parse_int("apogee lockout", apogee_lockout_value.getSelectedItem().toString(), false);
974         }
975
976         public void set_radio_frequency(double new_radio_frequency) {
977                 radio_frequency_value.set_frequency(new_radio_frequency);
978         }
979
980         public double radio_frequency() {
981                 return radio_frequency_value.frequency();
982         }
983
984         public void set_radio_calibration(int new_radio_calibration) {
985                 radio_calibration_value.setVisible(new_radio_calibration >= 0);
986                 if (new_radio_calibration < 0)
987                         radio_calibration_value.setText("Disabled");
988                 else
989                         radio_calibration_value.setText(String.format("%d", new_radio_calibration));
990         }
991
992         public int radio_calibration() throws AltosConfigDataException {
993                 return parse_int("radio calibration", radio_calibration_value.getText(), false);
994         }
995
996         public void set_radio_enable(int new_radio_enable) {
997                 if (new_radio_enable >= 0) {
998                         radio_enable_value.setSelected(new_radio_enable > 0);
999                         radio_enable_value.setEnabled(true);
1000                 } else {
1001                         radio_enable_value.setSelected(true);
1002                         radio_enable_value.setVisible(radio_frequency() > 0);
1003                         radio_enable_value.setEnabled(false);
1004                 }
1005                 set_radio_enable_tool_tip();
1006         }
1007
1008         public int radio_enable() {
1009                 if (radio_enable_value.isEnabled())
1010                         return radio_enable_value.isSelected() ? 1 : 0;
1011                 else
1012                         return -1;
1013         }
1014
1015         public void set_callsign(String new_callsign) {
1016                 callsign_value.setVisible(new_callsign != null);
1017                 callsign_value.setText(new_callsign);
1018         }
1019
1020         public String callsign() {
1021                 return callsign_value.getText();
1022         }
1023
1024         int     flight_log_max_limit;
1025         int     flight_log_max;
1026
1027         public String flight_log_max_label(int flight_log_max) {
1028                 if (flight_log_max_limit != 0) {
1029                         int     nflight = flight_log_max_limit / flight_log_max;
1030                         String  plural = nflight > 1 ? "s" : "";
1031
1032                         return String.format("%d (%d flight%s)", flight_log_max, nflight, plural);
1033                 }
1034                 return String.format("%d", flight_log_max);
1035         }
1036
1037         public void set_flight_log_max(int new_flight_log_max) {
1038                 flight_log_max_value.setSelectedItem(flight_log_max_label(new_flight_log_max));
1039                 flight_log_max = new_flight_log_max;
1040                 set_flight_log_max_tool_tip();
1041         }
1042
1043         public void set_flight_log_max_enabled(boolean enable) {
1044                 flight_log_max_value.setEnabled(enable);
1045                 set_flight_log_max_tool_tip();
1046         }
1047
1048         public int flight_log_max() throws AltosConfigDataException {
1049                 return parse_int("flight log max", flight_log_max_value.getSelectedItem().toString(), true);
1050         }
1051
1052         public void set_flight_log_max_limit(int new_flight_log_max_limit) {
1053                 flight_log_max_limit = new_flight_log_max_limit;
1054                 flight_log_max_value.removeAllItems();
1055                 for (int i = 8; i >= 1; i--) {
1056                         int     size = flight_log_max_limit / i;
1057                         flight_log_max_value.addItem(String.format("%d (%d flights)", size, i));
1058                 }
1059                 if (flight_log_max != 0)
1060                         set_flight_log_max(flight_log_max);
1061         }
1062
1063         public void set_ignite_mode(int new_ignite_mode) {
1064                 if (new_ignite_mode >= ignite_mode_values.length)
1065                         new_ignite_mode = 0;
1066                 if (new_ignite_mode < 0) {
1067                         ignite_mode_value.setEnabled(false);
1068                         new_ignite_mode = 0;
1069                 } else {
1070                         ignite_mode_value.setEnabled(true);
1071                 }
1072                 ignite_mode_value.setSelectedIndex(new_ignite_mode);
1073                 set_ignite_mode_tool_tip();
1074         }
1075
1076         public int ignite_mode() {
1077                 if (ignite_mode_value.isEnabled())
1078                         return ignite_mode_value.getSelectedIndex();
1079                 else
1080                         return -1;
1081         }
1082
1083
1084         public void set_pad_orientation(int new_pad_orientation) {
1085                 if (new_pad_orientation >= pad_orientation_values.length)
1086                         new_pad_orientation = 0;
1087                 if (new_pad_orientation < 0) {
1088                         pad_orientation_value.setVisible(false);
1089                         new_pad_orientation = 0;
1090                 } else {
1091                         pad_orientation_value.setVisible(true);
1092                 }
1093                 pad_orientation_value.setSelectedIndex(new_pad_orientation);
1094                 set_pad_orientation_tool_tip();
1095         }
1096
1097         public int pad_orientation() {
1098                 if (pad_orientation_value.isEnabled())
1099                         return pad_orientation_value.getSelectedIndex();
1100                 else
1101                         return -1;
1102         }
1103
1104         public void set_beep(int new_beep) {
1105                 int new_freq = (int) Math.floor (AltosConvert.beep_value_to_freq(new_beep) + 0.5);
1106                 for (int i = 0; i < beep_values.length; i++)
1107                         if (new_beep == AltosConvert.beep_freq_to_value(Integer.parseInt(beep_values[i]))) {
1108                                 beep_value.setSelectedIndex(i);
1109                                 set_beep_tool_tip();
1110                                 return;
1111                         }
1112                 beep_value.setSelectedItem(String.format("%d", new_freq));
1113                 beep_value.setEnabled(new_beep >= 0);
1114                 set_beep_tool_tip();
1115         }
1116
1117         public int beep() {
1118                 if (beep_value.isEnabled())
1119                         return AltosConvert.beep_freq_to_value(Integer.parseInt(beep_value.getSelectedItem().toString()));
1120                 else
1121                         return -1;
1122         }
1123
1124         String[] tracker_motion_values() {
1125                 if (AltosConvert.imperial_units)
1126                         return tracker_motion_values_ft;
1127                 else
1128                         return tracker_motion_values_m;
1129         }
1130
1131         void set_tracker_motion_values() {
1132                 String[]        v = tracker_motion_values();
1133                 while (tracker_motion_value.getItemCount() > 0)
1134                         tracker_motion_value.removeItemAt(0);
1135                 for (int i = 0; i < v.length; i++)
1136                         tracker_motion_value.addItem(v[i]);
1137                 tracker_motion_value.setMaximumRowCount(v.length);
1138         }
1139
1140         String get_tracker_motion_label() {
1141                 return String.format("Logging Trigger Motion (%s):", AltosConvert.height.show_units());
1142         }
1143
1144         void set_tracker_tool_tip() {
1145                 if (tracker_motion_value.isEnabled())
1146                         tracker_motion_value.setToolTipText("How far the device must move before logging");
1147                 else
1148                         tracker_motion_value.setToolTipText("This device doesn't disable logging when stationary");
1149                 if (tracker_interval_value.isEnabled())
1150                         tracker_interval_value.setToolTipText("How often to report GPS position");
1151                 else
1152                         tracker_interval_value.setToolTipText("This device can't configure interval");
1153         }
1154
1155         public void set_tracker_motion(int tracker_motion) {
1156                 if (tracker_motion < 0) {
1157                         tracker_motion_label.setVisible(false);
1158                         tracker_motion_value.setVisible(false);
1159                         tracker_motion_value.setEnabled(false);
1160                 } else {
1161                         tracker_motion_label.setVisible(true);
1162                         tracker_motion_value.setVisible(true);
1163                         tracker_motion_value.setEnabled(true);
1164                         tracker_motion_value.setSelectedItem(AltosConvert.height.say(tracker_motion));
1165                 }
1166         }
1167
1168         public int tracker_motion() throws AltosConfigDataException {
1169                 return (int) AltosConvert.height.parse(tracker_motion_value.getSelectedItem().toString());
1170         }
1171
1172         public void set_tracker_interval(int tracker_interval) {
1173                 if (tracker_interval< 0) {
1174                         tracker_interval_label.setVisible(false);
1175                         tracker_interval_value.setVisible(false);
1176                         tracker_interval_value.setEnabled(false);
1177                 } else {
1178                         tracker_interval_label.setVisible(true);
1179                         tracker_interval_value.setVisible(true);
1180                         tracker_interval_value.setEnabled(true);
1181                         tracker_interval_value.setSelectedItem(String.format("%d", tracker_interval));
1182                 }
1183         }
1184
1185         public int tracker_interval() throws AltosConfigDataException {
1186                 return parse_int ("tracker interval", tracker_interval_value.getSelectedItem().toString(), false);
1187         }
1188
1189         public void set_pyros(AltosPyro[] new_pyros) {
1190                 pyros = new_pyros;
1191                 pyro.setVisible(pyros != null);
1192                 if (pyros != null && pyro_ui != null)
1193                         pyro_ui.set_pyros(pyros);
1194         }
1195
1196         public AltosPyro[] pyros() throws AltosConfigDataException {
1197                 if (pyro_ui != null)
1198                         pyros = pyro_ui.get_pyros();
1199                 return pyros;
1200         }
1201
1202         public void set_pyro_firing_time(double new_pyro_firing_time) {
1203                 pyro_firing_time = new_pyro_firing_time;
1204                 pyro.setVisible(pyro_firing_time >= 0);
1205                 if (pyro_firing_time >= 0 && pyro_ui != null)
1206                         pyro_ui.set_pyro_firing_time(pyro_firing_time);
1207         }
1208
1209         public double pyro_firing_time() throws AltosConfigDataException {
1210                 if (pyro_ui != null)
1211                         pyro_firing_time = pyro_ui.get_pyro_firing_time();
1212                 return pyro_firing_time;
1213         }
1214
1215         public void set_aprs_interval(int new_aprs_interval) {
1216                 String  s;
1217
1218                 if (new_aprs_interval <= 0)
1219                         s = "Disabled";
1220                 else
1221                         s = Integer.toString(new_aprs_interval);
1222                 aprs_interval_value.setSelectedItem(s);
1223                 aprs_interval_value.setVisible(new_aprs_interval >= 0);
1224                 set_aprs_interval_tool_tip();
1225         }
1226
1227         public int aprs_interval() throws AltosConfigDataException {
1228                 String  s = aprs_interval_value.getSelectedItem().toString();
1229
1230                 if (s.equals("Disabled"))
1231                         return 0;
1232                 return parse_int("aprs interval", s, false);
1233         }
1234
1235         public void set_aprs_ssid(int new_aprs_ssid) {
1236                 aprs_ssid_value.setSelectedItem(Math.max(0,new_aprs_ssid));
1237                 aprs_ssid_value.setVisible(new_aprs_ssid >= 0);
1238                 set_aprs_ssid_tool_tip();
1239         }
1240
1241         public int aprs_ssid() throws AltosConfigDataException {
1242                 Integer i = (Integer) aprs_ssid_value.getSelectedItem();
1243                 return i;
1244         }
1245 }