2 * Copyright © 2011 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.
18 package org.altusmetrum.altosuilib_6;
21 import java.awt.event.*;
23 import javax.swing.event.*;
27 import java.util.concurrent.*;
28 import org.altusmetrum.altoslib_6.*;
30 class AltosScanResult {
34 AltosFrequency frequency;
38 boolean interrupted = false;
40 public String toString() {
41 return String.format("%-9.9s serial %-4d flight %-4d (%s %s %d)",
42 callsign, serial, flight,
43 frequency.toShortString(),
44 AltosLib.telemetry_name(telemetry),
45 AltosLib.ao_telemetry_rate_values[rate]);
48 public String toShortString() {
49 return String.format("%s %d %d %7.3f %d %d",
50 callsign, serial, flight, frequency, telemetry, rate);
53 public AltosScanResult(String in_callsign, int in_serial,
54 int in_flight, AltosFrequency in_frequency,
57 callsign = in_callsign;
60 frequency = in_frequency;
61 telemetry = in_telemetry;
65 public boolean equals(AltosScanResult other) {
66 return (serial == other.serial &&
67 frequency.frequency == other.frequency.frequency &&
68 telemetry == other.telemetry &&
72 public boolean up_to_date(AltosScanResult other) {
73 if (flight == 0 && other.flight != 0) {
74 flight = other.flight;
77 if (callsign.equals("N0CALL") && !other.callsign.equals("N0CALL")) {
78 callsign = other.callsign;
85 class AltosScanResults extends LinkedList<AltosScanResult> implements ListModel<AltosScanResult> {
87 LinkedList<ListDataListener> listeners = new LinkedList<ListDataListener>();
89 void changed(ListDataEvent de) {
90 for (ListDataListener l : listeners)
91 l.contentsChanged(de);
94 public boolean add(AltosScanResult r) {
96 for (AltosScanResult old : this) {
98 if (!old.up_to_date(r))
99 changed (new ListDataEvent(this,
100 ListDataEvent.CONTENTS_CHANGED,
108 changed(new ListDataEvent(this,
109 ListDataEvent.INTERVAL_ADDED,
110 this.size() - 2, this.size() - 1));
114 public void addListDataListener(ListDataListener l) {
118 public void removeListDataListener(ListDataListener l) {
122 public AltosScanResult getElementAt(int i) {
126 public int getSize() {
131 public class AltosScanUI
132 extends AltosUIDialog
133 implements ActionListener
137 AltosConfigData config_data;
138 AltosTelemetryReader reader;
139 private JList<AltosScanResult> list;
140 private JLabel scanning_label;
141 private JLabel frequency_label;
142 private JLabel telemetry_label;
143 private JLabel rate_label;
144 private JButton cancel_button;
145 private JButton monitor_button;
146 private JCheckBox[] telemetry_boxes;
147 private JCheckBox[] rate_boxes;
148 javax.swing.Timer timer;
149 AltosScanResults results = new AltosScanResults();
152 boolean select_telemetry = false;
155 boolean select_rate = false;
157 final static int timeout = 1200;
158 TelemetryHandler handler;
160 AltosFrequency[] frequencies;
165 void scan_exception(Exception e) {
166 if (e instanceof FileNotFoundException) {
167 JOptionPane.showMessageDialog(owner,
168 ((FileNotFoundException) e).getMessage(),
169 "Cannot open target device",
170 JOptionPane.ERROR_MESSAGE);
171 } else if (e instanceof AltosSerialInUseException) {
172 JOptionPane.showMessageDialog(owner,
173 String.format("Device \"%s\" already in use",
174 device.toShortString()),
176 JOptionPane.ERROR_MESSAGE);
177 } else if (e instanceof IOException) {
178 IOException ee = (IOException) e;
179 JOptionPane.showMessageDialog(owner,
180 device.toShortString(),
181 ee.getLocalizedMessage(),
182 JOptionPane.ERROR_MESSAGE);
184 JOptionPane.showMessageDialog(owner,
185 String.format("Connection to \"%s\" failed",
186 device.toShortString()),
188 JOptionPane.ERROR_MESSAGE);
193 class TelemetryHandler implements Runnable {
197 boolean interrupted = false;
202 AltosState state = reader.read();
206 if (state.flight != AltosLib.MISSING) {
207 final AltosScanResult result = new AltosScanResult(state.callsign,
210 frequencies[frequency_index],
213 Runnable r = new Runnable() {
218 SwingUtilities.invokeLater(r);
220 } catch (ParseException pp) {
221 } catch (AltosCRCException ce) {
224 } catch (InterruptedException ee) {
226 } catch (IOException ie) {
228 reader.close(interrupted);
234 frequency_label.setText(String.format("Frequency: %s", frequencies[frequency_index].toString()));
235 if (select_telemetry)
236 telemetry_label.setText(String.format("Telemetry: %s", AltosLib.telemetry_name(telemetry)));
238 rate_label.setText(String.format("Rate: %d baud", AltosLib.ao_telemetry_rate_values[rate]));
241 void set_telemetry() {
242 reader.set_telemetry(telemetry);
246 reader.set_telemetry_rate(rate);
249 void set_frequency() throws InterruptedException, TimeoutException {
250 reader.set_frequency(frequencies[frequency_index].frequency);
254 void next() throws InterruptedException, TimeoutException {
255 reader.set_monitor(false);
258 boolean wrapped = false;
261 if (rate > AltosLib.ao_telemetry_rate_max) {
265 } while (!rate_boxes[rate].isSelected());
272 if (select_telemetry) {
273 boolean wrapped = false;
276 if (telemetry > AltosLib.ao_telemetry_max) {
278 telemetry = AltosLib.ao_telemetry_min;
280 } while (!telemetry_boxes[telemetry - AltosLib.ao_telemetry_min].isSelected());
290 if (frequency_index >= frequencies.length)
294 reader.set_monitor(true);
299 if (thread != null && thread.isAlive()) {
303 } catch (InterruptedException ie) {}
312 void tick_timer() throws InterruptedException, TimeoutException {
314 if (packet_count == 0 || tick_count > 5)
318 public void actionPerformed(ActionEvent e) {
319 String cmd = e.getActionCommand();
322 if (cmd.equals("cancel"))
325 if (cmd.equals("tick"))
328 if (cmd.equals("telemetry")) {
330 int scanning_telemetry = 0;
331 for (k = AltosLib.ao_telemetry_min; k <= AltosLib.ao_telemetry_max; k++) {
332 int j = k - AltosLib.ao_telemetry_min;
333 if (telemetry_boxes[j].isSelected())
334 scanning_telemetry |= (1 << k);
336 if (scanning_telemetry == 0) {
337 scanning_telemetry |= (1 << AltosLib.ao_telemetry_standard);
338 telemetry_boxes[AltosLib.ao_telemetry_standard - AltosLib.ao_telemetry_min].setSelected(true);
340 AltosUIPreferences.set_scanning_telemetry(scanning_telemetry);
343 if (cmd.equals("rate")) {
345 int scanning_rate = 0;
346 for (k = 0; k <= AltosLib.ao_telemetry_rate_max; k++) {
347 if (rate_boxes[k].isSelected())
348 scanning_rate |= (1 << k);
350 if (scanning_rate == 0) {
351 scanning_rate = (1 << 0);
352 rate_boxes[0].setSelected(true);
354 AltosUIPreferences.set_scanning_telemetry_rate(scanning_rate);
357 if (cmd.equals("monitor")) {
359 AltosScanResult r = (AltosScanResult) (list.getSelectedValue());
361 if (device != null) {
362 if (reader != null) {
363 reader.set_telemetry(r.telemetry);
364 reader.set_telemetry_rate(r.rate);
365 reader.set_frequency(r.frequency.frequency);
366 reader.save_frequency();
367 reader.save_telemetry();
368 reader.save_telemetry_rate();
369 owner.scan_device_selected(device);
374 } catch (TimeoutException te) {
376 } catch (InterruptedException ie) {
381 /* A window listener to catch closing events and tell the config code */
382 class ConfigListener extends WindowAdapter {
385 public ConfigListener(AltosScanUI this_ui) {
389 public void windowClosing(WindowEvent e) {
390 ui.actionPerformed(new ActionEvent(e.getSource(),
391 ActionEvent.ACTION_PERFORMED,
396 private boolean open() {
397 device = AltosDeviceUIDialog.show(owner, AltosLib.product_basestation);
401 reader = new AltosTelemetryReader(new AltosSerial(device));
407 } catch (InterruptedException ie) {
410 handler = new TelemetryHandler();
411 thread = new Thread(handler);
414 } catch (FileNotFoundException ee) {
415 JOptionPane.showMessageDialog(owner,
417 "Cannot open target device",
418 JOptionPane.ERROR_MESSAGE);
419 } catch (AltosSerialInUseException si) {
420 JOptionPane.showMessageDialog(owner,
421 String.format("Device \"%s\" already in use",
422 device.toShortString()),
424 JOptionPane.ERROR_MESSAGE);
425 } catch (IOException ee) {
426 JOptionPane.showMessageDialog(owner,
427 device.toShortString(),
429 JOptionPane.ERROR_MESSAGE);
430 } catch (TimeoutException te) {
431 JOptionPane.showMessageDialog(owner,
432 device.toShortString(),
434 JOptionPane.ERROR_MESSAGE);
435 } catch (InterruptedException ie) {
436 JOptionPane.showMessageDialog(owner,
437 device.toShortString(),
438 "Interrupted exception",
439 JOptionPane.ERROR_MESSAGE);
446 public AltosScanUI(AltosUIFrame in_owner, boolean in_select_telemetry) {
447 super(in_owner, "Scan Telemetry", false);
450 select_telemetry = in_select_telemetry;
453 frequencies = AltosUIPreferences.common_frequencies();
456 telemetry = AltosLib.ao_telemetry_standard;
462 Container pane = getContentPane();
463 GridBagConstraints c = new GridBagConstraints();
464 Insets i = new Insets(4,4,4,4);
466 timer = new javax.swing.Timer(timeout, this);
467 timer.setActionCommand("tick");
472 pane.setLayout(new GridBagLayout());
474 scanning_label = new JLabel("Scanning:");
475 frequency_label = new JLabel("");
477 if (select_telemetry) {
478 telemetry_label = new JLabel("");
479 telemetry_label.setPreferredSize(new Dimension(100, 16));
480 telemetry = AltosLib.ao_telemetry_min;
482 telemetry = AltosLib.ao_telemetry_standard;
486 rate_label = new JLabel("");
487 rate_label.setPreferredSize(new Dimension(100, 16));
492 c.fill = GridBagConstraints.HORIZONTAL;
493 c.anchor = GridBagConstraints.LINE_START;
502 pane.add(scanning_label, c);
504 pane.add(frequency_label, c);
506 int y_offset_rate = 3;
507 int y_offset_telem = 3;
511 if (select_rate && select_telemetry)
517 pane.add(rate_label, c);
519 int scanning_rate = AltosUIPreferences.scanning_telemetry_rate();
520 rate_boxes = new JCheckBox[AltosLib.ao_telemetry_rate_max + 1];
521 for (int k = 0; k <= AltosLib.ao_telemetry_rate_max; k++) {
522 rate_boxes[k] = new JCheckBox(String.format("%d baud", AltosLib.ao_telemetry_rate_values[k]));
523 c.gridy = y_offset_rate + k;
524 pane.add(rate_boxes[k], c);
525 rate_boxes[k].setActionCommand("rate");
526 rate_boxes[k].addActionListener(this);
527 rate_boxes[k].setSelected((scanning_rate & (1 << k)) != 0);
529 y_offset_rate += AltosLib.ao_telemetry_rate_max + 1;
532 if (select_telemetry) {
535 pane.add(telemetry_label, c);
537 int scanning_telemetry = AltosUIPreferences.scanning_telemetry();
538 telemetry_boxes = new JCheckBox[AltosLib.ao_telemetry_max - AltosLib.ao_telemetry_min + 1];
539 for (int k = AltosLib.ao_telemetry_min; k <= AltosLib.ao_telemetry_max; k++) {
540 int j = k - AltosLib.ao_telemetry_min;
541 telemetry_boxes[j] = new JCheckBox(AltosLib.telemetry_name(k));
542 c.gridy = y_offset_telem + j;
543 pane.add(telemetry_boxes[j], c);
544 telemetry_boxes[j].setActionCommand("telemetry");
545 telemetry_boxes[j].addActionListener(this);
546 telemetry_boxes[j].setSelected((scanning_telemetry & (1 << k)) != 0);
548 y_offset_telem += (AltosLib.ao_telemetry_max - AltosLib.ao_telemetry_min + 1);
551 int y_offset = Math.max(y_offset_rate, y_offset_telem);
553 list = new JList<AltosScanResult>(results) {
554 //Subclass JList to workaround bug 4832765, which can cause the
555 //scroll pane to not let the user easily scroll up to the beginning
556 //of the list. An alternative would be to set the unitIncrement
557 //of the JScrollBar to a fixed value. You wouldn't get the nice
558 //aligned scrolling, but it should work.
559 public int getScrollableUnitIncrement(Rectangle visibleRect,
563 if (orientation == SwingConstants.VERTICAL &&
564 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
565 Rectangle r = getCellBounds(row, row);
566 if ((r.y == visibleRect.y) && (row != 0)) {
567 Point loc = r.getLocation();
569 int prevIndex = locationToIndex(loc);
570 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
572 if (prevR == null || prevR.y >= r.y) {
578 return super.getScrollableUnitIncrement(
579 visibleRect, orientation, direction);
583 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
584 list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
585 list.setVisibleRowCount(-1);
587 list.addMouseListener(new MouseAdapter() {
588 public void mouseClicked(MouseEvent e) {
589 if (e.getClickCount() == 2) {
590 monitor_button.doClick(); //emulate button click
594 JScrollPane listScroller = new JScrollPane(list);
595 listScroller.setPreferredSize(new Dimension(400, 80));
596 listScroller.setAlignmentX(LEFT_ALIGNMENT);
598 //Create a container so that we can add a title around
599 //the scroll pane. Can't add a title directly to the
600 //scroll pane because its background would be white.
601 //Lay out the label and scroll pane from top to bottom.
602 JPanel listPane = new JPanel();
603 listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
605 JLabel label = new JLabel("Select Device");
606 label.setLabelFor(list);
608 listPane.add(Box.createRigidArea(new Dimension(0,5)));
609 listPane.add(listScroller);
610 listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
612 c.fill = GridBagConstraints.BOTH;
613 c.anchor = GridBagConstraints.CENTER;
621 c.anchor = GridBagConstraints.CENTER;
623 pane.add(listPane, c);
625 cancel_button = new JButton("Cancel");
626 cancel_button.addActionListener(this);
627 cancel_button.setActionCommand("cancel");
629 c.fill = GridBagConstraints.NONE;
630 c.anchor = GridBagConstraints.CENTER;
636 c.gridy = y_offset + 1;
638 c.anchor = GridBagConstraints.CENTER;
640 pane.add(cancel_button, c);
642 monitor_button = new JButton("Monitor");
643 monitor_button.addActionListener(this);
644 monitor_button.setActionCommand("monitor");
646 c.fill = GridBagConstraints.NONE;
647 c.anchor = GridBagConstraints.CENTER;
653 c.gridy = y_offset + 1;
655 c.anchor = GridBagConstraints.CENTER;
657 pane.add(monitor_button, c);
660 setLocationRelativeTo(owner);
662 addWindowListener(new ConfigListener(this));