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_5.*;
25 import org.altusmetrum.altosuilib_3.*;
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 aprs_ssid_label;
42 JLabel flight_log_max_label;
43 JLabel callsign_label;
44 JLabel tracker_motion_label;
45 JLabel tracker_interval_label;
53 AltosUIFreqList radio_frequency_value;
54 JTextField radio_calibration_value;
55 JRadioButton radio_enable_value;
56 JComboBox<String> aprs_interval_value;
57 JComboBox<Integer> aprs_ssid_value;
58 JComboBox<String> flight_log_max_value;
59 JTextField callsign_value;
60 JComboBox<String> tracker_motion_value;
61 JComboBox<String> tracker_interval_value;
68 ActionListener listener;
70 static String[] aprs_interval_values = {
77 static Integer[] aprs_ssid_values = {
78 0, 1, 2 ,3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
81 static String[] tracker_motion_values_m = {
88 static String[] tracker_motion_values_ft = {
95 static String[] tracker_interval_values = {
102 /* A window listener to catch closing events and tell the config code */
103 class ConfigListener extends WindowAdapter {
106 public ConfigListener(TeleGPSConfigUI this_ui) {
110 public void windowClosing(WindowEvent e) {
111 ui.actionPerformed(new ActionEvent(e.getSource(),
112 ActionEvent.ACTION_PERFORMED,
117 public void set_pyros(AltosPyro[] new_pyros) {
120 public AltosPyro[] pyros() {
124 public void set_pyro_firing_time(double new_pyro_firing_time) {
127 public double pyro_firing_time() {
131 boolean is_telemetrum() {
132 String product = product_value.getText();
133 return product != null && product.startsWith("TeleGPS");
136 void set_radio_calibration_tool_tip() {
137 if (radio_calibration_value.isEnabled())
138 radio_calibration_value.setToolTipText("Tune radio output to match desired frequency");
140 radio_calibration_value.setToolTipText("Cannot tune radio while connected over packet mode");
143 void set_radio_enable_tool_tip() {
144 if (radio_enable_value.isEnabled())
145 radio_enable_value.setToolTipText("Enable/Disable telemetry and RDF transmissions");
147 radio_enable_value.setToolTipText("Firmware version does not support disabling radio");
150 void set_aprs_interval_tool_tip() {
151 if (aprs_interval_value.isEnabled())
152 aprs_interval_value.setToolTipText("Enable APRS and set the interval between APRS reports");
154 aprs_interval_value.setToolTipText("Hardware doesn't support APRS");
157 void set_aprs_ssid_tool_tip() {
158 if (aprs_ssid_value.isEnabled())
159 aprs_interval_value.setToolTipText("Set the APRS SSID (secondary station identifier)");
160 else if (aprs_interval_value.isEnabled())
161 aprs_interval_value.setToolTipText("Software version doesn't support setting the APRS SSID");
163 aprs_interval_value.setToolTipText("Hardware doesn't support APRS");
166 void set_flight_log_max_tool_tip() {
167 if (flight_log_max_value.isEnabled())
168 flight_log_max_value.setToolTipText("Size reserved for each flight log (in kB)");
170 flight_log_max_value.setToolTipText("Cannot set max value with flight logs in memory");
173 /* Build the UI using a grid bag */
174 public TeleGPSConfigUI(JFrame in_owner) {
175 super (in_owner, "Configure Device", false);
178 GridBagConstraints c;
181 Insets il = new Insets(4,4,4,4);
182 Insets ir = new Insets(4,4,4,4);
184 pane = getContentPane();
185 pane.setLayout(new GridBagLayout());
188 c = new GridBagConstraints();
189 c.gridx = 0; c.gridy = row;
191 c.fill = GridBagConstraints.NONE;
192 c.anchor = GridBagConstraints.LINE_START;
194 product_label = new JLabel("Product:");
195 pane.add(product_label, c);
197 c = new GridBagConstraints();
198 c.gridx = 4; c.gridy = row;
200 c.fill = GridBagConstraints.HORIZONTAL;
202 c.anchor = GridBagConstraints.LINE_START;
204 product_value = new JLabel("");
205 pane.add(product_value, c);
209 c = new GridBagConstraints();
210 c.gridx = 0; c.gridy = row;
212 c.fill = GridBagConstraints.NONE;
213 c.anchor = GridBagConstraints.LINE_START;
216 version_label = new JLabel("Software version:");
217 pane.add(version_label, c);
219 c = new GridBagConstraints();
220 c.gridx = 4; c.gridy = row;
222 c.fill = GridBagConstraints.HORIZONTAL;
224 c.anchor = GridBagConstraints.LINE_START;
227 version_value = new JLabel("");
228 pane.add(version_value, c);
232 c = new GridBagConstraints();
233 c.gridx = 0; c.gridy = row;
235 c.fill = GridBagConstraints.NONE;
236 c.anchor = GridBagConstraints.LINE_START;
239 serial_label = new JLabel("Serial:");
240 pane.add(serial_label, c);
242 c = new GridBagConstraints();
243 c.gridx = 4; c.gridy = row;
245 c.fill = GridBagConstraints.HORIZONTAL;
247 c.anchor = GridBagConstraints.LINE_START;
250 serial_value = new JLabel("");
251 pane.add(serial_value, c);
255 c = new GridBagConstraints();
256 c.gridx = 0; c.gridy = row;
258 c.fill = GridBagConstraints.NONE;
259 c.anchor = GridBagConstraints.LINE_START;
262 radio_frequency_label = new JLabel("Frequency:");
263 pane.add(radio_frequency_label, c);
265 c = new GridBagConstraints();
266 c.gridx = 4; c.gridy = row;
268 c.fill = GridBagConstraints.HORIZONTAL;
270 c.anchor = GridBagConstraints.LINE_START;
273 radio_frequency_value = new AltosUIFreqList();
274 radio_frequency_value.addItemListener(this);
275 pane.add(radio_frequency_value, c);
276 radio_frequency_value.setToolTipText("Telemetry, RDF and packet frequency");
279 /* Radio Calibration */
280 c = new GridBagConstraints();
281 c.gridx = 0; c.gridy = row;
283 c.fill = GridBagConstraints.NONE;
284 c.anchor = GridBagConstraints.LINE_START;
287 radio_calibration_label = new JLabel("RF Calibration:");
288 pane.add(radio_calibration_label, c);
290 c = new GridBagConstraints();
291 c.gridx = 4; c.gridy = row;
293 c.fill = GridBagConstraints.HORIZONTAL;
295 c.anchor = GridBagConstraints.LINE_START;
298 radio_calibration_value = new JTextField(String.format("%d", 1186611));
299 radio_calibration_value.getDocument().addDocumentListener(this);
300 pane.add(radio_calibration_value, c);
301 set_radio_calibration_tool_tip();
305 c = new GridBagConstraints();
306 c.gridx = 0; c.gridy = row;
308 c.fill = GridBagConstraints.NONE;
309 c.anchor = GridBagConstraints.LINE_START;
312 radio_enable_label = new JLabel("Telemetry/RDF/APRS Enable:");
313 pane.add(radio_enable_label, c);
315 c = new GridBagConstraints();
316 c.gridx = 4; c.gridy = row;
318 c.fill = GridBagConstraints.HORIZONTAL;
320 c.anchor = GridBagConstraints.LINE_START;
323 radio_enable_value = new JRadioButton("Enabled");
324 radio_enable_value.addItemListener(this);
325 pane.add(radio_enable_value, c);
326 set_radio_enable_tool_tip();
330 c = new GridBagConstraints();
331 c.gridx = 0; c.gridy = row;
333 c.fill = GridBagConstraints.NONE;
334 c.anchor = GridBagConstraints.LINE_START;
337 aprs_interval_label = new JLabel("APRS Interval(s):");
338 pane.add(aprs_interval_label, c);
340 c = new GridBagConstraints();
341 c.gridx = 4; c.gridy = row;
343 c.fill = GridBagConstraints.HORIZONTAL;
345 c.anchor = GridBagConstraints.LINE_START;
348 aprs_interval_value = new JComboBox<String>(aprs_interval_values);
349 aprs_interval_value.setEditable(true);
350 aprs_interval_value.addItemListener(this);
351 pane.add(aprs_interval_value, c);
352 set_aprs_interval_tool_tip();
356 c = new GridBagConstraints();
357 c.gridx = 0; c.gridy = row;
359 c.fill = GridBagConstraints.NONE;
360 c.anchor = GridBagConstraints.LINE_START;
363 aprs_ssid_label = new JLabel("APRS SSID:");
364 pane.add(aprs_ssid_label, c);
366 c = new GridBagConstraints();
367 c.gridx = 4; c.gridy = row;
369 c.fill = GridBagConstraints.HORIZONTAL;
371 c.anchor = GridBagConstraints.LINE_START;
374 aprs_ssid_value = new JComboBox<Integer>(aprs_ssid_values);
375 aprs_ssid_value.setEditable(false);
376 aprs_ssid_value.addItemListener(this);
377 aprs_ssid_value.setMaximumRowCount(aprs_ssid_values.length);
378 pane.add(aprs_ssid_value, c);
379 set_aprs_ssid_tool_tip();
383 c = new GridBagConstraints();
384 c.gridx = 0; c.gridy = row;
386 c.fill = GridBagConstraints.NONE;
387 c.anchor = GridBagConstraints.LINE_START;
390 callsign_label = new JLabel("Callsign:");
391 pane.add(callsign_label, c);
393 c = new GridBagConstraints();
394 c.gridx = 4; c.gridy = row;
396 c.fill = GridBagConstraints.HORIZONTAL;
398 c.anchor = GridBagConstraints.LINE_START;
401 callsign_value = new JTextField(AltosUIPreferences.callsign());
402 callsign_value.getDocument().addDocumentListener(this);
403 pane.add(callsign_value, c);
404 callsign_value.setToolTipText("Callsign reported in telemetry data");
408 c = new GridBagConstraints();
409 c.gridx = 0; c.gridy = row;
411 c.fill = GridBagConstraints.NONE;
412 c.anchor = GridBagConstraints.LINE_START;
415 flight_log_max_label = new JLabel("Maximum Log Size (kB):");
416 pane.add(flight_log_max_label, c);
418 c = new GridBagConstraints();
419 c.gridx = 4; c.gridy = row;
421 c.fill = GridBagConstraints.HORIZONTAL;
423 c.anchor = GridBagConstraints.LINE_START;
426 flight_log_max_value = new JComboBox<String>();
427 flight_log_max_value.setEditable(true);
428 flight_log_max_value.addItemListener(this);
429 pane.add(flight_log_max_value, c);
430 set_flight_log_max_tool_tip();
433 /* Tracker triger horiz distances */
434 c = new GridBagConstraints();
435 c.gridx = 0; c.gridy = row;
437 c.fill = GridBagConstraints.NONE;
438 c.anchor = GridBagConstraints.LINE_START;
441 tracker_motion_label = new JLabel(get_tracker_motion_label());
442 pane.add(tracker_motion_label, c);
444 c = new GridBagConstraints();
445 c.gridx = 4; c.gridy = row;
447 c.fill = GridBagConstraints.HORIZONTAL;
449 c.anchor = GridBagConstraints.LINE_START;
452 tracker_motion_value = new JComboBox<String>(tracker_motion_values());
453 tracker_motion_value.setEditable(true);
454 tracker_motion_value.addItemListener(this);
455 pane.add(tracker_motion_value, c);
458 /* Tracker triger vert distances */
459 c = new GridBagConstraints();
460 c.gridx = 0; c.gridy = row;
462 c.fill = GridBagConstraints.NONE;
463 c.anchor = GridBagConstraints.LINE_START;
466 tracker_interval_label = new JLabel("Position Reporting Interval (s):");
467 pane.add(tracker_interval_label, c);
469 c = new GridBagConstraints();
470 c.gridx = 4; c.gridy = row;
472 c.fill = GridBagConstraints.HORIZONTAL;
474 c.anchor = GridBagConstraints.LINE_START;
477 tracker_interval_value = new JComboBox<String>(tracker_interval_values);
478 tracker_interval_value.setEditable(true);
479 tracker_interval_value.addItemListener(this);
480 pane.add(tracker_interval_value, c);
481 set_tracker_tool_tip();
485 c = new GridBagConstraints();
486 c.gridx = 0; c.gridy = row;
488 c.fill = GridBagConstraints.NONE;
489 c.anchor = GridBagConstraints.LINE_START;
491 save = new JButton("Save");
493 save.addActionListener(this);
494 save.setActionCommand("Save");
496 c = new GridBagConstraints();
497 c.gridx = 2; c.gridy = row;
499 c.fill = GridBagConstraints.NONE;
500 c.anchor = GridBagConstraints.CENTER;
502 reset = new JButton("Reset");
504 reset.addActionListener(this);
505 reset.setActionCommand("Reset");
507 c = new GridBagConstraints();
508 c.gridx = 4; c.gridy = row;
510 c.fill = GridBagConstraints.NONE;
511 c.anchor = GridBagConstraints.CENTER;
513 reboot = new JButton("Reboot");
515 reboot.addActionListener(this);
516 reboot.setActionCommand("Reboot");
518 c = new GridBagConstraints();
519 c.gridx = 6; c.gridy = row;
521 c.fill = GridBagConstraints.NONE;
522 c.anchor = GridBagConstraints.LINE_END;
524 close = new JButton("Close");
526 close.addActionListener(this);
527 close.setActionCommand("Close");
529 addWindowListener(new ConfigListener(this));
530 AltosPreferences.register_units_listener(this);
533 /* Once the initial values are set, the config code will show the dialog */
534 public void make_visible() {
536 setLocationRelativeTo(owner);
540 /* If any values have been changed, confirm before closing */
541 public boolean check_dirty(String operation) {
543 Object[] options = { String.format("%s anyway", operation), "Keep editing" };
545 i = JOptionPane.showOptionDialog(this,
546 String.format("Configuration modified. %s anyway?", operation),
547 "Configuration Modified",
548 JOptionPane.DEFAULT_OPTION,
549 JOptionPane.WARNING_MESSAGE,
550 null, options, options[1]);
559 save.setEnabled(true);
562 public void set_clean() {
564 save.setEnabled(false);
567 public void dispose() {
568 AltosPreferences.unregister_units_listener(this);
572 /* Listen for events from our buttons */
573 public void actionPerformed(ActionEvent e) {
574 String cmd = e.getActionCommand();
576 if (cmd.equals("Close") || cmd.equals("Reboot"))
577 if (!check_dirty(cmd))
579 listener.actionPerformed(e);
580 if (cmd.equals("Close") || cmd.equals("Reboot")) {
587 /* ItemListener interface method */
588 public void itemStateChanged(ItemEvent e) {
592 /* DocumentListener interface methods */
593 public void changedUpdate(DocumentEvent e) {
597 public void insertUpdate(DocumentEvent e) {
601 public void removeUpdate(DocumentEvent e) {
605 /* Let the config code hook on a listener */
606 public void addActionListener(ActionListener l) {
610 public void units_changed(boolean imperial_units) {
611 boolean was_dirty = dirty;
613 if (tracker_motion_value.isEnabled()) {
614 String motion = tracker_motion_value.getSelectedItem().toString();
615 tracker_motion_label.setText(get_tracker_motion_label());
616 set_tracker_motion_values();
617 set_tracker_motion((int) (AltosConvert.height.parse(motion, !imperial_units) + 0.5));
623 /* set and get all of the dialog values */
624 public void set_product(String product) {
625 radio_frequency_value.set_product(product);
626 product_value.setText(product);
627 set_flight_log_max_tool_tip();
630 public void set_version(String version) {
631 version_value.setText(version);
634 public void set_serial(int serial) {
635 radio_frequency_value.set_serial(serial);
636 serial_value.setText(String.format("%d", serial));
639 public void set_main_deploy(int new_main_deploy) {
642 public int main_deploy() {
646 public void set_apogee_delay(int new_apogee_delay) { }
648 public int apogee_delay() {
652 public void set_apogee_lockout(int new_apogee_lockout) { }
654 public int apogee_lockout() { return -1; }
656 public void set_radio_frequency(double new_radio_frequency) {
657 radio_frequency_value.set_frequency(new_radio_frequency);
660 public double radio_frequency() {
661 return radio_frequency_value.frequency();
664 public void set_radio_calibration(int new_radio_calibration) {
665 radio_calibration_value.setVisible(new_radio_calibration >= 0);
666 if (new_radio_calibration < 0)
667 radio_calibration_value.setText("Disabled");
669 radio_calibration_value.setText(String.format("%d", new_radio_calibration));
672 private int parse_int(String name, String s, boolean split) throws AltosConfigDataException {
675 v = s.split("\\s+")[0];
677 return Integer.parseInt(v);
678 } catch (NumberFormatException ne) {
679 throw new AltosConfigDataException("Invalid %s \"%s\"", name, s);
683 public int radio_calibration() throws AltosConfigDataException {
684 return parse_int("radio calibration", radio_calibration_value.getText(), false);
687 public void set_radio_enable(int new_radio_enable) {
688 if (new_radio_enable >= 0) {
689 radio_enable_value.setSelected(new_radio_enable > 0);
690 radio_enable_value.setEnabled(true);
692 radio_enable_value.setSelected(true);
693 radio_enable_value.setVisible(radio_frequency() > 0);
694 radio_enable_value.setEnabled(false);
696 set_radio_enable_tool_tip();
699 public int radio_enable() {
700 if (radio_enable_value.isEnabled())
701 return radio_enable_value.isSelected() ? 1 : 0;
706 public void set_callsign(String new_callsign) {
707 callsign_value.setVisible(new_callsign != null);
708 callsign_value.setText(new_callsign);
711 public String callsign() {
712 return callsign_value.getText();
715 int flight_log_max_limit;
718 public String flight_log_max_label(int flight_log_max) {
719 if (flight_log_max_limit != 0) {
720 int nflight = flight_log_max_limit / flight_log_max;
721 String plural = nflight > 1 ? "s" : "";
723 return String.format("%d (%d flight%s)", flight_log_max, nflight, plural);
725 return String.format("%d", flight_log_max);
728 public void set_flight_log_max(int new_flight_log_max) {
729 flight_log_max_value.setSelectedItem(flight_log_max_label(new_flight_log_max));
730 flight_log_max = new_flight_log_max;
731 set_flight_log_max_tool_tip();
734 public void set_flight_log_max_enabled(boolean enable) {
735 flight_log_max_value.setEnabled(enable);
736 set_flight_log_max_tool_tip();
739 public int flight_log_max() throws AltosConfigDataException {
740 return parse_int("flight log max", flight_log_max_value.getSelectedItem().toString(), true);
743 public void set_flight_log_max_limit(int new_flight_log_max_limit) {
744 flight_log_max_limit = new_flight_log_max_limit;
745 flight_log_max_value.removeAllItems();
746 for (int i = 8; i >= 1; i--) {
747 int size = flight_log_max_limit / i;
748 flight_log_max_value.addItem(String.format("%d (%d flights)", size, i));
750 if (flight_log_max != 0)
751 set_flight_log_max(flight_log_max);
754 public void set_ignite_mode(int new_ignite_mode) { }
755 public int ignite_mode() { return -1; }
758 public void set_pad_orientation(int new_pad_orientation) { }
759 public int pad_orientation() { return -1; }
761 public void set_beep(int new_beep) { }
763 public int beep() { return -1; }
765 String[] tracker_motion_values() {
766 if (AltosConvert.imperial_units)
767 return tracker_motion_values_ft;
769 return tracker_motion_values_m;
772 void set_tracker_motion_values() {
773 String[] v = tracker_motion_values();
774 while (tracker_motion_value.getItemCount() > 0)
775 tracker_motion_value.removeItemAt(0);
776 for (int i = 0; i < v.length; i++)
777 tracker_motion_value.addItem(v[i]);
778 tracker_motion_value.setMaximumRowCount(v.length);
781 String get_tracker_motion_label() {
782 return String.format("Logging Trigger Motion (%s):", AltosConvert.height.show_units());
785 void set_tracker_tool_tip() {
786 if (tracker_motion_value.isEnabled())
787 tracker_motion_value.setToolTipText("How far the device must move before logging");
789 tracker_motion_value.setToolTipText("This device doesn't disable logging when stationary");
790 if (tracker_interval_value.isEnabled())
791 tracker_interval_value.setToolTipText("How often to report GPS position");
793 tracker_interval_value.setToolTipText("This device can't configure interval");
796 public void set_tracker_motion(int tracker_motion) {
797 if (tracker_motion < 0) {
798 tracker_motion_value.setEnabled(false);
800 tracker_motion_value.setEnabled(true);
801 tracker_motion_value.setSelectedItem(AltosConvert.height.say(tracker_motion));
805 public int tracker_motion() throws AltosConfigDataException {
806 return (int) AltosConvert.height.parse(tracker_motion_value.getSelectedItem().toString());
809 public void set_tracker_interval(int tracker_interval) {
810 if (tracker_interval< 0) {
811 tracker_interval_value.setEnabled(false);
813 tracker_interval_value.setEnabled(true);
814 tracker_interval_value.setSelectedItem(String.format("%d", tracker_interval));
818 public int tracker_interval() throws AltosConfigDataException {
819 return parse_int ("tracker interval", tracker_interval_value.getSelectedItem().toString(), false);
822 public void set_aprs_interval(int new_aprs_interval) {
825 if (new_aprs_interval <= 0)
828 s = Integer.toString(new_aprs_interval);
829 aprs_interval_value.setSelectedItem(s);
830 aprs_interval_value.setVisible(new_aprs_interval >= 0);
831 set_aprs_interval_tool_tip();
834 public int aprs_interval() throws AltosConfigDataException {
835 String s = aprs_interval_value.getSelectedItem().toString();
837 if (s.equals("Disabled"))
839 return parse_int("aprs interval", s, false);
842 public void set_aprs_ssid(int new_aprs_ssid) {
843 aprs_ssid_value.setSelectedItem(Math.max(0,new_aprs_ssid));
844 aprs_ssid_value.setVisible(new_aprs_ssid >= 0);
845 set_aprs_ssid_tool_tip();
848 public int aprs_ssid() throws AltosConfigDataException {
849 Integer i = (Integer) aprs_ssid_value.getSelectedItem();