Add telemetry data parsing code
[fw/altos] / ao-tools / altosui / AltosSerial.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 /*
19  * Deal with TeleDongle on a serial port
20  */
21
22 package altosui;
23
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;
36
37 /*
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
40  * threads.
41  */
42 class AltosSerialReader implements Runnable {
43         FileInputStream serial_in;
44         LinkedBlockingQueue<String> monitor_queue;
45         LinkedBlockingQueue<String> reply_queue;
46         String line;
47
48         public void run () {
49                 int c;
50
51                 try {
52                         while ((c = serial_in.read()) != -1) {
53                                 if (c == '\r')
54                                         continue;
55                                 synchronized(this) {
56                                         if (c == '\n') {
57                                                 if (line != "") {
58                                                         if (line.startsWith("VERSION"))
59                                                                 monitor_queue.put(line);
60                                                         else
61                                                                 reply_queue.put(line);
62                                                         line = "";
63                                                 }
64                                         } else {
65                                                 line = line + (char) c;
66                                         }
67                                 }
68                         }
69                 } catch (IOException e) {
70                 } catch (InterruptedException e) {
71                 }
72         }
73
74         public String get_telem() {
75                 try {
76                         return monitor_queue.take();
77                 } catch (InterruptedException e) {
78                         return "";
79                 }
80         }
81
82         public String get_reply() {
83                 try {
84                         return reply_queue.take();
85                 } catch (InterruptedException e) {
86                         return "";
87                 }
88         }
89
90         public void flush () {
91                 synchronized(this) {
92                         if (!"VERSION".startsWith(line) && !line.startsWith("VERSION"))
93                                 line = "";
94                         reply_queue.clear();
95                 }
96         }
97         public AltosSerialReader (FileInputStream in) {
98                 serial_in = in;
99                 monitor_queue = new LinkedBlockingQueue<String> ();
100                 reply_queue = new LinkedBlockingQueue<String> ();
101                 line = "";
102         }
103
104 }
105
106 public class AltosSerial implements Runnable {
107         FileInputStream serial_in = null;
108         FileOutputStream serial_out = null;
109         AltosSerialReader reader;
110         LinkedList<AltosSerialMonitor> callbacks;
111
112         public void run() {
113                 for (;;) {
114                         String s = reader.get_reply();
115                         synchronized(callbacks) {
116                                 Iterator<AltosSerialMonitor> i = callbacks.iterator();
117                                 while (i.hasNext()) {
118                                         i.next().data(s);
119                                 }
120                         }
121                 }
122         }
123
124         public void start () {
125                 try {
126                         serial_out.write('?');
127                         serial_out.write('\r');
128                 } catch (IOException e) {
129                 }
130                 (new Thread(reader)).start();
131                 (new Thread(this)).start();
132         }
133
134         public void monitor(AltosSerialMonitor monitor) {
135                 synchronized(callbacks) {
136                         callbacks.add(monitor);
137                 }
138         }
139
140         public AltosSerial(String serial_name) {
141                 try {
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) {
147                 }
148         }
149 }