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_horiz_label;
44 JLabel tracker_vert_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_horiz_value;
59 JComboBox<String> tracker_vert_value;
66 ActionListener listener;
68 static String[] flight_log_max_values = {
69 "64", "128", "192", "256", "320",
70 "384", "448", "512", "576", "640",
71 "704", "768", "832", "896", "960",
74 static String[] aprs_interval_values = {
81 static String[] tracker_horiz_values_m = {
88 static String[] tracker_horiz_values_ft = {
95 static String[] tracker_vert_values_m = {
102 static String[] tracker_vert_values_ft = {
109 /* A window listener to catch closing events and tell the config code */
110 class ConfigListener extends WindowAdapter {
113 public ConfigListener(TeleGPSConfigUI this_ui) {
117 public void windowClosing(WindowEvent e) {
118 ui.actionPerformed(new ActionEvent(e.getSource(),
119 ActionEvent.ACTION_PERFORMED,
124 public void set_pyros(AltosPyro[] new_pyros) {
127 public AltosPyro[] pyros() {
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_flight_log_max_tool_tip() {
158 if (flight_log_max_value.isEnabled())
159 flight_log_max_value.setToolTipText("Size reserved for each flight log (in kB)");
161 flight_log_max_value.setToolTipText("Cannot set max value with flight logs in memory");
164 /* Build the UI using a grid bag */
165 public TeleGPSConfigUI(JFrame in_owner) {
166 super (in_owner, "Configure Flight Computer", false);
169 GridBagConstraints c;
172 Insets il = new Insets(4,4,4,4);
173 Insets ir = new Insets(4,4,4,4);
175 pane = getContentPane();
176 pane.setLayout(new GridBagLayout());
179 c = new GridBagConstraints();
180 c.gridx = 0; c.gridy = row;
182 c.fill = GridBagConstraints.NONE;
183 c.anchor = GridBagConstraints.LINE_START;
185 product_label = new JLabel("Product:");
186 pane.add(product_label, c);
188 c = new GridBagConstraints();
189 c.gridx = 4; c.gridy = row;
191 c.fill = GridBagConstraints.HORIZONTAL;
193 c.anchor = GridBagConstraints.LINE_START;
195 product_value = new JLabel("");
196 pane.add(product_value, c);
200 c = new GridBagConstraints();
201 c.gridx = 0; c.gridy = row;
203 c.fill = GridBagConstraints.NONE;
204 c.anchor = GridBagConstraints.LINE_START;
207 version_label = new JLabel("Software version:");
208 pane.add(version_label, c);
210 c = new GridBagConstraints();
211 c.gridx = 4; c.gridy = row;
213 c.fill = GridBagConstraints.HORIZONTAL;
215 c.anchor = GridBagConstraints.LINE_START;
218 version_value = new JLabel("");
219 pane.add(version_value, c);
223 c = new GridBagConstraints();
224 c.gridx = 0; c.gridy = row;
226 c.fill = GridBagConstraints.NONE;
227 c.anchor = GridBagConstraints.LINE_START;
230 serial_label = new JLabel("Serial:");
231 pane.add(serial_label, c);
233 c = new GridBagConstraints();
234 c.gridx = 4; c.gridy = row;
236 c.fill = GridBagConstraints.HORIZONTAL;
238 c.anchor = GridBagConstraints.LINE_START;
241 serial_value = new JLabel("");
242 pane.add(serial_value, c);
246 c = new GridBagConstraints();
247 c.gridx = 0; c.gridy = row;
249 c.fill = GridBagConstraints.NONE;
250 c.anchor = GridBagConstraints.LINE_START;
253 radio_frequency_label = new JLabel("Frequency:");
254 pane.add(radio_frequency_label, c);
256 c = new GridBagConstraints();
257 c.gridx = 4; c.gridy = row;
259 c.fill = GridBagConstraints.HORIZONTAL;
261 c.anchor = GridBagConstraints.LINE_START;
264 radio_frequency_value = new AltosFreqList();
265 radio_frequency_value.addItemListener(this);
266 pane.add(radio_frequency_value, c);
267 radio_frequency_value.setToolTipText("Telemetry, RDF and packet frequency");
270 /* Radio Calibration */
271 c = new GridBagConstraints();
272 c.gridx = 0; c.gridy = row;
274 c.fill = GridBagConstraints.NONE;
275 c.anchor = GridBagConstraints.LINE_START;
278 radio_calibration_label = new JLabel("RF Calibration:");
279 pane.add(radio_calibration_label, c);
281 c = new GridBagConstraints();
282 c.gridx = 4; c.gridy = row;
284 c.fill = GridBagConstraints.HORIZONTAL;
286 c.anchor = GridBagConstraints.LINE_START;
289 radio_calibration_value = new JTextField(String.format("%d", 1186611));
290 radio_calibration_value.getDocument().addDocumentListener(this);
291 pane.add(radio_calibration_value, c);
292 set_radio_calibration_tool_tip();
296 c = new GridBagConstraints();
297 c.gridx = 0; c.gridy = row;
299 c.fill = GridBagConstraints.NONE;
300 c.anchor = GridBagConstraints.LINE_START;
303 radio_enable_label = new JLabel("Telemetry/RDF/APRS Enable:");
304 pane.add(radio_enable_label, c);
306 c = new GridBagConstraints();
307 c.gridx = 4; c.gridy = row;
309 c.fill = GridBagConstraints.HORIZONTAL;
311 c.anchor = GridBagConstraints.LINE_START;
314 radio_enable_value = new JRadioButton("Enabled");
315 radio_enable_value.addItemListener(this);
316 pane.add(radio_enable_value, c);
317 set_radio_enable_tool_tip();
321 c = new GridBagConstraints();
322 c.gridx = 0; c.gridy = row;
324 c.fill = GridBagConstraints.NONE;
325 c.anchor = GridBagConstraints.LINE_START;
328 aprs_interval_label = new JLabel("APRS Interval(s):");
329 pane.add(aprs_interval_label, c);
331 c = new GridBagConstraints();
332 c.gridx = 4; c.gridy = row;
334 c.fill = GridBagConstraints.HORIZONTAL;
336 c.anchor = GridBagConstraints.LINE_START;
339 aprs_interval_value = new JComboBox<String>(aprs_interval_values);
340 aprs_interval_value.setEditable(true);
341 aprs_interval_value.addItemListener(this);
342 pane.add(aprs_interval_value, c);
343 set_aprs_interval_tool_tip();
347 c = new GridBagConstraints();
348 c.gridx = 0; c.gridy = row;
350 c.fill = GridBagConstraints.NONE;
351 c.anchor = GridBagConstraints.LINE_START;
354 callsign_label = new JLabel("Callsign:");
355 pane.add(callsign_label, c);
357 c = new GridBagConstraints();
358 c.gridx = 4; c.gridy = row;
360 c.fill = GridBagConstraints.HORIZONTAL;
362 c.anchor = GridBagConstraints.LINE_START;
365 callsign_value = new JTextField(AltosUIPreferences.callsign());
366 callsign_value.getDocument().addDocumentListener(this);
367 pane.add(callsign_value, c);
368 callsign_value.setToolTipText("Callsign reported in telemetry data");
372 c = new GridBagConstraints();
373 c.gridx = 0; c.gridy = row;
375 c.fill = GridBagConstraints.NONE;
376 c.anchor = GridBagConstraints.LINE_START;
379 flight_log_max_label = new JLabel("Maximum Flight Log Size:");
380 pane.add(flight_log_max_label, c);
382 c = new GridBagConstraints();
383 c.gridx = 4; c.gridy = row;
385 c.fill = GridBagConstraints.HORIZONTAL;
387 c.anchor = GridBagConstraints.LINE_START;
390 flight_log_max_value = new JComboBox<String>(flight_log_max_values);
391 flight_log_max_value.setEditable(true);
392 flight_log_max_value.addItemListener(this);
393 pane.add(flight_log_max_value, c);
394 set_flight_log_max_tool_tip();
397 /* Tracker triger horiz distances */
398 c = new GridBagConstraints();
399 c.gridx = 0; c.gridy = row;
401 c.fill = GridBagConstraints.NONE;
402 c.anchor = GridBagConstraints.LINE_START;
405 tracker_horiz_label = new JLabel(get_tracker_horiz_label());
406 pane.add(tracker_horiz_label, c);
408 c = new GridBagConstraints();
409 c.gridx = 4; c.gridy = row;
411 c.fill = GridBagConstraints.HORIZONTAL;
413 c.anchor = GridBagConstraints.LINE_START;
416 tracker_horiz_value = new JComboBox<String>(tracker_horiz_values());
417 tracker_horiz_value.setEditable(true);
418 tracker_horiz_value.addItemListener(this);
419 pane.add(tracker_horiz_value, c);
422 /* Tracker triger vert distances */
423 c = new GridBagConstraints();
424 c.gridx = 0; c.gridy = row;
426 c.fill = GridBagConstraints.NONE;
427 c.anchor = GridBagConstraints.LINE_START;
430 tracker_vert_label = new JLabel(get_tracker_vert_label());
431 pane.add(tracker_vert_label, c);
433 c = new GridBagConstraints();
434 c.gridx = 4; c.gridy = row;
436 c.fill = GridBagConstraints.HORIZONTAL;
438 c.anchor = GridBagConstraints.LINE_START;
441 tracker_vert_value = new JComboBox<String>(tracker_vert_values());
442 tracker_vert_value.setEditable(true);
443 tracker_vert_value.addItemListener(this);
444 pane.add(tracker_vert_value, c);
445 set_tracker_tool_tip();
449 c = new GridBagConstraints();
450 c.gridx = 0; c.gridy = row;
452 c.fill = GridBagConstraints.NONE;
453 c.anchor = GridBagConstraints.LINE_START;
455 save = new JButton("Save");
457 save.addActionListener(this);
458 save.setActionCommand("Save");
460 c = new GridBagConstraints();
461 c.gridx = 2; c.gridy = row;
463 c.fill = GridBagConstraints.NONE;
464 c.anchor = GridBagConstraints.CENTER;
466 reset = new JButton("Reset");
468 reset.addActionListener(this);
469 reset.setActionCommand("Reset");
471 c = new GridBagConstraints();
472 c.gridx = 4; c.gridy = row;
474 c.fill = GridBagConstraints.NONE;
475 c.anchor = GridBagConstraints.CENTER;
477 reboot = new JButton("Reboot");
479 reboot.addActionListener(this);
480 reboot.setActionCommand("Reboot");
482 c = new GridBagConstraints();
483 c.gridx = 6; c.gridy = row;
485 c.fill = GridBagConstraints.NONE;
486 c.anchor = GridBagConstraints.LINE_END;
488 close = new JButton("Close");
490 close.addActionListener(this);
491 close.setActionCommand("Close");
493 addWindowListener(new ConfigListener(this));
494 AltosPreferences.register_units_listener(this);
497 /* Once the initial values are set, the config code will show the dialog */
498 public void make_visible() {
500 setLocationRelativeTo(owner);
504 /* If any values have been changed, confirm before closing */
505 public boolean check_dirty(String operation) {
507 Object[] options = { String.format("%s anyway", operation), "Keep editing" };
509 i = JOptionPane.showOptionDialog(this,
510 String.format("Configuration modified. %s anyway?", operation),
511 "Configuration Modified",
512 JOptionPane.DEFAULT_OPTION,
513 JOptionPane.WARNING_MESSAGE,
514 null, options, options[1]);
523 save.setEnabled(true);
526 public void set_clean() {
528 save.setEnabled(false);
531 public void dispose() {
532 AltosPreferences.unregister_units_listener(this);
536 /* Listen for events from our buttons */
537 public void actionPerformed(ActionEvent e) {
538 String cmd = e.getActionCommand();
540 if (cmd.equals("Close") || cmd.equals("Reboot"))
541 if (!check_dirty(cmd))
543 listener.actionPerformed(e);
544 if (cmd.equals("Close") || cmd.equals("Reboot")) {
551 /* ItemListener interface method */
552 public void itemStateChanged(ItemEvent e) {
556 /* DocumentListener interface methods */
557 public void changedUpdate(DocumentEvent e) {
561 public void insertUpdate(DocumentEvent e) {
565 public void removeUpdate(DocumentEvent e) {
569 /* Let the config code hook on a listener */
570 public void addActionListener(ActionListener l) {
574 public void units_changed(boolean imperial_units) {
575 if (tracker_horiz_value.isEnabled() && tracker_vert_value.isEnabled()) {
576 String th = tracker_horiz_value.getSelectedItem().toString();
577 String tv = tracker_vert_value.getSelectedItem().toString();
578 tracker_horiz_label.setText(get_tracker_horiz_label());
579 tracker_vert_label.setText(get_tracker_vert_label());
580 set_tracker_horiz_values();
581 set_tracker_vert_values();
583 (int) (AltosConvert.height.parse(th, !imperial_units) + 0.5),
584 (int) (AltosConvert.height.parse(tv, !imperial_units) + 0.5)
586 set_tracker_distances(t);
590 /* set and get all of the dialog values */
591 public void set_product(String product) {
592 radio_frequency_value.set_product(product);
593 product_value.setText(product);
594 set_flight_log_max_tool_tip();
597 public void set_version(String version) {
598 version_value.setText(version);
601 public void set_serial(int serial) {
602 radio_frequency_value.set_serial(serial);
603 serial_value.setText(String.format("%d", serial));
606 public void set_main_deploy(int new_main_deploy) {
609 public int main_deploy() {
613 public void set_apogee_delay(int new_apogee_delay) { }
615 public int apogee_delay() {
619 public void set_apogee_lockout(int new_apogee_lockout) { }
621 public int apogee_lockout() { return -1; }
623 public void set_radio_frequency(double new_radio_frequency) {
624 radio_frequency_value.set_frequency(new_radio_frequency);
627 public double radio_frequency() {
628 return radio_frequency_value.frequency();
631 public void set_radio_calibration(int new_radio_calibration) {
632 radio_calibration_value.setVisible(new_radio_calibration >= 0);
633 if (new_radio_calibration < 0)
634 radio_calibration_value.setText("Disabled");
636 radio_calibration_value.setText(String.format("%d", new_radio_calibration));
639 public int radio_calibration() {
640 return Integer.parseInt(radio_calibration_value.getText());
643 public void set_radio_enable(int new_radio_enable) {
644 if (new_radio_enable >= 0) {
645 radio_enable_value.setSelected(new_radio_enable > 0);
646 radio_enable_value.setEnabled(true);
648 radio_enable_value.setSelected(true);
649 radio_enable_value.setVisible(radio_frequency() > 0);
650 radio_enable_value.setEnabled(false);
652 set_radio_enable_tool_tip();
655 public int radio_enable() {
656 if (radio_enable_value.isEnabled())
657 return radio_enable_value.isSelected() ? 1 : 0;
662 public void set_callsign(String new_callsign) {
663 callsign_value.setVisible(new_callsign != null);
664 callsign_value.setText(new_callsign);
667 public String callsign() {
668 return callsign_value.getText();
671 public void set_flight_log_max(int new_flight_log_max) {
672 flight_log_max_value.setSelectedItem(Integer.toString(new_flight_log_max));
673 set_flight_log_max_tool_tip();
676 public void set_flight_log_max_enabled(boolean enable) {
677 flight_log_max_value.setEnabled(enable);
678 set_flight_log_max_tool_tip();
681 public int flight_log_max() {
682 return Integer.parseInt(flight_log_max_value.getSelectedItem().toString());
685 public void set_flight_log_max_limit(int flight_log_max_limit) {
686 //boolean any_added = false;
687 flight_log_max_value.removeAllItems();
688 for (int i = 0; i < flight_log_max_values.length; i++) {
689 if (Integer.parseInt(flight_log_max_values[i]) < flight_log_max_limit){
690 flight_log_max_value.addItem(flight_log_max_values[i]);
694 flight_log_max_value.addItem(String.format("%d", flight_log_max_limit));
697 public void set_ignite_mode(int new_ignite_mode) { }
698 public int ignite_mode() { return -1; }
701 public void set_pad_orientation(int new_pad_orientation) { }
702 public int pad_orientation() { return -1; }
704 public void set_beep(int new_beep) { }
706 public int beep() { return -1; }
708 String[] tracker_horiz_values() {
709 if (AltosConvert.imperial_units)
710 return tracker_horiz_values_ft;
712 return tracker_horiz_values_m;
715 void set_tracker_horiz_values() {
716 String[] v = tracker_horiz_values();
717 while (tracker_horiz_value.getItemCount() > 0)
718 tracker_horiz_value.removeItemAt(0);
719 for (int i = 0; i < v.length; i++)
720 tracker_horiz_value.addItem(v[i]);
721 tracker_horiz_value.setMaximumRowCount(v.length);
724 String get_tracker_horiz_label() {
725 return String.format("Logging Trigger Horizontal (%s):", AltosConvert.height.show_units());
728 String[] tracker_vert_values() {
729 if (AltosConvert.imperial_units)
730 return tracker_vert_values_ft;
732 return tracker_vert_values_m;
735 void set_tracker_vert_values() {
736 String[] v = tracker_vert_values();
737 while (tracker_vert_value.getItemCount() > 0)
738 tracker_vert_value.removeItemAt(0);
739 for (int i = 0; i < v.length; i++)
740 tracker_vert_value.addItem(v[i]);
741 tracker_vert_value.setMaximumRowCount(v.length);
744 void set_tracker_tool_tip() {
745 if (tracker_horiz_value.isEnabled())
746 tracker_horiz_value.setToolTipText("How far the device must move before logging is enabled");
748 tracker_horiz_value.setToolTipText("This device doesn't disable logging before motion");
749 if (tracker_vert_value.isEnabled())
750 tracker_vert_value.setToolTipText("How far the device must move before logging is enabled");
752 tracker_vert_value.setToolTipText("This device doesn't disable logging before motion");
755 String get_tracker_vert_label() {
756 return String.format("Logging Trigger Vertical (%s):", AltosConvert.height.show_units());
759 public void set_tracker_distances(int[] tracker_distances) {
760 if (tracker_distances != null) {
761 tracker_horiz_value.setSelectedItem(AltosConvert.height.say(tracker_distances[0]));
762 tracker_vert_value.setSelectedItem(AltosConvert.height.say(tracker_distances[1]));
763 tracker_horiz_value.setEnabled(true);
764 tracker_vert_value.setEnabled(true);
766 tracker_horiz_value.setEnabled(false);
767 tracker_vert_value.setEnabled(false);
771 public int[] tracker_distances() {
772 if (tracker_horiz_value.isEnabled() && tracker_vert_value.isEnabled()) {
774 (int) (AltosConvert.height.parse(tracker_horiz_value.getSelectedItem().toString()) + 0.5),
775 (int) (AltosConvert.height.parse(tracker_vert_value.getSelectedItem().toString()) + 0.5),
782 public void set_aprs_interval(int new_aprs_interval) {
785 if (new_aprs_interval <= 0)
788 s = Integer.toString(new_aprs_interval);
789 aprs_interval_value.setSelectedItem(s);
790 aprs_interval_value.setVisible(new_aprs_interval >= 0);
791 set_aprs_interval_tool_tip();
794 public int aprs_interval() {
795 String s = aprs_interval_value.getSelectedItem().toString();
797 if (s.equals("Disabled"))
799 return Integer.parseInt(s);