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_9;
21 import java.awt.event.*;
23 import javax.swing.event.*;
27 import java.util.concurrent.*;
28 import org.altusmetrum.altoslib_9.*;
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 int hashCode() {
66 return serial ^ frequency.hashCode() ^ telemetry ^ rate;
69 public boolean equals(Object o) {
72 if (!(o instanceof AltosScanResult))
74 AltosScanResult other = (AltosScanResult) o;
75 return (serial == other.serial &&
76 frequency.equals(other.frequency) &&
77 telemetry == other.telemetry &&
81 public boolean up_to_date(AltosScanResult other) {
82 if (flight == 0 && other.flight != 0) {
83 flight = other.flight;
86 if (callsign.equals("N0CALL") && !other.callsign.equals("N0CALL")) {
87 callsign = other.callsign;
94 class AltosScanResults extends LinkedList<AltosScanResult> implements ListModel<AltosScanResult> {
96 LinkedList<ListDataListener> listeners = new LinkedList<ListDataListener>();
98 void changed(ListDataEvent de) {
99 for (ListDataListener l : listeners)
100 l.contentsChanged(de);
103 public boolean add(AltosScanResult r) {
105 for (AltosScanResult old : this) {
107 if (!old.up_to_date(r))
108 changed (new ListDataEvent(this,
109 ListDataEvent.CONTENTS_CHANGED,
117 changed(new ListDataEvent(this,
118 ListDataEvent.INTERVAL_ADDED,
119 this.size() - 2, this.size() - 1));
123 public void addListDataListener(ListDataListener l) {
127 public void removeListDataListener(ListDataListener l) {
131 public AltosScanResult getElementAt(int i) {
135 public int getSize() {
140 public class AltosScanUI
141 extends AltosUIDialog
142 implements ActionListener
146 AltosConfigData config_data;
147 AltosTelemetryReader reader;
148 private JList<AltosScanResult> list;
149 private JLabel scanning_label;
150 private JLabel frequency_label;
151 private JLabel telemetry_label;
152 private JLabel rate_label;
153 private JButton cancel_button;
154 private JButton monitor_button;
155 private JCheckBox[] telemetry_boxes;
156 private JCheckBox[] rate_boxes;
157 javax.swing.Timer timer;
158 AltosScanResults results = new AltosScanResults();
161 boolean select_telemetry = false;
164 boolean select_rate = false;
166 final static int timeout = 1200;
167 TelemetryHandler handler;
169 AltosFrequency[] frequencies;
174 void scan_exception(Exception e) {
175 if (e instanceof FileNotFoundException) {
176 JOptionPane.showMessageDialog(owner,
177 ((FileNotFoundException) e).getMessage(),
178 "Cannot open target device",
179 JOptionPane.ERROR_MESSAGE);
180 } else if (e instanceof AltosSerialInUseException) {
181 JOptionPane.showMessageDialog(owner,
182 String.format("Device \"%s\" already in use",
183 device.toShortString()),
185 JOptionPane.ERROR_MESSAGE);
186 } else if (e instanceof IOException) {
187 IOException ee = (IOException) e;
188 JOptionPane.showMessageDialog(owner,
189 device.toShortString(),
190 ee.getLocalizedMessage(),
191 JOptionPane.ERROR_MESSAGE);
193 JOptionPane.showMessageDialog(owner,
194 String.format("Connection to \"%s\" failed",
195 device.toShortString()),
197 JOptionPane.ERROR_MESSAGE);
202 class TelemetryHandler implements Runnable {
206 boolean interrupted = false;
211 AltosState state = reader.read();
215 if (state.flight != AltosLib.MISSING) {
216 final AltosScanResult result = new AltosScanResult(state.callsign,
219 frequencies[frequency_index],
222 Runnable r = new Runnable() {
227 SwingUtilities.invokeLater(r);
229 } catch (ParseException pp) {
230 } catch (AltosCRCException ce) {
233 } catch (InterruptedException ee) {
235 } catch (IOException ie) {
237 reader.close(interrupted);
243 frequency_label.setText(String.format("Frequency: %s", frequencies[frequency_index].toString()));
244 if (select_telemetry)
245 telemetry_label.setText(String.format("Telemetry: %s", AltosLib.telemetry_name(telemetry)));
247 rate_label.setText(String.format("Rate: %d baud", AltosLib.ao_telemetry_rate_values[rate]));
250 void set_telemetry() {
251 reader.set_telemetry(telemetry);
255 reader.set_telemetry_rate(rate);
258 void set_frequency() throws InterruptedException, TimeoutException {
259 reader.set_frequency(frequencies[frequency_index].frequency);
263 void next() throws InterruptedException, TimeoutException {
264 reader.set_monitor(false);
267 boolean wrapped = false;
270 if (rate > AltosLib.ao_telemetry_rate_max) {
274 } while (!rate_boxes[rate].isSelected());
281 if (select_telemetry) {
282 boolean wrapped = false;
285 if (telemetry > AltosLib.ao_telemetry_max) {
287 telemetry = AltosLib.ao_telemetry_min;
289 } while (!telemetry_boxes[telemetry - AltosLib.ao_telemetry_min].isSelected());
299 if (frequency_index >= frequencies.length)
303 reader.set_monitor(true);
308 if (thread != null && thread.isAlive()) {
312 } catch (InterruptedException ie) {}
321 void tick_timer() throws InterruptedException, TimeoutException {
323 if (packet_count == 0 || tick_count > 5)
327 public void actionPerformed(ActionEvent e) {
328 String cmd = e.getActionCommand();
331 if (cmd.equals("cancel"))
334 if (cmd.equals("tick"))
337 if (cmd.equals("telemetry")) {
339 int scanning_telemetry = 0;
340 for (k = AltosLib.ao_telemetry_min; k <= AltosLib.ao_telemetry_max; k++) {
341 int j = k - AltosLib.ao_telemetry_min;
342 if (telemetry_boxes[j].isSelected())
343 scanning_telemetry |= (1 << k);
345 if (scanning_telemetry == 0) {
346 scanning_telemetry |= (1 << AltosLib.ao_telemetry_standard);
347 telemetry_boxes[AltosLib.ao_telemetry_standard - AltosLib.ao_telemetry_min].setSelected(true);
349 AltosUIPreferences.set_scanning_telemetry(scanning_telemetry);
352 if (cmd.equals("rate")) {
354 int scanning_rate = 0;
355 for (k = 0; k <= AltosLib.ao_telemetry_rate_max; k++) {
356 if (rate_boxes[k].isSelected())
357 scanning_rate |= (1 << k);
359 if (scanning_rate == 0) {
360 scanning_rate = (1 << 0);
361 rate_boxes[0].setSelected(true);
363 AltosUIPreferences.set_scanning_telemetry_rate(scanning_rate);
366 if (cmd.equals("monitor")) {
368 AltosScanResult r = (AltosScanResult) (list.getSelectedValue());
370 if (device != null) {
371 if (reader != null) {
372 reader.set_telemetry(r.telemetry);
373 reader.set_telemetry_rate(r.rate);
374 reader.set_frequency(r.frequency.frequency);
375 reader.save_frequency();
376 reader.save_telemetry();
377 reader.save_telemetry_rate();
378 owner.scan_device_selected(device);
383 } catch (TimeoutException te) {
385 } catch (InterruptedException ie) {
390 /* A window listener to catch closing events and tell the config code */
391 class ConfigListener extends WindowAdapter {
394 public ConfigListener(AltosScanUI this_ui) {
398 public void windowClosing(WindowEvent e) {
399 ui.actionPerformed(new ActionEvent(e.getSource(),
400 ActionEvent.ACTION_PERFORMED,
405 private boolean open() {
406 device = AltosDeviceUIDialog.show(owner, AltosLib.product_basestation);
410 reader = new AltosTelemetryReader(new AltosSerial(device));
416 } catch (InterruptedException ie) {
419 handler = new TelemetryHandler();
420 thread = new Thread(handler);
423 } catch (FileNotFoundException ee) {
424 JOptionPane.showMessageDialog(owner,
426 "Cannot open target device",
427 JOptionPane.ERROR_MESSAGE);
428 } catch (AltosSerialInUseException si) {
429 JOptionPane.showMessageDialog(owner,
430 String.format("Device \"%s\" already in use",
431 device.toShortString()),
433 JOptionPane.ERROR_MESSAGE);
434 } catch (IOException ee) {
435 JOptionPane.showMessageDialog(owner,
436 device.toShortString(),
438 JOptionPane.ERROR_MESSAGE);
439 } catch (TimeoutException te) {
440 JOptionPane.showMessageDialog(owner,
441 device.toShortString(),
443 JOptionPane.ERROR_MESSAGE);
444 } catch (InterruptedException ie) {
445 JOptionPane.showMessageDialog(owner,
446 device.toShortString(),
447 "Interrupted exception",
448 JOptionPane.ERROR_MESSAGE);
455 public AltosScanUI(AltosUIFrame in_owner, boolean in_select_telemetry) {
456 super(in_owner, "Scan Telemetry", false);
459 select_telemetry = in_select_telemetry;
462 frequencies = AltosUIPreferences.common_frequencies();
465 telemetry = AltosLib.ao_telemetry_standard;
471 Container pane = getContentPane();
472 GridBagConstraints c = new GridBagConstraints();
473 Insets i = new Insets(4,4,4,4);
475 timer = new javax.swing.Timer(timeout, this);
476 timer.setActionCommand("tick");
481 pane.setLayout(new GridBagLayout());
483 scanning_label = new JLabel("Scanning:");
484 frequency_label = new JLabel("");
486 if (select_telemetry) {
487 telemetry_label = new JLabel("");
488 telemetry_label.setPreferredSize(new Dimension(100, 16));
489 telemetry = AltosLib.ao_telemetry_min;
491 telemetry = AltosLib.ao_telemetry_standard;
495 rate_label = new JLabel("");
496 rate_label.setPreferredSize(new Dimension(100, 16));
501 c.fill = GridBagConstraints.HORIZONTAL;
502 c.anchor = GridBagConstraints.LINE_START;
511 pane.add(scanning_label, c);
513 pane.add(frequency_label, c);
515 int y_offset_rate = 3;
516 int y_offset_telem = 3;
520 if (select_rate && select_telemetry)
526 pane.add(rate_label, c);
528 int scanning_rate = AltosUIPreferences.scanning_telemetry_rate();
529 rate_boxes = new JCheckBox[AltosLib.ao_telemetry_rate_max + 1];
530 for (int k = 0; k <= AltosLib.ao_telemetry_rate_max; k++) {
531 rate_boxes[k] = new JCheckBox(String.format("%d baud", AltosLib.ao_telemetry_rate_values[k]));
532 c.gridy = y_offset_rate + k;
533 pane.add(rate_boxes[k], c);
534 rate_boxes[k].setActionCommand("rate");
535 rate_boxes[k].addActionListener(this);
536 rate_boxes[k].setSelected((scanning_rate & (1 << k)) != 0);
538 y_offset_rate += AltosLib.ao_telemetry_rate_max + 1;
541 if (select_telemetry) {
544 pane.add(telemetry_label, c);
546 int scanning_telemetry = AltosUIPreferences.scanning_telemetry();
547 telemetry_boxes = new JCheckBox[AltosLib.ao_telemetry_max - AltosLib.ao_telemetry_min + 1];
548 for (int k = AltosLib.ao_telemetry_min; k <= AltosLib.ao_telemetry_max; k++) {
549 int j = k - AltosLib.ao_telemetry_min;
550 telemetry_boxes[j] = new JCheckBox(AltosLib.telemetry_name(k));
551 c.gridy = y_offset_telem + j;
552 pane.add(telemetry_boxes[j], c);
553 telemetry_boxes[j].setActionCommand("telemetry");
554 telemetry_boxes[j].addActionListener(this);
555 telemetry_boxes[j].setSelected((scanning_telemetry & (1 << k)) != 0);
557 y_offset_telem += (AltosLib.ao_telemetry_max - AltosLib.ao_telemetry_min + 1);
560 int y_offset = Math.max(y_offset_rate, y_offset_telem);
562 list = new JList<AltosScanResult>(results) {
563 //Subclass JList to workaround bug 4832765, which can cause the
564 //scroll pane to not let the user easily scroll up to the beginning
565 //of the list. An alternative would be to set the unitIncrement
566 //of the JScrollBar to a fixed value. You wouldn't get the nice
567 //aligned scrolling, but it should work.
568 public int getScrollableUnitIncrement(Rectangle visibleRect,
572 if (orientation == SwingConstants.VERTICAL &&
573 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
574 Rectangle r = getCellBounds(row, row);
575 if ((r.y == visibleRect.y) && (row != 0)) {
576 Point loc = r.getLocation();
578 int prevIndex = locationToIndex(loc);
579 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
581 if (prevR == null || prevR.y >= r.y) {
587 return super.getScrollableUnitIncrement(
588 visibleRect, orientation, direction);
592 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
593 list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
594 list.setVisibleRowCount(-1);
596 list.addMouseListener(new MouseAdapter() {
597 public void mouseClicked(MouseEvent e) {
598 if (e.getClickCount() == 2) {
599 monitor_button.doClick(); //emulate button click
603 JScrollPane listScroller = new JScrollPane(list);
604 listScroller.setPreferredSize(new Dimension(400, 80));
605 listScroller.setAlignmentX(LEFT_ALIGNMENT);
607 //Create a container so that we can add a title around
608 //the scroll pane. Can't add a title directly to the
609 //scroll pane because its background would be white.
610 //Lay out the label and scroll pane from top to bottom.
611 JPanel listPane = new JPanel();
612 listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
614 JLabel label = new JLabel("Select Device");
615 label.setLabelFor(list);
617 listPane.add(Box.createRigidArea(new Dimension(0,5)));
618 listPane.add(listScroller);
619 listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
621 c.fill = GridBagConstraints.BOTH;
622 c.anchor = GridBagConstraints.CENTER;
630 c.anchor = GridBagConstraints.CENTER;
632 pane.add(listPane, c);
634 cancel_button = new JButton("Cancel");
635 cancel_button.addActionListener(this);
636 cancel_button.setActionCommand("cancel");
638 c.fill = GridBagConstraints.NONE;
639 c.anchor = GridBagConstraints.CENTER;
645 c.gridy = y_offset + 1;
647 c.anchor = GridBagConstraints.CENTER;
649 pane.add(cancel_button, c);
651 monitor_button = new JButton("Monitor");
652 monitor_button.addActionListener(this);
653 monitor_button.setActionCommand("monitor");
655 c.fill = GridBagConstraints.NONE;
656 c.anchor = GridBagConstraints.CENTER;
662 c.gridy = y_offset + 1;
664 c.anchor = GridBagConstraints.CENTER;
666 pane.add(monitor_button, c);
669 setLocationRelativeTo(owner);
671 addWindowListener(new ConfigListener(this));