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 boolean is_telemetrum() {
119 String product = product_value.getText();
120 return product != null && product.startsWith("TeleGPS");
123 void set_radio_calibration_tool_tip() {
124 if (radio_calibration_value.isEnabled())
125 radio_calibration_value.setToolTipText("Tune radio output to match desired frequency");
127 radio_calibration_value.setToolTipText("Cannot tune radio while connected over packet mode");
130 void set_radio_enable_tool_tip() {
131 if (radio_enable_value.isEnabled())
132 radio_enable_value.setToolTipText("Enable/Disable telemetry and RDF transmissions");
134 radio_enable_value.setToolTipText("Firmware version does not support disabling radio");
137 void set_aprs_interval_tool_tip() {
138 if (aprs_interval_value.isEnabled())
139 aprs_interval_value.setToolTipText("Enable APRS and set the interval between APRS reports");
141 aprs_interval_value.setToolTipText("Hardware doesn't support APRS");
144 void set_flight_log_max_tool_tip() {
145 if (flight_log_max_value.isEnabled())
146 flight_log_max_value.setToolTipText("Size reserved for each flight log (in kB)");
148 flight_log_max_value.setToolTipText("Cannot set max value with flight logs in memory");
151 /* Build the UI using a grid bag */
152 public TeleGPSConfigUI(JFrame in_owner) {
153 super (in_owner, "Configure Flight Computer", false);
156 GridBagConstraints c;
159 Insets il = new Insets(4,4,4,4);
160 Insets ir = new Insets(4,4,4,4);
162 pane = getContentPane();
163 pane.setLayout(new GridBagLayout());
166 c = new GridBagConstraints();
167 c.gridx = 0; c.gridy = row;
169 c.fill = GridBagConstraints.NONE;
170 c.anchor = GridBagConstraints.LINE_START;
172 product_label = new JLabel("Product:");
173 pane.add(product_label, c);
175 c = new GridBagConstraints();
176 c.gridx = 4; c.gridy = row;
178 c.fill = GridBagConstraints.HORIZONTAL;
180 c.anchor = GridBagConstraints.LINE_START;
182 product_value = new JLabel("");
183 pane.add(product_value, c);
187 c = new GridBagConstraints();
188 c.gridx = 0; c.gridy = row;
190 c.fill = GridBagConstraints.NONE;
191 c.anchor = GridBagConstraints.LINE_START;
194 version_label = new JLabel("Software version:");
195 pane.add(version_label, c);
197 c = new GridBagConstraints();
198 c.gridx = 4; c.gridy = row;
200 c.fill = GridBagConstraints.HORIZONTAL;
202 c.anchor = GridBagConstraints.LINE_START;
205 version_value = new JLabel("");
206 pane.add(version_value, c);
210 c = new GridBagConstraints();
211 c.gridx = 0; c.gridy = row;
213 c.fill = GridBagConstraints.NONE;
214 c.anchor = GridBagConstraints.LINE_START;
217 serial_label = new JLabel("Serial:");
218 pane.add(serial_label, c);
220 c = new GridBagConstraints();
221 c.gridx = 4; c.gridy = row;
223 c.fill = GridBagConstraints.HORIZONTAL;
225 c.anchor = GridBagConstraints.LINE_START;
228 serial_value = new JLabel("");
229 pane.add(serial_value, c);
233 c = new GridBagConstraints();
234 c.gridx = 0; c.gridy = row;
236 c.fill = GridBagConstraints.NONE;
237 c.anchor = GridBagConstraints.LINE_START;
240 radio_frequency_label = new JLabel("Frequency:");
241 pane.add(radio_frequency_label, c);
243 c = new GridBagConstraints();
244 c.gridx = 4; c.gridy = row;
246 c.fill = GridBagConstraints.HORIZONTAL;
248 c.anchor = GridBagConstraints.LINE_START;
251 radio_frequency_value = new AltosFreqList();
252 radio_frequency_value.addItemListener(this);
253 pane.add(radio_frequency_value, c);
254 radio_frequency_value.setToolTipText("Telemetry, RDF and packet frequency");
257 /* Radio Calibration */
258 c = new GridBagConstraints();
259 c.gridx = 0; c.gridy = row;
261 c.fill = GridBagConstraints.NONE;
262 c.anchor = GridBagConstraints.LINE_START;
265 radio_calibration_label = new JLabel("RF Calibration:");
266 pane.add(radio_calibration_label, c);
268 c = new GridBagConstraints();
269 c.gridx = 4; c.gridy = row;
271 c.fill = GridBagConstraints.HORIZONTAL;
273 c.anchor = GridBagConstraints.LINE_START;
276 radio_calibration_value = new JTextField(String.format("%d", 1186611));
277 radio_calibration_value.getDocument().addDocumentListener(this);
278 pane.add(radio_calibration_value, c);
279 set_radio_calibration_tool_tip();
283 c = new GridBagConstraints();
284 c.gridx = 0; c.gridy = row;
286 c.fill = GridBagConstraints.NONE;
287 c.anchor = GridBagConstraints.LINE_START;
290 radio_enable_label = new JLabel("Telemetry/RDF/APRS Enable:");
291 pane.add(radio_enable_label, c);
293 c = new GridBagConstraints();
294 c.gridx = 4; c.gridy = row;
296 c.fill = GridBagConstraints.HORIZONTAL;
298 c.anchor = GridBagConstraints.LINE_START;
301 radio_enable_value = new JRadioButton("Enabled");
302 radio_enable_value.addItemListener(this);
303 pane.add(radio_enable_value, c);
304 set_radio_enable_tool_tip();
308 c = new GridBagConstraints();
309 c.gridx = 0; c.gridy = row;
311 c.fill = GridBagConstraints.NONE;
312 c.anchor = GridBagConstraints.LINE_START;
315 aprs_interval_label = new JLabel("APRS Interval(s):");
316 pane.add(aprs_interval_label, c);
318 c = new GridBagConstraints();
319 c.gridx = 4; c.gridy = row;
321 c.fill = GridBagConstraints.HORIZONTAL;
323 c.anchor = GridBagConstraints.LINE_START;
326 aprs_interval_value = new JComboBox<String>(aprs_interval_values);
327 aprs_interval_value.setEditable(true);
328 aprs_interval_value.addItemListener(this);
329 pane.add(aprs_interval_value, c);
330 set_aprs_interval_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 callsign_label = new JLabel("Callsign:");
342 pane.add(callsign_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 callsign_value = new JTextField(AltosUIPreferences.callsign());
353 callsign_value.getDocument().addDocumentListener(this);
354 pane.add(callsign_value, c);
355 callsign_value.setToolTipText("Callsign reported in telemetry data");
359 c = new GridBagConstraints();
360 c.gridx = 0; c.gridy = row;
362 c.fill = GridBagConstraints.NONE;
363 c.anchor = GridBagConstraints.LINE_START;
366 flight_log_max_label = new JLabel("Maximum Log Size (kB):");
367 pane.add(flight_log_max_label, c);
369 c = new GridBagConstraints();
370 c.gridx = 4; c.gridy = row;
372 c.fill = GridBagConstraints.HORIZONTAL;
374 c.anchor = GridBagConstraints.LINE_START;
377 flight_log_max_value = new JComboBox<String>();
378 flight_log_max_value.setEditable(true);
379 flight_log_max_value.addItemListener(this);
380 pane.add(flight_log_max_value, c);
381 set_flight_log_max_tool_tip();
384 /* Tracker triger horiz distances */
385 c = new GridBagConstraints();
386 c.gridx = 0; c.gridy = row;
388 c.fill = GridBagConstraints.NONE;
389 c.anchor = GridBagConstraints.LINE_START;
392 tracker_motion_label = new JLabel(get_tracker_motion_label());
393 pane.add(tracker_motion_label, c);
395 c = new GridBagConstraints();
396 c.gridx = 4; c.gridy = row;
398 c.fill = GridBagConstraints.HORIZONTAL;
400 c.anchor = GridBagConstraints.LINE_START;
403 tracker_motion_value = new JComboBox<String>(tracker_motion_values());
404 tracker_motion_value.setEditable(true);
405 tracker_motion_value.addItemListener(this);
406 pane.add(tracker_motion_value, c);
409 /* Tracker triger vert distances */
410 c = new GridBagConstraints();
411 c.gridx = 0; c.gridy = row;
413 c.fill = GridBagConstraints.NONE;
414 c.anchor = GridBagConstraints.LINE_START;
417 tracker_interval_label = new JLabel("Position Reporting Interval (s):");
418 pane.add(tracker_interval_label, c);
420 c = new GridBagConstraints();
421 c.gridx = 4; c.gridy = row;
423 c.fill = GridBagConstraints.HORIZONTAL;
425 c.anchor = GridBagConstraints.LINE_START;
428 tracker_interval_value = new JComboBox<String>(tracker_interval_values);
429 tracker_interval_value.setEditable(true);
430 tracker_interval_value.addItemListener(this);
431 pane.add(tracker_interval_value, c);
432 set_tracker_tool_tip();
436 c = new GridBagConstraints();
437 c.gridx = 0; c.gridy = row;
439 c.fill = GridBagConstraints.NONE;
440 c.anchor = GridBagConstraints.LINE_START;
442 save = new JButton("Save");
444 save.addActionListener(this);
445 save.setActionCommand("Save");
447 c = new GridBagConstraints();
448 c.gridx = 2; c.gridy = row;
450 c.fill = GridBagConstraints.NONE;
451 c.anchor = GridBagConstraints.CENTER;
453 reset = new JButton("Reset");
455 reset.addActionListener(this);
456 reset.setActionCommand("Reset");
458 c = new GridBagConstraints();
459 c.gridx = 4; c.gridy = row;
461 c.fill = GridBagConstraints.NONE;
462 c.anchor = GridBagConstraints.CENTER;
464 reboot = new JButton("Reboot");
466 reboot.addActionListener(this);
467 reboot.setActionCommand("Reboot");
469 c = new GridBagConstraints();
470 c.gridx = 6; c.gridy = row;
472 c.fill = GridBagConstraints.NONE;
473 c.anchor = GridBagConstraints.LINE_END;
475 close = new JButton("Close");
477 close.addActionListener(this);
478 close.setActionCommand("Close");
480 addWindowListener(new ConfigListener(this));
481 AltosPreferences.register_units_listener(this);
484 /* Once the initial values are set, the config code will show the dialog */
485 public void make_visible() {
487 setLocationRelativeTo(owner);
491 /* If any values have been changed, confirm before closing */
492 public boolean check_dirty(String operation) {
494 Object[] options = { String.format("%s anyway", operation), "Keep editing" };
496 i = JOptionPane.showOptionDialog(this,
497 String.format("Configuration modified. %s anyway?", operation),
498 "Configuration Modified",
499 JOptionPane.DEFAULT_OPTION,
500 JOptionPane.WARNING_MESSAGE,
501 null, options, options[1]);
510 save.setEnabled(true);
513 public void set_clean() {
515 save.setEnabled(false);
518 public void dispose() {
519 AltosPreferences.unregister_units_listener(this);
523 /* Listen for events from our buttons */
524 public void actionPerformed(ActionEvent e) {
525 String cmd = e.getActionCommand();
527 if (cmd.equals("Close") || cmd.equals("Reboot"))
528 if (!check_dirty(cmd))
530 listener.actionPerformed(e);
531 if (cmd.equals("Close") || cmd.equals("Reboot")) {
538 /* ItemListener interface method */
539 public void itemStateChanged(ItemEvent e) {
543 /* DocumentListener interface methods */
544 public void changedUpdate(DocumentEvent e) {
548 public void insertUpdate(DocumentEvent e) {
552 public void removeUpdate(DocumentEvent e) {
556 /* Let the config code hook on a listener */
557 public void addActionListener(ActionListener l) {
561 public void units_changed(boolean imperial_units) {
562 if (tracker_motion_value.isEnabled()) {
563 String motion = tracker_motion_value.getSelectedItem().toString();
564 tracker_motion_label.setText(get_tracker_motion_label());
565 set_tracker_motion_values();
566 set_tracker_motion((int) (AltosConvert.height.parse(motion, !imperial_units) + 0.5));
570 /* set and get all of the dialog values */
571 public void set_product(String product) {
572 radio_frequency_value.set_product(product);
573 product_value.setText(product);
574 set_flight_log_max_tool_tip();
577 public void set_version(String version) {
578 version_value.setText(version);
581 public void set_serial(int serial) {
582 radio_frequency_value.set_serial(serial);
583 serial_value.setText(String.format("%d", serial));
586 public void set_main_deploy(int new_main_deploy) {
589 public int main_deploy() {
593 public void set_apogee_delay(int new_apogee_delay) { }
595 public int apogee_delay() {
599 public void set_apogee_lockout(int new_apogee_lockout) { }
601 public int apogee_lockout() { return -1; }
603 public void set_radio_frequency(double new_radio_frequency) {
604 radio_frequency_value.set_frequency(new_radio_frequency);
607 public double radio_frequency() {
608 return radio_frequency_value.frequency();
611 public void set_radio_calibration(int new_radio_calibration) {
612 radio_calibration_value.setVisible(new_radio_calibration >= 0);
613 if (new_radio_calibration < 0)
614 radio_calibration_value.setText("Disabled");
616 radio_calibration_value.setText(String.format("%d", new_radio_calibration));
619 private int parse_int(String name, String s, boolean split) throws AltosConfigDataException {
622 v = s.split("\\s+")[0];
624 return Integer.parseInt(v);
625 } catch (NumberFormatException ne) {
626 throw new AltosConfigDataException("Invalid %s \"%s\"", name, s);
630 public int radio_calibration() throws AltosConfigDataException {
631 return parse_int("radio calibration", radio_calibration_value.getText(), false);
634 public void set_radio_enable(int new_radio_enable) {
635 if (new_radio_enable >= 0) {
636 radio_enable_value.setSelected(new_radio_enable > 0);
637 radio_enable_value.setEnabled(true);
639 radio_enable_value.setSelected(true);
640 radio_enable_value.setVisible(radio_frequency() > 0);
641 radio_enable_value.setEnabled(false);
643 set_radio_enable_tool_tip();
646 public int radio_enable() {
647 if (radio_enable_value.isEnabled())
648 return radio_enable_value.isSelected() ? 1 : 0;
653 public void set_callsign(String new_callsign) {
654 callsign_value.setVisible(new_callsign != null);
655 callsign_value.setText(new_callsign);
658 public String callsign() {
659 return callsign_value.getText();
662 int flight_log_max_limit;
665 public String flight_log_max_label(int flight_log_max) {
666 if (flight_log_max_limit != 0) {
667 int nflight = flight_log_max_limit / flight_log_max;
668 String plural = nflight > 1 ? "s" : "";
670 return String.format("%d (%d flight%s)", flight_log_max, nflight, plural);
672 return String.format("%d", flight_log_max);
675 public void set_flight_log_max(int new_flight_log_max) {
676 flight_log_max_value.setSelectedItem(flight_log_max_label(new_flight_log_max));
677 flight_log_max = new_flight_log_max;
678 set_flight_log_max_tool_tip();
681 public void set_flight_log_max_enabled(boolean enable) {
682 flight_log_max_value.setEnabled(enable);
683 set_flight_log_max_tool_tip();
686 public int flight_log_max() throws AltosConfigDataException {
687 return parse_int("flight log max", flight_log_max_value.getSelectedItem().toString(), true);
690 public void set_flight_log_max_limit(int new_flight_log_max_limit) {
691 flight_log_max_limit = new_flight_log_max_limit;
692 flight_log_max_value.removeAllItems();
693 for (int i = 8; i >= 1; i--) {
694 int size = flight_log_max_limit / i;
695 flight_log_max_value.addItem(String.format("%d (%d flights)", size, i));
697 if (flight_log_max != 0)
698 set_flight_log_max(flight_log_max);
701 public void set_ignite_mode(int new_ignite_mode) { }
702 public int ignite_mode() { return -1; }
705 public void set_pad_orientation(int new_pad_orientation) { }
706 public int pad_orientation() { return -1; }
708 public void set_beep(int new_beep) { }
710 public int beep() { return -1; }
712 String[] tracker_motion_values() {
713 if (AltosConvert.imperial_units)
714 return tracker_motion_values_ft;
716 return tracker_motion_values_m;
719 void set_tracker_motion_values() {
720 String[] v = tracker_motion_values();
721 while (tracker_motion_value.getItemCount() > 0)
722 tracker_motion_value.removeItemAt(0);
723 for (int i = 0; i < v.length; i++)
724 tracker_motion_value.addItem(v[i]);
725 tracker_motion_value.setMaximumRowCount(v.length);
728 String get_tracker_motion_label() {
729 return String.format("Logging Trigger Motion (%s):", AltosConvert.height.show_units());
732 void set_tracker_tool_tip() {
733 if (tracker_motion_value.isEnabled())
734 tracker_motion_value.setToolTipText("How far the device must move before logging");
736 tracker_motion_value.setToolTipText("This device doesn't disable logging when stationary");
737 if (tracker_interval_value.isEnabled())
738 tracker_interval_value.setToolTipText("How often to report GPS position");
740 tracker_interval_value.setToolTipText("This device can't configure interval");
743 public void set_tracker_motion(int tracker_motion) {
744 tracker_motion_value.setSelectedItem(AltosConvert.height.say(tracker_motion));
747 public int tracker_motion() throws AltosConfigDataException {
748 return (int) AltosConvert.height.parse(tracker_motion_value.getSelectedItem().toString());
751 public void set_tracker_interval(int tracker_interval) {
752 tracker_interval_value.setSelectedItem(String.format("%d", tracker_interval));
755 public int tracker_interval() throws AltosConfigDataException {
756 return parse_int ("tracker interval", tracker_interval_value.getSelectedItem().toString(), false);
759 public void set_aprs_interval(int new_aprs_interval) {
762 if (new_aprs_interval <= 0)
765 s = Integer.toString(new_aprs_interval);
766 aprs_interval_value.setSelectedItem(s);
767 aprs_interval_value.setVisible(new_aprs_interval >= 0);
768 set_aprs_interval_tool_tip();
771 public int aprs_interval() throws AltosConfigDataException {
772 String s = aprs_interval_value.getSelectedItem().toString();
774 if (s.equals("Disabled"))
776 return parse_int("aprs interval", s, false);