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 extends AltosLink implements Runnable {
46 static java.util.List<String> devices_opened = Collections.synchronizedList(new LinkedList<String>());
49 SWIGTYPE_p_altos_file altos;
58 byte[] line_bytes = null;
63 c = libaltos.altos_getchar(altos, 0);
64 if (Thread.interrupted())
66 if (c == libaltosConstants.LIBALTOS_ERROR) {
67 add_telem (new AltosLine());
68 add_reply (new AltosLine());
71 if (c == libaltosConstants.LIBALTOS_TIMEOUT)
77 if (line_count != 0) {
78 add_bytes(line_bytes, line_count);
82 if (line_bytes == null) {
83 line_bytes = new byte[256];
84 } else if (line_count == line_bytes.length) {
85 byte[] new_line_bytes = new byte[line_count * 2];
86 System.arraycopy(line_bytes, 0, new_line_bytes, 0, line_count);
87 line_bytes = new_line_bytes;
89 line_bytes[line_count] = (byte) c;
94 } catch (InterruptedException e) {
98 public void flush_output() {
101 libaltos.altos_flush(altos);
106 JDialog timeout_dialog;
107 boolean timeout_started = false;
109 private void stop_timeout_dialog() {
110 if (timeout_started) {
111 timeout_started = false;
112 Runnable r = new Runnable() {
114 timeout_dialog.setVisible(false);
117 SwingUtilities.invokeLater(r);
121 private void start_timeout_dialog_internal() {
123 Object[] options = { "Cancel" };
125 JOptionPane pane = new JOptionPane();
126 pane.setMessage(String.format("Connecting to %s, %7.3f MHz", device.toShortString(), frequency));
127 pane.setOptions(options);
128 pane.setInitialValue(null);
130 timeout_dialog = pane.createDialog(frame, "Connecting...");
132 timeout_dialog.setVisible(true);
134 Object o = pane.getValue();
137 if (options[0].equals(o))
139 timeout_dialog.dispose();
140 timeout_dialog = null;
143 private boolean check_timeout() {
144 if (!timeout_started && frame != null) {
145 if (!SwingUtilities.isEventDispatchThread()) {
146 timeout_started = true;
147 Runnable r = new Runnable() {
149 start_timeout_dialog_internal();
152 SwingUtilities.invokeLater(r);
158 public void flush_input() throws InterruptedException {
167 public String get_reply(int timeout) throws InterruptedException {
168 boolean can_cancel = true;
174 if (SwingUtilities.isEventDispatchThread()) {
177 System.out.printf("Uh-oh, reading remote serial device from swing thread\n");
180 if (remote && can_cancel) {
184 timeout_started = false;
186 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
188 stop_timeout_dialog();
192 if (!remote || !can_cancel || check_timeout()) {
203 public void close() {
207 } catch (InterruptedException ie) {
211 System.out.printf("Uh-oh. Closing active serial device\n");
214 libaltos.altos_close(altos);
216 if (input_thread != null) {
218 input_thread.interrupt();
220 } catch (InterruptedException e) {
225 libaltos.altos_free(altos);
228 synchronized (devices_opened) {
229 devices_opened.remove(device.getPath());
232 System.out.printf("Closing %s\n", device.getPath());
235 private void putc(char c) {
237 libaltos.altos_putchar(altos, c);
240 public void print(String data) {
241 for (int i = 0; i < data.length(); i++)
242 putc(data.charAt(i));
245 private void open() throws FileNotFoundException, AltosSerialInUseException {
246 synchronized (devices_opened) {
247 if (devices_opened.contains(device.getPath()))
248 throw new AltosSerialInUseException(device);
249 devices_opened.add(device.getPath());
251 altos = device.open();
253 final String message = device.getErrorString();
255 throw new FileNotFoundException(String.format("%s (%s)",
256 device.toShortString(), message));
259 System.out.printf("Open %s\n", device.getPath());
260 input_thread = new Thread(this);
261 input_thread.start();
267 private int telemetry_len() {
268 return Altos.telemetry_len(telemetry);
271 private void set_channel(int channel) {
274 printf("m 0\nc r %d\nm %x\n",
275 channel, telemetry_len());
277 printf("c r %d\n", channel);
282 private void set_radio_setting(int setting) {
285 printf("m 0\nc R %d\nm %x\n",
286 setting, telemetry_len());
288 printf("c R %d\n", setting);
293 private void set_radio_freq(int frequency) {
296 printf("m 0\nc F %d\nm %x\n",
297 frequency, telemetry_len());
299 printf("c F %d\n", frequency);
304 public void set_radio_frequency(double frequency,
305 boolean has_frequency,
309 System.out.printf("set_radio_frequency %7.3f (freq %b) (set %b) %d\n", frequency, has_frequency, has_setting, cal);
313 set_radio_freq((int) Math.floor (frequency * 1000));
314 else if (has_setting)
315 set_radio_setting(AltosConvert.radio_frequency_to_setting(frequency, cal));
317 set_channel(AltosConvert.radio_frequency_to_channel(frequency));
320 public void set_frame(Frame in_frame) {
324 public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
327 serial = device.getSerial();
328 name = device.toShortString();