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
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 flight_log_max_label;
43 JLabel ignite_mode_label;
44 JLabel pad_orientation_label;
45 JLabel callsign_label;
53 JComboBox main_deploy_value;
54 JComboBox apogee_delay_value;
55 JComboBox apogee_lockout_value;
56 AltosFreqList radio_frequency_value;
57 JTextField radio_calibration_value;
58 JRadioButton radio_enable_value;
59 JComboBox flight_log_max_value;
60 JComboBox ignite_mode_value;
61 JComboBox pad_orientation_value;
62 JTextField callsign_value;
73 ActionListener listener;
75 static String[] main_deploy_values = {
76 "100", "150", "200", "250", "300", "350",
80 static String[] apogee_delay_values = {
81 "0", "1", "2", "3", "4", "5"
84 static String[] apogee_lockout_values = {
85 "0", "5", "10", "15", "20"
88 static String[] flight_log_max_values = {
89 "64", "128", "192", "256", "320",
90 "384", "448", "512", "576", "640",
91 "704", "768", "832", "896", "960",
94 static String[] ignite_mode_values = {
100 static String[] pad_orientation_values = {
105 /* A window listener to catch closing events and tell the config code */
106 class ConfigListener extends WindowAdapter {
109 public ConfigListener(AltosConfigUI this_ui) {
113 public void windowClosing(WindowEvent e) {
114 ui.actionPerformed(new ActionEvent(e.getSource(),
115 ActionEvent.ACTION_PERFORMED,
120 boolean is_telemini() {
121 String product = product_value.getText();
122 return product != null && product.startsWith("TeleMini");
125 boolean is_telemetrum() {
126 String product = product_value.getText();
127 return product != null && product.startsWith("TeleMetrum");
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_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)");
149 flight_log_max_value.setToolTipText("Cannot set max value with flight logs in memory");
150 else if (is_telemini())
151 flight_log_max_value.setToolTipText("TeleMini stores only one flight");
153 flight_log_max_value.setToolTipText("Cannot set max flight log value");
157 void set_ignite_mode_tool_tip() {
158 if (ignite_mode_value.isEnabled())
159 ignite_mode_value.setToolTipText("Select when igniters will be fired");
161 ignite_mode_value.setToolTipText("Older firmware could not select ignite mode");
164 void set_pad_orientation_tool_tip() {
165 if (pad_orientation_value.isEnabled())
166 pad_orientation_value.setToolTipText("How will TeleMetrum be mounted in the airframe");
169 pad_orientation_value.setToolTipText("Older TeleMetrum firmware must fly antenna forward");
170 else if (is_telemini())
171 pad_orientation_value.setToolTipText("TeleMini doesn't care how it is mounted");
173 pad_orientation_value.setToolTipText("Can't select orientation");
177 /* Build the UI using a grid bag */
178 public AltosConfigUI(JFrame in_owner, boolean remote) {
179 super (in_owner, "Configure TeleMetrum", false);
182 GridBagConstraints c;
185 Insets il = new Insets(4,4,4,4);
186 Insets ir = new Insets(4,4,4,4);
188 pane = getContentPane();
189 pane.setLayout(new GridBagLayout());
192 c = new GridBagConstraints();
193 c.gridx = 0; c.gridy = row;
195 c.fill = GridBagConstraints.NONE;
196 c.anchor = GridBagConstraints.LINE_START;
198 product_label = new JLabel("Product:");
199 pane.add(product_label, c);
201 c = new GridBagConstraints();
202 c.gridx = 4; c.gridy = row;
204 c.fill = GridBagConstraints.HORIZONTAL;
206 c.anchor = GridBagConstraints.LINE_START;
208 product_value = new JLabel("");
209 pane.add(product_value, c);
213 c = new GridBagConstraints();
214 c.gridx = 0; c.gridy = row;
216 c.fill = GridBagConstraints.NONE;
217 c.anchor = GridBagConstraints.LINE_START;
220 version_label = new JLabel("Software version:");
221 pane.add(version_label, c);
223 c = new GridBagConstraints();
224 c.gridx = 4; c.gridy = row;
226 c.fill = GridBagConstraints.HORIZONTAL;
228 c.anchor = GridBagConstraints.LINE_START;
231 version_value = new JLabel("");
232 pane.add(version_value, c);
236 c = new GridBagConstraints();
237 c.gridx = 0; c.gridy = row;
239 c.fill = GridBagConstraints.NONE;
240 c.anchor = GridBagConstraints.LINE_START;
243 serial_label = new JLabel("Serial:");
244 pane.add(serial_label, c);
246 c = new GridBagConstraints();
247 c.gridx = 4; c.gridy = row;
249 c.fill = GridBagConstraints.HORIZONTAL;
251 c.anchor = GridBagConstraints.LINE_START;
254 serial_value = new JLabel("");
255 pane.add(serial_value, c);
259 c = new GridBagConstraints();
260 c.gridx = 0; c.gridy = row;
262 c.fill = GridBagConstraints.NONE;
263 c.anchor = GridBagConstraints.LINE_START;
266 main_deploy_label = new JLabel("Main Deploy Altitude(m):");
267 pane.add(main_deploy_label, c);
269 c = new GridBagConstraints();
270 c.gridx = 4; c.gridy = row;
272 c.fill = GridBagConstraints.HORIZONTAL;
274 c.anchor = GridBagConstraints.LINE_START;
277 main_deploy_value = new JComboBox(main_deploy_values);
278 main_deploy_value.setEditable(true);
279 main_deploy_value.addItemListener(this);
280 pane.add(main_deploy_value, c);
281 main_deploy_value.setToolTipText("Height above pad altitude to fire main charge");
285 c = new GridBagConstraints();
286 c.gridx = 0; c.gridy = row;
288 c.fill = GridBagConstraints.NONE;
289 c.anchor = GridBagConstraints.LINE_START;
292 apogee_delay_label = new JLabel("Apogee Delay(s):");
293 pane.add(apogee_delay_label, c);
295 c = new GridBagConstraints();
296 c.gridx = 4; c.gridy = row;
298 c.fill = GridBagConstraints.HORIZONTAL;
300 c.anchor = GridBagConstraints.LINE_START;
303 apogee_delay_value = new JComboBox(apogee_delay_values);
304 apogee_delay_value.setEditable(true);
305 apogee_delay_value.addItemListener(this);
306 pane.add(apogee_delay_value, c);
307 apogee_delay_value.setToolTipText("Delay after apogee before charge fires");
311 c = new GridBagConstraints();
312 c.gridx = 0; c.gridy = row;
314 c.fill = GridBagConstraints.NONE;
315 c.anchor = GridBagConstraints.LINE_START;
318 apogee_lockout_label = new JLabel("Apogee Lockout(s):");
319 pane.add(apogee_lockout_label, c);
321 c = new GridBagConstraints();
322 c.gridx = 4; c.gridy = row;
324 c.fill = GridBagConstraints.HORIZONTAL;
326 c.anchor = GridBagConstraints.LINE_START;
329 apogee_lockout_value = new JComboBox(apogee_lockout_values);
330 apogee_lockout_value.setEditable(true);
331 apogee_lockout_value.addItemListener(this);
332 pane.add(apogee_lockout_value, c);
333 apogee_lockout_value.setToolTipText("Time after boost while apogee detection is locked out");
337 c = new GridBagConstraints();
338 c.gridx = 0; c.gridy = row;
340 c.fill = GridBagConstraints.NONE;
341 c.anchor = GridBagConstraints.LINE_START;
344 radio_frequency_label = new JLabel("Frequency:");
345 pane.add(radio_frequency_label, c);
347 c = new GridBagConstraints();
348 c.gridx = 4; c.gridy = row;
350 c.fill = GridBagConstraints.HORIZONTAL;
352 c.anchor = GridBagConstraints.LINE_START;
355 radio_frequency_value = new AltosFreqList();
356 radio_frequency_value.addItemListener(this);
357 pane.add(radio_frequency_value, c);
358 radio_frequency_value.setToolTipText("Telemetry, RDF and packet frequency");
361 /* Radio Calibration */
362 c = new GridBagConstraints();
363 c.gridx = 0; c.gridy = row;
365 c.fill = GridBagConstraints.NONE;
366 c.anchor = GridBagConstraints.LINE_START;
369 radio_calibration_label = new JLabel("RF Calibration:");
370 pane.add(radio_calibration_label, c);
372 c = new GridBagConstraints();
373 c.gridx = 4; c.gridy = row;
375 c.fill = GridBagConstraints.HORIZONTAL;
377 c.anchor = GridBagConstraints.LINE_START;
380 radio_calibration_value = new JTextField(String.format("%d", 1186611));
381 radio_calibration_value.getDocument().addDocumentListener(this);
383 radio_calibration_value.setEnabled(false);
384 pane.add(radio_calibration_value, c);
385 set_radio_calibration_tool_tip();
389 c = new GridBagConstraints();
390 c.gridx = 0; c.gridy = row;
392 c.fill = GridBagConstraints.NONE;
393 c.anchor = GridBagConstraints.LINE_START;
396 radio_enable_label = new JLabel("Telemetry/RDF Enable:");
397 pane.add(radio_enable_label, c);
399 c = new GridBagConstraints();
400 c.gridx = 4; c.gridy = row;
402 c.fill = GridBagConstraints.HORIZONTAL;
404 c.anchor = GridBagConstraints.LINE_START;
407 radio_enable_value = new JRadioButton("Enabled");
408 radio_enable_value.addItemListener(this);
409 pane.add(radio_enable_value, c);
410 set_radio_enable_tool_tip();
414 c = new GridBagConstraints();
415 c.gridx = 0; c.gridy = row;
417 c.fill = GridBagConstraints.NONE;
418 c.anchor = GridBagConstraints.LINE_START;
421 callsign_label = new JLabel("Callsign:");
422 pane.add(callsign_label, c);
424 c = new GridBagConstraints();
425 c.gridx = 4; c.gridy = row;
427 c.fill = GridBagConstraints.HORIZONTAL;
429 c.anchor = GridBagConstraints.LINE_START;
432 callsign_value = new JTextField(AltosUIPreferences.callsign());
433 callsign_value.getDocument().addDocumentListener(this);
434 pane.add(callsign_value, c);
435 callsign_value.setToolTipText("Callsign reported in telemetry data");
439 c = new GridBagConstraints();
440 c.gridx = 0; c.gridy = row;
442 c.fill = GridBagConstraints.NONE;
443 c.anchor = GridBagConstraints.LINE_START;
446 flight_log_max_label = new JLabel("Maximum Flight Log Size:");
447 pane.add(flight_log_max_label, c);
449 c = new GridBagConstraints();
450 c.gridx = 4; c.gridy = row;
452 c.fill = GridBagConstraints.HORIZONTAL;
454 c.anchor = GridBagConstraints.LINE_START;
457 flight_log_max_value = new JComboBox(flight_log_max_values);
458 flight_log_max_value.setEditable(true);
459 flight_log_max_value.addItemListener(this);
460 pane.add(flight_log_max_value, c);
461 set_flight_log_max_tool_tip();
465 c = new GridBagConstraints();
466 c.gridx = 0; c.gridy = row;
468 c.fill = GridBagConstraints.NONE;
469 c.anchor = GridBagConstraints.LINE_START;
472 ignite_mode_label = new JLabel("Igniter Firing Mode:");
473 pane.add(ignite_mode_label, c);
475 c = new GridBagConstraints();
476 c.gridx = 4; c.gridy = row;
478 c.fill = GridBagConstraints.HORIZONTAL;
480 c.anchor = GridBagConstraints.LINE_START;
483 ignite_mode_value = new JComboBox(ignite_mode_values);
484 ignite_mode_value.setEditable(false);
485 ignite_mode_value.addItemListener(this);
486 pane.add(ignite_mode_value, c);
487 set_ignite_mode_tool_tip();
490 /* Pad orientation */
491 c = new GridBagConstraints();
492 c.gridx = 0; c.gridy = row;
494 c.fill = GridBagConstraints.NONE;
495 c.anchor = GridBagConstraints.LINE_START;
498 pad_orientation_label = new JLabel("Pad Orientation:");
499 pane.add(pad_orientation_label, c);
501 c = new GridBagConstraints();
502 c.gridx = 4; c.gridy = row;
504 c.fill = GridBagConstraints.HORIZONTAL;
506 c.anchor = GridBagConstraints.LINE_START;
509 pad_orientation_value = new JComboBox(pad_orientation_values);
510 pad_orientation_value.setEditable(false);
511 pad_orientation_value.addItemListener(this);
512 pane.add(pad_orientation_value, c);
513 set_pad_orientation_tool_tip();
517 c = new GridBagConstraints();
518 c.gridx = 4; c.gridy = row;
520 c.fill = GridBagConstraints.HORIZONTAL;
521 c.anchor = GridBagConstraints.LINE_START;
524 pyro = new JButton("Configure Pyro Channels");
526 pyro.addActionListener(this);
527 pyro.setActionCommand("Pyro");
531 c = new GridBagConstraints();
532 c.gridx = 0; c.gridy = row;
534 c.fill = GridBagConstraints.NONE;
535 c.anchor = GridBagConstraints.LINE_START;
537 save = new JButton("Save");
539 save.addActionListener(this);
540 save.setActionCommand("Save");
542 c = new GridBagConstraints();
543 c.gridx = 2; c.gridy = row;
545 c.fill = GridBagConstraints.NONE;
546 c.anchor = GridBagConstraints.CENTER;
548 reset = new JButton("Reset");
550 reset.addActionListener(this);
551 reset.setActionCommand("Reset");
553 c = new GridBagConstraints();
554 c.gridx = 4; c.gridy = row;
556 c.fill = GridBagConstraints.NONE;
557 c.anchor = GridBagConstraints.CENTER;
559 reboot = new JButton("Reboot");
561 reboot.addActionListener(this);
562 reboot.setActionCommand("Reboot");
564 c = new GridBagConstraints();
565 c.gridx = 6; c.gridy = row;
567 c.fill = GridBagConstraints.NONE;
568 c.anchor = GridBagConstraints.LINE_END;
570 close = new JButton("Close");
572 close.addActionListener(this);
573 close.setActionCommand("Close");
575 addWindowListener(new ConfigListener(this));
578 /* Once the initial values are set, the config code will show the dialog */
579 public void make_visible() {
581 setLocationRelativeTo(owner);
585 /* If any values have been changed, confirm before closing */
586 public boolean check_dirty(String operation) {
588 Object[] options = { String.format("%s anyway", operation), "Keep editing" };
590 i = JOptionPane.showOptionDialog(this,
591 String.format("Configuration modified. %s anyway?", operation),
592 "Configuration Modified",
593 JOptionPane.DEFAULT_OPTION,
594 JOptionPane.WARNING_MESSAGE,
595 null, options, options[1]);
604 save.setEnabled(true);
607 public void set_clean() {
609 save.setEnabled(false);
612 AltosConfigPyroUI pyro_ui;
614 /* Listen for events from our buttons */
615 public void actionPerformed(ActionEvent e) {
616 String cmd = e.getActionCommand();
618 if (cmd.equals("Pyro")) {
619 if (pyro_ui == null && pyros != null) {
620 pyro_ui = new AltosConfigPyroUI(this, pyros);
621 pyro_ui.make_visible();
626 if (cmd.equals("Close") || cmd.equals("Reboot"))
627 if (!check_dirty(cmd))
629 listener.actionPerformed(e);
630 if (cmd.equals("Close") || cmd.equals("Reboot")) {
637 /* ItemListener interface method */
638 public void itemStateChanged(ItemEvent e) {
642 /* DocumentListener interface methods */
643 public void changedUpdate(DocumentEvent e) {
647 public void insertUpdate(DocumentEvent e) {
651 public void removeUpdate(DocumentEvent e) {
655 /* Let the config code hook on a listener */
656 public void addActionListener(ActionListener l) {
660 /* set and get all of the dialog values */
661 public void set_product(String product) {
662 radio_frequency_value.set_product(product);
663 product_value.setText(product);
664 set_pad_orientation_tool_tip();
665 set_flight_log_max_tool_tip();
668 public void set_version(String version) {
669 version_value.setText(version);
672 public void set_serial(int serial) {
673 radio_frequency_value.set_serial(serial);
674 serial_value.setText(String.format("%d", serial));
677 public void set_main_deploy(int new_main_deploy) {
678 main_deploy_value.setSelectedItem(Integer.toString(new_main_deploy));
681 public int main_deploy() {
682 return Integer.parseInt(main_deploy_value.getSelectedItem().toString());
685 public void set_apogee_delay(int new_apogee_delay) {
686 apogee_delay_value.setSelectedItem(Integer.toString(new_apogee_delay));
689 public int apogee_delay() {
690 return Integer.parseInt(apogee_delay_value.getSelectedItem().toString());
693 public void set_apogee_lockout(int new_apogee_lockout) {
694 apogee_lockout_value.setSelectedItem(Integer.toString(new_apogee_lockout));
697 public int apogee_lockout() {
698 return Integer.parseInt(apogee_lockout_value.getSelectedItem().toString());
701 public void set_radio_frequency(double new_radio_frequency) {
703 for (i = 0; i < radio_frequency_value.getItemCount(); i++) {
704 AltosFrequency f = (AltosFrequency) radio_frequency_value.getItemAt(i);
706 if (f.close(new_radio_frequency)) {
707 radio_frequency_value.setSelectedIndex(i);
711 for (i = 0; i < radio_frequency_value.getItemCount(); i++) {
712 AltosFrequency f = (AltosFrequency) radio_frequency_value.getItemAt(i);
714 if (new_radio_frequency < f.frequency)
717 String description = String.format("%s serial %s",
718 product_value.getText(),
719 serial_value.getText());
720 AltosFrequency new_frequency = new AltosFrequency(new_radio_frequency, description);
721 AltosUIPreferences.add_common_frequency(new_frequency);
722 radio_frequency_value.insertItemAt(new_frequency, i);
723 radio_frequency_value.setSelectedIndex(i);
726 public double radio_frequency() {
727 return radio_frequency_value.frequency();
730 public void set_radio_calibration(int new_radio_calibration) {
731 radio_calibration_value.setText(String.format("%d", new_radio_calibration));
734 public int radio_calibration() {
735 return Integer.parseInt(radio_calibration_value.getText());
738 public void set_radio_enable(int new_radio_enable) {
739 if (new_radio_enable >= 0) {
740 radio_enable_value.setSelected(new_radio_enable > 0);
741 radio_enable_value.setEnabled(true);
743 radio_enable_value.setSelected(true);
744 radio_enable_value.setEnabled(false);
746 set_radio_enable_tool_tip();
749 public int radio_enable() {
750 if (radio_enable_value.isEnabled())
751 return radio_enable_value.isSelected() ? 1 : 0;
756 public void set_callsign(String new_callsign) {
757 callsign_value.setText(new_callsign);
760 public String callsign() {
761 return callsign_value.getText();
764 public void set_flight_log_max(int new_flight_log_max) {
765 flight_log_max_value.setEnabled(new_flight_log_max > 0);
766 flight_log_max_value.setSelectedItem(Integer.toString(new_flight_log_max));
767 set_flight_log_max_tool_tip();
770 public void set_flight_log_max_enabled(boolean enable) {
771 flight_log_max_value.setEnabled(enable);
772 set_flight_log_max_tool_tip();
775 public int flight_log_max() {
776 return Integer.parseInt(flight_log_max_value.getSelectedItem().toString());
779 public void set_flight_log_max_limit(int flight_log_max_limit) {
780 //boolean any_added = false;
781 flight_log_max_value.removeAllItems();
782 for (int i = 0; i < flight_log_max_values.length; i++) {
783 if (Integer.parseInt(flight_log_max_values[i]) < flight_log_max_limit){
784 flight_log_max_value.addItem(flight_log_max_values[i]);
788 flight_log_max_value.addItem(String.format("%d", flight_log_max_limit));
791 public void set_ignite_mode(int new_ignite_mode) {
792 if (new_ignite_mode >= ignite_mode_values.length)
794 if (new_ignite_mode < 0) {
795 ignite_mode_value.setEnabled(false);
798 ignite_mode_value.setEnabled(true);
800 ignite_mode_value.setSelectedIndex(new_ignite_mode);
801 set_ignite_mode_tool_tip();
804 public int ignite_mode() {
805 if (ignite_mode_value.isEnabled())
806 return ignite_mode_value.getSelectedIndex();
812 public void set_pad_orientation(int new_pad_orientation) {
813 if (new_pad_orientation >= pad_orientation_values.length)
814 new_pad_orientation = 0;
815 if (new_pad_orientation < 0) {
816 pad_orientation_value.setEnabled(false);
817 new_pad_orientation = 0;
819 pad_orientation_value.setEnabled(true);
821 pad_orientation_value.setSelectedIndex(new_pad_orientation);
822 set_pad_orientation_tool_tip();
825 public int pad_orientation() {
826 if (pad_orientation_value.isEnabled())
827 return pad_orientation_value.getSelectedIndex();
832 public void set_has_pyro(boolean has_pyro) {
833 pyro.setEnabled(has_pyro);
836 public void set_pyros(AltosPyro[] new_pyros) {
839 pyro_ui.set_pyros(pyros);
842 public AltosPyro[] pyros() {
844 pyros = pyro_ui.get_pyros();