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