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.*;
30 import java.awt.event.*;
32 import javax.swing.filechooser.FileNameExtensionFilter;
33 import javax.swing.table.*;
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
43 public class AltosSerial implements Runnable {
45 static java.util.List<String> devices_opened = Collections.synchronizedList(new LinkedList<String>());
48 SWIGTYPE_p_altos_file altos;
49 LinkedList<LinkedBlockingQueue<AltosLine>> monitors;
50 LinkedBlockingQueue<AltosLine> reply_queue;
60 LinkedList<String> pending_output = new LinkedList<String>();
62 AltosConfigData config_data;
64 static void set_debug(boolean new_debug) {
73 c = libaltos.altos_getchar(altos, 0);
74 if (Thread.interrupted())
76 if (c == libaltosConstants.LIBALTOS_ERROR) {
77 for (int e = 0; e < monitors.size(); e++) {
78 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
79 q.put(new AltosLine());
81 reply_queue.put (new AltosLine());
84 if (c == libaltosConstants.LIBALTOS_TIMEOUT)
90 if (line_count != 0) {
92 line = new String(line_bytes, 0, line_count, "UTF-8");
93 } catch (UnsupportedEncodingException ue) {
95 for (int i = 0; i < line_count; i++)
96 line = line + line_bytes[i];
99 System.out.printf("\t\t\t\t\t%s\n", line);
100 if (line.startsWith("TELEM") || line.startsWith("VERSION") || line.startsWith("CRC")) {
101 for (int e = 0; e < monitors.size(); e++) {
102 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
103 q.put(new AltosLine (line));
106 reply_queue.put(new AltosLine (line));
112 if (line_bytes == null) {
113 line_bytes = new byte[256];
114 } else if (line_count == line_bytes.length) {
115 byte[] new_line_bytes = new byte[line_count * 2];
116 System.arraycopy(line_bytes, 0, new_line_bytes, 0, line_count);
117 line_bytes = new_line_bytes;
119 line_bytes[line_count] = (byte) c;
124 } catch (InterruptedException e) {
128 public void flush_output() {
130 for (String s : pending_output)
132 pending_output.clear();
133 libaltos.altos_flush(altos);
138 JDialog timeout_dialog;
139 boolean timeout_started = false;
141 private void stop_timeout_dialog() {
142 if (timeout_started) {
143 timeout_started = false;
144 Runnable r = new Runnable() {
146 timeout_dialog.setVisible(false);
149 SwingUtilities.invokeLater(r);
153 private void start_timeout_dialog_internal() {
155 Object[] options = { "Cancel" };
157 JOptionPane pane = new JOptionPane();
158 pane.setMessage(String.format("Connecting to %s, %7.3f MHz", device.toShortString(), frequency));
159 pane.setOptions(options);
160 pane.setInitialValue(null);
162 timeout_dialog = pane.createDialog(frame, "Connecting...");
164 timeout_dialog.setVisible(true);
166 Object o = pane.getValue();
169 if (options[0].equals(o))
171 timeout_dialog.dispose();
172 timeout_dialog = null;
175 private boolean check_timeout() {
176 if (!timeout_started && frame != null) {
177 if (!SwingUtilities.isEventDispatchThread()) {
178 timeout_started = true;
179 Runnable r = new Runnable() {
181 start_timeout_dialog_internal();
184 SwingUtilities.invokeLater(r);
190 public void flush_input() {
199 Thread.sleep(timeout);
200 } catch (InterruptedException ie) {
202 got_some = !reply_queue.isEmpty();
204 if (!"VERSION".startsWith(line) &&
205 !line.startsWith("VERSION"))
214 public String get_reply(int timeout) throws InterruptedException {
215 boolean can_cancel = true;
221 if (SwingUtilities.isEventDispatchThread()) {
224 System.out.printf("Uh-oh, reading remote serial device from swing thread\n");
227 if (remote && can_cancel) {
231 timeout_started = false;
233 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
235 stop_timeout_dialog();
239 if (!remote || !can_cancel || check_timeout()) {
250 public String get_reply() throws InterruptedException {
251 return get_reply(5000);
254 public String get_reply_no_dialog(int timeout) throws InterruptedException, TimeoutException {
256 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
262 public void add_monitor(LinkedBlockingQueue<AltosLine> q) {
267 public void remove_monitor(LinkedBlockingQueue<AltosLine> q) {
269 if (monitors.isEmpty())
273 public void close() {
277 System.out.printf("Uh-oh. Closing active serial device\n");
280 libaltos.altos_close(altos);
282 if (input_thread != null) {
284 input_thread.interrupt();
286 } catch (InterruptedException e) {
291 libaltos.altos_free(altos);
294 synchronized (devices_opened) {
295 devices_opened.remove(device.getPath());
298 System.out.printf("Closing %s\n", device.getPath());
301 private void putc(char c) {
303 libaltos.altos_putchar(altos, c);
306 public void print(String data) {
308 pending_output.add(data);
309 for (int i = 0; i < data.length(); i++)
310 putc(data.charAt(i));
313 public void printf(String format, Object ... arguments) {
314 print(String.format(format, arguments));
317 private void open() throws FileNotFoundException, AltosSerialInUseException {
318 synchronized (devices_opened) {
319 if (devices_opened.contains(device.getPath()))
320 throw new AltosSerialInUseException(device);
321 devices_opened.add(device.getPath());
323 altos = device.open();
326 throw new FileNotFoundException(device.toShortString());
329 System.out.printf("Open %s\n", device.getPath());
330 input_thread = new Thread(this);
331 input_thread.start();
337 private int telemetry_len() {
338 return Altos.telemetry_len(telemetry);
341 private void set_channel(int channel) {
344 printf("m 0\nc r %d\nm %x\n",
345 channel, telemetry_len());
347 printf("c r %d\n", channel);
352 private void set_radio_setting(int setting) {
355 printf("m 0\nc R %d\nc r 0\nm %x\n",
356 setting, telemetry_len());
358 printf("c R %d\nc r 0\n", setting);
363 public void set_radio_frequency(double frequency,
367 set_radio_setting(AltosConvert.radio_frequency_to_setting(frequency, cal));
369 set_channel(AltosConvert.radio_frequency_to_channel(frequency));
372 public void set_radio_frequency(double in_frequency) throws InterruptedException, TimeoutException {
373 frequency = in_frequency;
375 set_radio_frequency(frequency,
376 config_data.radio_setting != 0,
377 config_data.radio_calibration);
380 public void set_telemetry(int in_telemetry) {
381 telemetry = in_telemetry;
384 printf("m 0\nm %x\n", telemetry_len());
389 void set_monitor(boolean monitor) {
390 monitor_mode = monitor;
393 printf("m %x\n", telemetry_len());
400 public void set_callsign(String callsign) {
402 printf ("c c %s\n", callsign);
407 public AltosConfigData config_data() throws InterruptedException, TimeoutException {
408 if (config_data == null)
409 config_data = new AltosConfigData(this);
413 public void start_remote() throws TimeoutException, InterruptedException {
415 System.out.printf("start remote\n");
416 if (frequency == 0.0)
417 frequency = AltosPreferences.frequency(device.getSerial());
418 set_radio_frequency(frequency);
419 set_callsign(AltosPreferences.callsign());
425 public void stop_remote() {
427 System.out.printf("stop remote\n");
437 public void set_frame(Frame in_frame) {
441 public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
444 monitor_mode = false;
446 telemetry = Altos.ao_telemetry_standard;
447 monitors = new LinkedList<LinkedBlockingQueue<AltosLine>> ();
448 reply_queue = new LinkedBlockingQueue<AltosLine> ();