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.*;
34 import org.altusmetrum.AltosLib.*;
39 * This class reads from the serial port and places each received
40 * line in a queue. Dealing with that queue is left up to other
44 public class AltosSerial implements Runnable, AltosLink {
46 static java.util.List<String> devices_opened = Collections.synchronizedList(new LinkedList<String>());
49 SWIGTYPE_p_altos_file altos;
50 LinkedList<LinkedBlockingQueue<AltosLine>> monitors;
51 LinkedBlockingQueue<AltosLine> reply_queue;
59 public static boolean debug;
61 LinkedList<String> pending_output = new LinkedList<String>();
63 AltosConfigData config_data;
65 static void set_debug(boolean new_debug) {
74 c = libaltos.altos_getchar(altos, 0);
75 if (Thread.interrupted())
77 if (c == libaltosConstants.LIBALTOS_ERROR) {
78 for (int e = 0; e < monitors.size(); e++) {
79 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
80 q.put(new AltosLine());
82 reply_queue.put (new AltosLine());
85 if (c == libaltosConstants.LIBALTOS_TIMEOUT)
91 if (line_count != 0) {
93 line = new String(line_bytes, 0, line_count, "UTF-8");
94 } catch (UnsupportedEncodingException ue) {
96 for (int i = 0; i < line_count; i++)
97 line = line + line_bytes[i];
100 System.out.printf("\t\t\t\t\t%s\n", line);
101 if (line.startsWith("TELEM") || line.startsWith("VERSION") || line.startsWith("CRC")) {
102 for (int e = 0; e < monitors.size(); e++) {
103 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
104 q.put(new AltosLine (line));
107 reply_queue.put(new AltosLine (line));
113 if (line_bytes == null) {
114 line_bytes = new byte[256];
115 } else if (line_count == line_bytes.length) {
116 byte[] new_line_bytes = new byte[line_count * 2];
117 System.arraycopy(line_bytes, 0, new_line_bytes, 0, line_count);
118 line_bytes = new_line_bytes;
120 line_bytes[line_count] = (byte) c;
125 } catch (InterruptedException e) {
129 public void flush_output() {
131 for (String s : pending_output)
133 pending_output.clear();
134 libaltos.altos_flush(altos);
139 JDialog timeout_dialog;
140 boolean timeout_started = false;
142 private void stop_timeout_dialog() {
143 if (timeout_started) {
144 timeout_started = false;
145 Runnable r = new Runnable() {
147 timeout_dialog.setVisible(false);
150 SwingUtilities.invokeLater(r);
154 private void start_timeout_dialog_internal() {
156 Object[] options = { "Cancel" };
158 JOptionPane pane = new JOptionPane();
159 pane.setMessage(String.format("Connecting to %s, %7.3f MHz", device.toShortString(), frequency));
160 pane.setOptions(options);
161 pane.setInitialValue(null);
163 timeout_dialog = pane.createDialog(frame, "Connecting...");
165 timeout_dialog.setVisible(true);
167 Object o = pane.getValue();
170 if (options[0].equals(o))
172 timeout_dialog.dispose();
173 timeout_dialog = null;
176 private boolean check_timeout() {
177 if (!timeout_started && frame != null) {
178 if (!SwingUtilities.isEventDispatchThread()) {
179 timeout_started = true;
180 Runnable r = new Runnable() {
182 start_timeout_dialog_internal();
185 SwingUtilities.invokeLater(r);
191 public void flush_input() throws InterruptedException {
199 Thread.sleep(timeout);
200 got_some = !reply_queue.isEmpty();
202 if (!"VERSION".startsWith(line) &&
203 !line.startsWith("VERSION"))
212 public String get_reply(int timeout) throws InterruptedException {
213 boolean can_cancel = true;
219 if (SwingUtilities.isEventDispatchThread()) {
222 System.out.printf("Uh-oh, reading remote serial device from swing thread\n");
225 if (remote && can_cancel) {
229 timeout_started = false;
231 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
233 stop_timeout_dialog();
237 if (!remote || !can_cancel || check_timeout()) {
248 public String get_reply() throws InterruptedException {
249 return get_reply(5000);
252 public String get_reply_no_dialog(int timeout) throws InterruptedException, TimeoutException {
254 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
260 public void add_monitor(LinkedBlockingQueue<AltosLine> q) {
265 public void remove_monitor(LinkedBlockingQueue<AltosLine> q) {
267 if (monitors.isEmpty())
271 public void close() {
275 } catch (InterruptedException ie) {
279 System.out.printf("Uh-oh. Closing active serial device\n");
282 libaltos.altos_close(altos);
284 if (input_thread != null) {
286 input_thread.interrupt();
288 } catch (InterruptedException e) {
293 libaltos.altos_free(altos);
296 synchronized (devices_opened) {
297 devices_opened.remove(device.getPath());
300 System.out.printf("Closing %s\n", device.getPath());
303 private void putc(char c) {
305 libaltos.altos_putchar(altos, c);
308 public void print(String data) {
310 pending_output.add(data);
311 for (int i = 0; i < data.length(); i++)
312 putc(data.charAt(i));
315 public void printf(String format, Object ... arguments) {
316 print(String.format(format, arguments));
319 private void open() throws FileNotFoundException, AltosSerialInUseException {
320 synchronized (devices_opened) {
321 if (devices_opened.contains(device.getPath()))
322 throw new AltosSerialInUseException(device);
323 devices_opened.add(device.getPath());
325 altos = device.open();
327 final String message = device.getErrorString();
329 throw new FileNotFoundException(String.format("%s (%s)",
330 device.toShortString(), message));
333 System.out.printf("Open %s\n", device.getPath());
334 input_thread = new Thread(this);
335 input_thread.start();
341 private int telemetry_len() {
342 return Altos.telemetry_len(telemetry);
345 private void set_channel(int channel) {
348 printf("m 0\nc r %d\nm %x\n",
349 channel, telemetry_len());
351 printf("c r %d\n", channel);
356 private void set_radio_setting(int setting) {
359 printf("m 0\nc R %d\nm %x\n",
360 setting, telemetry_len());
362 printf("c R %d\n", setting);
367 private void set_radio_freq(int frequency) {
370 printf("m 0\nc F %d\nm %x\n",
371 frequency, telemetry_len());
373 printf("c F %d\n", frequency);
378 public void set_radio_frequency(double frequency,
379 boolean has_frequency,
383 System.out.printf("set_radio_frequency %7.3f (freq %b) (set %b) %d\n", frequency, has_frequency, has_setting, cal);
387 set_radio_freq((int) Math.floor (frequency * 1000));
388 else if (has_setting)
389 set_radio_setting(AltosConvert.radio_frequency_to_setting(frequency, cal));
391 set_channel(AltosConvert.radio_frequency_to_channel(frequency));
394 public void set_radio_frequency(double in_frequency) throws InterruptedException, TimeoutException {
395 frequency = in_frequency;
396 if (frequency == 0.0)
397 frequency = AltosPreferences.frequency(device.getSerial());
399 set_radio_frequency(frequency,
400 config_data.radio_frequency != 0,
401 config_data.radio_setting != 0,
402 config_data.radio_calibration);
405 public void set_telemetry(int in_telemetry) {
406 telemetry = in_telemetry;
409 printf("m 0\nm %x\n", telemetry_len());
414 void set_monitor(boolean monitor) {
415 monitor_mode = monitor;
418 printf("m %x\n", telemetry_len());
425 public void set_callsign(String callsign) {
427 printf ("c c %s\n", callsign);
432 public AltosConfigData config_data() throws InterruptedException, TimeoutException {
433 if (config_data == null)
434 config_data = new AltosConfigData(this);
438 public void start_remote() throws TimeoutException, InterruptedException {
440 System.out.printf("start remote %7.3f\n", frequency);
441 if (frequency == 0.0)
442 frequency = AltosUIPreferences.frequency(device.getSerial());
443 set_radio_frequency(frequency);
444 set_callsign(AltosUIPreferences.callsign());
450 public void stop_remote() throws InterruptedException {
452 System.out.printf("stop remote\n");
462 public void set_frame(Frame in_frame) {
466 public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
469 monitor_mode = false;
471 telemetry = Altos.ao_telemetry_standard;
472 monitors = new LinkedList<LinkedBlockingQueue<AltosLine>> ();
473 reply_queue = new LinkedBlockingQueue<AltosLine> ();