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
24 import java.lang.String;
25 import java.lang.System;
26 import java.lang.Character;
27 import java.io.FileInputStream;
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 import java.io.FileNotFoundException;
31 import java.util.concurrent.LinkedBlockingQueue;
32 import java.lang.InterruptedException;
33 import java.util.LinkedList;
34 import altosui.AltosSerialMonitor;
35 import java.util.Iterator;
38 * This class reads from the serial port and places each received
39 * line in a queue. Dealing with that queue is left up to other
42 class AltosSerialReader implements Runnable {
43 FileInputStream serial_in;
44 LinkedBlockingQueue<String> monitor_queue;
45 LinkedBlockingQueue<String> reply_queue;
52 while ((c = serial_in.read()) != -1) {
58 if (line.startsWith("VERSION"))
59 monitor_queue.put(line);
61 reply_queue.put(line);
65 line = line + (char) c;
69 } catch (IOException e) {
70 } catch (InterruptedException e) {
74 public String get_telem() {
76 return monitor_queue.take();
77 } catch (InterruptedException e) {
82 public String get_reply() {
84 return reply_queue.take();
85 } catch (InterruptedException e) {
90 public void flush () {
92 if (!"VERSION".startsWith(line) && !line.startsWith("VERSION"))
97 public AltosSerialReader (FileInputStream in) {
99 monitor_queue = new LinkedBlockingQueue<String> ();
100 reply_queue = new LinkedBlockingQueue<String> ();
106 public class AltosSerial implements Runnable {
107 FileInputStream serial_in = null;
108 FileOutputStream serial_out = null;
109 AltosSerialReader reader;
110 LinkedList<AltosSerialMonitor> callbacks;
114 String s = reader.get_reply();
115 synchronized(callbacks) {
116 Iterator<AltosSerialMonitor> i = callbacks.iterator();
117 while (i.hasNext()) {
124 public void start () {
126 serial_out.write('?');
127 serial_out.write('\r');
128 } catch (IOException e) {
130 (new Thread(reader)).start();
131 (new Thread(this)).start();
134 public void monitor(AltosSerialMonitor monitor) {
135 synchronized(callbacks) {
136 callbacks.add(monitor);
140 public AltosSerial(String serial_name) {
142 serial_in = new FileInputStream(serial_name);
143 serial_out = new FileOutputStream(serial_name);
144 reader = new AltosSerialReader(serial_in);
145 callbacks = new LinkedList<AltosSerialMonitor>();
146 } catch (FileNotFoundException e) {