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.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import javax.swing.event.*;
29 import java.util.prefs.*;
30 import java.util.concurrent.LinkedBlockingQueue;
34 public class AltosConfigUI
36 implements ActionListener, ItemListener, DocumentListener
44 JLabel main_deploy_label;
45 JLabel apogee_delay_label;
46 JLabel apogee_lockout_label;
47 JLabel frequency_label;
48 JLabel radio_calibration_label;
49 JLabel radio_frequency_label;
50 JLabel radio_enable_label;
51 JLabel flight_log_max_label;
52 JLabel ignite_mode_label;
53 JLabel pad_orientation_label;
54 JLabel callsign_label;
62 JComboBox main_deploy_value;
63 JComboBox apogee_delay_value;
64 JComboBox apogee_lockout_value;
65 AltosFreqList radio_frequency_value;
66 JTextField radio_calibration_value;
67 JRadioButton radio_enable_value;
68 JComboBox flight_log_max_value;
69 JComboBox ignite_mode_value;
70 JComboBox pad_orientation_value;
71 JTextField callsign_value;
78 ActionListener listener;
80 static String[] main_deploy_values = {
81 "100", "150", "200", "250", "300", "350",
85 static String[] apogee_delay_values = {
86 "0", "1", "2", "3", "4", "5"
89 static String[] apogee_lockout_values = {
90 "0", "5", "10", "15", "20"
93 static String[] flight_log_max_values = {
94 "64", "128", "192", "256", "320",
95 "384", "448", "512", "576", "640",
96 "704", "768", "832", "896", "960",
99 static String[] ignite_mode_values = {
105 static String[] pad_orientation_values = {
110 /* A window listener to catch closing events and tell the config code */
111 class ConfigListener extends WindowAdapter {
114 public ConfigListener(AltosConfigUI this_ui) {
118 public void windowClosing(WindowEvent e) {
119 ui.actionPerformed(new ActionEvent(e.getSource(),
120 ActionEvent.ACTION_PERFORMED,
125 boolean is_telemini() {
126 String product = product_value.getText();
127 return product != null && product.startsWith("TeleMini");
130 boolean is_telemetrum() {
131 String product = product_value.getText();
132 return product != null && product.startsWith("TeleMetrum");
135 void set_radio_calibration_tool_tip() {
136 if (radio_calibration_value.isEnabled())
137 radio_calibration_value.setToolTipText("Tune radio output to match desired frequency");
139 radio_calibration_value.setToolTipText("Cannot tune radio while connected over packet mode");
142 void set_radio_enable_tool_tip() {
143 if (radio_enable_value.isEnabled())
144 radio_enable_value.setToolTipText("Enable/Disable telemetry and RDF transmissions");
146 radio_enable_value.setToolTipText("Firmware version does not support disabling radio");
149 void set_flight_log_max_tool_tip() {
150 if (flight_log_max_value.isEnabled())
151 flight_log_max_value.setToolTipText("Size reserved for each flight log (in kB)");
154 flight_log_max_value.setToolTipText("Cannot set max value with flight logs in memory");
155 else if (is_telemini())
156 flight_log_max_value.setToolTipText("TeleMini stores only one flight");
158 flight_log_max_value.setToolTipText("Cannot set max flight log value");
162 void set_ignite_mode_tool_tip() {
163 if (ignite_mode_value.isEnabled())
164 ignite_mode_value.setToolTipText("Select when igniters will be fired");
166 ignite_mode_value.setToolTipText("Older firmware could not select ignite mode");
169 void set_pad_orientation_tool_tip() {
170 if (pad_orientation_value.isEnabled())
171 pad_orientation_value.setToolTipText("How will TeleMetrum be mounted in the airframe");
174 pad_orientation_value.setToolTipText("Older TeleMetrum firmware must fly antenna forward");
175 else if (is_telemini())
176 pad_orientation_value.setToolTipText("TeleMini doesn't care how it is mounted");
178 pad_orientation_value.setToolTipText("Can't select orientation");
182 /* Build the UI using a grid bag */
183 public AltosConfigUI(JFrame in_owner, boolean remote) {
184 super (in_owner, "Configure TeleMetrum", false);
187 GridBagConstraints c;
190 Insets il = new Insets(4,4,4,4);
191 Insets ir = new Insets(4,4,4,4);
193 pane = getContentPane();
194 pane.setLayout(new GridBagLayout());
197 c = new GridBagConstraints();
198 c.gridx = 0; c.gridy = row;
200 c.fill = GridBagConstraints.NONE;
201 c.anchor = GridBagConstraints.LINE_START;
203 product_label = new JLabel("Product:");
204 pane.add(product_label, c);
206 c = new GridBagConstraints();
207 c.gridx = 4; c.gridy = row;
209 c.fill = GridBagConstraints.HORIZONTAL;
211 c.anchor = GridBagConstraints.LINE_START;
213 product_value = new JLabel("");
214 pane.add(product_value, c);
218 c = new GridBagConstraints();
219 c.gridx = 0; c.gridy = row;
221 c.fill = GridBagConstraints.NONE;
222 c.anchor = GridBagConstraints.LINE_START;
225 version_label = new JLabel("Software version:");
226 pane.add(version_label, c);
228 c = new GridBagConstraints();
229 c.gridx = 4; c.gridy = row;
231 c.fill = GridBagConstraints.HORIZONTAL;
233 c.anchor = GridBagConstraints.LINE_START;
236 version_value = new JLabel("");
237 pane.add(version_value, c);
241 c = new GridBagConstraints();
242 c.gridx = 0; c.gridy = row;
244 c.fill = GridBagConstraints.NONE;
245 c.anchor = GridBagConstraints.LINE_START;
248 serial_label = new JLabel("Serial:");
249 pane.add(serial_label, c);
251 c = new GridBagConstraints();
252 c.gridx = 4; c.gridy = row;
254 c.fill = GridBagConstraints.HORIZONTAL;
256 c.anchor = GridBagConstraints.LINE_START;
259 serial_value = new JLabel("");
260 pane.add(serial_value, c);
264 c = new GridBagConstraints();
265 c.gridx = 0; c.gridy = row;
267 c.fill = GridBagConstraints.NONE;
268 c.anchor = GridBagConstraints.LINE_START;
271 main_deploy_label = new JLabel("Main Deploy Altitude(m):");
272 pane.add(main_deploy_label, c);
274 c = new GridBagConstraints();
275 c.gridx = 4; c.gridy = row;
277 c.fill = GridBagConstraints.HORIZONTAL;
279 c.anchor = GridBagConstraints.LINE_START;
282 main_deploy_value = new JComboBox(main_deploy_values);
283 main_deploy_value.setEditable(true);
284 main_deploy_value.addItemListener(this);
285 pane.add(main_deploy_value, c);
286 main_deploy_value.setToolTipText("Height above pad altitude to fire main charge");
290 c = new GridBagConstraints();
291 c.gridx = 0; c.gridy = row;
293 c.fill = GridBagConstraints.NONE;
294 c.anchor = GridBagConstraints.LINE_START;
297 apogee_delay_label = new JLabel("Apogee Delay(s):");
298 pane.add(apogee_delay_label, c);
300 c = new GridBagConstraints();
301 c.gridx = 4; c.gridy = row;
303 c.fill = GridBagConstraints.HORIZONTAL;
305 c.anchor = GridBagConstraints.LINE_START;
308 apogee_delay_value = new JComboBox(apogee_delay_values);
309 apogee_delay_value.setEditable(true);
310 apogee_delay_value.addItemListener(this);
311 pane.add(apogee_delay_value, c);
312 apogee_delay_value.setToolTipText("Delay after apogee before charge fires");
316 c = new GridBagConstraints();
317 c.gridx = 0; c.gridy = row;
319 c.fill = GridBagConstraints.NONE;
320 c.anchor = GridBagConstraints.LINE_START;
323 apogee_lockout_label = new JLabel("Apogee Lockout(s):");
324 pane.add(apogee_lockout_label, c);
326 c = new GridBagConstraints();
327 c.gridx = 4; c.gridy = row;
329 c.fill = GridBagConstraints.HORIZONTAL;
331 c.anchor = GridBagConstraints.LINE_START;
334 apogee_lockout_value = new JComboBox(apogee_lockout_values);
335 apogee_lockout_value.setEditable(true);
336 apogee_lockout_value.addItemListener(this);
337 pane.add(apogee_lockout_value, c);
338 apogee_lockout_value.setToolTipText("Time after boost while apogee detection is locked out");
342 c = new GridBagConstraints();
343 c.gridx = 0; c.gridy = row;
345 c.fill = GridBagConstraints.NONE;
346 c.anchor = GridBagConstraints.LINE_START;
349 radio_frequency_label = new JLabel("Frequency:");
350 pane.add(radio_frequency_label, c);
352 c = new GridBagConstraints();
353 c.gridx = 4; c.gridy = row;
355 c.fill = GridBagConstraints.HORIZONTAL;
357 c.anchor = GridBagConstraints.LINE_START;
360 radio_frequency_value = new AltosFreqList();
361 radio_frequency_value.addItemListener(this);
362 pane.add(radio_frequency_value, c);
363 radio_frequency_value.setToolTipText("Telemetry, RDF and packet frequency");
366 /* Radio Calibration */
367 c = new GridBagConstraints();
368 c.gridx = 0; c.gridy = row;
370 c.fill = GridBagConstraints.NONE;
371 c.anchor = GridBagConstraints.LINE_START;
374 radio_calibration_label = new JLabel("RF Calibration:");
375 pane.add(radio_calibration_label, c);
377 c = new GridBagConstraints();
378 c.gridx = 4; c.gridy = row;
380 c.fill = GridBagConstraints.HORIZONTAL;
382 c.anchor = GridBagConstraints.LINE_START;
385 radio_calibration_value = new JTextField(String.format("%d", 1186611));
386 radio_calibration_value.getDocument().addDocumentListener(this);
388 radio_calibration_value.setEnabled(false);
389 pane.add(radio_calibration_value, c);
390 set_radio_calibration_tool_tip();
394 c = new GridBagConstraints();
395 c.gridx = 0; c.gridy = row;
397 c.fill = GridBagConstraints.NONE;
398 c.anchor = GridBagConstraints.LINE_START;
401 radio_enable_label = new JLabel("Telemetry/RDF Enable:");
402 pane.add(radio_enable_label, c);
404 c = new GridBagConstraints();
405 c.gridx = 4; c.gridy = row;
407 c.fill = GridBagConstraints.HORIZONTAL;
409 c.anchor = GridBagConstraints.LINE_START;
412 radio_enable_value = new JRadioButton("Enabled");
413 radio_enable_value.addItemListener(this);
414 pane.add(radio_enable_value, c);
415 set_radio_enable_tool_tip();
419 c = new GridBagConstraints();
420 c.gridx = 0; c.gridy = row;
422 c.fill = GridBagConstraints.NONE;
423 c.anchor = GridBagConstraints.LINE_START;
426 callsign_label = new JLabel("Callsign:");
427 pane.add(callsign_label, c);
429 c = new GridBagConstraints();
430 c.gridx = 4; c.gridy = row;
432 c.fill = GridBagConstraints.HORIZONTAL;
434 c.anchor = GridBagConstraints.LINE_START;
437 callsign_value = new JTextField(AltosPreferences.callsign());
438 callsign_value.getDocument().addDocumentListener(this);
439 pane.add(callsign_value, c);
440 callsign_value.setToolTipText("Callsign reported in telemetry data");
444 c = new GridBagConstraints();
445 c.gridx = 0; c.gridy = row;
447 c.fill = GridBagConstraints.NONE;
448 c.anchor = GridBagConstraints.LINE_START;
451 flight_log_max_label = new JLabel("Maximum Flight Log Size:");
452 pane.add(flight_log_max_label, c);
454 c = new GridBagConstraints();
455 c.gridx = 4; c.gridy = row;
457 c.fill = GridBagConstraints.HORIZONTAL;
459 c.anchor = GridBagConstraints.LINE_START;
462 flight_log_max_value = new JComboBox(flight_log_max_values);
463 flight_log_max_value.setEditable(true);
464 flight_log_max_value.addItemListener(this);
465 pane.add(flight_log_max_value, c);
466 set_flight_log_max_tool_tip();
470 c = new GridBagConstraints();
471 c.gridx = 0; c.gridy = row;
473 c.fill = GridBagConstraints.NONE;
474 c.anchor = GridBagConstraints.LINE_START;
477 ignite_mode_label = new JLabel("Igniter Firing Mode:");
478 pane.add(ignite_mode_label, c);
480 c = new GridBagConstraints();
481 c.gridx = 4; c.gridy = row;
483 c.fill = GridBagConstraints.HORIZONTAL;
485 c.anchor = GridBagConstraints.LINE_START;
488 ignite_mode_value = new JComboBox(ignite_mode_values);
489 ignite_mode_value.setEditable(false);
490 ignite_mode_value.addItemListener(this);
491 pane.add(ignite_mode_value, c);
492 set_ignite_mode_tool_tip();
495 /* Pad orientation */
496 c = new GridBagConstraints();
497 c.gridx = 0; c.gridy = row;
499 c.fill = GridBagConstraints.NONE;
500 c.anchor = GridBagConstraints.LINE_START;
503 pad_orientation_label = new JLabel("Pad Orientation:");
504 pane.add(pad_orientation_label, c);
506 c = new GridBagConstraints();
507 c.gridx = 4; c.gridy = row;
509 c.fill = GridBagConstraints.HORIZONTAL;
511 c.anchor = GridBagConstraints.LINE_START;
514 pad_orientation_value = new JComboBox(pad_orientation_values);
515 pad_orientation_value.setEditable(false);
516 pad_orientation_value.addItemListener(this);
517 pane.add(pad_orientation_value, c);
518 set_pad_orientation_tool_tip();
522 c = new GridBagConstraints();
523 c.gridx = 0; c.gridy = row;
525 c.fill = GridBagConstraints.NONE;
526 c.anchor = GridBagConstraints.LINE_START;
528 save = new JButton("Save");
530 save.addActionListener(this);
531 save.setActionCommand("Save");
533 c = new GridBagConstraints();
534 c.gridx = 2; c.gridy = row;
536 c.fill = GridBagConstraints.NONE;
537 c.anchor = GridBagConstraints.CENTER;
539 reset = new JButton("Reset");
541 reset.addActionListener(this);
542 reset.setActionCommand("Reset");
544 c = new GridBagConstraints();
545 c.gridx = 4; c.gridy = row;
547 c.fill = GridBagConstraints.NONE;
548 c.anchor = GridBagConstraints.CENTER;
550 reboot = new JButton("Reboot");
552 reboot.addActionListener(this);
553 reboot.setActionCommand("Reboot");
555 c = new GridBagConstraints();
556 c.gridx = 6; c.gridy = row;
558 c.fill = GridBagConstraints.NONE;
559 c.anchor = GridBagConstraints.LINE_END;
561 close = new JButton("Close");
563 close.addActionListener(this);
564 close.setActionCommand("Close");
566 addWindowListener(new ConfigListener(this));
569 /* Once the initial values are set, the config code will show the dialog */
570 public void make_visible() {
572 setLocationRelativeTo(owner);
576 /* If any values have been changed, confirm before closing */
577 public boolean check_dirty(String operation) {
579 Object[] options = { String.format("%s anyway", operation), "Keep editing" };
581 i = JOptionPane.showOptionDialog(this,
582 String.format("Configuration modified. %s anyway?", operation),
583 "Configuration Modified",
584 JOptionPane.DEFAULT_OPTION,
585 JOptionPane.WARNING_MESSAGE,
586 null, options, options[1]);
593 /* Listen for events from our buttons */
594 public void actionPerformed(ActionEvent e) {
595 String cmd = e.getActionCommand();
597 if (cmd.equals("Close") || cmd.equals("Reboot"))
598 if (!check_dirty(cmd))
600 listener.actionPerformed(e);
601 if (cmd.equals("Close") || cmd.equals("Reboot")) {
608 /* ItemListener interface method */
609 public void itemStateChanged(ItemEvent e) {
613 /* DocumentListener interface methods */
614 public void changedUpdate(DocumentEvent e) {
618 public void insertUpdate(DocumentEvent e) {
622 public void removeUpdate(DocumentEvent e) {
626 /* Let the config code hook on a listener */
627 public void addActionListener(ActionListener l) {
631 /* set and get all of the dialog values */
632 public void set_product(String product) {
633 radio_frequency_value.set_product(product);
634 product_value.setText(product);
635 set_pad_orientation_tool_tip();
636 set_flight_log_max_tool_tip();
639 public void set_version(String version) {
640 version_value.setText(version);
643 public void set_serial(int serial) {
644 radio_frequency_value.set_serial(serial);
645 serial_value.setText(String.format("%d", serial));
648 public void set_main_deploy(int new_main_deploy) {
649 main_deploy_value.setSelectedItem(Integer.toString(new_main_deploy));
652 public int main_deploy() {
653 return Integer.parseInt(main_deploy_value.getSelectedItem().toString());
656 public void set_apogee_delay(int new_apogee_delay) {
657 apogee_delay_value.setSelectedItem(Integer.toString(new_apogee_delay));
660 public int apogee_delay() {
661 return Integer.parseInt(apogee_delay_value.getSelectedItem().toString());
664 public void set_apogee_lockout(int new_apogee_lockout) {
665 apogee_lockout_value.setSelectedItem(Integer.toString(new_apogee_lockout));
668 public int apogee_lockout() {
669 return Integer.parseInt(apogee_lockout_value.getSelectedItem().toString());
672 public void set_radio_frequency(double new_radio_frequency) {
674 for (i = 0; i < radio_frequency_value.getItemCount(); i++) {
675 AltosFrequency f = (AltosFrequency) radio_frequency_value.getItemAt(i);
677 if (f.close(new_radio_frequency)) {
678 radio_frequency_value.setSelectedIndex(i);
682 for (i = 0; i < radio_frequency_value.getItemCount(); i++) {
683 AltosFrequency f = (AltosFrequency) radio_frequency_value.getItemAt(i);
685 if (new_radio_frequency < f.frequency)
688 String description = String.format("%s serial %s",
689 product_value.getText(),
690 serial_value.getText());
691 AltosFrequency new_frequency = new AltosFrequency(new_radio_frequency, description);
692 AltosPreferences.add_common_frequency(new_frequency);
693 radio_frequency_value.insertItemAt(new_frequency, i);
694 radio_frequency_value.setSelectedIndex(i);
697 public double radio_frequency() {
698 return radio_frequency_value.frequency();
701 public void set_radio_calibration(int new_radio_calibration) {
702 radio_calibration_value.setText(String.format("%d", new_radio_calibration));
705 public int radio_calibration() {
706 return Integer.parseInt(radio_calibration_value.getText());
709 public void set_radio_enable(int new_radio_enable) {
710 if (new_radio_enable >= 0) {
711 radio_enable_value.setSelected(new_radio_enable > 0);
712 radio_enable_value.setEnabled(true);
714 radio_enable_value.setSelected(true);
715 radio_enable_value.setEnabled(false);
717 set_radio_enable_tool_tip();
720 public int radio_enable() {
721 if (radio_enable_value.isEnabled())
722 return radio_enable_value.isSelected() ? 1 : 0;
727 public void set_callsign(String new_callsign) {
728 callsign_value.setText(new_callsign);
731 public String callsign() {
732 return callsign_value.getText();
735 public void set_flight_log_max(int new_flight_log_max) {
736 if (new_flight_log_max == 0)
737 flight_log_max_value.setEnabled(false);
738 flight_log_max_value.setSelectedItem(Integer.toString(new_flight_log_max));
739 set_flight_log_max_tool_tip();
742 public void set_flight_log_max_enabled(boolean enable) {
743 flight_log_max_value.setEnabled(enable);
744 set_flight_log_max_tool_tip();
747 public int flight_log_max() {
748 return Integer.parseInt(flight_log_max_value.getSelectedItem().toString());
751 public void set_flight_log_max_limit(int flight_log_max_limit) {
752 boolean any_added = false;
753 flight_log_max_value.removeAllItems();
754 for (int i = 0; i < flight_log_max_values.length; i++) {
755 if (Integer.parseInt(flight_log_max_values[i]) < flight_log_max_limit){
756 flight_log_max_value.addItem(flight_log_max_values[i]);
760 flight_log_max_value.addItem(String.format("%d", flight_log_max_limit));
763 public void set_ignite_mode(int new_ignite_mode) {
764 if (new_ignite_mode >= ignite_mode_values.length)
766 if (new_ignite_mode < 0) {
767 ignite_mode_value.setEnabled(false);
770 ignite_mode_value.setEnabled(true);
772 ignite_mode_value.setSelectedIndex(new_ignite_mode);
773 set_ignite_mode_tool_tip();
776 public int ignite_mode() {
777 if (ignite_mode_value.isEnabled())
778 return ignite_mode_value.getSelectedIndex();
784 public void set_pad_orientation(int new_pad_orientation) {
785 if (new_pad_orientation >= pad_orientation_values.length)
786 new_pad_orientation = 0;
787 if (new_pad_orientation < 0) {
788 pad_orientation_value.setEnabled(false);
789 new_pad_orientation = 0;
791 pad_orientation_value.setEnabled(true);
793 pad_orientation_value.setSelectedIndex(new_pad_orientation);
794 set_pad_orientation_tool_tip();
797 public int pad_orientation() {
798 if (pad_orientation_value.isEnabled())
799 return pad_orientation_value.getSelectedIndex();
804 public void set_clean() {