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() throws InterruptedException {
198 Thread.sleep(timeout);
199 got_some = !reply_queue.isEmpty();
201 if (!"VERSION".startsWith(line) &&
202 !line.startsWith("VERSION"))
211 public String get_reply(int timeout) throws InterruptedException {
212 boolean can_cancel = true;
218 if (SwingUtilities.isEventDispatchThread()) {
221 System.out.printf("Uh-oh, reading remote serial device from swing thread\n");
224 if (remote && can_cancel) {
228 timeout_started = false;
230 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
232 stop_timeout_dialog();
236 if (!remote || !can_cancel || check_timeout()) {
247 public String get_reply() throws InterruptedException {
248 return get_reply(5000);
251 public String get_reply_no_dialog(int timeout) throws InterruptedException, TimeoutException {
253 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
259 public void add_monitor(LinkedBlockingQueue<AltosLine> q) {
264 public void remove_monitor(LinkedBlockingQueue<AltosLine> q) {
266 if (monitors.isEmpty())
270 public void close() {
274 } catch (InterruptedException ie) {
278 System.out.printf("Uh-oh. Closing active serial device\n");
281 libaltos.altos_close(altos);
283 if (input_thread != null) {
285 input_thread.interrupt();
287 } catch (InterruptedException e) {
292 libaltos.altos_free(altos);
295 synchronized (devices_opened) {
296 devices_opened.remove(device.getPath());
299 System.out.printf("Closing %s\n", device.getPath());
302 private void putc(char c) {
304 libaltos.altos_putchar(altos, c);
307 public void print(String data) {
309 pending_output.add(data);
310 for (int i = 0; i < data.length(); i++)
311 putc(data.charAt(i));
314 public void printf(String format, Object ... arguments) {
315 print(String.format(format, arguments));
318 private void open() throws FileNotFoundException, AltosSerialInUseException {
319 synchronized (devices_opened) {
320 if (devices_opened.contains(device.getPath()))
321 throw new AltosSerialInUseException(device);
322 devices_opened.add(device.getPath());
324 altos = device.open();
327 throw new FileNotFoundException(device.toShortString());
330 System.out.printf("Open %s\n", device.getPath());
331 input_thread = new Thread(this);
332 input_thread.start();
338 private int telemetry_len() {
339 return Altos.telemetry_len(telemetry);
342 private void set_channel(int channel) {
345 printf("m 0\nc r %d\nm %x\n",
346 channel, telemetry_len());
348 printf("c r %d\n", channel);
353 private void set_radio_setting(int setting) {
356 printf("m 0\nc R %d\nm %x\n",
357 setting, telemetry_len());
359 printf("c R %d\n", setting);
364 public void set_radio_frequency(double frequency,
368 System.out.printf("set_radio_frequency %7.3f %b %d\n", frequency, has_setting, cal);
370 set_radio_setting(AltosConvert.radio_frequency_to_setting(frequency, cal));
372 set_channel(AltosConvert.radio_frequency_to_channel(frequency));
375 public void set_radio_frequency(double in_frequency) throws InterruptedException, TimeoutException {
376 frequency = in_frequency;
378 set_radio_frequency(frequency,
379 config_data.radio_setting != 0,
380 config_data.radio_calibration);
383 public void set_telemetry(int in_telemetry) {
384 telemetry = in_telemetry;
387 printf("m 0\nm %x\n", telemetry_len());
392 void set_monitor(boolean monitor) {
393 monitor_mode = monitor;
396 printf("m %x\n", telemetry_len());
403 public void set_callsign(String callsign) {
405 printf ("c c %s\n", callsign);
410 public AltosConfigData config_data() throws InterruptedException, TimeoutException {
411 if (config_data == null)
412 config_data = new AltosConfigData(this);
416 public void start_remote() throws TimeoutException, InterruptedException {
418 System.out.printf("start remote %7.3f\n", frequency);
419 if (frequency == 0.0)
420 frequency = AltosPreferences.frequency(device.getSerial());
421 set_radio_frequency(frequency);
422 set_callsign(AltosPreferences.callsign());
428 public void stop_remote() throws InterruptedException {
430 System.out.printf("stop remote\n");
440 public void set_frame(Frame in_frame) {
444 public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
447 monitor_mode = false;
449 telemetry = Altos.ao_telemetry_standard;
450 monitors = new LinkedList<LinkedBlockingQueue<AltosLine>> ();
451 reply_queue = new LinkedBlockingQueue<AltosLine> ();