2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 package org.altusmetrum.telegps;
21 import java.awt.event.*;
23 import javax.swing.event.*;
24 import org.altusmetrum.altoslib_4.*;
25 import org.altusmetrum.altosuilib_2.*;
27 public class TeleGPSConfigUI
29 implements ActionListener, ItemListener, DocumentListener, AltosConfigValues, AltosUnitsListener
36 JLabel frequency_label;
37 JLabel radio_calibration_label;
38 JLabel radio_frequency_label;
39 JLabel radio_enable_label;
40 JLabel aprs_interval_label;
41 JLabel flight_log_max_label;
42 JLabel callsign_label;
43 JLabel tracker_horiz_label;
44 JLabel tracker_vert_label;
52 AltosFreqList radio_frequency_value;
53 JTextField radio_calibration_value;
54 JRadioButton radio_enable_value;
55 JComboBox<String> aprs_interval_value;
56 JComboBox<String> flight_log_max_value;
57 JTextField callsign_value;
58 JComboBox<String> tracker_horiz_value;
59 JComboBox<String> tracker_vert_value;
66 ActionListener listener;
68 static String[] aprs_interval_values = {
75 static String[] tracker_horiz_values_m = {
82 static String[] tracker_horiz_values_ft = {
89 static String[] tracker_vert_values_m = {
96 static String[] tracker_vert_values_ft = {
103 /* A window listener to catch closing events and tell the config code */
104 class ConfigListener extends WindowAdapter {
107 public ConfigListener(TeleGPSConfigUI this_ui) {
111 public void windowClosing(WindowEvent e) {
112 ui.actionPerformed(new ActionEvent(e.getSource(),
113 ActionEvent.ACTION_PERFORMED,
118 public void set_pyros(AltosPyro[] new_pyros) {
121 public AltosPyro[] pyros() {
125 boolean is_telemetrum() {
126 String product = product_value.getText();
127 return product != null && product.startsWith("TeleGPS");
130 void set_radio_calibration_tool_tip() {
131 if (radio_calibration_value.isEnabled())
132 radio_calibration_value.setToolTipText("Tune radio output to match desired frequency");
134 radio_calibration_value.setToolTipText("Cannot tune radio while connected over packet mode");
137 void set_radio_enable_tool_tip() {
138 if (radio_enable_value.isEnabled())
139 radio_enable_value.setToolTipText("Enable/Disable telemetry and RDF transmissions");
141 radio_enable_value.setToolTipText("Firmware version does not support disabling radio");
144 void set_aprs_interval_tool_tip() {
145 if (aprs_interval_value.isEnabled())
146 aprs_interval_value.setToolTipText("Enable APRS and set the interval between APRS reports");
148 aprs_interval_value.setToolTipText("Hardware doesn't support APRS");
151 void set_flight_log_max_tool_tip() {
152 if (flight_log_max_value.isEnabled())
153 flight_log_max_value.setToolTipText("Size reserved for each flight log (in kB)");
155 flight_log_max_value.setToolTipText("Cannot set max value with flight logs in memory");
158 /* Build the UI using a grid bag */
159 public TeleGPSConfigUI(JFrame in_owner) {
160 super (in_owner, "Configure Flight Computer", false);
163 GridBagConstraints c;
166 Insets il = new Insets(4,4,4,4);
167 Insets ir = new Insets(4,4,4,4);
169 pane = getContentPane();
170 pane.setLayout(new GridBagLayout());
173 c = new GridBagConstraints();
174 c.gridx = 0; c.gridy = row;
176 c.fill = GridBagConstraints.NONE;
177 c.anchor = GridBagConstraints.LINE_START;
179 product_label = new JLabel("Product:");
180 pane.add(product_label, c);
182 c = new GridBagConstraints();
183 c.gridx = 4; c.gridy = row;
185 c.fill = GridBagConstraints.HORIZONTAL;
187 c.anchor = GridBagConstraints.LINE_START;
189 product_value = new JLabel("");
190 pane.add(product_value, c);
194 c = new GridBagConstraints();
195 c.gridx = 0; c.gridy = row;
197 c.fill = GridBagConstraints.NONE;
198 c.anchor = GridBagConstraints.LINE_START;
201 version_label = new JLabel("Software version:");
202 pane.add(version_label, c);
204 c = new GridBagConstraints();
205 c.gridx = 4; c.gridy = row;
207 c.fill = GridBagConstraints.HORIZONTAL;
209 c.anchor = GridBagConstraints.LINE_START;
212 version_value = new JLabel("");
213 pane.add(version_value, c);
217 c = new GridBagConstraints();
218 c.gridx = 0; c.gridy = row;
220 c.fill = GridBagConstraints.NONE;
221 c.anchor = GridBagConstraints.LINE_START;
224 serial_label = new JLabel("Serial:");
225 pane.add(serial_label, c);
227 c = new GridBagConstraints();
228 c.gridx = 4; c.gridy = row;
230 c.fill = GridBagConstraints.HORIZONTAL;
232 c.anchor = GridBagConstraints.LINE_START;
235 serial_value = new JLabel("");
236 pane.add(serial_value, c);
240 c = new GridBagConstraints();
241 c.gridx = 0; c.gridy = row;
243 c.fill = GridBagConstraints.NONE;
244 c.anchor = GridBagConstraints.LINE_START;
247 radio_frequency_label = new JLabel("Frequency:");
248 pane.add(radio_frequency_label, c);
250 c = new GridBagConstraints();
251 c.gridx = 4; c.gridy = row;
253 c.fill = GridBagConstraints.HORIZONTAL;
255 c.anchor = GridBagConstraints.LINE_START;
258 radio_frequency_value = new AltosFreqList();
259 radio_frequency_value.addItemListener(this);
260 pane.add(radio_frequency_value, c);
261 radio_frequency_value.setToolTipText("Telemetry, RDF and packet frequency");
264 /* Radio Calibration */
265 c = new GridBagConstraints();
266 c.gridx = 0; c.gridy = row;
268 c.fill = GridBagConstraints.NONE;
269 c.anchor = GridBagConstraints.LINE_START;
272 radio_calibration_label = new JLabel("RF Calibration:");
273 pane.add(radio_calibration_label, c);
275 c = new GridBagConstraints();
276 c.gridx = 4; c.gridy = row;
278 c.fill = GridBagConstraints.HORIZONTAL;
280 c.anchor = GridBagConstraints.LINE_START;
283 radio_calibration_value = new JTextField(String.format("%d", 1186611));
284 radio_calibration_value.getDocument().addDocumentListener(this);
285 pane.add(radio_calibration_value, c);
286 set_radio_calibration_tool_tip();
290 c = new GridBagConstraints();
291 c.gridx = 0; c.gridy = row;
293 c.fill = GridBagConstraints.NONE;
294 c.anchor = GridBagConstraints.LINE_START;
297 radio_enable_label = new JLabel("Telemetry/RDF/APRS Enable:");
298 pane.add(radio_enable_label, c);
300 c = new GridBagConstraints();
301 c.gridx = 4; c.gridy = row;
303 c.fill = GridBagConstraints.HORIZONTAL;
305 c.anchor = GridBagConstraints.LINE_START;
308 radio_enable_value = new JRadioButton("Enabled");
309 radio_enable_value.addItemListener(this);
310 pane.add(radio_enable_value, c);
311 set_radio_enable_tool_tip();
315 c = new GridBagConstraints();
316 c.gridx = 0; c.gridy = row;
318 c.fill = GridBagConstraints.NONE;
319 c.anchor = GridBagConstraints.LINE_START;
322 aprs_interval_label = new JLabel("APRS Interval(s):");
323 pane.add(aprs_interval_label, c);
325 c = new GridBagConstraints();
326 c.gridx = 4; c.gridy = row;
328 c.fill = GridBagConstraints.HORIZONTAL;
330 c.anchor = GridBagConstraints.LINE_START;
333 aprs_interval_value = new JComboBox<String>(aprs_interval_values);
334 aprs_interval_value.setEditable(true);
335 aprs_interval_value.addItemListener(this);
336 pane.add(aprs_interval_value, c);
337 set_aprs_interval_tool_tip();
341 c = new GridBagConstraints();
342 c.gridx = 0; c.gridy = row;
344 c.fill = GridBagConstraints.NONE;
345 c.anchor = GridBagConstraints.LINE_START;
348 callsign_label = new JLabel("Callsign:");
349 pane.add(callsign_label, c);
351 c = new GridBagConstraints();
352 c.gridx = 4; c.gridy = row;
354 c.fill = GridBagConstraints.HORIZONTAL;
356 c.anchor = GridBagConstraints.LINE_START;
359 callsign_value = new JTextField(AltosUIPreferences.callsign());
360 callsign_value.getDocument().addDocumentListener(this);
361 pane.add(callsign_value, c);
362 callsign_value.setToolTipText("Callsign reported in telemetry data");
366 c = new GridBagConstraints();
367 c.gridx = 0; c.gridy = row;
369 c.fill = GridBagConstraints.NONE;
370 c.anchor = GridBagConstraints.LINE_START;
373 flight_log_max_label = new JLabel("Maximum Log Size (kB):");
374 pane.add(flight_log_max_label, c);
376 c = new GridBagConstraints();
377 c.gridx = 4; c.gridy = row;
379 c.fill = GridBagConstraints.HORIZONTAL;
381 c.anchor = GridBagConstraints.LINE_START;
384 flight_log_max_value = new JComboBox<String>();
385 flight_log_max_value.setEditable(true);
386 flight_log_max_value.addItemListener(this);
387 pane.add(flight_log_max_value, c);
388 set_flight_log_max_tool_tip();
391 /* Tracker triger horiz distances */
392 c = new GridBagConstraints();
393 c.gridx = 0; c.gridy = row;
395 c.fill = GridBagConstraints.NONE;
396 c.anchor = GridBagConstraints.LINE_START;
399 tracker_horiz_label = new JLabel(get_tracker_horiz_label());
400 pane.add(tracker_horiz_label, c);
402 c = new GridBagConstraints();
403 c.gridx = 4; c.gridy = row;
405 c.fill = GridBagConstraints.HORIZONTAL;
407 c.anchor = GridBagConstraints.LINE_START;
410 tracker_horiz_value = new JComboBox<String>(tracker_horiz_values());
411 tracker_horiz_value.setEditable(true);
412 tracker_horiz_value.addItemListener(this);
413 pane.add(tracker_horiz_value, c);
416 /* Tracker triger vert distances */
417 c = new GridBagConstraints();
418 c.gridx = 0; c.gridy = row;
420 c.fill = GridBagConstraints.NONE;
421 c.anchor = GridBagConstraints.LINE_START;
424 tracker_vert_label = new JLabel(get_tracker_vert_label());
425 pane.add(tracker_vert_label, c);
427 c = new GridBagConstraints();
428 c.gridx = 4; c.gridy = row;
430 c.fill = GridBagConstraints.HORIZONTAL;
432 c.anchor = GridBagConstraints.LINE_START;
435 tracker_vert_value = new JComboBox<String>(tracker_vert_values());
436 tracker_vert_value.setEditable(true);
437 tracker_vert_value.addItemListener(this);
438 pane.add(tracker_vert_value, c);
439 set_tracker_tool_tip();
443 c = new GridBagConstraints();
444 c.gridx = 0; c.gridy = row;
446 c.fill = GridBagConstraints.NONE;
447 c.anchor = GridBagConstraints.LINE_START;
449 save = new JButton("Save");
451 save.addActionListener(this);
452 save.setActionCommand("Save");
454 c = new GridBagConstraints();
455 c.gridx = 2; c.gridy = row;
457 c.fill = GridBagConstraints.NONE;
458 c.anchor = GridBagConstraints.CENTER;
460 reset = new JButton("Reset");
462 reset.addActionListener(this);
463 reset.setActionCommand("Reset");
465 c = new GridBagConstraints();
466 c.gridx = 4; c.gridy = row;
468 c.fill = GridBagConstraints.NONE;
469 c.anchor = GridBagConstraints.CENTER;
471 reboot = new JButton("Reboot");
473 reboot.addActionListener(this);
474 reboot.setActionCommand("Reboot");
476 c = new GridBagConstraints();
477 c.gridx = 6; c.gridy = row;
479 c.fill = GridBagConstraints.NONE;
480 c.anchor = GridBagConstraints.LINE_END;
482 close = new JButton("Close");
484 close.addActionListener(this);
485 close.setActionCommand("Close");
487 addWindowListener(new ConfigListener(this));
488 AltosPreferences.register_units_listener(this);
491 /* Once the initial values are set, the config code will show the dialog */
492 public void make_visible() {
494 setLocationRelativeTo(owner);
498 /* If any values have been changed, confirm before closing */
499 public boolean check_dirty(String operation) {
501 Object[] options = { String.format("%s anyway", operation), "Keep editing" };
503 i = JOptionPane.showOptionDialog(this,
504 String.format("Configuration modified. %s anyway?", operation),
505 "Configuration Modified",
506 JOptionPane.DEFAULT_OPTION,
507 JOptionPane.WARNING_MESSAGE,
508 null, options, options[1]);
517 save.setEnabled(true);
520 public void set_clean() {
522 save.setEnabled(false);
525 public void dispose() {
526 AltosPreferences.unregister_units_listener(this);
530 /* Listen for events from our buttons */
531 public void actionPerformed(ActionEvent e) {
532 String cmd = e.getActionCommand();
534 if (cmd.equals("Close") || cmd.equals("Reboot"))
535 if (!check_dirty(cmd))
537 listener.actionPerformed(e);
538 if (cmd.equals("Close") || cmd.equals("Reboot")) {
545 /* ItemListener interface method */
546 public void itemStateChanged(ItemEvent e) {
550 /* DocumentListener interface methods */
551 public void changedUpdate(DocumentEvent e) {
555 public void insertUpdate(DocumentEvent e) {
559 public void removeUpdate(DocumentEvent e) {
563 /* Let the config code hook on a listener */
564 public void addActionListener(ActionListener l) {
568 public void units_changed(boolean imperial_units) {
569 if (tracker_horiz_value.isEnabled() && tracker_vert_value.isEnabled()) {
570 String th = tracker_horiz_value.getSelectedItem().toString();
571 String tv = tracker_vert_value.getSelectedItem().toString();
572 tracker_horiz_label.setText(get_tracker_horiz_label());
573 tracker_vert_label.setText(get_tracker_vert_label());
574 set_tracker_horiz_values();
575 set_tracker_vert_values();
577 (int) (AltosConvert.height.parse(th, !imperial_units) + 0.5),
578 (int) (AltosConvert.height.parse(tv, !imperial_units) + 0.5)
580 set_tracker_distances(t);
584 /* set and get all of the dialog values */
585 public void set_product(String product) {
586 radio_frequency_value.set_product(product);
587 product_value.setText(product);
588 set_flight_log_max_tool_tip();
591 public void set_version(String version) {
592 version_value.setText(version);
595 public void set_serial(int serial) {
596 radio_frequency_value.set_serial(serial);
597 serial_value.setText(String.format("%d", serial));
600 public void set_main_deploy(int new_main_deploy) {
603 public int main_deploy() {
607 public void set_apogee_delay(int new_apogee_delay) { }
609 public int apogee_delay() {
613 public void set_apogee_lockout(int new_apogee_lockout) { }
615 public int apogee_lockout() { return -1; }
617 public void set_radio_frequency(double new_radio_frequency) {
618 radio_frequency_value.set_frequency(new_radio_frequency);
621 public double radio_frequency() {
622 return radio_frequency_value.frequency();
625 public void set_radio_calibration(int new_radio_calibration) {
626 radio_calibration_value.setVisible(new_radio_calibration >= 0);
627 if (new_radio_calibration < 0)
628 radio_calibration_value.setText("Disabled");
630 radio_calibration_value.setText(String.format("%d", new_radio_calibration));
633 private int parse_int(String name, String s, boolean split) throws AltosConfigDataException {
636 v = s.split("\\s+")[0];
638 return Integer.parseInt(v);
639 } catch (NumberFormatException ne) {
640 throw new AltosConfigDataException("Invalid %s \"%s\"", name, s);
644 public int radio_calibration() throws AltosConfigDataException {
645 return parse_int("radio calibration", radio_calibration_value.getText(), false);
648 public void set_radio_enable(int new_radio_enable) {
649 if (new_radio_enable >= 0) {
650 radio_enable_value.setSelected(new_radio_enable > 0);
651 radio_enable_value.setEnabled(true);
653 radio_enable_value.setSelected(true);
654 radio_enable_value.setVisible(radio_frequency() > 0);
655 radio_enable_value.setEnabled(false);
657 set_radio_enable_tool_tip();
660 public int radio_enable() {
661 if (radio_enable_value.isEnabled())
662 return radio_enable_value.isSelected() ? 1 : 0;
667 public void set_callsign(String new_callsign) {
668 callsign_value.setVisible(new_callsign != null);
669 callsign_value.setText(new_callsign);
672 public String callsign() {
673 return callsign_value.getText();
676 int flight_log_max_limit;
679 public String flight_log_max_label(int flight_log_max) {
680 if (flight_log_max_limit != 0) {
681 int nflight = flight_log_max_limit / flight_log_max;
682 String plural = nflight > 1 ? "s" : "";
684 return String.format("%d (%d flight%s)", flight_log_max, nflight, plural);
686 return String.format("%d", flight_log_max);
689 public void set_flight_log_max(int new_flight_log_max) {
690 flight_log_max_value.setSelectedItem(flight_log_max_label(new_flight_log_max));
691 flight_log_max = new_flight_log_max;
692 set_flight_log_max_tool_tip();
695 public void set_flight_log_max_enabled(boolean enable) {
696 flight_log_max_value.setEnabled(enable);
697 set_flight_log_max_tool_tip();
700 public int flight_log_max() throws AltosConfigDataException {
701 return parse_int("flight log max", flight_log_max_value.getSelectedItem().toString(), true);
704 public void set_flight_log_max_limit(int new_flight_log_max_limit) {
705 flight_log_max_limit = new_flight_log_max_limit;
706 flight_log_max_value.removeAllItems();
707 for (int i = 8; i >= 1; i--) {
708 int size = flight_log_max_limit / i;
709 flight_log_max_value.addItem(String.format("%d (%d flights)", size, i));
711 if (flight_log_max != 0)
712 set_flight_log_max(flight_log_max);
715 public void set_ignite_mode(int new_ignite_mode) { }
716 public int ignite_mode() { return -1; }
719 public void set_pad_orientation(int new_pad_orientation) { }
720 public int pad_orientation() { return -1; }
722 public void set_beep(int new_beep) { }
724 public int beep() { return -1; }
726 String[] tracker_horiz_values() {
727 if (AltosConvert.imperial_units)
728 return tracker_horiz_values_ft;
730 return tracker_horiz_values_m;
733 void set_tracker_horiz_values() {
734 String[] v = tracker_horiz_values();
735 while (tracker_horiz_value.getItemCount() > 0)
736 tracker_horiz_value.removeItemAt(0);
737 for (int i = 0; i < v.length; i++)
738 tracker_horiz_value.addItem(v[i]);
739 tracker_horiz_value.setMaximumRowCount(v.length);
742 String get_tracker_horiz_label() {
743 return String.format("Logging Trigger Horizontal (%s):", AltosConvert.height.show_units());
746 String[] tracker_vert_values() {
747 if (AltosConvert.imperial_units)
748 return tracker_vert_values_ft;
750 return tracker_vert_values_m;
753 void set_tracker_vert_values() {
754 String[] v = tracker_vert_values();
755 while (tracker_vert_value.getItemCount() > 0)
756 tracker_vert_value.removeItemAt(0);
757 for (int i = 0; i < v.length; i++)
758 tracker_vert_value.addItem(v[i]);
759 tracker_vert_value.setMaximumRowCount(v.length);
762 void set_tracker_tool_tip() {
763 if (tracker_horiz_value.isEnabled())
764 tracker_horiz_value.setToolTipText("How far the device must move before logging is enabled");
766 tracker_horiz_value.setToolTipText("This device doesn't disable logging before motion");
767 if (tracker_vert_value.isEnabled())
768 tracker_vert_value.setToolTipText("How far the device must move before logging is enabled");
770 tracker_vert_value.setToolTipText("This device doesn't disable logging before motion");
773 String get_tracker_vert_label() {
774 return String.format("Logging Trigger Vertical (%s):", AltosConvert.height.show_units());
777 public void set_tracker_distances(int[] tracker_distances) {
778 if (tracker_distances != null) {
779 tracker_horiz_value.setSelectedItem(AltosConvert.height.say(tracker_distances[0]));
780 tracker_vert_value.setSelectedItem(AltosConvert.height.say(tracker_distances[1]));
781 tracker_horiz_value.setEnabled(true);
782 tracker_vert_value.setEnabled(true);
784 tracker_horiz_value.setEnabled(false);
785 tracker_vert_value.setEnabled(false);
789 public int[] tracker_distances() {
790 if (tracker_horiz_value.isEnabled() && tracker_vert_value.isEnabled()) {
792 (int) (AltosConvert.height.parse(tracker_horiz_value.getSelectedItem().toString()) + 0.5),
793 (int) (AltosConvert.height.parse(tracker_vert_value.getSelectedItem().toString()) + 0.5),
800 public void set_aprs_interval(int new_aprs_interval) {
803 if (new_aprs_interval <= 0)
806 s = Integer.toString(new_aprs_interval);
807 aprs_interval_value.setSelectedItem(s);
808 aprs_interval_value.setVisible(new_aprs_interval >= 0);
809 set_aprs_interval_tool_tip();
812 public int aprs_interval() throws AltosConfigDataException {
813 String s = aprs_interval_value.getSelectedItem().toString();
815 if (s.equals("Disabled"))
817 return parse_int("aprs interval", s, false);