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