altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / telegps / TeleGPSConfigUI.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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.telegps;
20
21 import java.text.*;
22 import java.awt.*;
23 import java.awt.event.*;
24 import javax.swing.*;
25 import javax.swing.event.*;
26 import org.altusmetrum.altoslib_14.*;
27 import org.altusmetrum.altosuilib_14.*;
28
29 public class TeleGPSConfigUI
30         extends AltosUIDialog
31         implements ActionListener, ItemListener, DocumentListener, AltosConfigValues, AltosUnitsListener
32 {
33
34         Container               pane;
35         JLabel                  product_label;
36         JLabel                  version_label;
37         JLabel                  serial_label;
38         JLabel                  frequency_label;
39         JLabel                  radio_calibration_label;
40         JLabel                  radio_frequency_label;
41         JLabel                  radio_enable_label;
42         JLabel                  rate_label;
43         JLabel                  aprs_interval_label;
44         JLabel                  aprs_ssid_label;
45         JLabel                  aprs_format_label;
46         JLabel                  aprs_offset_label;
47         JLabel                  flight_log_max_label;
48         JLabel                  callsign_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         AltosUIFreqList         radio_frequency_value;
59         JLabel                  radio_calibration_value;
60         JRadioButton            radio_enable_value;
61         AltosUIRateList         rate_value;
62         JComboBox<String>       aprs_interval_value;
63         JComboBox<Integer>      aprs_ssid_value;
64         JComboBox<String>       aprs_format_value;
65         JComboBox<Integer>      aprs_offset_value;
66         JComboBox<String>       flight_log_max_value;
67         JTextField              callsign_value;
68         JComboBox<String>       tracker_motion_value;
69         JComboBox<String>       tracker_interval_value;
70
71         JButton                 save;
72         JButton                 reset;
73         JButton                 reboot;
74         JButton                 close;
75
76         ActionListener          listener;
77
78         static String[]         aprs_interval_values = {
79                 "Disabled",
80                 "2",
81                 "5",
82                 "10"
83         };
84
85         static Integer[]        aprs_ssid_values = {
86                 0, 1, 2 ,3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
87         };
88
89         static Integer[]        aprs_offset_values = {
90                 0, 2, 4, 6, 8, 10, 12, 14, 16, 18
91         };
92
93         static String[]         tracker_motion_values_m = {
94                 "2",
95                 "5",
96                 "10",
97                 "25",
98         };
99
100         static String[]         tracker_motion_values_ft = {
101                 "5",
102                 "20",
103                 "50",
104                 "100"
105         };
106
107         static String[]         tracker_interval_values = {
108                 "1",
109                 "2",
110                 "5",
111                 "10"
112         };
113
114         /* A window listener to catch closing events and tell the config code */
115         class ConfigListener extends WindowAdapter {
116                 TeleGPSConfigUI ui;
117
118                 public ConfigListener(TeleGPSConfigUI this_ui) {
119                         ui = this_ui;
120                 }
121
122                 public void windowClosing(WindowEvent e) {
123                         ui.actionPerformed(new ActionEvent(e.getSource(),
124                                                            ActionEvent.ACTION_PERFORMED,
125                                                            "Close"));
126                 }
127         }
128
129         public void set_pyros(AltosPyro[] new_pyros) {
130         }
131
132         public AltosPyro[] pyros() {
133                 return null;
134         }
135
136         public void set_pyro_firing_time(double new_pyro_firing_time) {
137         }
138
139         public double pyro_firing_time() {
140                 return AltosLib.MISSING;
141         }
142
143         boolean is_telemetrum() {
144                 String  product = product_value.getText();
145                 return product != null && product.startsWith("TeleGPS");
146         }
147
148         void set_radio_enable_tool_tip() {
149                 if (radio_enable_value.isVisible())
150                         radio_enable_value.setToolTipText("Enable/Disable telemetry and RDF transmissions");
151                 else
152                         radio_enable_value.setToolTipText("Firmware version does not support disabling radio");
153         }
154
155         void set_rate_tool_tip() {
156                 if (rate_value.isVisible())
157                         rate_value.setToolTipText("Select telemetry baud rate");
158                 else
159                         rate_value.setToolTipText("Firmware version does not support variable telemetry rates");
160         }
161
162         void set_aprs_interval_tool_tip() {
163                 if (aprs_interval_value.isVisible())
164                         aprs_interval_value.setToolTipText("Enable APRS and set the interval between APRS reports");
165                 else
166                         aprs_interval_value.setToolTipText("Hardware doesn't support APRS");
167         }
168
169         void set_aprs_ssid_tool_tip() {
170                 if (aprs_ssid_value.isVisible())
171                         aprs_ssid_value.setToolTipText("Set the APRS SSID (secondary station identifier)");
172                 else if (aprs_ssid_value.isVisible())
173                         aprs_ssid_value.setToolTipText("Software version doesn't support setting the APRS SSID");
174                 else
175                         aprs_ssid_value.setToolTipText("Hardware doesn't support APRS");
176         }
177
178         void set_aprs_format_tool_tip() {
179                 if (aprs_format_value.isVisible())
180                         aprs_format_value.setToolTipText("Set the APRS format (compressed/uncompressed)");
181                 else if (aprs_format_value.isVisible())
182                         aprs_format_value.setToolTipText("Software version doesn't support setting the APRS format");
183                 else
184                         aprs_format_value.setToolTipText("Hardware doesn't support APRS");
185         }
186
187         void set_aprs_offset_tool_tip() {
188                 if (aprs_offset_value.isVisible())
189                         aprs_offset_value.setToolTipText("Set the APRS offset from top of minute");
190                 else if (aprs_offset_value.isVisible())
191                         aprs_offset_value.setToolTipText("Software version doesn't support setting the APRS offset");
192                 else
193                         aprs_offset_value.setToolTipText("Hardware doesn't support APRS");
194         }
195
196         void set_flight_log_max_tool_tip() {
197                 if (flight_log_max_value.isVisible())
198                         flight_log_max_value.setToolTipText("Size reserved for each flight log (in kB)");
199                 else
200                         flight_log_max_value.setToolTipText("Cannot set max value with flight logs in memory");
201         }
202
203         /* Build the UI using a grid bag */
204         public TeleGPSConfigUI(JFrame in_owner) {
205                 super (in_owner, "Configure Device", false);
206
207                 owner = in_owner;
208                 GridBagConstraints c;
209                 int row = 0;
210
211                 Insets il = new Insets(4,4,4,4);
212                 Insets ir = new Insets(4,4,4,4);
213
214                 pane = getScrollablePane();
215                 pane.setLayout(new GridBagLayout());
216
217                 /* Product */
218                 c = new GridBagConstraints();
219                 c.gridx = 0; c.gridy = row;
220                 c.gridwidth = 4;
221                 c.fill = GridBagConstraints.NONE;
222                 c.anchor = GridBagConstraints.LINE_START;
223                 c.insets = il;
224                 product_label = new JLabel("Product:");
225                 pane.add(product_label, c);
226
227                 c = new GridBagConstraints();
228                 c.gridx = 4; c.gridy = row;
229                 c.gridwidth = 4;
230                 c.fill = GridBagConstraints.HORIZONTAL;
231                 c.weightx = 1;
232                 c.anchor = GridBagConstraints.LINE_START;
233                 c.insets = ir;
234                 product_value = new JLabel("");
235                 pane.add(product_value, c);
236                 row++;
237
238                 /* Version */
239                 c = new GridBagConstraints();
240                 c.gridx = 0; c.gridy = row;
241                 c.gridwidth = 4;
242                 c.fill = GridBagConstraints.NONE;
243                 c.anchor = GridBagConstraints.LINE_START;
244                 c.insets = il;
245                 c.ipady = 5;
246                 version_label = new JLabel("Software version:");
247                 pane.add(version_label, c);
248
249                 c = new GridBagConstraints();
250                 c.gridx = 4; c.gridy = row;
251                 c.gridwidth = 4;
252                 c.fill = GridBagConstraints.HORIZONTAL;
253                 c.weightx = 1;
254                 c.anchor = GridBagConstraints.LINE_START;
255                 c.insets = ir;
256                 c.ipady = 5;
257                 version_value = new JLabel("");
258                 pane.add(version_value, c);
259                 row++;
260
261                 /* Serial */
262                 c = new GridBagConstraints();
263                 c.gridx = 0; c.gridy = row;
264                 c.gridwidth = 4;
265                 c.fill = GridBagConstraints.NONE;
266                 c.anchor = GridBagConstraints.LINE_START;
267                 c.insets = il;
268                 c.ipady = 5;
269                 serial_label = new JLabel("Serial:");
270                 pane.add(serial_label, c);
271
272                 c = new GridBagConstraints();
273                 c.gridx = 4; c.gridy = row;
274                 c.gridwidth = 4;
275                 c.fill = GridBagConstraints.HORIZONTAL;
276                 c.weightx = 1;
277                 c.anchor = GridBagConstraints.LINE_START;
278                 c.insets = ir;
279                 c.ipady = 5;
280                 serial_value = new JLabel("");
281                 pane.add(serial_value, c);
282                 row++;
283
284                 /* Frequency */
285                 c = new GridBagConstraints();
286                 c.gridx = 0; c.gridy = row;
287                 c.gridwidth = 4;
288                 c.fill = GridBagConstraints.NONE;
289                 c.anchor = GridBagConstraints.LINE_START;
290                 c.insets = il;
291                 c.ipady = 5;
292                 radio_frequency_label = new JLabel("Frequency:");
293                 pane.add(radio_frequency_label, c);
294
295                 c = new GridBagConstraints();
296                 c.gridx = 4; c.gridy = row;
297                 c.gridwidth = 4;
298                 c.fill = GridBagConstraints.HORIZONTAL;
299                 c.weightx = 1;
300                 c.anchor = GridBagConstraints.LINE_START;
301                 c.insets = ir;
302                 c.ipady = 5;
303                 radio_frequency_value = new AltosUIFreqList();
304                 radio_frequency_value.addItemListener(this);
305                 pane.add(radio_frequency_value, c);
306                 radio_frequency_value.setToolTipText("Telemetry, RDF and packet frequency");
307                 row++;
308
309                 /* Radio Calibration */
310                 c = new GridBagConstraints();
311                 c.gridx = 0; c.gridy = row;
312                 c.gridwidth = 4;
313                 c.fill = GridBagConstraints.NONE;
314                 c.anchor = GridBagConstraints.LINE_START;
315                 c.insets = il;
316                 c.ipady = 5;
317                 radio_calibration_label = new JLabel("RF Calibration:");
318                 pane.add(radio_calibration_label, c);
319
320                 c = new GridBagConstraints();
321                 c.gridx = 4; c.gridy = row;
322                 c.gridwidth = 4;
323                 c.fill = GridBagConstraints.HORIZONTAL;
324                 c.weightx = 1;
325                 c.anchor = GridBagConstraints.LINE_START;
326                 c.insets = ir;
327                 c.ipady = 5;
328                 radio_calibration_value = new JLabel(String.format("%d", 1186611));
329                 pane.add(radio_calibration_value, c);
330                 row++;
331
332                 /* Radio Enable */
333                 c = new GridBagConstraints();
334                 c.gridx = 0; c.gridy = row;
335                 c.gridwidth = 4;
336                 c.fill = GridBagConstraints.NONE;
337                 c.anchor = GridBagConstraints.LINE_START;
338                 c.insets = il;
339                 c.ipady = 5;
340                 radio_enable_label = new JLabel("Telemetry/RDF/APRS Enable:");
341                 pane.add(radio_enable_label, c);
342
343                 c = new GridBagConstraints();
344                 c.gridx = 4; c.gridy = row;
345                 c.gridwidth = 4;
346                 c.fill = GridBagConstraints.HORIZONTAL;
347                 c.weightx = 1;
348                 c.anchor = GridBagConstraints.LINE_START;
349                 c.insets = ir;
350                 c.ipady = 5;
351                 radio_enable_value = new JRadioButton("Enabled");
352                 radio_enable_value.addItemListener(this);
353                 pane.add(radio_enable_value, c);
354                 set_radio_enable_tool_tip();
355                 row++;
356
357                 /* Telemetry Rate */
358                 c = new GridBagConstraints();
359                 c.gridx = 0; c.gridy = row;
360                 c.gridwidth = 4;
361                 c.fill = GridBagConstraints.NONE;
362                 c.anchor = GridBagConstraints.LINE_START;
363                 c.insets = il;
364                 c.ipady = 5;
365                 rate_label = new JLabel("Telemetry baud rate:");
366                 pane.add(rate_label, c);
367
368                 c = new GridBagConstraints();
369                 c.gridx = 4; c.gridy = row;
370                 c.gridwidth = 4;
371                 c.fill = GridBagConstraints.HORIZONTAL;
372                 c.weightx = 1;
373                 c.anchor = GridBagConstraints.LINE_START;
374                 c.insets = ir;
375                 c.ipady = 5;
376                 rate_value = new AltosUIRateList();
377                 rate_value.addItemListener(this);
378                 pane.add(rate_value, c);
379                 set_rate_tool_tip();
380                 row++;
381
382                 /* APRS interval */
383                 c = new GridBagConstraints();
384                 c.gridx = 0; c.gridy = row;
385                 c.gridwidth = 4;
386                 c.fill = GridBagConstraints.NONE;
387                 c.anchor = GridBagConstraints.LINE_START;
388                 c.insets = il;
389                 c.ipady = 5;
390                 aprs_interval_label = new JLabel("APRS Interval(s):");
391                 pane.add(aprs_interval_label, c);
392
393                 c = new GridBagConstraints();
394                 c.gridx = 4; c.gridy = row;
395                 c.gridwidth = 4;
396                 c.fill = GridBagConstraints.HORIZONTAL;
397                 c.weightx = 1;
398                 c.anchor = GridBagConstraints.LINE_START;
399                 c.insets = ir;
400                 c.ipady = 5;
401                 aprs_interval_value = new JComboBox<String>(aprs_interval_values);
402                 aprs_interval_value.setEditable(true);
403                 aprs_interval_value.addItemListener(this);
404                 pane.add(aprs_interval_value, c);
405                 set_aprs_interval_tool_tip();
406                 row++;
407
408                 /* APRS SSID */
409                 c = new GridBagConstraints();
410                 c.gridx = 0; c.gridy = row;
411                 c.gridwidth = 4;
412                 c.fill = GridBagConstraints.NONE;
413                 c.anchor = GridBagConstraints.LINE_START;
414                 c.insets = il;
415                 c.ipady = 5;
416                 aprs_ssid_label = new JLabel("APRS SSID:");
417                 pane.add(aprs_ssid_label, c);
418
419                 c = new GridBagConstraints();
420                 c.gridx = 4; c.gridy = row;
421                 c.gridwidth = 4;
422                 c.fill = GridBagConstraints.HORIZONTAL;
423                 c.weightx = 1;
424                 c.anchor = GridBagConstraints.LINE_START;
425                 c.insets = ir;
426                 c.ipady = 5;
427                 aprs_ssid_value = new JComboBox<Integer>(aprs_ssid_values);
428                 aprs_ssid_value.setEditable(false);
429                 aprs_ssid_value.addItemListener(this);
430                 aprs_ssid_value.setMaximumRowCount(aprs_ssid_values.length);
431                 pane.add(aprs_ssid_value, c);
432                 set_aprs_ssid_tool_tip();
433                 row++;
434
435                 /* APRS format */
436                 c = new GridBagConstraints();
437                 c.gridx = 0; c.gridy = row;
438                 c.gridwidth = 4;
439                 c.fill = GridBagConstraints.NONE;
440                 c.anchor = GridBagConstraints.LINE_START;
441                 c.insets = il;
442                 c.ipady = 5;
443                 aprs_format_label = new JLabel("APRS format:");
444                 pane.add(aprs_format_label, c);
445
446                 c = new GridBagConstraints();
447                 c.gridx = 4; c.gridy = row;
448                 c.gridwidth = 4;
449                 c.fill = GridBagConstraints.HORIZONTAL;
450                 c.weightx = 1;
451                 c.anchor = GridBagConstraints.LINE_START;
452                 c.insets = ir;
453                 c.ipady = 5;
454                 aprs_format_value = new JComboBox<String>(AltosLib.ao_aprs_format_name);
455                 aprs_format_value.setEditable(false);
456                 aprs_format_value.addItemListener(this);
457                 aprs_format_value.setMaximumRowCount(AltosLib.ao_aprs_format_name.length);
458                 pane.add(aprs_format_value, c);
459                 set_aprs_format_tool_tip();
460                 row++;
461
462                 /* APRS offset */
463                 c = new GridBagConstraints();
464                 c.gridx = 0; c.gridy = row;
465                 c.gridwidth = 4;
466                 c.fill = GridBagConstraints.NONE;
467                 c.anchor = GridBagConstraints.LINE_START;
468                 c.insets = il;
469                 c.ipady = 5;
470                 aprs_offset_label = new JLabel("APRS offset:");
471                 pane.add(aprs_offset_label, c);
472
473                 c = new GridBagConstraints();
474                 c.gridx = 4; c.gridy = row;
475                 c.gridwidth = 4;
476                 c.fill = GridBagConstraints.HORIZONTAL;
477                 c.weightx = 1;
478                 c.anchor = GridBagConstraints.LINE_START;
479                 c.insets = ir;
480                 c.ipady = 5;
481                 aprs_offset_value = new JComboBox<Integer>(aprs_offset_values);
482                 aprs_offset_value.setEditable(false);
483                 aprs_offset_value.addItemListener(this);
484                 aprs_offset_value.setMaximumRowCount(aprs_offset_values.length);
485                 pane.add(aprs_offset_value, c);
486                 set_aprs_offset_tool_tip();
487                 row++;
488
489                 /* Callsign */
490                 c = new GridBagConstraints();
491                 c.gridx = 0; c.gridy = row;
492                 c.gridwidth = 4;
493                 c.fill = GridBagConstraints.NONE;
494                 c.anchor = GridBagConstraints.LINE_START;
495                 c.insets = il;
496                 c.ipady = 5;
497                 callsign_label = new JLabel("Callsign:");
498                 pane.add(callsign_label, c);
499
500                 c = new GridBagConstraints();
501                 c.gridx = 4; c.gridy = row;
502                 c.gridwidth = 4;
503                 c.fill = GridBagConstraints.HORIZONTAL;
504                 c.weightx = 1;
505                 c.anchor = GridBagConstraints.LINE_START;
506                 c.insets = ir;
507                 c.ipady = 5;
508                 callsign_value = new JTextField(AltosUIPreferences.callsign());
509                 callsign_value.getDocument().addDocumentListener(this);
510                 pane.add(callsign_value, c);
511                 callsign_value.setToolTipText("Callsign reported in telemetry data");
512                 row++;
513
514                 /* Flight log max */
515                 c = new GridBagConstraints();
516                 c.gridx = 0; c.gridy = row;
517                 c.gridwidth = 4;
518                 c.fill = GridBagConstraints.NONE;
519                 c.anchor = GridBagConstraints.LINE_START;
520                 c.insets = il;
521                 c.ipady = 5;
522                 flight_log_max_label = new JLabel("Maximum Log Size (kB):");
523                 pane.add(flight_log_max_label, c);
524
525                 c = new GridBagConstraints();
526                 c.gridx = 4; c.gridy = row;
527                 c.gridwidth = 4;
528                 c.fill = GridBagConstraints.HORIZONTAL;
529                 c.weightx = 1;
530                 c.anchor = GridBagConstraints.LINE_START;
531                 c.insets = ir;
532                 c.ipady = 5;
533                 flight_log_max_value = new JComboBox<String>();
534                 flight_log_max_value.setEditable(true);
535                 flight_log_max_value.addItemListener(this);
536                 pane.add(flight_log_max_value, c);
537                 set_flight_log_max_tool_tip();
538                 row++;
539
540                 /* Tracker triger horiz distances */
541                 c = new GridBagConstraints();
542                 c.gridx = 0; c.gridy = row;
543                 c.gridwidth = 4;
544                 c.fill = GridBagConstraints.NONE;
545                 c.anchor = GridBagConstraints.LINE_START;
546                 c.insets = il;
547                 c.ipady = 5;
548                 tracker_motion_label = new JLabel(get_tracker_motion_label());
549                 pane.add(tracker_motion_label, c);
550
551                 c = new GridBagConstraints();
552                 c.gridx = 4; c.gridy = row;
553                 c.gridwidth = 4;
554                 c.fill = GridBagConstraints.HORIZONTAL;
555                 c.weightx = 1;
556                 c.anchor = GridBagConstraints.LINE_START;
557                 c.insets = ir;
558                 c.ipady = 5;
559                 tracker_motion_value = new JComboBox<String>(tracker_motion_values());
560                 tracker_motion_value.setEditable(true);
561                 tracker_motion_value.addItemListener(this);
562                 pane.add(tracker_motion_value, c);
563                 row++;
564
565                 /* Tracker triger vert distances */
566                 c = new GridBagConstraints();
567                 c.gridx = 0; c.gridy = row;
568                 c.gridwidth = 4;
569                 c.fill = GridBagConstraints.NONE;
570                 c.anchor = GridBagConstraints.LINE_START;
571                 c.insets = il;
572                 c.ipady = 5;
573                 tracker_interval_label = new JLabel("Position Reporting Interval (s):");
574                 pane.add(tracker_interval_label, c);
575
576                 c = new GridBagConstraints();
577                 c.gridx = 4; c.gridy = row;
578                 c.gridwidth = 4;
579                 c.fill = GridBagConstraints.HORIZONTAL;
580                 c.weightx = 1;
581                 c.anchor = GridBagConstraints.LINE_START;
582                 c.insets = ir;
583                 c.ipady = 5;
584                 tracker_interval_value = new JComboBox<String>(tracker_interval_values);
585                 tracker_interval_value.setEditable(true);
586                 tracker_interval_value.addItemListener(this);
587                 pane.add(tracker_interval_value, c);
588                 set_tracker_tool_tip();
589                 row++;
590
591                 /* Buttons */
592                 c = new GridBagConstraints();
593                 c.gridx = 0; c.gridy = row;
594                 c.gridwidth = 2;
595                 c.fill = GridBagConstraints.NONE;
596                 c.anchor = GridBagConstraints.LINE_START;
597                 c.insets = il;
598                 save = new JButton("Save");
599                 pane.add(save, c);
600                 save.addActionListener(this);
601                 save.setActionCommand("Save");
602
603                 c = new GridBagConstraints();
604                 c.gridx = 2; c.gridy = row;
605                 c.gridwidth = 2;
606                 c.fill = GridBagConstraints.NONE;
607                 c.anchor = GridBagConstraints.CENTER;
608                 c.insets = il;
609                 reset = new JButton("Reset");
610                 pane.add(reset, c);
611                 reset.addActionListener(this);
612                 reset.setActionCommand("Reset");
613
614                 c = new GridBagConstraints();
615                 c.gridx = 4; c.gridy = row;
616                 c.gridwidth = 2;
617                 c.fill = GridBagConstraints.NONE;
618                 c.anchor = GridBagConstraints.CENTER;
619                 c.insets = il;
620                 reboot = new JButton("Reboot");
621                 pane.add(reboot, c);
622                 reboot.addActionListener(this);
623                 reboot.setActionCommand("Reboot");
624
625                 c = new GridBagConstraints();
626                 c.gridx = 6; c.gridy = row;
627                 c.gridwidth = 2;
628                 c.fill = GridBagConstraints.NONE;
629                 c.anchor = GridBagConstraints.LINE_END;
630                 c.insets = il;
631                 close = new JButton("Close");
632                 pane.add(close, c);
633                 close.addActionListener(this);
634                 close.setActionCommand("Close");
635
636                 addWindowListener(new ConfigListener(this));
637                 AltosPreferences.register_units_listener(this);
638         }
639
640         /* Once the initial values are set, the config code will show the dialog */
641         public void make_visible() {
642                 pack();
643                 setLocationRelativeTo(owner);
644                 setVisible(true);
645         }
646
647         /* If any values have been changed, confirm before closing */
648         public boolean check_dirty(String operation) {
649                 if (dirty) {
650                         Object[] options = { String.format("%s anyway", operation), "Keep editing" };
651                         int i;
652                         i = JOptionPane.showOptionDialog(this,
653                                                          String.format("Configuration modified. %s anyway?", operation),
654                                                          "Configuration Modified",
655                                                          JOptionPane.DEFAULT_OPTION,
656                                                          JOptionPane.WARNING_MESSAGE,
657                                                          null, options, options[1]);
658                         if (i != 0)
659                                 return false;
660                 }
661                 return true;
662         }
663
664         public void set_dirty() {
665                 dirty = true;
666                 save.setEnabled(true);
667         }
668
669         public void set_clean() {
670                 dirty = false;
671                 save.setEnabled(false);
672         }
673
674         public void dispose() {
675                 AltosPreferences.unregister_units_listener(this);
676                 super.dispose();
677         }
678
679         public int accel_cal_plus() {
680                 return AltosLib.MISSING;
681         }
682
683         public int accel_cal_minus() {
684                 return AltosLib.MISSING;
685         }
686
687         public void set_accel_cal(int accel_plus, int accel_minus) {
688         }
689
690         /* Listen for events from our buttons */
691         public void actionPerformed(ActionEvent e) {
692                 String  cmd = e.getActionCommand();
693
694                 if (cmd.equals("Close") || cmd.equals("Reboot"))
695                         if (!check_dirty(cmd))
696                                 return;
697                 listener.actionPerformed(e);
698                 if (cmd.equals("Close") || cmd.equals("Reboot")) {
699                         setVisible(false);
700                         dispose();
701                 }
702                 set_clean();
703         }
704
705         /* ItemListener interface method */
706         public void itemStateChanged(ItemEvent e) {
707                 set_dirty();
708         }
709
710         /* DocumentListener interface methods */
711         public void changedUpdate(DocumentEvent e) {
712                 set_dirty();
713         }
714
715         public void insertUpdate(DocumentEvent e) {
716                 set_dirty();
717         }
718
719         public void removeUpdate(DocumentEvent e) {
720                 set_dirty();
721         }
722
723         /* Let the config code hook on a listener */
724         public void addActionListener(ActionListener l) {
725                 listener = l;
726         }
727
728         public void units_changed(boolean imperial_units) {
729                 boolean was_dirty = dirty;
730
731                 if (tracker_motion_value.isVisible()) {
732                         String motion = tracker_motion_value.getSelectedItem().toString();
733                         tracker_motion_label.setText(get_tracker_motion_label());
734                         set_tracker_motion_values();
735                         try {
736                                 int m = (int) (AltosConvert.height.parse_locale(motion, !imperial_units) + 0.5);
737                                 set_tracker_motion(m);
738                         } catch (ParseException pe) {
739                         }
740                 }
741                 if (!was_dirty)
742                         set_clean();
743         }
744
745         /* set and get all of the dialog values */
746         public void set_product(String product) {
747                 radio_frequency_value.set_product(product);
748                 product_value.setText(product);
749                 set_flight_log_max_tool_tip();
750         }
751
752         public void set_version(String version) {
753                 version_value.setText(version);
754         }
755
756         public void set_serial(int serial) {
757                 radio_frequency_value.set_serial(serial);
758                 serial_value.setText(String.format("%d", serial));
759         }
760
761         public void set_altitude_32(int altitude_32) {
762         }
763
764         public void set_main_deploy(int new_main_deploy) {
765         }
766
767         public int main_deploy() {
768                 return AltosLib.MISSING;
769         }
770
771         public void set_apogee_delay(int new_apogee_delay) { }
772
773         public int apogee_delay() {
774                 return AltosLib.MISSING;
775         }
776
777         public void set_apogee_lockout(int new_apogee_lockout) { }
778
779         public int apogee_lockout() { return AltosLib.MISSING; }
780
781         public void set_radio_frequency(double new_radio_frequency) {
782                 if (new_radio_frequency != AltosLib.MISSING)
783                         radio_frequency_value.set_frequency(new_radio_frequency);
784                 radio_frequency_label.setVisible(new_radio_frequency != AltosLib.MISSING);
785                 radio_frequency_value.setVisible(new_radio_frequency != AltosLib.MISSING);
786         }
787
788         public double radio_frequency() {
789                 return radio_frequency_value.frequency();
790         }
791
792         public void set_radio_calibration(int new_radio_calibration) {
793                 if (new_radio_calibration != AltosLib.MISSING)
794                         radio_calibration_value.setText(String.format("%d", new_radio_calibration));
795                 radio_calibration_value.setVisible(new_radio_calibration == AltosLib.MISSING);
796                 radio_calibration_label.setVisible(new_radio_calibration == AltosLib.MISSING);
797         }
798
799         public void set_radio_enable(int new_radio_enable) {
800                 if (new_radio_enable != AltosLib.MISSING)
801                         radio_enable_value.setSelected(new_radio_enable != 0);
802                 radio_enable_label.setVisible(new_radio_enable != AltosLib.MISSING);
803                 radio_enable_value.setVisible(new_radio_enable != AltosLib.MISSING);
804                 set_radio_enable_tool_tip();
805         }
806
807         public int radio_enable() {
808                 if (radio_enable_value.isVisible())
809                         return radio_enable_value.isSelected() ? 1 : 0;
810                 else
811                         return AltosLib.MISSING;
812         }
813
814         public void set_telemetry_rate(int new_rate) {
815                 if (new_rate != AltosLib.MISSING)
816                         rate_value.set_rate(new_rate);
817                 rate_label.setVisible(new_rate != AltosLib.MISSING);
818                 rate_value.setVisible(new_rate != AltosLib.MISSING);
819         }
820
821         public int telemetry_rate() {
822                 return rate_value.rate();
823         }
824
825         public void set_callsign(String new_callsign) {
826                 if (new_callsign != null)
827                         callsign_value.setText(new_callsign);
828                 callsign_value.setVisible(new_callsign != null);
829                 callsign_label.setVisible(new_callsign != null);
830         }
831
832         public String callsign() {
833                 if (callsign_value.isVisible())
834                         return callsign_value.getText();
835                 return null;
836         }
837
838         private int parse_int(String name, String s, boolean split) throws AltosConfigDataException {
839                 String v = s;
840                 if (split)
841                         v = s.split("\\s+")[0];
842                 try {
843                         return Integer.parseInt(v);
844                 } catch (NumberFormatException ne) {
845                         throw new AltosConfigDataException("Invalid %s \"%s\"", name, s);
846                 }
847         }
848
849         int     flight_log_max_limit;
850         int     flight_log_max;
851
852         public String flight_log_max_label(int flight_log_max) {
853                 if (flight_log_max_limit != 0) {
854                         int     nflight = flight_log_max_limit / flight_log_max;
855                         String  plural = nflight > 1 ? "s" : "";
856
857                         return String.format("%d (%d flight%s)", flight_log_max, nflight, plural);
858                 }
859                 return String.format("%d", flight_log_max);
860         }
861
862         public void set_flight_log_max(int new_flight_log_max) {
863                 flight_log_max_value.setSelectedItem(flight_log_max_label(new_flight_log_max));
864                 flight_log_max = new_flight_log_max;
865                 set_flight_log_max_tool_tip();
866         }
867
868         public void set_flight_log_max_enabled(boolean enable) {
869                 flight_log_max_value.setEnabled(enable);
870                 set_flight_log_max_tool_tip();
871         }
872
873         public int flight_log_max() throws AltosConfigDataException {
874                 return parse_int("flight log max", flight_log_max_value.getSelectedItem().toString(), true);
875         }
876
877         public void set_flight_log_max_limit(int new_flight_log_max_limit, int new_storage_erase_unit) {
878                 flight_log_max_limit = new_flight_log_max_limit;
879                 if (new_flight_log_max_limit != AltosLib.MISSING) {
880                         flight_log_max_value.removeAllItems();
881                         for (int i = 8; i >= 1; i--) {
882                                 int     size = flight_log_max_limit / i;
883                                 if (new_storage_erase_unit != 0)
884                                         size &= ~(new_storage_erase_unit - 1);
885                                 flight_log_max_value.addItem(String.format("%d (%d flights)", size, i));
886                         }
887                 }
888                 if (flight_log_max != 0 && flight_log_max != AltosLib.MISSING)
889                         set_flight_log_max(flight_log_max);
890         }
891
892         public void set_ignite_mode(int new_ignite_mode) { }
893         public int ignite_mode() { return AltosLib.MISSING; }
894
895
896         public void set_pad_orientation(int new_pad_orientation) { }
897         public int pad_orientation() { return AltosLib.MISSING; }
898
899         public void set_beep(int new_beep) { }
900
901         public int beep() { return AltosLib.MISSING; }
902
903         String[] tracker_motion_values() {
904                 if (AltosConvert.imperial_units)
905                         return tracker_motion_values_ft;
906                 else
907                         return tracker_motion_values_m;
908         }
909
910         void set_tracker_motion_values() {
911                 String[]        v = tracker_motion_values();
912                 while (tracker_motion_value.getItemCount() > 0)
913                         tracker_motion_value.removeItemAt(0);
914                 for (int i = 0; i < v.length; i++)
915                         tracker_motion_value.addItem(v[i]);
916                 tracker_motion_value.setMaximumRowCount(v.length);
917         }
918
919         String get_tracker_motion_label() {
920                 return String.format("Logging Trigger Motion (%s):", AltosConvert.height.parse_units());
921         }
922
923         void set_tracker_tool_tip() {
924                 if (tracker_motion_value.isVisible())
925                         tracker_motion_value.setToolTipText("How far the device must move before logging");
926                 else
927                         tracker_motion_value.setToolTipText("This device doesn't disable logging when stationary");
928                 if (tracker_interval_value.isVisible())
929                         tracker_interval_value.setToolTipText("How often to report GPS position");
930                 else
931                         tracker_interval_value.setToolTipText("This device can't configure interval");
932         }
933
934         public void set_tracker_motion(int tracker_motion) {
935                 if (tracker_motion != AltosLib.MISSING)
936                         tracker_motion_value.setSelectedItem(AltosConvert.height.say(tracker_motion));
937                 tracker_motion_label.setVisible(tracker_motion != AltosLib.MISSING);
938                 tracker_motion_value.setVisible(tracker_motion != AltosLib.MISSING);
939         }
940
941         public int tracker_motion() throws AltosConfigDataException {
942                 if (tracker_motion_value.isVisible()) {
943                         String str = tracker_motion_value.getSelectedItem().toString();
944                         try {
945                                 return (int) (AltosConvert.height.parse_locale(str) + 0.5);
946                         } catch (ParseException pe) {
947                                 throw new AltosConfigDataException("invalid tracker motion %s", str);
948                         }
949                 }
950                 return AltosLib.MISSING;
951         }
952
953         public void set_tracker_interval(int tracker_interval) {
954                 if (tracker_interval != AltosLib.MISSING)
955                         tracker_interval_value.setSelectedItem(String.format("%d", tracker_interval));
956                 tracker_interval_label.setVisible(tracker_interval != AltosLib.MISSING);
957                 tracker_interval_value.setVisible(tracker_interval != AltosLib.MISSING);
958         }
959
960         public int tracker_interval() throws AltosConfigDataException {
961                 if (tracker_interval_value.isVisible())
962                         return parse_int ("tracker interval", tracker_interval_value.getSelectedItem().toString(), false);
963                 return AltosLib.MISSING;
964         }
965
966         public void set_aprs_interval(int new_aprs_interval) {
967                 if (new_aprs_interval != AltosLib.MISSING)
968                         aprs_interval_value.setSelectedItem(Integer.toString(new_aprs_interval));
969                 aprs_interval_value.setVisible(new_aprs_interval != AltosLib.MISSING);
970                 aprs_interval_label.setVisible(new_aprs_interval != AltosLib.MISSING);
971                 set_aprs_interval_tool_tip();
972         }
973
974         public int aprs_interval() throws AltosConfigDataException {
975                 if (aprs_interval_value.isVisible()) {
976                         String  s = aprs_interval_value.getSelectedItem().toString();
977
978                         return parse_int("aprs interval", s, false);
979                 }
980                 return AltosLib.MISSING;
981         }
982
983         public void set_aprs_ssid(int new_aprs_ssid) {
984                 if (new_aprs_ssid != AltosLib.MISSING)
985                         aprs_ssid_value.setSelectedItem(new_aprs_ssid);
986                 aprs_ssid_value.setVisible(new_aprs_ssid != AltosLib.MISSING);
987                 aprs_ssid_label.setVisible(new_aprs_ssid != AltosLib.MISSING);
988                 set_aprs_ssid_tool_tip();
989         }
990
991         public int aprs_ssid() throws AltosConfigDataException {
992                 if (aprs_ssid_value.isVisible()) {
993                         Integer i = (Integer) aprs_ssid_value.getSelectedItem();
994                         return i;
995                 }
996                 return AltosLib.MISSING;
997         }
998
999         public void set_aprs_format(int new_aprs_format) {
1000                 if (new_aprs_format != AltosLib.MISSING)
1001                         aprs_format_value.setSelectedIndex(new_aprs_format);
1002                 aprs_format_value.setVisible(new_aprs_format != AltosLib.MISSING);
1003                 aprs_format_label.setVisible(new_aprs_format != AltosLib.MISSING);
1004                 set_aprs_format_tool_tip();
1005         }
1006
1007         public int aprs_format() throws AltosConfigDataException {
1008                 if (aprs_format_value.isVisible())
1009                         return aprs_format_value.getSelectedIndex();
1010                 return AltosLib.MISSING;
1011         }
1012         public void set_aprs_offset(int new_aprs_offset) {
1013                 if (new_aprs_offset != AltosLib.MISSING)
1014                         aprs_offset_value.setSelectedItem(new_aprs_offset);
1015                 aprs_offset_value.setVisible(new_aprs_offset != AltosLib.MISSING);
1016                 aprs_offset_label.setVisible(new_aprs_offset != AltosLib.MISSING);
1017                 set_aprs_offset_tool_tip();
1018         }
1019
1020         public int aprs_offset() throws AltosConfigDataException {
1021                 if (aprs_offset_value.isVisible()) {
1022                         Integer i = (Integer) aprs_offset_value.getSelectedItem();
1023                         return i;
1024                 }
1025                 return AltosLib.MISSING;
1026         }
1027 }