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