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;
58 byte[] line_bytes = null;
63 c = libaltos.altos_getchar(altos, 0);
64 if (Thread.interrupted())
66 if (c == libaltosConstants.LIBALTOS_ERROR) {
67 add_telem (new AltosLine());
68 add_reply (new AltosLine());
71 if (c == libaltosConstants.LIBALTOS_TIMEOUT)
77 if (line_count != 0) {
78 add_bytes(line_bytes, line_count);
82 if (line_bytes == null) {
83 line_bytes = new byte[256];
84 } else if (line_count == line_bytes.length) {
85 byte[] new_line_bytes = new byte[line_count * 2];
86 System.arraycopy(line_bytes, 0, new_line_bytes, 0, line_count);
87 line_bytes = new_line_bytes;
89 line_bytes[line_count] = (byte) c;
94 } catch (InterruptedException e) {
98 public void flush_output() {
101 libaltos.altos_flush(altos);
105 JDialog timeout_dialog;
107 private void start_timeout_dialog_internal() {
109 Object[] options = { "Cancel" };
111 JOptionPane pane = new JOptionPane();
112 pane.setMessage(String.format("Connecting to %s, %7.3f MHz", device.toShortString(), frequency));
113 pane.setOptions(options);
114 pane.setInitialValue(null);
116 timeout_dialog = pane.createDialog(frame, "Connecting...");
118 timeout_dialog.setVisible(true);
120 Object o = pane.getValue();
123 if (options[0].equals(o))
125 timeout_dialog.dispose();
126 timeout_dialog = null;
130 * These are required by the AltosLink implementation
133 public boolean can_cancel_reply() {
135 * Can cancel any replies not called from the dispatch thread
137 return !SwingUtilities.isEventDispatchThread();
140 public boolean show_reply_timeout() {
141 if (!SwingUtilities.isEventDispatchThread() && frame != null) {
142 Runnable r = new Runnable() {
144 start_timeout_dialog_internal();
147 SwingUtilities.invokeLater(r);
153 public void hide_reply_timeout() {
154 Runnable r = new Runnable() {
156 timeout_dialog.setVisible(false);
159 SwingUtilities.invokeLater(r);
162 public void close() {
166 } catch (InterruptedException ie) {
170 System.out.printf("Uh-oh. Closing active serial device\n");
173 libaltos.altos_close(altos);
175 if (input_thread != null) {
177 input_thread.interrupt();
179 } catch (InterruptedException e) {
184 libaltos.altos_free(altos);
187 synchronized (devices_opened) {
188 devices_opened.remove(device.getPath());
191 System.out.printf("Closing %s\n", device.getPath());
194 private void putc(char c) {
196 libaltos.altos_putchar(altos, c);
199 public void print(String data) {
200 for (int i = 0; i < data.length(); i++)
201 putc(data.charAt(i));
204 private void open() throws FileNotFoundException, AltosSerialInUseException {
205 synchronized (devices_opened) {
206 if (devices_opened.contains(device.getPath()))
207 throw new AltosSerialInUseException(device);
208 devices_opened.add(device.getPath());
210 altos = device.open();
212 final String message = device.getErrorString();
214 throw new FileNotFoundException(String.format("%s (%s)",
215 device.toShortString(), message));
218 System.out.printf("Open %s\n", device.getPath());
219 input_thread = new Thread(this);
220 input_thread.start();
226 public void set_frame(Frame in_frame) {
230 public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
233 serial = device.getSerial();
234 name = device.toShortString();