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_6.*;
25 import org.altusmetrum.altosuilib_6.*;
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;
41 JLabel aprs_interval_label;
42 JLabel aprs_ssid_label;
43 JLabel flight_log_max_label;
44 JLabel callsign_label;
45 JLabel tracker_motion_label;
46 JLabel tracker_interval_label;
54 AltosUIFreqList radio_frequency_value;
55 JTextField radio_calibration_value;
56 JRadioButton radio_enable_value;
57 AltosUIRateList rate_value;
58 JComboBox<String> aprs_interval_value;
59 JComboBox<Integer> aprs_ssid_value;
60 JComboBox<String> flight_log_max_value;
61 JTextField callsign_value;
62 JComboBox<String> tracker_motion_value;
63 JComboBox<String> tracker_interval_value;
70 ActionListener listener;
72 static String[] aprs_interval_values = {
79 static Integer[] aprs_ssid_values = {
80 0, 1, 2 ,3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
83 static String[] tracker_motion_values_m = {
90 static String[] tracker_motion_values_ft = {
97 static String[] tracker_interval_values = {
104 /* A window listener to catch closing events and tell the config code */
105 class ConfigListener extends WindowAdapter {
108 public ConfigListener(TeleGPSConfigUI this_ui) {
112 public void windowClosing(WindowEvent e) {
113 ui.actionPerformed(new ActionEvent(e.getSource(),
114 ActionEvent.ACTION_PERFORMED,
119 public void set_pyros(AltosPyro[] new_pyros) {
122 public AltosPyro[] pyros() {
126 public void set_pyro_firing_time(double new_pyro_firing_time) {
129 public double pyro_firing_time() {
133 boolean is_telemetrum() {
134 String product = product_value.getText();
135 return product != null && product.startsWith("TeleGPS");
138 void set_radio_calibration_tool_tip() {
139 if (radio_calibration_value.isEnabled())
140 radio_calibration_value.setToolTipText("Tune radio output to match desired frequency");
142 radio_calibration_value.setToolTipText("Cannot tune radio while connected over packet mode");
145 void set_radio_enable_tool_tip() {
146 if (radio_enable_value.isEnabled())
147 radio_enable_value.setToolTipText("Enable/Disable telemetry and RDF transmissions");
149 radio_enable_value.setToolTipText("Firmware version does not support disabling radio");
152 void set_rate_tool_tip() {
153 if (rate_value.isEnabled())
154 rate_value.setToolTipText("Select telemetry baud rate");
156 rate_value.setToolTipText("Firmware version does not support variable telemetry rates");
159 void set_aprs_interval_tool_tip() {
160 if (aprs_interval_value.isEnabled())
161 aprs_interval_value.setToolTipText("Enable APRS and set the interval between APRS reports");
163 aprs_interval_value.setToolTipText("Hardware doesn't support APRS");
166 void set_aprs_ssid_tool_tip() {
167 if (aprs_ssid_value.isEnabled())
168 aprs_interval_value.setToolTipText("Set the APRS SSID (secondary station identifier)");
169 else if (aprs_interval_value.isEnabled())
170 aprs_interval_value.setToolTipText("Software version doesn't support setting the APRS SSID");
172 aprs_interval_value.setToolTipText("Hardware doesn't support APRS");
175 void set_flight_log_max_tool_tip() {
176 if (flight_log_max_value.isEnabled())
177 flight_log_max_value.setToolTipText("Size reserved for each flight log (in kB)");
179 flight_log_max_value.setToolTipText("Cannot set max value with flight logs in memory");
182 /* Build the UI using a grid bag */
183 public TeleGPSConfigUI(JFrame in_owner) {
184 super (in_owner, "Configure Device", false);
187 GridBagConstraints c;
190 Insets il = new Insets(4,4,4,4);
191 Insets ir = new Insets(4,4,4,4);
193 pane = getContentPane();
194 pane.setLayout(new GridBagLayout());
197 c = new GridBagConstraints();
198 c.gridx = 0; c.gridy = row;
200 c.fill = GridBagConstraints.NONE;
201 c.anchor = GridBagConstraints.LINE_START;
203 product_label = new JLabel("Product:");
204 pane.add(product_label, c);
206 c = new GridBagConstraints();
207 c.gridx = 4; c.gridy = row;
209 c.fill = GridBagConstraints.HORIZONTAL;
211 c.anchor = GridBagConstraints.LINE_START;
213 product_value = new JLabel("");
214 pane.add(product_value, c);
218 c = new GridBagConstraints();
219 c.gridx = 0; c.gridy = row;
221 c.fill = GridBagConstraints.NONE;
222 c.anchor = GridBagConstraints.LINE_START;
225 version_label = new JLabel("Software version:");
226 pane.add(version_label, c);
228 c = new GridBagConstraints();
229 c.gridx = 4; c.gridy = row;
231 c.fill = GridBagConstraints.HORIZONTAL;
233 c.anchor = GridBagConstraints.LINE_START;
236 version_value = new JLabel("");
237 pane.add(version_value, c);
241 c = new GridBagConstraints();
242 c.gridx = 0; c.gridy = row;
244 c.fill = GridBagConstraints.NONE;
245 c.anchor = GridBagConstraints.LINE_START;
248 serial_label = new JLabel("Serial:");
249 pane.add(serial_label, c);
251 c = new GridBagConstraints();
252 c.gridx = 4; c.gridy = row;
254 c.fill = GridBagConstraints.HORIZONTAL;
256 c.anchor = GridBagConstraints.LINE_START;
259 serial_value = new JLabel("");
260 pane.add(serial_value, c);
264 c = new GridBagConstraints();
265 c.gridx = 0; c.gridy = row;
267 c.fill = GridBagConstraints.NONE;
268 c.anchor = GridBagConstraints.LINE_START;
271 radio_frequency_label = new JLabel("Frequency:");
272 pane.add(radio_frequency_label, c);
274 c = new GridBagConstraints();
275 c.gridx = 4; c.gridy = row;
277 c.fill = GridBagConstraints.HORIZONTAL;
279 c.anchor = GridBagConstraints.LINE_START;
282 radio_frequency_value = new AltosUIFreqList();
283 radio_frequency_value.addItemListener(this);
284 pane.add(radio_frequency_value, c);
285 radio_frequency_value.setToolTipText("Telemetry, RDF and packet frequency");
288 /* Radio Calibration */
289 c = new GridBagConstraints();
290 c.gridx = 0; c.gridy = row;
292 c.fill = GridBagConstraints.NONE;
293 c.anchor = GridBagConstraints.LINE_START;
296 radio_calibration_label = new JLabel("RF Calibration:");
297 pane.add(radio_calibration_label, c);
299 c = new GridBagConstraints();
300 c.gridx = 4; c.gridy = row;
302 c.fill = GridBagConstraints.HORIZONTAL;
304 c.anchor = GridBagConstraints.LINE_START;
307 radio_calibration_value = new JTextField(String.format("%d", 1186611));
308 radio_calibration_value.getDocument().addDocumentListener(this);
309 pane.add(radio_calibration_value, c);
310 set_radio_calibration_tool_tip();
314 c = new GridBagConstraints();
315 c.gridx = 0; c.gridy = row;
317 c.fill = GridBagConstraints.NONE;
318 c.anchor = GridBagConstraints.LINE_START;
321 radio_enable_label = new JLabel("Telemetry/RDF/APRS Enable:");
322 pane.add(radio_enable_label, c);
324 c = new GridBagConstraints();
325 c.gridx = 4; c.gridy = row;
327 c.fill = GridBagConstraints.HORIZONTAL;
329 c.anchor = GridBagConstraints.LINE_START;
332 radio_enable_value = new JRadioButton("Enabled");
333 radio_enable_value.addItemListener(this);
334 pane.add(radio_enable_value, c);
335 set_radio_enable_tool_tip();
339 c = new GridBagConstraints();
340 c.gridx = 0; c.gridy = row;
342 c.fill = GridBagConstraints.NONE;
343 c.anchor = GridBagConstraints.LINE_START;
346 rate_label = new JLabel("Telemetry baud rate:");
347 pane.add(rate_label, c);
349 c = new GridBagConstraints();
350 c.gridx = 4; c.gridy = row;
352 c.fill = GridBagConstraints.HORIZONTAL;
354 c.anchor = GridBagConstraints.LINE_START;
357 rate_value = new AltosUIRateList();
358 rate_value.addItemListener(this);
359 pane.add(rate_value, c);
364 c = new GridBagConstraints();
365 c.gridx = 0; c.gridy = row;
367 c.fill = GridBagConstraints.NONE;
368 c.anchor = GridBagConstraints.LINE_START;
371 aprs_interval_label = new JLabel("APRS Interval(s):");
372 pane.add(aprs_interval_label, c);
374 c = new GridBagConstraints();
375 c.gridx = 4; c.gridy = row;
377 c.fill = GridBagConstraints.HORIZONTAL;
379 c.anchor = GridBagConstraints.LINE_START;
382 aprs_interval_value = new JComboBox<String>(aprs_interval_values);
383 aprs_interval_value.setEditable(true);
384 aprs_interval_value.addItemListener(this);
385 pane.add(aprs_interval_value, c);
386 set_aprs_interval_tool_tip();
390 c = new GridBagConstraints();
391 c.gridx = 0; c.gridy = row;
393 c.fill = GridBagConstraints.NONE;
394 c.anchor = GridBagConstraints.LINE_START;
397 aprs_ssid_label = new JLabel("APRS SSID:");
398 pane.add(aprs_ssid_label, c);
400 c = new GridBagConstraints();
401 c.gridx = 4; c.gridy = row;
403 c.fill = GridBagConstraints.HORIZONTAL;
405 c.anchor = GridBagConstraints.LINE_START;
408 aprs_ssid_value = new JComboBox<Integer>(aprs_ssid_values);
409 aprs_ssid_value.setEditable(false);
410 aprs_ssid_value.addItemListener(this);
411 aprs_ssid_value.setMaximumRowCount(aprs_ssid_values.length);
412 pane.add(aprs_ssid_value, c);
413 set_aprs_ssid_tool_tip();
417 c = new GridBagConstraints();
418 c.gridx = 0; c.gridy = row;
420 c.fill = GridBagConstraints.NONE;
421 c.anchor = GridBagConstraints.LINE_START;
424 callsign_label = new JLabel("Callsign:");
425 pane.add(callsign_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 callsign_value = new JTextField(AltosUIPreferences.callsign());
436 callsign_value.getDocument().addDocumentListener(this);
437 pane.add(callsign_value, c);
438 callsign_value.setToolTipText("Callsign reported in telemetry data");
442 c = new GridBagConstraints();
443 c.gridx = 0; c.gridy = row;
445 c.fill = GridBagConstraints.NONE;
446 c.anchor = GridBagConstraints.LINE_START;
449 flight_log_max_label = new JLabel("Maximum Log Size (kB):");
450 pane.add(flight_log_max_label, c);
452 c = new GridBagConstraints();
453 c.gridx = 4; c.gridy = row;
455 c.fill = GridBagConstraints.HORIZONTAL;
457 c.anchor = GridBagConstraints.LINE_START;
460 flight_log_max_value = new JComboBox<String>();
461 flight_log_max_value.setEditable(true);
462 flight_log_max_value.addItemListener(this);
463 pane.add(flight_log_max_value, c);
464 set_flight_log_max_tool_tip();
467 /* Tracker triger horiz distances */
468 c = new GridBagConstraints();
469 c.gridx = 0; c.gridy = row;
471 c.fill = GridBagConstraints.NONE;
472 c.anchor = GridBagConstraints.LINE_START;
475 tracker_motion_label = new JLabel(get_tracker_motion_label());
476 pane.add(tracker_motion_label, c);
478 c = new GridBagConstraints();
479 c.gridx = 4; c.gridy = row;
481 c.fill = GridBagConstraints.HORIZONTAL;
483 c.anchor = GridBagConstraints.LINE_START;
486 tracker_motion_value = new JComboBox<String>(tracker_motion_values());
487 tracker_motion_value.setEditable(true);
488 tracker_motion_value.addItemListener(this);
489 pane.add(tracker_motion_value, c);
492 /* Tracker triger vert distances */
493 c = new GridBagConstraints();
494 c.gridx = 0; c.gridy = row;
496 c.fill = GridBagConstraints.NONE;
497 c.anchor = GridBagConstraints.LINE_START;
500 tracker_interval_label = new JLabel("Position Reporting Interval (s):");
501 pane.add(tracker_interval_label, c);
503 c = new GridBagConstraints();
504 c.gridx = 4; c.gridy = row;
506 c.fill = GridBagConstraints.HORIZONTAL;
508 c.anchor = GridBagConstraints.LINE_START;
511 tracker_interval_value = new JComboBox<String>(tracker_interval_values);
512 tracker_interval_value.setEditable(true);
513 tracker_interval_value.addItemListener(this);
514 pane.add(tracker_interval_value, c);
515 set_tracker_tool_tip();
519 c = new GridBagConstraints();
520 c.gridx = 0; c.gridy = row;
522 c.fill = GridBagConstraints.NONE;
523 c.anchor = GridBagConstraints.LINE_START;
525 save = new JButton("Save");
527 save.addActionListener(this);
528 save.setActionCommand("Save");
530 c = new GridBagConstraints();
531 c.gridx = 2; c.gridy = row;
533 c.fill = GridBagConstraints.NONE;
534 c.anchor = GridBagConstraints.CENTER;
536 reset = new JButton("Reset");
538 reset.addActionListener(this);
539 reset.setActionCommand("Reset");
541 c = new GridBagConstraints();
542 c.gridx = 4; c.gridy = row;
544 c.fill = GridBagConstraints.NONE;
545 c.anchor = GridBagConstraints.CENTER;
547 reboot = new JButton("Reboot");
549 reboot.addActionListener(this);
550 reboot.setActionCommand("Reboot");
552 c = new GridBagConstraints();
553 c.gridx = 6; c.gridy = row;
555 c.fill = GridBagConstraints.NONE;
556 c.anchor = GridBagConstraints.LINE_END;
558 close = new JButton("Close");
560 close.addActionListener(this);
561 close.setActionCommand("Close");
563 addWindowListener(new ConfigListener(this));
564 AltosPreferences.register_units_listener(this);
567 /* Once the initial values are set, the config code will show the dialog */
568 public void make_visible() {
570 setLocationRelativeTo(owner);
574 /* If any values have been changed, confirm before closing */
575 public boolean check_dirty(String operation) {
577 Object[] options = { String.format("%s anyway", operation), "Keep editing" };
579 i = JOptionPane.showOptionDialog(this,
580 String.format("Configuration modified. %s anyway?", operation),
581 "Configuration Modified",
582 JOptionPane.DEFAULT_OPTION,
583 JOptionPane.WARNING_MESSAGE,
584 null, options, options[1]);
593 save.setEnabled(true);
596 public void set_clean() {
598 save.setEnabled(false);
601 public void dispose() {
602 AltosPreferences.unregister_units_listener(this);
606 /* Listen for events from our buttons */
607 public void actionPerformed(ActionEvent e) {
608 String cmd = e.getActionCommand();
610 if (cmd.equals("Close") || cmd.equals("Reboot"))
611 if (!check_dirty(cmd))
613 listener.actionPerformed(e);
614 if (cmd.equals("Close") || cmd.equals("Reboot")) {
621 /* ItemListener interface method */
622 public void itemStateChanged(ItemEvent e) {
626 /* DocumentListener interface methods */
627 public void changedUpdate(DocumentEvent e) {
631 public void insertUpdate(DocumentEvent e) {
635 public void removeUpdate(DocumentEvent e) {
639 /* Let the config code hook on a listener */
640 public void addActionListener(ActionListener l) {
644 public void units_changed(boolean imperial_units) {
645 boolean was_dirty = dirty;
647 if (tracker_motion_value.isEnabled()) {
648 String motion = tracker_motion_value.getSelectedItem().toString();
649 tracker_motion_label.setText(get_tracker_motion_label());
650 set_tracker_motion_values();
651 set_tracker_motion((int) (AltosConvert.height.parse(motion, !imperial_units) + 0.5));
657 /* set and get all of the dialog values */
658 public void set_product(String product) {
659 radio_frequency_value.set_product(product);
660 product_value.setText(product);
661 set_flight_log_max_tool_tip();
664 public void set_version(String version) {
665 version_value.setText(version);
668 public void set_serial(int serial) {
669 radio_frequency_value.set_serial(serial);
670 serial_value.setText(String.format("%d", serial));
673 public void set_altitude_32(int altitude_32) {
676 public void set_main_deploy(int new_main_deploy) {
679 public int main_deploy() {
683 public void set_apogee_delay(int new_apogee_delay) { }
685 public int apogee_delay() {
689 public void set_apogee_lockout(int new_apogee_lockout) { }
691 public int apogee_lockout() { return -1; }
693 public void set_radio_frequency(double new_radio_frequency) {
694 radio_frequency_value.set_frequency(new_radio_frequency);
697 public double radio_frequency() {
698 return radio_frequency_value.frequency();
701 public void set_radio_calibration(int new_radio_calibration) {
702 radio_calibration_value.setVisible(new_radio_calibration >= 0);
703 if (new_radio_calibration < 0)
704 radio_calibration_value.setText("Disabled");
706 radio_calibration_value.setText(String.format("%d", new_radio_calibration));
709 private int parse_int(String name, String s, boolean split) throws AltosConfigDataException {
712 v = s.split("\\s+")[0];
714 return Integer.parseInt(v);
715 } catch (NumberFormatException ne) {
716 throw new AltosConfigDataException("Invalid %s \"%s\"", name, s);
720 public int radio_calibration() throws AltosConfigDataException {
721 return parse_int("radio calibration", radio_calibration_value.getText(), false);
724 public void set_radio_enable(int new_radio_enable) {
725 if (new_radio_enable >= 0) {
726 radio_enable_value.setSelected(new_radio_enable > 0);
727 radio_enable_value.setEnabled(true);
729 radio_enable_value.setSelected(true);
730 radio_enable_value.setVisible(radio_frequency() > 0);
731 radio_enable_value.setEnabled(false);
733 set_radio_enable_tool_tip();
736 public int radio_enable() {
737 if (radio_enable_value.isEnabled())
738 return radio_enable_value.isSelected() ? 1 : 0;
743 public void set_telemetry_rate(int new_rate) {
744 rate_value.set_rate(new_rate);
747 public int telemetry_rate() {
748 return rate_value.rate();
751 public void set_callsign(String new_callsign) {
752 callsign_value.setVisible(new_callsign != null);
753 callsign_value.setText(new_callsign);
756 public String callsign() {
757 return callsign_value.getText();
760 int flight_log_max_limit;
763 public String flight_log_max_label(int flight_log_max) {
764 if (flight_log_max_limit != 0) {
765 int nflight = flight_log_max_limit / flight_log_max;
766 String plural = nflight > 1 ? "s" : "";
768 return String.format("%d (%d flight%s)", flight_log_max, nflight, plural);
770 return String.format("%d", flight_log_max);
773 public void set_flight_log_max(int new_flight_log_max) {
774 flight_log_max_value.setSelectedItem(flight_log_max_label(new_flight_log_max));
775 flight_log_max = new_flight_log_max;
776 set_flight_log_max_tool_tip();
779 public void set_flight_log_max_enabled(boolean enable) {
780 flight_log_max_value.setEnabled(enable);
781 set_flight_log_max_tool_tip();
784 public int flight_log_max() throws AltosConfigDataException {
785 return parse_int("flight log max", flight_log_max_value.getSelectedItem().toString(), true);
788 public void set_flight_log_max_limit(int new_flight_log_max_limit) {
789 flight_log_max_limit = new_flight_log_max_limit;
790 flight_log_max_value.removeAllItems();
791 for (int i = 8; i >= 1; i--) {
792 int size = flight_log_max_limit / i;
793 flight_log_max_value.addItem(String.format("%d (%d flights)", size, i));
795 if (flight_log_max != 0)
796 set_flight_log_max(flight_log_max);
799 public void set_ignite_mode(int new_ignite_mode) { }
800 public int ignite_mode() { return -1; }
803 public void set_pad_orientation(int new_pad_orientation) { }
804 public int pad_orientation() { return -1; }
806 public void set_beep(int new_beep) { }
808 public int beep() { return -1; }
810 String[] tracker_motion_values() {
811 if (AltosConvert.imperial_units)
812 return tracker_motion_values_ft;
814 return tracker_motion_values_m;
817 void set_tracker_motion_values() {
818 String[] v = tracker_motion_values();
819 while (tracker_motion_value.getItemCount() > 0)
820 tracker_motion_value.removeItemAt(0);
821 for (int i = 0; i < v.length; i++)
822 tracker_motion_value.addItem(v[i]);
823 tracker_motion_value.setMaximumRowCount(v.length);
826 String get_tracker_motion_label() {
827 return String.format("Logging Trigger Motion (%s):", AltosConvert.height.show_units());
830 void set_tracker_tool_tip() {
831 if (tracker_motion_value.isEnabled())
832 tracker_motion_value.setToolTipText("How far the device must move before logging");
834 tracker_motion_value.setToolTipText("This device doesn't disable logging when stationary");
835 if (tracker_interval_value.isEnabled())
836 tracker_interval_value.setToolTipText("How often to report GPS position");
838 tracker_interval_value.setToolTipText("This device can't configure interval");
841 public void set_tracker_motion(int tracker_motion) {
842 if (tracker_motion < 0) {
843 tracker_motion_value.setEnabled(false);
845 tracker_motion_value.setEnabled(true);
846 tracker_motion_value.setSelectedItem(AltosConvert.height.say(tracker_motion));
850 public int tracker_motion() throws AltosConfigDataException {
851 return (int) AltosConvert.height.parse(tracker_motion_value.getSelectedItem().toString());
854 public void set_tracker_interval(int tracker_interval) {
855 if (tracker_interval< 0) {
856 tracker_interval_value.setEnabled(false);
858 tracker_interval_value.setEnabled(true);
859 tracker_interval_value.setSelectedItem(String.format("%d", tracker_interval));
863 public int tracker_interval() throws AltosConfigDataException {
864 return parse_int ("tracker interval", tracker_interval_value.getSelectedItem().toString(), false);
867 public void set_aprs_interval(int new_aprs_interval) {
870 if (new_aprs_interval <= 0)
873 s = Integer.toString(new_aprs_interval);
874 aprs_interval_value.setSelectedItem(s);
875 aprs_interval_value.setVisible(new_aprs_interval >= 0);
876 set_aprs_interval_tool_tip();
879 public int aprs_interval() throws AltosConfigDataException {
880 String s = aprs_interval_value.getSelectedItem().toString();
882 if (s.equals("Disabled"))
884 return parse_int("aprs interval", s, false);
887 public void set_aprs_ssid(int new_aprs_ssid) {
888 aprs_ssid_value.setSelectedItem(Math.max(0,new_aprs_ssid));
889 aprs_ssid_value.setVisible(new_aprs_ssid >= 0);
890 set_aprs_ssid_tool_tip();
893 public int aprs_ssid() throws AltosConfigDataException {
894 Integer i = (Integer) aprs_ssid_value.getSelectedItem();