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.
21 import java.awt.event.*;
23 import javax.swing.event.*;
24 import org.altusmetrum.AltosLib.*;
26 public class AltosConfigUI
28 implements ActionListener, ItemListener, DocumentListener, AltosConfigValues
35 JLabel main_deploy_label;
36 JLabel apogee_delay_label;
37 JLabel apogee_lockout_label;
38 JLabel frequency_label;
39 JLabel radio_calibration_label;
40 JLabel radio_frequency_label;
41 JLabel radio_enable_label;
42 JLabel aprs_interval_label;
43 JLabel flight_log_max_label;
44 JLabel ignite_mode_label;
45 JLabel pad_orientation_label;
46 JLabel callsign_label;
54 JComboBox main_deploy_value;
55 JComboBox apogee_delay_value;
56 JComboBox apogee_lockout_value;
57 AltosFreqList radio_frequency_value;
58 JTextField radio_calibration_value;
59 JRadioButton radio_enable_value;
60 JComboBox aprs_interval_value;
61 JComboBox flight_log_max_value;
62 JComboBox ignite_mode_value;
63 JComboBox pad_orientation_value;
64 JTextField callsign_value;
75 ActionListener listener;
77 static String[] main_deploy_values = {
78 "100", "150", "200", "250", "300", "350",
82 static String[] apogee_delay_values = {
83 "0", "1", "2", "3", "4", "5"
86 static String[] apogee_lockout_values = {
87 "0", "5", "10", "15", "20"
90 static String[] flight_log_max_values = {
91 "64", "128", "192", "256", "320",
92 "384", "448", "512", "576", "640",
93 "704", "768", "832", "896", "960",
96 static String[] ignite_mode_values = {
102 static String[] aprs_interval_values = {
109 static String[] pad_orientation_values = {
114 /* A window listener to catch closing events and tell the config code */
115 class ConfigListener extends WindowAdapter {
118 public ConfigListener(AltosConfigUI this_ui) {
122 public void windowClosing(WindowEvent e) {
123 ui.actionPerformed(new ActionEvent(e.getSource(),
124 ActionEvent.ACTION_PERFORMED,
129 boolean is_telemini() {
130 String product = product_value.getText();
131 return product != null && product.startsWith("TeleMini");
134 boolean is_telemetrum() {
135 String product = product_value.getText();
136 return product != null && product.startsWith("TeleMetrum");
139 void set_radio_calibration_tool_tip() {
140 if (radio_calibration_value.isEnabled())
141 radio_calibration_value.setToolTipText("Tune radio output to match desired frequency");
143 radio_calibration_value.setToolTipText("Cannot tune radio while connected over packet mode");
146 void set_radio_enable_tool_tip() {
147 if (radio_enable_value.isEnabled())
148 radio_enable_value.setToolTipText("Enable/Disable telemetry and RDF transmissions");
150 radio_enable_value.setToolTipText("Firmware version does not support disabling radio");
153 void set_aprs_interval_tool_tip() {
154 if (aprs_interval_value.isEnabled())
155 aprs_interval_value.setToolTipText("Enable APRS and set the interval between APRS reports");
157 aprs_interval_value.setToolTipText("Hardware doesn't support APRS");
160 void set_flight_log_max_tool_tip() {
161 if (flight_log_max_value.isEnabled())
162 flight_log_max_value.setToolTipText("Size reserved for each flight log (in kB)");
165 flight_log_max_value.setToolTipText("Cannot set max value with flight logs in memory");
166 else if (is_telemini())
167 flight_log_max_value.setToolTipText("TeleMini stores only one flight");
169 flight_log_max_value.setToolTipText("Cannot set max flight log value");
173 void set_ignite_mode_tool_tip() {
174 if (ignite_mode_value.isEnabled())
175 ignite_mode_value.setToolTipText("Select when igniters will be fired");
177 ignite_mode_value.setToolTipText("Older firmware could not select ignite mode");
180 void set_pad_orientation_tool_tip() {
181 if (pad_orientation_value.isEnabled())
182 pad_orientation_value.setToolTipText("How will TeleMetrum be mounted in the airframe");
185 pad_orientation_value.setToolTipText("Older TeleMetrum firmware must fly antenna forward");
186 else if (is_telemini())
187 pad_orientation_value.setToolTipText("TeleMini doesn't care how it is mounted");
189 pad_orientation_value.setToolTipText("Can't select orientation");
193 /* Build the UI using a grid bag */
194 public AltosConfigUI(JFrame in_owner, boolean remote) {
195 super (in_owner, "Configure TeleMetrum", false);
198 GridBagConstraints c;
201 Insets il = new Insets(4,4,4,4);
202 Insets ir = new Insets(4,4,4,4);
204 pane = getContentPane();
205 pane.setLayout(new GridBagLayout());
208 c = new GridBagConstraints();
209 c.gridx = 0; c.gridy = row;
211 c.fill = GridBagConstraints.NONE;
212 c.anchor = GridBagConstraints.LINE_START;
214 product_label = new JLabel("Product:");
215 pane.add(product_label, c);
217 c = new GridBagConstraints();
218 c.gridx = 4; c.gridy = row;
220 c.fill = GridBagConstraints.HORIZONTAL;
222 c.anchor = GridBagConstraints.LINE_START;
224 product_value = new JLabel("");
225 pane.add(product_value, c);
229 c = new GridBagConstraints();
230 c.gridx = 0; c.gridy = row;
232 c.fill = GridBagConstraints.NONE;
233 c.anchor = GridBagConstraints.LINE_START;
236 version_label = new JLabel("Software version:");
237 pane.add(version_label, c);
239 c = new GridBagConstraints();
240 c.gridx = 4; c.gridy = row;
242 c.fill = GridBagConstraints.HORIZONTAL;
244 c.anchor = GridBagConstraints.LINE_START;
247 version_value = new JLabel("");
248 pane.add(version_value, c);
252 c = new GridBagConstraints();
253 c.gridx = 0; c.gridy = row;
255 c.fill = GridBagConstraints.NONE;
256 c.anchor = GridBagConstraints.LINE_START;
259 serial_label = new JLabel("Serial:");
260 pane.add(serial_label, c);
262 c = new GridBagConstraints();
263 c.gridx = 4; c.gridy = row;
265 c.fill = GridBagConstraints.HORIZONTAL;
267 c.anchor = GridBagConstraints.LINE_START;
270 serial_value = new JLabel("");
271 pane.add(serial_value, c);
275 c = new GridBagConstraints();
276 c.gridx = 0; c.gridy = row;
278 c.fill = GridBagConstraints.NONE;
279 c.anchor = GridBagConstraints.LINE_START;
282 main_deploy_label = new JLabel("Main Deploy Altitude(m):");
283 pane.add(main_deploy_label, c);
285 c = new GridBagConstraints();
286 c.gridx = 4; c.gridy = row;
288 c.fill = GridBagConstraints.HORIZONTAL;
290 c.anchor = GridBagConstraints.LINE_START;
293 main_deploy_value = new JComboBox(main_deploy_values);
294 main_deploy_value.setEditable(true);
295 main_deploy_value.addItemListener(this);
296 pane.add(main_deploy_value, c);
297 main_deploy_value.setToolTipText("Height above pad altitude to fire main charge");
301 c = new GridBagConstraints();
302 c.gridx = 0; c.gridy = row;
304 c.fill = GridBagConstraints.NONE;
305 c.anchor = GridBagConstraints.LINE_START;
308 apogee_delay_label = new JLabel("Apogee Delay(s):");
309 pane.add(apogee_delay_label, c);
311 c = new GridBagConstraints();
312 c.gridx = 4; c.gridy = row;
314 c.fill = GridBagConstraints.HORIZONTAL;
316 c.anchor = GridBagConstraints.LINE_START;
319 apogee_delay_value = new JComboBox(apogee_delay_values);
320 apogee_delay_value.setEditable(true);
321 apogee_delay_value.addItemListener(this);
322 pane.add(apogee_delay_value, c);
323 apogee_delay_value.setToolTipText("Delay after apogee before charge fires");
327 c = new GridBagConstraints();
328 c.gridx = 0; c.gridy = row;
330 c.fill = GridBagConstraints.NONE;
331 c.anchor = GridBagConstraints.LINE_START;
334 apogee_lockout_label = new JLabel("Apogee Lockout(s):");
335 pane.add(apogee_lockout_label, c);
337 c = new GridBagConstraints();
338 c.gridx = 4; c.gridy = row;
340 c.fill = GridBagConstraints.HORIZONTAL;
342 c.anchor = GridBagConstraints.LINE_START;
345 apogee_lockout_value = new JComboBox(apogee_lockout_values);
346 apogee_lockout_value.setEditable(true);
347 apogee_lockout_value.addItemListener(this);
348 pane.add(apogee_lockout_value, c);
349 apogee_lockout_value.setToolTipText("Time after boost while apogee detection is locked out");
353 c = new GridBagConstraints();
354 c.gridx = 0; c.gridy = row;
356 c.fill = GridBagConstraints.NONE;
357 c.anchor = GridBagConstraints.LINE_START;
360 radio_frequency_label = new JLabel("Frequency:");
361 pane.add(radio_frequency_label, c);
363 c = new GridBagConstraints();
364 c.gridx = 4; c.gridy = row;
366 c.fill = GridBagConstraints.HORIZONTAL;
368 c.anchor = GridBagConstraints.LINE_START;
371 radio_frequency_value = new AltosFreqList();
372 radio_frequency_value.addItemListener(this);
373 pane.add(radio_frequency_value, c);
374 radio_frequency_value.setToolTipText("Telemetry, RDF and packet frequency");
377 /* Radio Calibration */
378 c = new GridBagConstraints();
379 c.gridx = 0; c.gridy = row;
381 c.fill = GridBagConstraints.NONE;
382 c.anchor = GridBagConstraints.LINE_START;
385 radio_calibration_label = new JLabel("RF Calibration:");
386 pane.add(radio_calibration_label, c);
388 c = new GridBagConstraints();
389 c.gridx = 4; c.gridy = row;
391 c.fill = GridBagConstraints.HORIZONTAL;
393 c.anchor = GridBagConstraints.LINE_START;
396 radio_calibration_value = new JTextField(String.format("%d", 1186611));
397 radio_calibration_value.getDocument().addDocumentListener(this);
399 radio_calibration_value.setEnabled(false);
400 pane.add(radio_calibration_value, c);
401 set_radio_calibration_tool_tip();
405 c = new GridBagConstraints();
406 c.gridx = 0; c.gridy = row;
408 c.fill = GridBagConstraints.NONE;
409 c.anchor = GridBagConstraints.LINE_START;
412 radio_enable_label = new JLabel("Telemetry/RDF/APRS Enable:");
413 pane.add(radio_enable_label, c);
415 c = new GridBagConstraints();
416 c.gridx = 4; c.gridy = row;
418 c.fill = GridBagConstraints.HORIZONTAL;
420 c.anchor = GridBagConstraints.LINE_START;
423 radio_enable_value = new JRadioButton("Enabled");
424 radio_enable_value.addItemListener(this);
425 pane.add(radio_enable_value, c);
426 set_radio_enable_tool_tip();
430 c = new GridBagConstraints();
431 c.gridx = 0; c.gridy = row;
433 c.fill = GridBagConstraints.NONE;
434 c.anchor = GridBagConstraints.LINE_START;
437 aprs_interval_label = new JLabel("APRS Interval(s):");
438 pane.add(aprs_interval_label, c);
440 c = new GridBagConstraints();
441 c.gridx = 4; c.gridy = row;
443 c.fill = GridBagConstraints.HORIZONTAL;
445 c.anchor = GridBagConstraints.LINE_START;
448 aprs_interval_value = new JComboBox(aprs_interval_values);
449 aprs_interval_value.setEditable(true);
450 aprs_interval_value.addItemListener(this);
451 pane.add(aprs_interval_value, c);
452 set_aprs_interval_tool_tip();
456 c = new GridBagConstraints();
457 c.gridx = 0; c.gridy = row;
459 c.fill = GridBagConstraints.NONE;
460 c.anchor = GridBagConstraints.LINE_START;
463 callsign_label = new JLabel("Callsign:");
464 pane.add(callsign_label, c);
466 c = new GridBagConstraints();
467 c.gridx = 4; c.gridy = row;
469 c.fill = GridBagConstraints.HORIZONTAL;
471 c.anchor = GridBagConstraints.LINE_START;
474 callsign_value = new JTextField(AltosUIPreferences.callsign());
475 callsign_value.getDocument().addDocumentListener(this);
476 pane.add(callsign_value, c);
477 callsign_value.setToolTipText("Callsign reported in telemetry data");
481 c = new GridBagConstraints();
482 c.gridx = 0; c.gridy = row;
484 c.fill = GridBagConstraints.NONE;
485 c.anchor = GridBagConstraints.LINE_START;
488 flight_log_max_label = new JLabel("Maximum Flight Log Size:");
489 pane.add(flight_log_max_label, c);
491 c = new GridBagConstraints();
492 c.gridx = 4; c.gridy = row;
494 c.fill = GridBagConstraints.HORIZONTAL;
496 c.anchor = GridBagConstraints.LINE_START;
499 flight_log_max_value = new JComboBox(flight_log_max_values);
500 flight_log_max_value.setEditable(true);
501 flight_log_max_value.addItemListener(this);
502 pane.add(flight_log_max_value, c);
503 set_flight_log_max_tool_tip();
507 c = new GridBagConstraints();
508 c.gridx = 0; c.gridy = row;
510 c.fill = GridBagConstraints.NONE;
511 c.anchor = GridBagConstraints.LINE_START;
514 ignite_mode_label = new JLabel("Igniter Firing Mode:");
515 pane.add(ignite_mode_label, c);
517 c = new GridBagConstraints();
518 c.gridx = 4; c.gridy = row;
520 c.fill = GridBagConstraints.HORIZONTAL;
522 c.anchor = GridBagConstraints.LINE_START;
525 ignite_mode_value = new JComboBox(ignite_mode_values);
526 ignite_mode_value.setEditable(false);
527 ignite_mode_value.addItemListener(this);
528 pane.add(ignite_mode_value, c);
529 set_ignite_mode_tool_tip();
532 /* Pad orientation */
533 c = new GridBagConstraints();
534 c.gridx = 0; c.gridy = row;
536 c.fill = GridBagConstraints.NONE;
537 c.anchor = GridBagConstraints.LINE_START;
540 pad_orientation_label = new JLabel("Pad Orientation:");
541 pane.add(pad_orientation_label, c);
543 c = new GridBagConstraints();
544 c.gridx = 4; c.gridy = row;
546 c.fill = GridBagConstraints.HORIZONTAL;
548 c.anchor = GridBagConstraints.LINE_START;
551 pad_orientation_value = new JComboBox(pad_orientation_values);
552 pad_orientation_value.setEditable(false);
553 pad_orientation_value.addItemListener(this);
554 pane.add(pad_orientation_value, c);
555 set_pad_orientation_tool_tip();
559 c = new GridBagConstraints();
560 c.gridx = 4; c.gridy = row;
562 c.fill = GridBagConstraints.HORIZONTAL;
563 c.anchor = GridBagConstraints.LINE_START;
566 pyro = new JButton("Configure Pyro Channels");
568 pyro.addActionListener(this);
569 pyro.setActionCommand("Pyro");
573 c = new GridBagConstraints();
574 c.gridx = 0; c.gridy = row;
576 c.fill = GridBagConstraints.NONE;
577 c.anchor = GridBagConstraints.LINE_START;
579 save = new JButton("Save");
581 save.addActionListener(this);
582 save.setActionCommand("Save");
584 c = new GridBagConstraints();
585 c.gridx = 2; c.gridy = row;
587 c.fill = GridBagConstraints.NONE;
588 c.anchor = GridBagConstraints.CENTER;
590 reset = new JButton("Reset");
592 reset.addActionListener(this);
593 reset.setActionCommand("Reset");
595 c = new GridBagConstraints();
596 c.gridx = 4; c.gridy = row;
598 c.fill = GridBagConstraints.NONE;
599 c.anchor = GridBagConstraints.CENTER;
601 reboot = new JButton("Reboot");
603 reboot.addActionListener(this);
604 reboot.setActionCommand("Reboot");
606 c = new GridBagConstraints();
607 c.gridx = 6; c.gridy = row;
609 c.fill = GridBagConstraints.NONE;
610 c.anchor = GridBagConstraints.LINE_END;
612 close = new JButton("Close");
614 close.addActionListener(this);
615 close.setActionCommand("Close");
617 addWindowListener(new ConfigListener(this));
620 /* Once the initial values are set, the config code will show the dialog */
621 public void make_visible() {
623 setLocationRelativeTo(owner);
627 /* If any values have been changed, confirm before closing */
628 public boolean check_dirty(String operation) {
630 Object[] options = { String.format("%s anyway", operation), "Keep editing" };
632 i = JOptionPane.showOptionDialog(this,
633 String.format("Configuration modified. %s anyway?", operation),
634 "Configuration Modified",
635 JOptionPane.DEFAULT_OPTION,
636 JOptionPane.WARNING_MESSAGE,
637 null, options, options[1]);
646 save.setEnabled(true);
649 public void set_clean() {
651 save.setEnabled(false);
654 AltosConfigPyroUI pyro_ui;
656 /* Listen for events from our buttons */
657 public void actionPerformed(ActionEvent e) {
658 String cmd = e.getActionCommand();
660 if (cmd.equals("Pyro")) {
661 if (pyro_ui == null && pyros != null) {
662 pyro_ui = new AltosConfigPyroUI(this, pyros);
663 pyro_ui.make_visible();
668 if (cmd.equals("Close") || cmd.equals("Reboot"))
669 if (!check_dirty(cmd))
671 listener.actionPerformed(e);
672 if (cmd.equals("Close") || cmd.equals("Reboot")) {
679 /* ItemListener interface method */
680 public void itemStateChanged(ItemEvent e) {
684 /* DocumentListener interface methods */
685 public void changedUpdate(DocumentEvent e) {
689 public void insertUpdate(DocumentEvent e) {
693 public void removeUpdate(DocumentEvent e) {
697 /* Let the config code hook on a listener */
698 public void addActionListener(ActionListener l) {
702 /* set and get all of the dialog values */
703 public void set_product(String product) {
704 radio_frequency_value.set_product(product);
705 product_value.setText(product);
706 set_pad_orientation_tool_tip();
707 set_flight_log_max_tool_tip();
710 public void set_version(String version) {
711 version_value.setText(version);
714 public void set_serial(int serial) {
715 radio_frequency_value.set_serial(serial);
716 serial_value.setText(String.format("%d", serial));
719 public void set_main_deploy(int new_main_deploy) {
720 main_deploy_value.setSelectedItem(Integer.toString(new_main_deploy));
723 public int main_deploy() {
724 return Integer.parseInt(main_deploy_value.getSelectedItem().toString());
727 public void set_apogee_delay(int new_apogee_delay) {
728 apogee_delay_value.setSelectedItem(Integer.toString(new_apogee_delay));
729 apogee_delay_value.setEnabled(new_apogee_delay >= 0);
732 public int apogee_delay() {
733 return Integer.parseInt(apogee_delay_value.getSelectedItem().toString());
736 public void set_apogee_lockout(int new_apogee_lockout) {
737 apogee_lockout_value.setSelectedItem(Integer.toString(new_apogee_lockout));
738 apogee_lockout_value.setEnabled(new_apogee_lockout >= 0);
741 public int apogee_lockout() {
742 return Integer.parseInt(apogee_lockout_value.getSelectedItem().toString());
745 public void set_radio_frequency(double new_radio_frequency) {
747 for (i = 0; i < radio_frequency_value.getItemCount(); i++) {
748 AltosFrequency f = (AltosFrequency) radio_frequency_value.getItemAt(i);
750 if (f.close(new_radio_frequency)) {
751 radio_frequency_value.setSelectedIndex(i);
755 for (i = 0; i < radio_frequency_value.getItemCount(); i++) {
756 AltosFrequency f = (AltosFrequency) radio_frequency_value.getItemAt(i);
758 if (new_radio_frequency < f.frequency)
761 String description = String.format("%s serial %s",
762 product_value.getText(),
763 serial_value.getText());
764 AltosFrequency new_frequency = new AltosFrequency(new_radio_frequency, description);
765 AltosUIPreferences.add_common_frequency(new_frequency);
766 radio_frequency_value.insertItemAt(new_frequency, i);
767 radio_frequency_value.setSelectedIndex(i);
770 public double radio_frequency() {
771 return radio_frequency_value.frequency();
774 public void set_radio_calibration(int new_radio_calibration) {
775 radio_calibration_value.setText(String.format("%d", new_radio_calibration));
778 public int radio_calibration() {
779 return Integer.parseInt(radio_calibration_value.getText());
782 public void set_radio_enable(int new_radio_enable) {
783 if (new_radio_enable >= 0) {
784 radio_enable_value.setSelected(new_radio_enable > 0);
785 radio_enable_value.setEnabled(true);
787 radio_enable_value.setSelected(true);
788 radio_enable_value.setEnabled(false);
790 set_radio_enable_tool_tip();
793 public int radio_enable() {
794 if (radio_enable_value.isEnabled())
795 return radio_enable_value.isSelected() ? 1 : 0;
800 public void set_callsign(String new_callsign) {
801 callsign_value.setText(new_callsign);
804 public String callsign() {
805 return callsign_value.getText();
808 public void set_flight_log_max(int new_flight_log_max) {
809 flight_log_max_value.setEnabled(new_flight_log_max > 0);
810 flight_log_max_value.setSelectedItem(Integer.toString(new_flight_log_max));
811 set_flight_log_max_tool_tip();
814 public void set_flight_log_max_enabled(boolean enable) {
815 flight_log_max_value.setEnabled(enable);
816 set_flight_log_max_tool_tip();
819 public int flight_log_max() {
820 return Integer.parseInt(flight_log_max_value.getSelectedItem().toString());
823 public void set_flight_log_max_limit(int flight_log_max_limit) {
824 //boolean any_added = false;
825 flight_log_max_value.removeAllItems();
826 for (int i = 0; i < flight_log_max_values.length; i++) {
827 if (Integer.parseInt(flight_log_max_values[i]) < flight_log_max_limit){
828 flight_log_max_value.addItem(flight_log_max_values[i]);
832 flight_log_max_value.addItem(String.format("%d", flight_log_max_limit));
835 public void set_ignite_mode(int new_ignite_mode) {
836 if (new_ignite_mode >= ignite_mode_values.length)
838 if (new_ignite_mode < 0) {
839 ignite_mode_value.setEnabled(false);
842 ignite_mode_value.setEnabled(true);
844 ignite_mode_value.setSelectedIndex(new_ignite_mode);
845 set_ignite_mode_tool_tip();
848 public int ignite_mode() {
849 if (ignite_mode_value.isEnabled())
850 return ignite_mode_value.getSelectedIndex();
856 public void set_pad_orientation(int new_pad_orientation) {
857 if (new_pad_orientation >= pad_orientation_values.length)
858 new_pad_orientation = 0;
859 if (new_pad_orientation < 0) {
860 pad_orientation_value.setEnabled(false);
861 new_pad_orientation = 0;
863 pad_orientation_value.setEnabled(true);
865 pad_orientation_value.setSelectedIndex(new_pad_orientation);
866 set_pad_orientation_tool_tip();
869 public int pad_orientation() {
870 if (pad_orientation_value.isEnabled())
871 return pad_orientation_value.getSelectedIndex();
876 public void set_pyros(AltosPyro[] new_pyros) {
878 pyro.setEnabled(pyros != null);
879 if (pyros != null && pyro_ui != null)
880 pyro_ui.set_pyros(pyros);
883 public AltosPyro[] pyros() {
885 pyros = pyro_ui.get_pyros();
889 public void set_aprs_interval(int new_aprs_interval) {
892 if (new_aprs_interval <= 0)
895 s = Integer.toString(new_aprs_interval);
896 aprs_interval_value.setSelectedItem(s);
897 aprs_interval_value.setEnabled(new_aprs_interval >= 0);
898 set_aprs_interval_tool_tip();
901 public int aprs_interval() {
902 String s = aprs_interval_value.getSelectedItem().toString();
904 if (s.equals("Disabled"))
906 return Integer.parseInt(s);