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;
32 * This class reads from the serial port and places each received
33 * line in a queue. Dealing with that queue is left up to other
36 class AltosSerialReader implements Runnable {
37 FileInputStream serial_in;
38 LinkedBlockingQueue<String> monitor_queue;
39 LinkedBlockingQueue<String> reply_queue;
47 while ((c = serial_in.read()) != -1) {
53 if (line.startsWith("VERSION"))
54 monitor_queue.put(line);
56 reply_queue.put(line);
60 line = line + (char) c;
64 } catch (IOException e) {
65 } catch (InterruptedException e) {
69 public String get_telem() throws InterruptedException {
70 return monitor_queue.take();
73 public String get_reply() throws InterruptedException {
74 return reply_queue.take();
77 public void flush () {
79 if (!"VERSION".startsWith(line) && !line.startsWith("VERSION"))
85 public boolean opened() {
86 return serial_in != null;
90 if (serial_in != null) {
93 } catch (IOException e) {
97 if (input_thread != null) {
100 } catch (InterruptedException e) {
106 public void open(File name) throws FileNotFoundException {
108 serial_in = new FileInputStream(name);
109 input_thread = new Thread(this);
110 input_thread.start();
112 public AltosSerialReader () {
116 monitor_queue = new LinkedBlockingQueue<String> ();
117 reply_queue = new LinkedBlockingQueue<String> ();
122 public class AltosSerial implements Runnable {
123 FileOutputStream serial_out = null;
124 Thread monitor_thread = null;
125 AltosSerialReader reader = null;
126 LinkedList<AltosSerialMonitor> callbacks;
131 String s = reader.get_telem();
132 synchronized(callbacks) {
133 Iterator<AltosSerialMonitor> i = callbacks.iterator();
134 while (i.hasNext()) {
139 } catch (InterruptedException e) {
143 boolean need_monitor() {
144 return reader.opened() && !callbacks.isEmpty();
147 void maybe_stop_monitor() {
148 if (!need_monitor() && monitor_thread != null) {
149 monitor_thread.interrupt();
151 monitor_thread.join();
152 } catch (InterruptedException e) {
154 monitor_thread = null;
159 void maybe_start_monitor() {
160 if (need_monitor() && monitor_thread == null) {
161 monitor_thread = new Thread(this);
162 monitor_thread.start();
166 public void monitor(AltosSerialMonitor monitor) {
167 synchronized(callbacks) {
168 callbacks.add(monitor);
169 maybe_start_monitor();
174 public void unmonitor(AltosSerialMonitor monitor) {
175 synchronized(callbacks) {
176 callbacks.remove(monitor);
177 maybe_stop_monitor();
181 public void close() {
182 synchronized(callbacks) {
184 maybe_stop_monitor();
188 public void open(File serial_name) throws FileNotFoundException {
189 reader.open(serial_name);
190 serial_out = new FileOutputStream(serial_name);
192 serial_out.write('?');
193 serial_out.write('\r');
194 } catch (IOException e) {
199 reader = new AltosSerialReader();
200 callbacks = new LinkedList<AltosSerialMonitor>();
203 public AltosSerial() {
207 public AltosSerial(File serial_name) throws FileNotFoundException {