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();
268 private int telemetry_len() {
269 return Altos.telemetry_len(telemetry);
272 private void set_channel(int channel) {
275 printf("m 0\nc r %d\nm %x\n",
276 channel, telemetry_len());
278 printf("c r %d\n", channel);
283 private void set_radio_setting(int setting) {
286 printf("m 0\nc R %d\nm %x\n",
287 setting, telemetry_len());
289 printf("c R %d\n", setting);
294 private void set_radio_freq(int frequency) {
297 printf("m 0\nc F %d\nm %x\n",
298 frequency, telemetry_len());
300 printf("c F %d\n", frequency);
305 public void set_radio_frequency(double frequency,
306 boolean has_frequency,
310 System.out.printf("set_radio_frequency %7.3f (freq %b) (set %b) %d\n", frequency, has_frequency, has_setting, cal);
314 set_radio_freq((int) Math.floor (frequency * 1000));
315 else if (has_setting)
316 set_radio_setting(AltosConvert.radio_frequency_to_setting(frequency, cal));
318 set_channel(AltosConvert.radio_frequency_to_channel(frequency));
321 public void set_radio_frequency(double in_frequency) throws InterruptedException, TimeoutException {
322 frequency = in_frequency;
323 if (frequency == 0.0)
324 frequency = AltosPreferences.frequency(device.getSerial());
326 set_radio_frequency(frequency,
327 config_data.radio_frequency != 0,
328 config_data.radio_setting != 0,
329 config_data.radio_calibration);
332 public void set_telemetry(int in_telemetry) {
333 telemetry = in_telemetry;
336 printf("m 0\nm %x\n", telemetry_len());
341 void set_monitor(boolean monitor) {
342 monitor_mode = monitor;
345 printf("m %x\n", telemetry_len());
352 public void set_callsign(String callsign) {
354 printf ("c c %s\n", callsign);
359 public AltosConfigData config_data() throws InterruptedException, TimeoutException {
360 if (config_data == null)
361 config_data = new AltosConfigData(this);
365 public void start_remote() throws TimeoutException, InterruptedException {
367 System.out.printf("start remote %7.3f\n", frequency);
368 if (frequency == 0.0)
369 frequency = AltosUIPreferences.frequency(device.getSerial());
370 set_radio_frequency(frequency);
371 set_callsign(AltosUIPreferences.callsign());
377 public void stop_remote() throws InterruptedException {
379 System.out.printf("stop remote\n");
390 >>>>>>> bc5e669... altosui: Pull most of AltosSerial into AltosLink
391 public void set_frame(Frame in_frame) {
395 public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
398 serial = device.getSerial();