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 implements Runnable {
46 static java.util.List<String> devices_opened = Collections.synchronizedList(new LinkedList<String>());
49 SWIGTYPE_p_altos_file altos;
55 byte[] line_bytes = null;
60 c = libaltos.altos_getchar(altos, 0);
61 if (Thread.interrupted())
63 if (c == libaltosConstants.LIBALTOS_ERROR) {
64 add_telem (new AltosLine());
65 add_reply (new AltosLine());
68 if (c == libaltosConstants.LIBALTOS_TIMEOUT)
74 if (line_count != 0) {
75 add_bytes(line_bytes, line_count);
79 if (line_bytes == null) {
80 line_bytes = new byte[256];
81 } else if (line_count == line_bytes.length) {
82 byte[] new_line_bytes = new byte[line_count * 2];
83 System.arraycopy(line_bytes, 0, new_line_bytes, 0, line_count);
84 line_bytes = new_line_bytes;
86 line_bytes[line_count] = (byte) c;
91 } catch (InterruptedException e) {
95 public void flush_output() {
98 libaltos.altos_flush(altos);
103 JDialog timeout_dialog;
104 boolean timeout_started = false;
106 private void stop_timeout_dialog() {
107 if (timeout_started) {
108 timeout_started = false;
109 Runnable r = new Runnable() {
111 timeout_dialog.setVisible(false);
114 SwingUtilities.invokeLater(r);
118 private void start_timeout_dialog_internal() {
120 Object[] options = { "Cancel" };
122 JOptionPane pane = new JOptionPane();
123 pane.setMessage(String.format("Connecting to %s, %7.3f MHz", device.toShortString(), frequency));
124 pane.setOptions(options);
125 pane.setInitialValue(null);
127 timeout_dialog = pane.createDialog(frame, "Connecting...");
129 timeout_dialog.setVisible(true);
131 Object o = pane.getValue();
134 if (options[0].equals(o))
136 timeout_dialog.dispose();
137 timeout_dialog = null;
140 private boolean check_timeout() {
141 if (!timeout_started && frame != null) {
142 if (!SwingUtilities.isEventDispatchThread()) {
143 timeout_started = true;
144 Runnable r = new Runnable() {
146 start_timeout_dialog_internal();
149 SwingUtilities.invokeLater(r);
155 public void flush_input() throws InterruptedException {
164 public String get_reply(int timeout) throws InterruptedException {
165 boolean can_cancel = true;
171 if (SwingUtilities.isEventDispatchThread()) {
174 System.out.printf("Uh-oh, reading remote serial device from swing thread\n");
177 if (remote && can_cancel) {
181 timeout_started = false;
183 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
185 stop_timeout_dialog();
189 if (!remote || !can_cancel || check_timeout()) {
200 public void close() {
204 } catch (InterruptedException ie) {
208 System.out.printf("Uh-oh. Closing active serial device\n");
211 libaltos.altos_close(altos);
213 if (input_thread != null) {
215 input_thread.interrupt();
217 } catch (InterruptedException e) {
222 libaltos.altos_free(altos);
225 synchronized (devices_opened) {
226 devices_opened.remove(device.getPath());
229 System.out.printf("Closing %s\n", device.getPath());
232 private void putc(char c) {
234 libaltos.altos_putchar(altos, c);
237 public void print(String data) {
238 for (int i = 0; i < data.length(); i++)
239 putc(data.charAt(i));
242 private void open() throws FileNotFoundException, AltosSerialInUseException {
243 synchronized (devices_opened) {
244 if (devices_opened.contains(device.getPath()))
245 throw new AltosSerialInUseException(device);
246 devices_opened.add(device.getPath());
248 altos = device.open();
250 final String message = device.getErrorString();
252 throw new FileNotFoundException(String.format("%s (%s)",
253 device.toShortString(), message));
256 System.out.printf("Open %s\n", device.getPath());
257 input_thread = new Thread(this);
258 input_thread.start();
264 public void set_frame(Frame in_frame) {
268 public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
271 serial = device.getSerial();
272 name = device.toShortString();