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
36 JLabel main_deploy_label;
37 JLabel apogee_delay_label;
38 JLabel apogee_lockout_label;
39 JLabel frequency_label;
40 JLabel radio_calibration_label;
41 JLabel radio_frequency_label;
42 JLabel radio_enable_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 flight_log_max_value;
61 JComboBox ignite_mode_value;
62 JComboBox pad_orientation_value;
63 JTextField callsign_value;
70 ActionListener listener;
72 static String[] main_deploy_values = {
73 "100", "150", "200", "250", "300", "350",
77 static String[] apogee_delay_values = {
78 "0", "1", "2", "3", "4", "5"
81 static String[] apogee_lockout_values = {
82 "0", "5", "10", "15", "20"
85 static String[] flight_log_max_values = {
86 "64", "128", "192", "256", "320",
87 "384", "448", "512", "576", "640",
88 "704", "768", "832", "896", "960",
91 static String[] ignite_mode_values = {
97 static String[] pad_orientation_values = {
102 /* A window listener to catch closing events and tell the config code */
103 class ConfigListener extends WindowAdapter {
106 public ConfigListener(AltosConfigUI this_ui) {
110 public void windowClosing(WindowEvent e) {
111 ui.actionPerformed(new ActionEvent(e.getSource(),
112 ActionEvent.ACTION_PERFORMED,
117 boolean is_telemini() {
118 String product = product_value.getText();
119 return product != null && product.startsWith("TeleMini");
122 boolean is_telemetrum() {
123 String product = product_value.getText();
124 return product != null && product.startsWith("TeleMetrum");
127 void set_radio_calibration_tool_tip() {
128 if (radio_calibration_value.isEnabled())
129 radio_calibration_value.setToolTipText("Tune radio output to match desired frequency");
131 radio_calibration_value.setToolTipText("Cannot tune radio while connected over packet mode");
134 void set_radio_enable_tool_tip() {
135 if (radio_enable_value.isEnabled())
136 radio_enable_value.setToolTipText("Enable/Disable telemetry and RDF transmissions");
138 radio_enable_value.setToolTipText("Firmware version does not support disabling radio");
141 void set_flight_log_max_tool_tip() {
142 if (flight_log_max_value.isEnabled())
143 flight_log_max_value.setToolTipText("Size reserved for each flight log (in kB)");
146 flight_log_max_value.setToolTipText("Cannot set max value with flight logs in memory");
147 else if (is_telemini())
148 flight_log_max_value.setToolTipText("TeleMini stores only one flight");
150 flight_log_max_value.setToolTipText("Cannot set max flight log value");
154 void set_ignite_mode_tool_tip() {
155 if (ignite_mode_value.isEnabled())
156 ignite_mode_value.setToolTipText("Select when igniters will be fired");
158 ignite_mode_value.setToolTipText("Older firmware could not select ignite mode");
161 void set_pad_orientation_tool_tip() {
162 if (pad_orientation_value.isEnabled())
163 pad_orientation_value.setToolTipText("How will TeleMetrum be mounted in the airframe");
166 pad_orientation_value.setToolTipText("Older TeleMetrum firmware must fly antenna forward");
167 else if (is_telemini())
168 pad_orientation_value.setToolTipText("TeleMini doesn't care how it is mounted");
170 pad_orientation_value.setToolTipText("Can't select orientation");
174 /* Build the UI using a grid bag */
175 public AltosConfigUI(JFrame in_owner, boolean remote) {
176 super (in_owner, "Configure TeleMetrum", false);
179 GridBagConstraints c;
182 Insets il = new Insets(4,4,4,4);
183 Insets ir = new Insets(4,4,4,4);
185 pane = getContentPane();
186 pane.setLayout(new GridBagLayout());
189 c = new GridBagConstraints();
190 c.gridx = 0; c.gridy = row;
192 c.fill = GridBagConstraints.NONE;
193 c.anchor = GridBagConstraints.LINE_START;
195 product_label = new JLabel("Product:");
196 pane.add(product_label, c);
198 c = new GridBagConstraints();
199 c.gridx = 4; c.gridy = row;
201 c.fill = GridBagConstraints.HORIZONTAL;
203 c.anchor = GridBagConstraints.LINE_START;
205 product_value = new JLabel("");
206 pane.add(product_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 version_label = new JLabel("Software version:");
218 pane.add(version_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 version_value = new JLabel("");
229 pane.add(version_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 serial_label = new JLabel("Serial:");
241 pane.add(serial_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 serial_value = new JLabel("");
252 pane.add(serial_value, c);
256 c = new GridBagConstraints();
257 c.gridx = 0; c.gridy = row;
259 c.fill = GridBagConstraints.NONE;
260 c.anchor = GridBagConstraints.LINE_START;
263 main_deploy_label = new JLabel("Main Deploy Altitude(m):");
264 pane.add(main_deploy_label, c);
266 c = new GridBagConstraints();
267 c.gridx = 4; c.gridy = row;
269 c.fill = GridBagConstraints.HORIZONTAL;
271 c.anchor = GridBagConstraints.LINE_START;
274 main_deploy_value = new JComboBox(main_deploy_values);
275 main_deploy_value.setEditable(true);
276 main_deploy_value.addItemListener(this);
277 pane.add(main_deploy_value, c);
278 main_deploy_value.setToolTipText("Height above pad altitude to fire main charge");
282 c = new GridBagConstraints();
283 c.gridx = 0; c.gridy = row;
285 c.fill = GridBagConstraints.NONE;
286 c.anchor = GridBagConstraints.LINE_START;
289 apogee_delay_label = new JLabel("Apogee Delay(s):");
290 pane.add(apogee_delay_label, c);
292 c = new GridBagConstraints();
293 c.gridx = 4; c.gridy = row;
295 c.fill = GridBagConstraints.HORIZONTAL;
297 c.anchor = GridBagConstraints.LINE_START;
300 apogee_delay_value = new JComboBox(apogee_delay_values);
301 apogee_delay_value.setEditable(true);
302 apogee_delay_value.addItemListener(this);
303 pane.add(apogee_delay_value, c);
304 apogee_delay_value.setToolTipText("Delay after apogee before charge fires");
308 c = new GridBagConstraints();
309 c.gridx = 0; c.gridy = row;
311 c.fill = GridBagConstraints.NONE;
312 c.anchor = GridBagConstraints.LINE_START;
315 apogee_lockout_label = new JLabel("Apogee Lockout(s):");
316 pane.add(apogee_lockout_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 apogee_lockout_value = new JComboBox(apogee_lockout_values);
327 apogee_lockout_value.setEditable(true);
328 apogee_lockout_value.addItemListener(this);
329 pane.add(apogee_lockout_value, c);
330 apogee_lockout_value.setToolTipText("Time after boost while apogee detection is locked out");
334 c = new GridBagConstraints();
335 c.gridx = 0; c.gridy = row;
337 c.fill = GridBagConstraints.NONE;
338 c.anchor = GridBagConstraints.LINE_START;
341 radio_frequency_label = new JLabel("Frequency:");
342 pane.add(radio_frequency_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 radio_frequency_value = new AltosFreqList();
353 radio_frequency_value.addItemListener(this);
354 pane.add(radio_frequency_value, c);
355 radio_frequency_value.setToolTipText("Telemetry, RDF and packet frequency");
358 /* Radio Calibration */
359 c = new GridBagConstraints();
360 c.gridx = 0; c.gridy = row;
362 c.fill = GridBagConstraints.NONE;
363 c.anchor = GridBagConstraints.LINE_START;
366 radio_calibration_label = new JLabel("RF Calibration:");
367 pane.add(radio_calibration_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 radio_calibration_value = new JTextField(String.format("%d", 1186611));
378 radio_calibration_value.getDocument().addDocumentListener(this);
380 radio_calibration_value.setEnabled(false);
381 pane.add(radio_calibration_value, c);
382 set_radio_calibration_tool_tip();
386 c = new GridBagConstraints();
387 c.gridx = 0; c.gridy = row;
389 c.fill = GridBagConstraints.NONE;
390 c.anchor = GridBagConstraints.LINE_START;
393 radio_enable_label = new JLabel("Telemetry/RDF Enable:");
394 pane.add(radio_enable_label, c);
396 c = new GridBagConstraints();
397 c.gridx = 4; c.gridy = row;
399 c.fill = GridBagConstraints.HORIZONTAL;
401 c.anchor = GridBagConstraints.LINE_START;
404 radio_enable_value = new JRadioButton("Enabled");
405 radio_enable_value.addItemListener(this);
406 pane.add(radio_enable_value, c);
407 set_radio_enable_tool_tip();
411 c = new GridBagConstraints();
412 c.gridx = 0; c.gridy = row;
414 c.fill = GridBagConstraints.NONE;
415 c.anchor = GridBagConstraints.LINE_START;
418 callsign_label = new JLabel("Callsign:");
419 pane.add(callsign_label, c);
421 c = new GridBagConstraints();
422 c.gridx = 4; c.gridy = row;
424 c.fill = GridBagConstraints.HORIZONTAL;
426 c.anchor = GridBagConstraints.LINE_START;
429 callsign_value = new JTextField(AltosUIPreferences.callsign());
430 callsign_value.getDocument().addDocumentListener(this);
431 pane.add(callsign_value, c);
432 callsign_value.setToolTipText("Callsign reported in telemetry data");
436 c = new GridBagConstraints();
437 c.gridx = 0; c.gridy = row;
439 c.fill = GridBagConstraints.NONE;
440 c.anchor = GridBagConstraints.LINE_START;
443 flight_log_max_label = new JLabel("Maximum Flight Log Size:");
444 pane.add(flight_log_max_label, c);
446 c = new GridBagConstraints();
447 c.gridx = 4; c.gridy = row;
449 c.fill = GridBagConstraints.HORIZONTAL;
451 c.anchor = GridBagConstraints.LINE_START;
454 flight_log_max_value = new JComboBox(flight_log_max_values);
455 flight_log_max_value.setEditable(true);
456 flight_log_max_value.addItemListener(this);
457 pane.add(flight_log_max_value, c);
458 set_flight_log_max_tool_tip();
462 c = new GridBagConstraints();
463 c.gridx = 0; c.gridy = row;
465 c.fill = GridBagConstraints.NONE;
466 c.anchor = GridBagConstraints.LINE_START;
469 ignite_mode_label = new JLabel("Igniter Firing Mode:");
470 pane.add(ignite_mode_label, c);
472 c = new GridBagConstraints();
473 c.gridx = 4; c.gridy = row;
475 c.fill = GridBagConstraints.HORIZONTAL;
477 c.anchor = GridBagConstraints.LINE_START;
480 ignite_mode_value = new JComboBox(ignite_mode_values);
481 ignite_mode_value.setEditable(false);
482 ignite_mode_value.addItemListener(this);
483 pane.add(ignite_mode_value, c);
484 set_ignite_mode_tool_tip();
487 /* Pad orientation */
488 c = new GridBagConstraints();
489 c.gridx = 0; c.gridy = row;
491 c.fill = GridBagConstraints.NONE;
492 c.anchor = GridBagConstraints.LINE_START;
495 pad_orientation_label = new JLabel("Pad Orientation:");
496 pane.add(pad_orientation_label, c);
498 c = new GridBagConstraints();
499 c.gridx = 4; c.gridy = row;
501 c.fill = GridBagConstraints.HORIZONTAL;
503 c.anchor = GridBagConstraints.LINE_START;
506 pad_orientation_value = new JComboBox(pad_orientation_values);
507 pad_orientation_value.setEditable(false);
508 pad_orientation_value.addItemListener(this);
509 pane.add(pad_orientation_value, c);
510 set_pad_orientation_tool_tip();
514 c = new GridBagConstraints();
515 c.gridx = 0; c.gridy = row;
517 c.fill = GridBagConstraints.NONE;
518 c.anchor = GridBagConstraints.LINE_START;
520 save = new JButton("Save");
522 save.addActionListener(this);
523 save.setActionCommand("Save");
525 c = new GridBagConstraints();
526 c.gridx = 2; c.gridy = row;
528 c.fill = GridBagConstraints.NONE;
529 c.anchor = GridBagConstraints.CENTER;
531 reset = new JButton("Reset");
533 reset.addActionListener(this);
534 reset.setActionCommand("Reset");
536 c = new GridBagConstraints();
537 c.gridx = 4; c.gridy = row;
539 c.fill = GridBagConstraints.NONE;
540 c.anchor = GridBagConstraints.CENTER;
542 reboot = new JButton("Reboot");
544 reboot.addActionListener(this);
545 reboot.setActionCommand("Reboot");
547 c = new GridBagConstraints();
548 c.gridx = 6; c.gridy = row;
550 c.fill = GridBagConstraints.NONE;
551 c.anchor = GridBagConstraints.LINE_END;
553 close = new JButton("Close");
555 close.addActionListener(this);
556 close.setActionCommand("Close");
558 addWindowListener(new ConfigListener(this));
561 /* Once the initial values are set, the config code will show the dialog */
562 public void make_visible() {
564 setLocationRelativeTo(owner);
568 /* If any values have been changed, confirm before closing */
569 public boolean check_dirty(String operation) {
571 Object[] options = { String.format("%s anyway", operation), "Keep editing" };
573 i = JOptionPane.showOptionDialog(this,
574 String.format("Configuration modified. %s anyway?", operation),
575 "Configuration Modified",
576 JOptionPane.DEFAULT_OPTION,
577 JOptionPane.WARNING_MESSAGE,
578 null, options, options[1]);
585 /* Listen for events from our buttons */
586 public void actionPerformed(ActionEvent e) {
587 String cmd = e.getActionCommand();
589 if (cmd.equals("Close") || cmd.equals("Reboot"))
590 if (!check_dirty(cmd))
592 listener.actionPerformed(e);
593 if (cmd.equals("Close") || cmd.equals("Reboot")) {
600 /* ItemListener interface method */
601 public void itemStateChanged(ItemEvent e) {
605 /* DocumentListener interface methods */
606 public void changedUpdate(DocumentEvent e) {
610 public void insertUpdate(DocumentEvent e) {
614 public void removeUpdate(DocumentEvent e) {
618 /* Let the config code hook on a listener */
619 public void addActionListener(ActionListener l) {
623 /* set and get all of the dialog values */
624 public void set_product(String product) {
625 radio_frequency_value.set_product(product);
626 product_value.setText(product);
627 set_pad_orientation_tool_tip();
628 set_flight_log_max_tool_tip();
631 public void set_version(String version) {
632 version_value.setText(version);
635 public void set_serial(int serial) {
636 radio_frequency_value.set_serial(serial);
637 serial_value.setText(String.format("%d", serial));
640 public void set_main_deploy(int new_main_deploy) {
641 main_deploy_value.setSelectedItem(Integer.toString(new_main_deploy));
644 public int main_deploy() {
645 return Integer.parseInt(main_deploy_value.getSelectedItem().toString());
648 public void set_apogee_delay(int new_apogee_delay) {
649 apogee_delay_value.setSelectedItem(Integer.toString(new_apogee_delay));
652 public int apogee_delay() {
653 return Integer.parseInt(apogee_delay_value.getSelectedItem().toString());
656 public void set_apogee_lockout(int new_apogee_lockout) {
657 apogee_lockout_value.setSelectedItem(Integer.toString(new_apogee_lockout));
660 public int apogee_lockout() {
661 return Integer.parseInt(apogee_lockout_value.getSelectedItem().toString());
664 public void set_radio_frequency(double new_radio_frequency) {
666 for (i = 0; i < radio_frequency_value.getItemCount(); i++) {
667 AltosFrequency f = (AltosFrequency) radio_frequency_value.getItemAt(i);
669 if (f.close(new_radio_frequency)) {
670 radio_frequency_value.setSelectedIndex(i);
674 for (i = 0; i < radio_frequency_value.getItemCount(); i++) {
675 AltosFrequency f = (AltosFrequency) radio_frequency_value.getItemAt(i);
677 if (new_radio_frequency < f.frequency)
680 String description = String.format("%s serial %s",
681 product_value.getText(),
682 serial_value.getText());
683 AltosFrequency new_frequency = new AltosFrequency(new_radio_frequency, description);
684 AltosUIPreferences.add_common_frequency(new_frequency);
685 radio_frequency_value.insertItemAt(new_frequency, i);
686 radio_frequency_value.setSelectedIndex(i);
689 public double radio_frequency() {
690 return radio_frequency_value.frequency();
693 public void set_radio_calibration(int new_radio_calibration) {
694 radio_calibration_value.setText(String.format("%d", new_radio_calibration));
697 public int radio_calibration() {
698 return Integer.parseInt(radio_calibration_value.getText());
701 public void set_radio_enable(int new_radio_enable) {
702 if (new_radio_enable >= 0) {
703 radio_enable_value.setSelected(new_radio_enable > 0);
704 radio_enable_value.setEnabled(true);
706 radio_enable_value.setSelected(true);
707 radio_enable_value.setEnabled(false);
709 set_radio_enable_tool_tip();
712 public int radio_enable() {
713 if (radio_enable_value.isEnabled())
714 return radio_enable_value.isSelected() ? 1 : 0;
719 public void set_callsign(String new_callsign) {
720 callsign_value.setText(new_callsign);
723 public String callsign() {
724 return callsign_value.getText();
727 public void set_flight_log_max(int new_flight_log_max) {
728 if (new_flight_log_max == 0)
729 flight_log_max_value.setEnabled(false);
730 flight_log_max_value.setSelectedItem(Integer.toString(new_flight_log_max));
731 set_flight_log_max_tool_tip();
734 public void set_flight_log_max_enabled(boolean enable) {
735 flight_log_max_value.setEnabled(enable);
736 set_flight_log_max_tool_tip();
739 public int flight_log_max() {
740 return Integer.parseInt(flight_log_max_value.getSelectedItem().toString());
743 public void set_flight_log_max_limit(int flight_log_max_limit) {
744 //boolean any_added = false;
745 flight_log_max_value.removeAllItems();
746 for (int i = 0; i < flight_log_max_values.length; i++) {
747 if (Integer.parseInt(flight_log_max_values[i]) < flight_log_max_limit){
748 flight_log_max_value.addItem(flight_log_max_values[i]);
752 flight_log_max_value.addItem(String.format("%d", flight_log_max_limit));
755 public void set_ignite_mode(int new_ignite_mode) {
756 if (new_ignite_mode >= ignite_mode_values.length)
758 if (new_ignite_mode < 0) {
759 ignite_mode_value.setEnabled(false);
762 ignite_mode_value.setEnabled(true);
764 ignite_mode_value.setSelectedIndex(new_ignite_mode);
765 set_ignite_mode_tool_tip();
768 public int ignite_mode() {
769 if (ignite_mode_value.isEnabled())
770 return ignite_mode_value.getSelectedIndex();
776 public void set_pad_orientation(int new_pad_orientation) {
777 if (new_pad_orientation >= pad_orientation_values.length)
778 new_pad_orientation = 0;
779 if (new_pad_orientation < 0) {
780 pad_orientation_value.setEnabled(false);
781 new_pad_orientation = 0;
783 pad_orientation_value.setEnabled(true);
785 pad_orientation_value.setSelectedIndex(new_pad_orientation);
786 set_pad_orientation_tool_tip();
789 public int pad_orientation() {
790 if (pad_orientation_value.isEnabled())
791 return pad_orientation_value.getSelectedIndex();
796 public void set_clean() {