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
28 import org.altusmetrum.AltosLib.*;
33 * This class reads from the serial port and places each received
34 * line in a queue. Dealing with that queue is left up to other
38 public class AltosSerial extends AltosLink {
40 static java.util.List<String> devices_opened = Collections.synchronizedList(new LinkedList<String>());
43 SWIGTYPE_p_altos_file altos;
50 public int getchar() {
53 return libaltos.altos_getchar(altos, 0);
56 public void flush_output() {
59 if (libaltos.altos_flush(altos) != 0) {
60 libaltos.altos_close(altos);
67 JDialog timeout_dialog;
69 private void start_timeout_dialog_internal() {
71 Object[] options = { "Cancel" };
73 JOptionPane pane = new JOptionPane();
74 pane.setMessage(String.format("Connecting to %s, %7.3f MHz", device.toShortString(), frequency));
75 pane.setOptions(options);
76 pane.setInitialValue(null);
78 timeout_dialog = pane.createDialog(frame, "Connecting...");
80 timeout_dialog.setVisible(true);
82 Object o = pane.getValue();
85 if (options[0].equals(o))
87 timeout_dialog.dispose();
88 timeout_dialog = null;
92 * These are required by the AltosLink implementation
95 public boolean can_cancel_reply() {
97 * Can cancel any replies not called from the dispatch thread
99 return !SwingUtilities.isEventDispatchThread();
102 public boolean show_reply_timeout() {
103 if (!SwingUtilities.isEventDispatchThread() && frame != null) {
104 Runnable r = new Runnable() {
106 start_timeout_dialog_internal();
109 SwingUtilities.invokeLater(r);
115 public void hide_reply_timeout() {
116 Runnable r = new Runnable() {
118 timeout_dialog.setVisible(false);
121 SwingUtilities.invokeLater(r);
124 public void close() {
128 } catch (InterruptedException ie) {
132 System.out.printf("Uh-oh. Closing active serial device\n");
135 libaltos.altos_close(altos);
137 if (input_thread != null) {
139 input_thread.interrupt();
141 } catch (InterruptedException e) {
146 libaltos.altos_free(altos);
149 synchronized (devices_opened) {
150 devices_opened.remove(device.getPath());
153 System.out.printf("Closing %s\n", device.getPath());
156 private void putc(char c) {
158 if (libaltos.altos_putchar(altos, c) != 0) {
159 libaltos.altos_close(altos);
165 public void print(String data) {
166 for (int i = 0; i < data.length(); i++)
167 putc(data.charAt(i));
170 private void open() throws FileNotFoundException, AltosSerialInUseException {
171 synchronized (devices_opened) {
172 if (devices_opened.contains(device.getPath()))
173 throw new AltosSerialInUseException(device);
174 devices_opened.add(device.getPath());
176 altos = device.open();
178 final String message = device.getErrorString();
180 throw new FileNotFoundException(String.format("%s (%s)",
181 device.toShortString(), message));
184 System.out.printf("Open %s\n", device.getPath());
185 input_thread = new Thread(this);
186 input_thread.start();
192 public void set_frame(Frame in_frame) {
196 public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
199 serial = device.getSerial();
200 name = device.toShortString();