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_motion_label;
44 JLabel tracker_interval_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_motion_value;
59 JComboBox<String> tracker_interval_value;
66 ActionListener listener;
68 static String[] aprs_interval_values = {
75 static String[] tracker_motion_values_m = {
82 static String[] tracker_motion_values_ft = {
89 static String[] tracker_interval_values = {
96 /* A window listener to catch closing events and tell the config code */
97 class ConfigListener extends WindowAdapter {
100 public ConfigListener(TeleGPSConfigUI this_ui) {
104 public void windowClosing(WindowEvent e) {
105 ui.actionPerformed(new ActionEvent(e.getSource(),
106 ActionEvent.ACTION_PERFORMED,
111 public void set_pyros(AltosPyro[] new_pyros) {
114 public AltosPyro[] pyros() {
118 public void set_pyro_firing_time(double new_pyro_firing_time) {
121 public double pyro_firing_time() {
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 Device", 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_motion_label = new JLabel(get_tracker_motion_label());
400 pane.add(tracker_motion_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_motion_value = new JComboBox<String>(tracker_motion_values());
411 tracker_motion_value.setEditable(true);
412 tracker_motion_value.addItemListener(this);
413 pane.add(tracker_motion_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_interval_label = new JLabel("Position Reporting Interval (s):");
425 pane.add(tracker_interval_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_interval_value = new JComboBox<String>(tracker_interval_values);
436 tracker_interval_value.setEditable(true);
437 tracker_interval_value.addItemListener(this);
438 pane.add(tracker_interval_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_motion_value.isEnabled()) {
570 String motion = tracker_motion_value.getSelectedItem().toString();
571 tracker_motion_label.setText(get_tracker_motion_label());
572 set_tracker_motion_values();
573 set_tracker_motion((int) (AltosConvert.height.parse(motion, !imperial_units) + 0.5));
577 /* set and get all of the dialog values */
578 public void set_product(String product) {
579 radio_frequency_value.set_product(product);
580 product_value.setText(product);
581 set_flight_log_max_tool_tip();
584 public void set_version(String version) {
585 version_value.setText(version);
588 public void set_serial(int serial) {
589 radio_frequency_value.set_serial(serial);
590 serial_value.setText(String.format("%d", serial));
593 public void set_main_deploy(int new_main_deploy) {
596 public int main_deploy() {
600 public void set_apogee_delay(int new_apogee_delay) { }
602 public int apogee_delay() {
606 public void set_apogee_lockout(int new_apogee_lockout) { }
608 public int apogee_lockout() { return -1; }
610 public void set_radio_frequency(double new_radio_frequency) {
611 radio_frequency_value.set_frequency(new_radio_frequency);
614 public double radio_frequency() {
615 return radio_frequency_value.frequency();
618 public void set_radio_calibration(int new_radio_calibration) {
619 radio_calibration_value.setVisible(new_radio_calibration >= 0);
620 if (new_radio_calibration < 0)
621 radio_calibration_value.setText("Disabled");
623 radio_calibration_value.setText(String.format("%d", new_radio_calibration));
626 private int parse_int(String name, String s, boolean split) throws AltosConfigDataException {
629 v = s.split("\\s+")[0];
631 return Integer.parseInt(v);
632 } catch (NumberFormatException ne) {
633 throw new AltosConfigDataException("Invalid %s \"%s\"", name, s);
637 public int radio_calibration() throws AltosConfigDataException {
638 return parse_int("radio calibration", radio_calibration_value.getText(), false);
641 public void set_radio_enable(int new_radio_enable) {
642 if (new_radio_enable >= 0) {
643 radio_enable_value.setSelected(new_radio_enable > 0);
644 radio_enable_value.setEnabled(true);
646 radio_enable_value.setSelected(true);
647 radio_enable_value.setVisible(radio_frequency() > 0);
648 radio_enable_value.setEnabled(false);
650 set_radio_enable_tool_tip();
653 public int radio_enable() {
654 if (radio_enable_value.isEnabled())
655 return radio_enable_value.isSelected() ? 1 : 0;
660 public void set_callsign(String new_callsign) {
661 callsign_value.setVisible(new_callsign != null);
662 callsign_value.setText(new_callsign);
665 public String callsign() {
666 return callsign_value.getText();
669 int flight_log_max_limit;
672 public String flight_log_max_label(int flight_log_max) {
673 if (flight_log_max_limit != 0) {
674 int nflight = flight_log_max_limit / flight_log_max;
675 String plural = nflight > 1 ? "s" : "";
677 return String.format("%d (%d flight%s)", flight_log_max, nflight, plural);
679 return String.format("%d", flight_log_max);
682 public void set_flight_log_max(int new_flight_log_max) {
683 flight_log_max_value.setSelectedItem(flight_log_max_label(new_flight_log_max));
684 flight_log_max = new_flight_log_max;
685 set_flight_log_max_tool_tip();
688 public void set_flight_log_max_enabled(boolean enable) {
689 flight_log_max_value.setEnabled(enable);
690 set_flight_log_max_tool_tip();
693 public int flight_log_max() throws AltosConfigDataException {
694 return parse_int("flight log max", flight_log_max_value.getSelectedItem().toString(), true);
697 public void set_flight_log_max_limit(int new_flight_log_max_limit) {
698 flight_log_max_limit = new_flight_log_max_limit;
699 flight_log_max_value.removeAllItems();
700 for (int i = 8; i >= 1; i--) {
701 int size = flight_log_max_limit / i;
702 flight_log_max_value.addItem(String.format("%d (%d flights)", size, i));
704 if (flight_log_max != 0)
705 set_flight_log_max(flight_log_max);
708 public void set_ignite_mode(int new_ignite_mode) { }
709 public int ignite_mode() { return -1; }
712 public void set_pad_orientation(int new_pad_orientation) { }
713 public int pad_orientation() { return -1; }
715 public void set_beep(int new_beep) { }
717 public int beep() { return -1; }
719 String[] tracker_motion_values() {
720 if (AltosConvert.imperial_units)
721 return tracker_motion_values_ft;
723 return tracker_motion_values_m;
726 void set_tracker_motion_values() {
727 String[] v = tracker_motion_values();
728 while (tracker_motion_value.getItemCount() > 0)
729 tracker_motion_value.removeItemAt(0);
730 for (int i = 0; i < v.length; i++)
731 tracker_motion_value.addItem(v[i]);
732 tracker_motion_value.setMaximumRowCount(v.length);
735 String get_tracker_motion_label() {
736 return String.format("Logging Trigger Motion (%s):", AltosConvert.height.show_units());
739 void set_tracker_tool_tip() {
740 if (tracker_motion_value.isEnabled())
741 tracker_motion_value.setToolTipText("How far the device must move before logging");
743 tracker_motion_value.setToolTipText("This device doesn't disable logging when stationary");
744 if (tracker_interval_value.isEnabled())
745 tracker_interval_value.setToolTipText("How often to report GPS position");
747 tracker_interval_value.setToolTipText("This device can't configure interval");
750 public void set_tracker_motion(int tracker_motion) {
751 tracker_motion_value.setSelectedItem(AltosConvert.height.say(tracker_motion));
754 public int tracker_motion() throws AltosConfigDataException {
755 return (int) AltosConvert.height.parse(tracker_motion_value.getSelectedItem().toString());
758 public void set_tracker_interval(int tracker_interval) {
759 tracker_interval_value.setSelectedItem(String.format("%d", tracker_interval));
762 public int tracker_interval() throws AltosConfigDataException {
763 return parse_int ("tracker interval", tracker_interval_value.getSelectedItem().toString(), false);
766 public void set_aprs_interval(int new_aprs_interval) {
769 if (new_aprs_interval <= 0)
772 s = Integer.toString(new_aprs_interval);
773 aprs_interval_value.setSelectedItem(s);
774 aprs_interval_value.setVisible(new_aprs_interval >= 0);
775 set_aprs_interval_tool_tip();
778 public int aprs_interval() throws AltosConfigDataException {
779 String s = aprs_interval_value.getSelectedItem().toString();
781 if (s.equals("Disabled"))
783 return parse_int("aprs interval", s, false);