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 AltosFreqList 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 AltosFreqList();
274 radio_frequency_value.addItemListener(this);
276 JMenuBar menu_bar = new JMenuBar();
277 menu_bar.add(radio_frequency_value);
279 pane.add(menu_bar, c);
280 radio_frequency_value.setToolTipText("Telemetry, RDF and packet frequency");
283 /* Radio Calibration */
284 c = new GridBagConstraints();
285 c.gridx = 0; c.gridy = row;
287 c.fill = GridBagConstraints.NONE;
288 c.anchor = GridBagConstraints.LINE_START;
291 radio_calibration_label = new JLabel("RF Calibration:");
292 pane.add(radio_calibration_label, c);
294 c = new GridBagConstraints();
295 c.gridx = 4; c.gridy = row;
297 c.fill = GridBagConstraints.HORIZONTAL;
299 c.anchor = GridBagConstraints.LINE_START;
302 radio_calibration_value = new JTextField(String.format("%d", 1186611));
303 radio_calibration_value.getDocument().addDocumentListener(this);
304 pane.add(radio_calibration_value, c);
305 set_radio_calibration_tool_tip();
309 c = new GridBagConstraints();
310 c.gridx = 0; c.gridy = row;
312 c.fill = GridBagConstraints.NONE;
313 c.anchor = GridBagConstraints.LINE_START;
316 radio_enable_label = new JLabel("Telemetry/RDF/APRS Enable:");
317 pane.add(radio_enable_label, c);
319 c = new GridBagConstraints();
320 c.gridx = 4; c.gridy = row;
322 c.fill = GridBagConstraints.HORIZONTAL;
324 c.anchor = GridBagConstraints.LINE_START;
327 radio_enable_value = new JRadioButton("Enabled");
328 radio_enable_value.addItemListener(this);
329 pane.add(radio_enable_value, c);
330 set_radio_enable_tool_tip();
334 c = new GridBagConstraints();
335 c.gridx = 0; c.gridy = row;
337 c.fill = GridBagConstraints.NONE;
338 c.anchor = GridBagConstraints.LINE_START;
341 aprs_interval_label = new JLabel("APRS Interval(s):");
342 pane.add(aprs_interval_label, c);
344 c = new GridBagConstraints();
345 c.gridx = 4; c.gridy = row;
347 c.fill = GridBagConstraints.HORIZONTAL;
349 c.anchor = GridBagConstraints.LINE_START;
352 aprs_interval_value = new JComboBox<String>(aprs_interval_values);
353 aprs_interval_value.setEditable(true);
354 aprs_interval_value.addItemListener(this);
355 pane.add(aprs_interval_value, c);
356 set_aprs_interval_tool_tip();
360 c = new GridBagConstraints();
361 c.gridx = 0; c.gridy = row;
363 c.fill = GridBagConstraints.NONE;
364 c.anchor = GridBagConstraints.LINE_START;
367 aprs_ssid_label = new JLabel("APRS SSID:");
368 pane.add(aprs_ssid_label, c);
370 c = new GridBagConstraints();
371 c.gridx = 4; c.gridy = row;
373 c.fill = GridBagConstraints.HORIZONTAL;
375 c.anchor = GridBagConstraints.LINE_START;
378 aprs_ssid_value = new JComboBox<Integer>(aprs_ssid_values);
379 aprs_ssid_value.setEditable(false);
380 aprs_ssid_value.addItemListener(this);
381 aprs_ssid_value.setMaximumRowCount(aprs_ssid_values.length);
382 pane.add(aprs_ssid_value, c);
383 set_aprs_ssid_tool_tip();
387 c = new GridBagConstraints();
388 c.gridx = 0; c.gridy = row;
390 c.fill = GridBagConstraints.NONE;
391 c.anchor = GridBagConstraints.LINE_START;
394 callsign_label = new JLabel("Callsign:");
395 pane.add(callsign_label, c);
397 c = new GridBagConstraints();
398 c.gridx = 4; c.gridy = row;
400 c.fill = GridBagConstraints.HORIZONTAL;
402 c.anchor = GridBagConstraints.LINE_START;
405 callsign_value = new JTextField(AltosUIPreferences.callsign());
406 callsign_value.getDocument().addDocumentListener(this);
407 pane.add(callsign_value, c);
408 callsign_value.setToolTipText("Callsign reported in telemetry data");
412 c = new GridBagConstraints();
413 c.gridx = 0; c.gridy = row;
415 c.fill = GridBagConstraints.NONE;
416 c.anchor = GridBagConstraints.LINE_START;
419 flight_log_max_label = new JLabel("Maximum Log Size (kB):");
420 pane.add(flight_log_max_label, c);
422 c = new GridBagConstraints();
423 c.gridx = 4; c.gridy = row;
425 c.fill = GridBagConstraints.HORIZONTAL;
427 c.anchor = GridBagConstraints.LINE_START;
430 flight_log_max_value = new JComboBox<String>();
431 flight_log_max_value.setEditable(true);
432 flight_log_max_value.addItemListener(this);
433 pane.add(flight_log_max_value, c);
434 set_flight_log_max_tool_tip();
437 /* Tracker triger horiz distances */
438 c = new GridBagConstraints();
439 c.gridx = 0; c.gridy = row;
441 c.fill = GridBagConstraints.NONE;
442 c.anchor = GridBagConstraints.LINE_START;
445 tracker_motion_label = new JLabel(get_tracker_motion_label());
446 pane.add(tracker_motion_label, c);
448 c = new GridBagConstraints();
449 c.gridx = 4; c.gridy = row;
451 c.fill = GridBagConstraints.HORIZONTAL;
453 c.anchor = GridBagConstraints.LINE_START;
456 tracker_motion_value = new JComboBox<String>(tracker_motion_values());
457 tracker_motion_value.setEditable(true);
458 tracker_motion_value.addItemListener(this);
459 pane.add(tracker_motion_value, c);
462 /* Tracker triger vert distances */
463 c = new GridBagConstraints();
464 c.gridx = 0; c.gridy = row;
466 c.fill = GridBagConstraints.NONE;
467 c.anchor = GridBagConstraints.LINE_START;
470 tracker_interval_label = new JLabel("Position Reporting Interval (s):");
471 pane.add(tracker_interval_label, c);
473 c = new GridBagConstraints();
474 c.gridx = 4; c.gridy = row;
476 c.fill = GridBagConstraints.HORIZONTAL;
478 c.anchor = GridBagConstraints.LINE_START;
481 tracker_interval_value = new JComboBox<String>(tracker_interval_values);
482 tracker_interval_value.setEditable(true);
483 tracker_interval_value.addItemListener(this);
484 pane.add(tracker_interval_value, c);
485 set_tracker_tool_tip();
489 c = new GridBagConstraints();
490 c.gridx = 0; c.gridy = row;
492 c.fill = GridBagConstraints.NONE;
493 c.anchor = GridBagConstraints.LINE_START;
495 save = new JButton("Save");
497 save.addActionListener(this);
498 save.setActionCommand("Save");
500 c = new GridBagConstraints();
501 c.gridx = 2; c.gridy = row;
503 c.fill = GridBagConstraints.NONE;
504 c.anchor = GridBagConstraints.CENTER;
506 reset = new JButton("Reset");
508 reset.addActionListener(this);
509 reset.setActionCommand("Reset");
511 c = new GridBagConstraints();
512 c.gridx = 4; c.gridy = row;
514 c.fill = GridBagConstraints.NONE;
515 c.anchor = GridBagConstraints.CENTER;
517 reboot = new JButton("Reboot");
519 reboot.addActionListener(this);
520 reboot.setActionCommand("Reboot");
522 c = new GridBagConstraints();
523 c.gridx = 6; c.gridy = row;
525 c.fill = GridBagConstraints.NONE;
526 c.anchor = GridBagConstraints.LINE_END;
528 close = new JButton("Close");
530 close.addActionListener(this);
531 close.setActionCommand("Close");
533 addWindowListener(new ConfigListener(this));
534 AltosPreferences.register_units_listener(this);
537 /* Once the initial values are set, the config code will show the dialog */
538 public void make_visible() {
540 setLocationRelativeTo(owner);
544 /* If any values have been changed, confirm before closing */
545 public boolean check_dirty(String operation) {
547 Object[] options = { String.format("%s anyway", operation), "Keep editing" };
549 i = JOptionPane.showOptionDialog(this,
550 String.format("Configuration modified. %s anyway?", operation),
551 "Configuration Modified",
552 JOptionPane.DEFAULT_OPTION,
553 JOptionPane.WARNING_MESSAGE,
554 null, options, options[1]);
563 save.setEnabled(true);
566 public void set_clean() {
568 save.setEnabled(false);
571 public void dispose() {
572 AltosPreferences.unregister_units_listener(this);
576 /* Listen for events from our buttons */
577 public void actionPerformed(ActionEvent e) {
578 String cmd = e.getActionCommand();
580 if (cmd.equals("Close") || cmd.equals("Reboot"))
581 if (!check_dirty(cmd))
583 listener.actionPerformed(e);
584 if (cmd.equals("Close") || cmd.equals("Reboot")) {
591 /* ItemListener interface method */
592 public void itemStateChanged(ItemEvent e) {
596 /* DocumentListener interface methods */
597 public void changedUpdate(DocumentEvent e) {
601 public void insertUpdate(DocumentEvent e) {
605 public void removeUpdate(DocumentEvent e) {
609 /* Let the config code hook on a listener */
610 public void addActionListener(ActionListener l) {
614 public void units_changed(boolean imperial_units) {
615 if (tracker_motion_value.isEnabled()) {
616 String motion = tracker_motion_value.getSelectedItem().toString();
617 tracker_motion_label.setText(get_tracker_motion_label());
618 set_tracker_motion_values();
619 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 tracker_motion_value.setSelectedItem(AltosConvert.height.say(tracker_motion));
800 public int tracker_motion() throws AltosConfigDataException {
801 return (int) AltosConvert.height.parse(tracker_motion_value.getSelectedItem().toString());
804 public void set_tracker_interval(int tracker_interval) {
805 tracker_interval_value.setSelectedItem(String.format("%d", tracker_interval));
808 public int tracker_interval() throws AltosConfigDataException {
809 return parse_int ("tracker interval", tracker_interval_value.getSelectedItem().toString(), false);
812 public void set_aprs_interval(int new_aprs_interval) {
815 if (new_aprs_interval <= 0)
818 s = Integer.toString(new_aprs_interval);
819 aprs_interval_value.setSelectedItem(s);
820 aprs_interval_value.setVisible(new_aprs_interval >= 0);
821 set_aprs_interval_tool_tip();
824 public int aprs_interval() throws AltosConfigDataException {
825 String s = aprs_interval_value.getSelectedItem().toString();
827 if (s.equals("Disabled"))
829 return parse_int("aprs interval", s, false);
832 public void set_aprs_ssid(int new_aprs_ssid) {
833 aprs_ssid_value.setSelectedItem(Math.max(0,new_aprs_ssid));
834 aprs_ssid_value.setVisible(new_aprs_ssid >= 0);
835 set_aprs_ssid_tool_tip();
838 public int aprs_ssid() throws AltosConfigDataException {
839 Integer i = (Integer) aprs_ssid_value.getSelectedItem();