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