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