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.LinkedBlockingQueue;
27 import java.util.LinkedList;
28 import java.util.Iterator;
29 import altosui.AltosSerialMonitor;
30 import libaltosJNI.libaltos;
31 import libaltosJNI.altos_device;
32 import libaltosJNI.SWIGTYPE_p_altos_file;
33 import libaltosJNI.SWIGTYPE_p_altos_list;
36 * This class reads from the serial port and places each received
37 * line in a queue. Dealing with that queue is left up to other
41 public class AltosSerial implements Runnable {
43 SWIGTYPE_p_altos_file altos;
44 LinkedList<LinkedBlockingQueue<String>> monitors;
45 LinkedBlockingQueue<String> reply_queue;
54 c = libaltos.altos_getchar(altos, 0);
55 if (Thread.interrupted())
64 if (line.startsWith("VERSION")) {
65 for (int e = 0; e < monitors.size(); e++) {
66 LinkedBlockingQueue<String> q = monitors.get(e);
70 // System.out.printf("GOT: %s\n", line);
71 reply_queue.put(line);
76 line = line + (char) c;
80 } catch (InterruptedException e) {
84 public void flush_reply() {
85 libaltos.altos_flush(altos);
88 } catch (InterruptedException ie) {
93 public String get_reply() throws InterruptedException {
94 libaltos.altos_flush(altos);
95 String line = reply_queue.take();
99 public void add_monitor(LinkedBlockingQueue<String> q) {
103 public void remove_monitor(LinkedBlockingQueue<String> q) {
107 public void flush () {
109 if (!"VERSION".startsWith(line) && !line.startsWith("VERSION"))
115 public boolean opened() {
116 return altos != null;
119 public void close() {
121 libaltos.altos_close(altos);
122 if (input_thread != null) {
124 input_thread.interrupt();
126 } catch (InterruptedException e) {
131 libaltos.altos_free(altos);
136 public void putc(char c) {
138 libaltos.altos_putchar(altos, c);
141 public void print(String data) {
142 // System.out.printf("\"%s\" ", data);
143 for (int i = 0; i < data.length(); i++)
144 putc(data.charAt(i));
147 public void printf(String format, Object ... arguments) {
148 print(String.format(format, arguments));
151 public void open(altos_device device) throws FileNotFoundException {
153 altos = libaltos.altos_open(device);
155 throw new FileNotFoundException(device.getPath());
156 input_thread = new Thread(this);
157 input_thread.start();
161 } catch (InterruptedException e) {
166 public void set_channel(int channel) {
168 printf("m 0\nc r %d\nm 1\n", channel);
171 public void set_callsign(String callsign) {
173 printf ("c c %s\n", callsign);
176 public AltosSerial() {
180 monitors = new LinkedList<LinkedBlockingQueue<String>> ();
181 reply_queue = new LinkedBlockingQueue<String> ();