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;
31 import org.altusmetrum.AltosLib.*;
35 public class AltosConfigUI
37 implements ActionListener, ItemListener, DocumentListener
45 JLabel main_deploy_label;
46 JLabel apogee_delay_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 AltosFreqList radio_frequency_value;
65 JTextField radio_calibration_value;
66 JRadioButton radio_enable_value;
67 JComboBox flight_log_max_value;
68 JComboBox ignite_mode_value;
69 JComboBox pad_orientation_value;
70 JTextField callsign_value;
77 ActionListener listener;
79 static String[] main_deploy_values = {
80 "100", "150", "200", "250", "300", "350",
84 static String[] apogee_delay_values = {
85 "0", "1", "2", "3", "4", "5"
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;
184 Insets il = new Insets(4,4,4,4);
185 Insets ir = new Insets(4,4,4,4);
187 pane = getContentPane();
188 pane.setLayout(new GridBagLayout());
191 c = new GridBagConstraints();
192 c.gridx = 0; c.gridy = 0;
194 c.fill = GridBagConstraints.NONE;
195 c.anchor = GridBagConstraints.LINE_START;
197 product_label = new JLabel("Product:");
198 pane.add(product_label, c);
200 c = new GridBagConstraints();
201 c.gridx = 4; c.gridy = 0;
203 c.fill = GridBagConstraints.HORIZONTAL;
205 c.anchor = GridBagConstraints.LINE_START;
207 product_value = new JLabel("");
208 pane.add(product_value, c);
211 c = new GridBagConstraints();
212 c.gridx = 0; c.gridy = 1;
214 c.fill = GridBagConstraints.NONE;
215 c.anchor = GridBagConstraints.LINE_START;
218 version_label = new JLabel("Software version:");
219 pane.add(version_label, c);
221 c = new GridBagConstraints();
222 c.gridx = 4; c.gridy = 1;
224 c.fill = GridBagConstraints.HORIZONTAL;
226 c.anchor = GridBagConstraints.LINE_START;
229 version_value = new JLabel("");
230 pane.add(version_value, c);
233 c = new GridBagConstraints();
234 c.gridx = 0; c.gridy = 2;
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 = 2;
246 c.fill = GridBagConstraints.HORIZONTAL;
248 c.anchor = GridBagConstraints.LINE_START;
251 serial_value = new JLabel("");
252 pane.add(serial_value, c);
255 c = new GridBagConstraints();
256 c.gridx = 0; c.gridy = 3;
258 c.fill = GridBagConstraints.NONE;
259 c.anchor = GridBagConstraints.LINE_START;
262 main_deploy_label = new JLabel("Main Deploy Altitude(m):");
263 pane.add(main_deploy_label, c);
265 c = new GridBagConstraints();
266 c.gridx = 4; c.gridy = 3;
268 c.fill = GridBagConstraints.HORIZONTAL;
270 c.anchor = GridBagConstraints.LINE_START;
273 main_deploy_value = new JComboBox(main_deploy_values);
274 main_deploy_value.setEditable(true);
275 main_deploy_value.addItemListener(this);
276 pane.add(main_deploy_value, c);
277 main_deploy_value.setToolTipText("Height above pad altitude to fire main charge");
280 c = new GridBagConstraints();
281 c.gridx = 0; c.gridy = 4;
283 c.fill = GridBagConstraints.NONE;
284 c.anchor = GridBagConstraints.LINE_START;
287 apogee_delay_label = new JLabel("Apogee Delay(s):");
288 pane.add(apogee_delay_label, c);
290 c = new GridBagConstraints();
291 c.gridx = 4; c.gridy = 4;
293 c.fill = GridBagConstraints.HORIZONTAL;
295 c.anchor = GridBagConstraints.LINE_START;
298 apogee_delay_value = new JComboBox(apogee_delay_values);
299 apogee_delay_value.setEditable(true);
300 apogee_delay_value.addItemListener(this);
301 pane.add(apogee_delay_value, c);
302 apogee_delay_value.setToolTipText("Delay after apogee before charge fires");
305 c = new GridBagConstraints();
306 c.gridx = 0; c.gridy = 5;
308 c.fill = GridBagConstraints.NONE;
309 c.anchor = GridBagConstraints.LINE_START;
312 radio_frequency_label = new JLabel("Frequency:");
313 pane.add(radio_frequency_label, c);
315 c = new GridBagConstraints();
316 c.gridx = 4; c.gridy = 5;
318 c.fill = GridBagConstraints.HORIZONTAL;
320 c.anchor = GridBagConstraints.LINE_START;
323 radio_frequency_value = new AltosFreqList();
324 radio_frequency_value.addItemListener(this);
325 pane.add(radio_frequency_value, c);
326 radio_frequency_value.setToolTipText("Telemetry, RDF and packet frequency");
328 /* Radio Calibration */
329 c = new GridBagConstraints();
330 c.gridx = 0; c.gridy = 6;
332 c.fill = GridBagConstraints.NONE;
333 c.anchor = GridBagConstraints.LINE_START;
336 radio_calibration_label = new JLabel("RF Calibration:");
337 pane.add(radio_calibration_label, c);
339 c = new GridBagConstraints();
340 c.gridx = 4; c.gridy = 6;
342 c.fill = GridBagConstraints.HORIZONTAL;
344 c.anchor = GridBagConstraints.LINE_START;
347 radio_calibration_value = new JTextField(String.format("%d", 1186611));
348 radio_calibration_value.getDocument().addDocumentListener(this);
350 radio_calibration_value.setEnabled(false);
351 pane.add(radio_calibration_value, c);
352 set_radio_calibration_tool_tip();
355 c = new GridBagConstraints();
356 c.gridx = 0; c.gridy = 7;
358 c.fill = GridBagConstraints.NONE;
359 c.anchor = GridBagConstraints.LINE_START;
362 radio_enable_label = new JLabel("Telemetry/RDF Enable:");
363 pane.add(radio_enable_label, c);
365 c = new GridBagConstraints();
366 c.gridx = 4; c.gridy = 7;
368 c.fill = GridBagConstraints.HORIZONTAL;
370 c.anchor = GridBagConstraints.LINE_START;
373 radio_enable_value = new JRadioButton("Enabled");
374 radio_enable_value.addItemListener(this);
375 pane.add(radio_enable_value, c);
376 set_radio_enable_tool_tip();
379 c = new GridBagConstraints();
380 c.gridx = 0; c.gridy = 8;
382 c.fill = GridBagConstraints.NONE;
383 c.anchor = GridBagConstraints.LINE_START;
386 callsign_label = new JLabel("Callsign:");
387 pane.add(callsign_label, c);
389 c = new GridBagConstraints();
390 c.gridx = 4; c.gridy = 8;
392 c.fill = GridBagConstraints.HORIZONTAL;
394 c.anchor = GridBagConstraints.LINE_START;
397 callsign_value = new JTextField(AltosUIPreferences.callsign());
398 callsign_value.getDocument().addDocumentListener(this);
399 pane.add(callsign_value, c);
400 callsign_value.setToolTipText("Callsign reported in telemetry data");
403 c = new GridBagConstraints();
404 c.gridx = 0; c.gridy = 9;
406 c.fill = GridBagConstraints.NONE;
407 c.anchor = GridBagConstraints.LINE_START;
410 flight_log_max_label = new JLabel("Maximum Flight Log Size:");
411 pane.add(flight_log_max_label, c);
413 c = new GridBagConstraints();
414 c.gridx = 4; c.gridy = 9;
416 c.fill = GridBagConstraints.HORIZONTAL;
418 c.anchor = GridBagConstraints.LINE_START;
421 flight_log_max_value = new JComboBox(flight_log_max_values);
422 flight_log_max_value.setEditable(true);
423 flight_log_max_value.addItemListener(this);
424 pane.add(flight_log_max_value, c);
425 set_flight_log_max_tool_tip();
428 c = new GridBagConstraints();
429 c.gridx = 0; c.gridy = 10;
431 c.fill = GridBagConstraints.NONE;
432 c.anchor = GridBagConstraints.LINE_START;
435 ignite_mode_label = new JLabel("Igniter Firing Mode:");
436 pane.add(ignite_mode_label, c);
438 c = new GridBagConstraints();
439 c.gridx = 4; c.gridy = 10;
441 c.fill = GridBagConstraints.HORIZONTAL;
443 c.anchor = GridBagConstraints.LINE_START;
446 ignite_mode_value = new JComboBox(ignite_mode_values);
447 ignite_mode_value.setEditable(false);
448 ignite_mode_value.addItemListener(this);
449 pane.add(ignite_mode_value, c);
450 set_ignite_mode_tool_tip();
452 /* Pad orientation */
453 c = new GridBagConstraints();
454 c.gridx = 0; c.gridy = 11;
456 c.fill = GridBagConstraints.NONE;
457 c.anchor = GridBagConstraints.LINE_START;
460 pad_orientation_label = new JLabel("Pad Orientation:");
461 pane.add(pad_orientation_label, c);
463 c = new GridBagConstraints();
464 c.gridx = 4; c.gridy = 11;
466 c.fill = GridBagConstraints.HORIZONTAL;
468 c.anchor = GridBagConstraints.LINE_START;
471 pad_orientation_value = new JComboBox(pad_orientation_values);
472 pad_orientation_value.setEditable(false);
473 pad_orientation_value.addItemListener(this);
474 pane.add(pad_orientation_value, c);
475 set_pad_orientation_tool_tip();
478 c = new GridBagConstraints();
479 c.gridx = 0; c.gridy = 12;
481 c.fill = GridBagConstraints.NONE;
482 c.anchor = GridBagConstraints.LINE_START;
484 save = new JButton("Save");
486 save.addActionListener(this);
487 save.setActionCommand("Save");
489 c = new GridBagConstraints();
490 c.gridx = 2; c.gridy = 12;
492 c.fill = GridBagConstraints.NONE;
493 c.anchor = GridBagConstraints.CENTER;
495 reset = new JButton("Reset");
497 reset.addActionListener(this);
498 reset.setActionCommand("Reset");
500 c = new GridBagConstraints();
501 c.gridx = 4; c.gridy = 12;
503 c.fill = GridBagConstraints.NONE;
504 c.anchor = GridBagConstraints.CENTER;
506 reboot = new JButton("Reboot");
508 reboot.addActionListener(this);
509 reboot.setActionCommand("Reboot");
511 c = new GridBagConstraints();
512 c.gridx = 6; c.gridy = 12;
514 c.fill = GridBagConstraints.NONE;
515 c.anchor = GridBagConstraints.LINE_END;
517 close = new JButton("Close");
519 close.addActionListener(this);
520 close.setActionCommand("Close");
522 addWindowListener(new ConfigListener(this));
525 /* Once the initial values are set, the config code will show the dialog */
526 public void make_visible() {
528 setLocationRelativeTo(owner);
532 /* If any values have been changed, confirm before closing */
533 public boolean check_dirty(String operation) {
535 Object[] options = { String.format("%s anyway", operation), "Keep editing" };
537 i = JOptionPane.showOptionDialog(this,
538 String.format("Configuration modified. %s anyway?", operation),
539 "Configuration Modified",
540 JOptionPane.DEFAULT_OPTION,
541 JOptionPane.WARNING_MESSAGE,
542 null, options, options[1]);
549 /* Listen for events from our buttons */
550 public void actionPerformed(ActionEvent e) {
551 String cmd = e.getActionCommand();
553 if (cmd.equals("Close") || cmd.equals("Reboot"))
554 if (!check_dirty(cmd))
556 listener.actionPerformed(e);
557 if (cmd.equals("Close") || cmd.equals("Reboot")) {
564 /* ItemListener interface method */
565 public void itemStateChanged(ItemEvent e) {
569 /* DocumentListener interface methods */
570 public void changedUpdate(DocumentEvent e) {
574 public void insertUpdate(DocumentEvent e) {
578 public void removeUpdate(DocumentEvent e) {
582 /* Let the config code hook on a listener */
583 public void addActionListener(ActionListener l) {
587 /* set and get all of the dialog values */
588 public void set_product(String product) {
589 radio_frequency_value.set_product(product);
590 product_value.setText(product);
591 set_pad_orientation_tool_tip();
592 set_flight_log_max_tool_tip();
595 public void set_version(String version) {
596 version_value.setText(version);
599 public void set_serial(int serial) {
600 radio_frequency_value.set_serial(serial);
601 serial_value.setText(String.format("%d", serial));
604 public void set_main_deploy(int new_main_deploy) {
605 main_deploy_value.setSelectedItem(Integer.toString(new_main_deploy));
608 public int main_deploy() {
609 return Integer.parseInt(main_deploy_value.getSelectedItem().toString());
612 public void set_apogee_delay(int new_apogee_delay) {
613 apogee_delay_value.setSelectedItem(Integer.toString(new_apogee_delay));
616 public int apogee_delay() {
617 return Integer.parseInt(apogee_delay_value.getSelectedItem().toString());
620 public void set_radio_frequency(double new_radio_frequency) {
622 for (i = 0; i < radio_frequency_value.getItemCount(); i++) {
623 AltosFrequency f = (AltosFrequency) radio_frequency_value.getItemAt(i);
625 if (f.close(new_radio_frequency)) {
626 radio_frequency_value.setSelectedIndex(i);
630 for (i = 0; i < radio_frequency_value.getItemCount(); i++) {
631 AltosFrequency f = (AltosFrequency) radio_frequency_value.getItemAt(i);
633 if (new_radio_frequency < f.frequency)
636 String description = String.format("%s serial %s",
637 product_value.getText(),
638 serial_value.getText());
639 AltosFrequency new_frequency = new AltosFrequency(new_radio_frequency, description);
640 AltosUIPreferences.add_common_frequency(new_frequency);
641 radio_frequency_value.insertItemAt(new_frequency, i);
644 public double radio_frequency() {
645 return radio_frequency_value.frequency();
648 public void set_radio_calibration(int new_radio_calibration) {
649 radio_calibration_value.setText(String.format("%d", new_radio_calibration));
652 public int radio_calibration() {
653 return Integer.parseInt(radio_calibration_value.getText());
656 public void set_radio_enable(int new_radio_enable) {
657 if (new_radio_enable >= 0) {
658 radio_enable_value.setSelected(new_radio_enable > 0);
659 radio_enable_value.setEnabled(true);
661 radio_enable_value.setSelected(true);
662 radio_enable_value.setEnabled(false);
664 set_radio_enable_tool_tip();
667 public int radio_enable() {
668 if (radio_enable_value.isEnabled())
669 return radio_enable_value.isSelected() ? 1 : 0;
674 public void set_callsign(String new_callsign) {
675 callsign_value.setText(new_callsign);
678 public String callsign() {
679 return callsign_value.getText();
682 public void set_flight_log_max(int new_flight_log_max) {
683 if (new_flight_log_max == 0)
684 flight_log_max_value.setEnabled(false);
685 flight_log_max_value.setSelectedItem(Integer.toString(new_flight_log_max));
686 set_flight_log_max_tool_tip();
689 public void set_flight_log_max_enabled(boolean enable) {
690 flight_log_max_value.setEnabled(enable);
691 set_flight_log_max_tool_tip();
694 public int flight_log_max() {
695 return Integer.parseInt(flight_log_max_value.getSelectedItem().toString());
698 public void set_flight_log_max_limit(int flight_log_max_limit) {
699 boolean any_added = false;
700 flight_log_max_value.removeAllItems();
701 for (int i = 0; i < flight_log_max_values.length; i++) {
702 if (Integer.parseInt(flight_log_max_values[i]) < flight_log_max_limit){
703 flight_log_max_value.addItem(flight_log_max_values[i]);
707 flight_log_max_value.addItem(String.format("%d", flight_log_max_limit));
710 public void set_ignite_mode(int new_ignite_mode) {
711 if (new_ignite_mode < 0) {
712 ignite_mode_value.setEnabled(false);
715 ignite_mode_value.setEnabled(true);
717 ignite_mode_value.setSelectedIndex(new_ignite_mode);
718 set_ignite_mode_tool_tip();
721 public int ignite_mode() {
722 if (ignite_mode_value.isEnabled())
723 return ignite_mode_value.getSelectedIndex();
729 public void set_pad_orientation(int new_pad_orientation) {
730 if (new_pad_orientation < 0) {
731 pad_orientation_value.setEnabled(false);
732 new_pad_orientation = 0;
734 pad_orientation_value.setEnabled(true);
736 pad_orientation_value.setSelectedIndex(new_pad_orientation);
737 set_pad_orientation_tool_tip();
740 public int pad_orientation() {
741 if (pad_orientation_value.isEnabled())
742 return pad_orientation_value.getSelectedIndex();
747 public void set_clean() {