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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altosuilib_11;
22 import java.awt.event.*;
24 import javax.swing.event.*;
28 import java.util.concurrent.*;
29 import org.altusmetrum.altoslib_11.*;
31 class AltosScanResult {
35 AltosFrequency frequency;
39 boolean interrupted = false;
41 public String toString() {
42 return String.format("%-9.9s serial %-4d flight %-4d (%s %s %d)",
43 callsign, serial, flight,
44 frequency.toShortString(),
45 AltosLib.telemetry_name(telemetry),
46 AltosLib.ao_telemetry_rate_values[rate]);
49 public String toShortString() {
50 return String.format("%s %d %d %7.3f %d %d",
51 callsign, serial, flight, frequency, telemetry, rate);
54 public AltosScanResult(String in_callsign, int in_serial,
55 int in_flight, AltosFrequency in_frequency,
58 callsign = in_callsign;
61 frequency = in_frequency;
62 telemetry = in_telemetry;
66 public int hashCode() {
67 return serial ^ frequency.hashCode() ^ telemetry ^ rate;
70 public boolean equals(Object o) {
73 if (!(o instanceof AltosScanResult))
75 AltosScanResult other = (AltosScanResult) o;
76 return (serial == other.serial &&
77 frequency.equals(other.frequency) &&
78 telemetry == other.telemetry &&
82 public boolean up_to_date(AltosScanResult other) {
83 if (flight == 0 && other.flight != 0) {
84 flight = other.flight;
87 if (callsign.equals("N0CALL") && !other.callsign.equals("N0CALL")) {
88 callsign = other.callsign;
95 class AltosScanResults extends LinkedList<AltosScanResult> implements ListModel<AltosScanResult> {
97 LinkedList<ListDataListener> listeners = new LinkedList<ListDataListener>();
99 void changed(ListDataEvent de) {
100 for (ListDataListener l : listeners)
101 l.contentsChanged(de);
104 public boolean add(AltosScanResult r) {
106 for (AltosScanResult old : this) {
108 if (!old.up_to_date(r))
109 changed (new ListDataEvent(this,
110 ListDataEvent.CONTENTS_CHANGED,
118 changed(new ListDataEvent(this,
119 ListDataEvent.INTERVAL_ADDED,
120 this.size() - 2, this.size() - 1));
124 public void addListDataListener(ListDataListener l) {
128 public void removeListDataListener(ListDataListener l) {
132 public AltosScanResult getElementAt(int i) {
136 public int getSize() {
141 public class AltosScanUI
142 extends AltosUIDialog
143 implements ActionListener
147 AltosConfigData config_data;
148 AltosTelemetryReader reader;
149 private JList<AltosScanResult> list;
150 private JLabel scanning_label;
151 private JLabel frequency_label;
152 private JLabel telemetry_label;
153 private JLabel rate_label;
154 private JButton cancel_button;
155 private JButton monitor_button;
156 private JCheckBox[] telemetry_boxes;
157 private JCheckBox[] rate_boxes;
158 javax.swing.Timer timer;
159 AltosScanResults results = new AltosScanResults();
162 boolean select_telemetry = false;
165 boolean select_rate = false;
167 final static int timeout = 1200;
168 TelemetryHandler handler;
170 AltosFrequency[] frequencies;
175 void scan_exception(Exception e) {
176 if (e instanceof FileNotFoundException) {
177 JOptionPane.showMessageDialog(owner,
178 ((FileNotFoundException) e).getMessage(),
179 "Cannot open target device",
180 JOptionPane.ERROR_MESSAGE);
181 } else if (e instanceof AltosSerialInUseException) {
182 JOptionPane.showMessageDialog(owner,
183 String.format("Device \"%s\" already in use",
184 device.toShortString()),
186 JOptionPane.ERROR_MESSAGE);
187 } else if (e instanceof IOException) {
188 IOException ee = (IOException) e;
189 JOptionPane.showMessageDialog(owner,
190 device.toShortString(),
191 ee.getLocalizedMessage(),
192 JOptionPane.ERROR_MESSAGE);
194 JOptionPane.showMessageDialog(owner,
195 String.format("Connection to \"%s\" failed",
196 device.toShortString()),
198 JOptionPane.ERROR_MESSAGE);
203 class TelemetryHandler implements Runnable {
207 boolean interrupted = false;
212 AltosState state = reader.read();
216 if (state.flight != AltosLib.MISSING) {
217 final AltosScanResult result = new AltosScanResult(state.callsign,
220 frequencies[frequency_index],
223 Runnable r = new Runnable() {
228 SwingUtilities.invokeLater(r);
230 } catch (ParseException pp) {
231 } catch (AltosCRCException ce) {
234 } catch (InterruptedException ee) {
236 } catch (IOException ie) {
238 reader.close(interrupted);
244 frequency_label.setText(String.format("Frequency: %s", frequencies[frequency_index].toString()));
245 if (select_telemetry)
246 telemetry_label.setText(String.format("Telemetry: %s", AltosLib.telemetry_name(telemetry)));
248 rate_label.setText(String.format("Rate: %d baud", AltosLib.ao_telemetry_rate_values[rate]));
251 void set_telemetry() {
252 reader.set_telemetry(telemetry);
256 reader.set_telemetry_rate(rate);
259 void set_frequency() throws InterruptedException, TimeoutException {
260 reader.set_frequency(frequencies[frequency_index].frequency);
264 void next() throws InterruptedException, TimeoutException {
265 reader.set_monitor(false);
268 boolean wrapped = false;
271 if (rate > AltosLib.ao_telemetry_rate_max) {
275 } while (!rate_boxes[rate].isSelected());
282 if (select_telemetry) {
283 boolean wrapped = false;
286 if (telemetry > AltosLib.ao_telemetry_max) {
288 telemetry = AltosLib.ao_telemetry_min;
290 } while (!telemetry_boxes[telemetry - AltosLib.ao_telemetry_min].isSelected());
300 if (frequency_index >= frequencies.length)
304 reader.set_monitor(true);
309 if (thread != null && thread.isAlive()) {
313 } catch (InterruptedException ie) {}
322 void tick_timer() throws InterruptedException, TimeoutException {
324 if (packet_count == 0 || tick_count > 5)
328 public void actionPerformed(ActionEvent e) {
329 String cmd = e.getActionCommand();
332 if (cmd.equals("cancel"))
335 if (cmd.equals("tick"))
338 if (cmd.equals("telemetry")) {
340 int scanning_telemetry = 0;
341 for (k = AltosLib.ao_telemetry_min; k <= AltosLib.ao_telemetry_max; k++) {
342 int j = k - AltosLib.ao_telemetry_min;
343 if (telemetry_boxes[j].isSelected())
344 scanning_telemetry |= (1 << k);
346 if (scanning_telemetry == 0) {
347 scanning_telemetry |= (1 << AltosLib.ao_telemetry_standard);
348 telemetry_boxes[AltosLib.ao_telemetry_standard - AltosLib.ao_telemetry_min].setSelected(true);
350 AltosUIPreferences.set_scanning_telemetry(scanning_telemetry);
353 if (cmd.equals("rate")) {
355 int scanning_rate = 0;
356 for (k = 0; k <= AltosLib.ao_telemetry_rate_max; k++) {
357 if (rate_boxes[k].isSelected())
358 scanning_rate |= (1 << k);
360 if (scanning_rate == 0) {
361 scanning_rate = (1 << 0);
362 rate_boxes[0].setSelected(true);
364 AltosUIPreferences.set_scanning_telemetry_rate(scanning_rate);
367 if (cmd.equals("monitor")) {
369 AltosScanResult r = (AltosScanResult) (list.getSelectedValue());
371 if (device != null) {
372 if (reader != null) {
373 reader.set_telemetry(r.telemetry);
374 reader.set_telemetry_rate(r.rate);
375 reader.set_frequency(r.frequency.frequency);
376 reader.save_frequency();
377 reader.save_telemetry();
378 reader.save_telemetry_rate();
379 owner.scan_device_selected(device);
384 } catch (TimeoutException te) {
386 } catch (InterruptedException ie) {
391 /* A window listener to catch closing events and tell the config code */
392 class ConfigListener extends WindowAdapter {
395 public ConfigListener(AltosScanUI this_ui) {
399 public void windowClosing(WindowEvent e) {
400 ui.actionPerformed(new ActionEvent(e.getSource(),
401 ActionEvent.ACTION_PERFORMED,
406 private boolean open() {
407 device = AltosDeviceUIDialog.show(owner, AltosLib.product_basestation);
411 reader = new AltosTelemetryReader(new AltosSerial(device));
417 } catch (InterruptedException ie) {
420 handler = new TelemetryHandler();
421 thread = new Thread(handler);
424 } catch (FileNotFoundException ee) {
425 JOptionPane.showMessageDialog(owner,
427 "Cannot open target device",
428 JOptionPane.ERROR_MESSAGE);
429 } catch (AltosSerialInUseException si) {
430 JOptionPane.showMessageDialog(owner,
431 String.format("Device \"%s\" already in use",
432 device.toShortString()),
434 JOptionPane.ERROR_MESSAGE);
435 } catch (IOException ee) {
436 JOptionPane.showMessageDialog(owner,
437 device.toShortString(),
439 JOptionPane.ERROR_MESSAGE);
440 } catch (TimeoutException te) {
441 JOptionPane.showMessageDialog(owner,
442 device.toShortString(),
444 JOptionPane.ERROR_MESSAGE);
445 } catch (InterruptedException ie) {
446 JOptionPane.showMessageDialog(owner,
447 device.toShortString(),
448 "Interrupted exception",
449 JOptionPane.ERROR_MESSAGE);
456 public AltosScanUI(AltosUIFrame in_owner, boolean in_select_telemetry) {
457 super(in_owner, "Scan Telemetry", false);
460 select_telemetry = in_select_telemetry;
463 frequencies = AltosUIPreferences.common_frequencies();
466 telemetry = AltosLib.ao_telemetry_standard;
472 Container pane = getContentPane();
473 GridBagConstraints c = new GridBagConstraints();
474 Insets i = new Insets(4,4,4,4);
476 timer = new javax.swing.Timer(timeout, this);
477 timer.setActionCommand("tick");
482 pane.setLayout(new GridBagLayout());
484 scanning_label = new JLabel("Scanning:");
485 frequency_label = new JLabel("");
487 if (select_telemetry) {
488 telemetry_label = new JLabel("");
489 telemetry_label.setPreferredSize(new Dimension(100, 16));
490 telemetry = AltosLib.ao_telemetry_min;
492 telemetry = AltosLib.ao_telemetry_standard;
496 rate_label = new JLabel("");
497 rate_label.setPreferredSize(new Dimension(100, 16));
502 c.fill = GridBagConstraints.HORIZONTAL;
503 c.anchor = GridBagConstraints.LINE_START;
512 pane.add(scanning_label, c);
514 pane.add(frequency_label, c);
516 int y_offset_rate = 3;
517 int y_offset_telem = 3;
521 if (select_rate && select_telemetry)
527 pane.add(rate_label, c);
529 int scanning_rate = AltosUIPreferences.scanning_telemetry_rate();
530 rate_boxes = new JCheckBox[AltosLib.ao_telemetry_rate_max + 1];
531 for (int k = 0; k <= AltosLib.ao_telemetry_rate_max; k++) {
532 rate_boxes[k] = new JCheckBox(String.format("%d baud", AltosLib.ao_telemetry_rate_values[k]));
533 c.gridy = y_offset_rate + k;
534 pane.add(rate_boxes[k], c);
535 rate_boxes[k].setActionCommand("rate");
536 rate_boxes[k].addActionListener(this);
537 rate_boxes[k].setSelected((scanning_rate & (1 << k)) != 0);
539 y_offset_rate += AltosLib.ao_telemetry_rate_max + 1;
542 if (select_telemetry) {
545 pane.add(telemetry_label, c);
547 int scanning_telemetry = AltosUIPreferences.scanning_telemetry();
548 telemetry_boxes = new JCheckBox[AltosLib.ao_telemetry_max - AltosLib.ao_telemetry_min + 1];
549 for (int k = AltosLib.ao_telemetry_min; k <= AltosLib.ao_telemetry_max; k++) {
550 int j = k - AltosLib.ao_telemetry_min;
551 telemetry_boxes[j] = new JCheckBox(AltosLib.telemetry_name(k));
552 c.gridy = y_offset_telem + j;
553 pane.add(telemetry_boxes[j], c);
554 telemetry_boxes[j].setActionCommand("telemetry");
555 telemetry_boxes[j].addActionListener(this);
556 telemetry_boxes[j].setSelected((scanning_telemetry & (1 << k)) != 0);
558 y_offset_telem += (AltosLib.ao_telemetry_max - AltosLib.ao_telemetry_min + 1);
561 int y_offset = Math.max(y_offset_rate, y_offset_telem);
563 list = new JList<AltosScanResult>(results) {
564 //Subclass JList to workaround bug 4832765, which can cause the
565 //scroll pane to not let the user easily scroll up to the beginning
566 //of the list. An alternative would be to set the unitIncrement
567 //of the JScrollBar to a fixed value. You wouldn't get the nice
568 //aligned scrolling, but it should work.
569 public int getScrollableUnitIncrement(Rectangle visibleRect,
573 if (orientation == SwingConstants.VERTICAL &&
574 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
575 Rectangle r = getCellBounds(row, row);
576 if ((r.y == visibleRect.y) && (row != 0)) {
577 Point loc = r.getLocation();
579 int prevIndex = locationToIndex(loc);
580 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
582 if (prevR == null || prevR.y >= r.y) {
588 return super.getScrollableUnitIncrement(
589 visibleRect, orientation, direction);
593 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
594 list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
595 list.setVisibleRowCount(-1);
597 list.addMouseListener(new MouseAdapter() {
598 public void mouseClicked(MouseEvent e) {
599 if (e.getClickCount() == 2) {
600 monitor_button.doClick(); //emulate button click
604 JScrollPane listScroller = new JScrollPane(list);
605 listScroller.setPreferredSize(new Dimension(400, 80));
606 listScroller.setAlignmentX(LEFT_ALIGNMENT);
608 //Create a container so that we can add a title around
609 //the scroll pane. Can't add a title directly to the
610 //scroll pane because its background would be white.
611 //Lay out the label and scroll pane from top to bottom.
612 JPanel listPane = new JPanel();
613 listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
615 JLabel label = new JLabel("Select Device");
616 label.setLabelFor(list);
618 listPane.add(Box.createRigidArea(new Dimension(0,5)));
619 listPane.add(listScroller);
620 listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
622 c.fill = GridBagConstraints.BOTH;
623 c.anchor = GridBagConstraints.CENTER;
631 c.anchor = GridBagConstraints.CENTER;
633 pane.add(listPane, c);
635 cancel_button = new JButton("Cancel");
636 cancel_button.addActionListener(this);
637 cancel_button.setActionCommand("cancel");
639 c.fill = GridBagConstraints.NONE;
640 c.anchor = GridBagConstraints.CENTER;
646 c.gridy = y_offset + 1;
648 c.anchor = GridBagConstraints.CENTER;
650 pane.add(cancel_button, c);
652 monitor_button = new JButton("Monitor");
653 monitor_button.addActionListener(this);
654 monitor_button.setActionCommand("monitor");
656 c.fill = GridBagConstraints.NONE;
657 c.anchor = GridBagConstraints.CENTER;
663 c.gridy = y_offset + 1;
665 c.anchor = GridBagConstraints.CENTER;
667 pane.add(monitor_button, c);
670 setLocationRelativeTo(owner);
672 addWindowListener(new ConfigListener(this));