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.
19 * Deal with TeleDongle on a serial port
26 import java.util.concurrent.*;
30 import java.awt.event.*;
32 import javax.swing.filechooser.FileNameExtensionFilter;
33 import javax.swing.table.*;
34 import org.altusmetrum.AltosLib.*;
39 * This class reads from the serial port and places each received
40 * line in a queue. Dealing with that queue is left up to other
44 public class AltosSerial extends AltosLink {
46 static java.util.List<String> devices_opened = Collections.synchronizedList(new LinkedList<String>());
49 SWIGTYPE_p_altos_file altos;
56 public int getchar() {
59 return libaltos.altos_getchar(altos, 0);
62 public void flush_output() {
65 if (libaltos.altos_flush(altos) != 0) {
66 libaltos.altos_close(altos);
73 JDialog timeout_dialog;
75 private void start_timeout_dialog_internal() {
77 Object[] options = { "Cancel" };
79 JOptionPane pane = new JOptionPane();
80 pane.setMessage(String.format("Connecting to %s, %7.3f MHz", device.toShortString(), frequency));
81 pane.setOptions(options);
82 pane.setInitialValue(null);
84 timeout_dialog = pane.createDialog(frame, "Connecting...");
86 timeout_dialog.setVisible(true);
88 Object o = pane.getValue();
91 if (options[0].equals(o))
93 timeout_dialog.dispose();
94 timeout_dialog = null;
98 * These are required by the AltosLink implementation
101 public boolean can_cancel_reply() {
103 * Can cancel any replies not called from the dispatch thread
105 return !SwingUtilities.isEventDispatchThread();
108 public boolean show_reply_timeout() {
109 if (!SwingUtilities.isEventDispatchThread() && frame != null) {
110 Runnable r = new Runnable() {
112 start_timeout_dialog_internal();
115 SwingUtilities.invokeLater(r);
121 public void hide_reply_timeout() {
122 Runnable r = new Runnable() {
124 timeout_dialog.setVisible(false);
127 SwingUtilities.invokeLater(r);
130 public void close() {
134 } catch (InterruptedException ie) {
138 System.out.printf("Uh-oh. Closing active serial device\n");
141 libaltos.altos_close(altos);
143 if (input_thread != null) {
145 input_thread.interrupt();
147 } catch (InterruptedException e) {
152 libaltos.altos_free(altos);
155 synchronized (devices_opened) {
156 devices_opened.remove(device.getPath());
159 System.out.printf("Closing %s\n", device.getPath());
162 private void putc(char c) {
164 if (libaltos.altos_putchar(altos, c) != 0) {
165 libaltos.altos_close(altos);
171 public void print(String data) {
172 for (int i = 0; i < data.length(); i++)
173 putc(data.charAt(i));
176 private void open() throws FileNotFoundException, AltosSerialInUseException {
177 synchronized (devices_opened) {
178 if (devices_opened.contains(device.getPath()))
179 throw new AltosSerialInUseException(device);
180 devices_opened.add(device.getPath());
182 altos = device.open();
184 final String message = device.getErrorString();
186 throw new FileNotFoundException(String.format("%s (%s)",
187 device.toShortString(), message));
190 System.out.printf("Open %s\n", device.getPath());
191 input_thread = new Thread(this);
192 input_thread.start();
198 public void set_frame(Frame in_frame) {
202 public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
205 serial = device.getSerial();
206 name = device.toShortString();