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