2 * Copyright © 2011 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.
18 package org.altusmetrum.altoslib_2;
21 import java.util.concurrent.*;
24 public abstract class AltosLink implements Runnable {
26 public final static int ERROR = -1;
27 public final static int TIMEOUT = -2;
29 public abstract int getchar();
30 public abstract void print(String data);
31 public abstract void close();
33 public static boolean debug = false;
34 public static void set_debug(boolean in_debug) { debug = in_debug; }
36 public boolean has_error;
38 LinkedList<String> pending_output = new LinkedList<String>();
40 public LinkedList<LinkedBlockingQueue<AltosLine>> monitors = new LinkedList<LinkedBlockingQueue<AltosLine>> ();;
41 public LinkedBlockingQueue<AltosLine> reply_queue = new LinkedBlockingQueue<AltosLine>();
43 public synchronized void add_monitor(LinkedBlockingQueue<AltosLine> q) {
48 public synchronized void remove_monitor(LinkedBlockingQueue<AltosLine> q) {
50 if (monitors.isEmpty())
54 public void printf(String format, Object ... arguments) {
55 String line = String.format(format, arguments);
57 pending_output.add(line);
61 public String get_reply_no_dialog(int timeout) throws InterruptedException, TimeoutException {
63 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
69 public String get_reply() throws InterruptedException {
70 return get_reply(5000);
74 public abstract boolean can_cancel_reply();
75 public abstract boolean show_reply_timeout();
76 public abstract void hide_reply_timeout();
78 public boolean reply_abort;
81 boolean reply_timeout_shown = false;
83 private boolean check_reply_timeout() {
84 if (!reply_timeout_shown)
85 reply_timeout_shown = show_reply_timeout();
89 private void cleanup_reply_timeout() {
90 if (reply_timeout_shown) {
91 reply_timeout_shown = false;
99 byte[] line_bytes = null;
105 if (Thread.interrupted()) {
107 System.out.printf("INTERRUPTED\n");
112 System.out.printf("ERROR\n");
114 add_telem (new AltosLine());
115 add_reply (new AltosLine());
120 System.out.printf("TIMEOUT\n");
127 if (line_count != 0) {
128 add_bytes(line_bytes, line_count);
132 if (line_bytes == null) {
133 line_bytes = new byte[256];
134 } else if (line_count == line_bytes.length) {
135 byte[] new_line_bytes = new byte[line_count * 2];
136 System.arraycopy(line_bytes, 0, new_line_bytes, 0, line_count);
137 line_bytes = new_line_bytes;
139 line_bytes[line_count] = (byte) c;
144 } catch (InterruptedException e) {
148 public String get_reply(int timeout) throws InterruptedException {
149 boolean can_cancel = can_cancel_reply();
152 if (!can_cancel && remote)
153 System.out.printf("Uh-oh, reading remote serial device from swing thread\n");
155 if (remote && can_cancel)
163 reply_timeout_shown = false;
165 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
167 cleanup_reply_timeout();
171 if (!remote || !can_cancel || check_reply_timeout()) {
182 public void add_telem(AltosLine line) throws InterruptedException {
183 for (int e = 0; e < monitors.size(); e++) {
184 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
189 public void add_reply(AltosLine line) throws InterruptedException {
190 reply_queue.put (line);
193 public void abort_reply() {
195 add_telem (new AltosLine());
196 add_reply (new AltosLine());
197 } catch (InterruptedException e) {
201 public void add_string(String line) throws InterruptedException {
202 if (line.startsWith("TELEM") || line.startsWith("VERSION") || line.startsWith("CRC")) {
203 add_telem(new AltosLine(line));
205 add_reply(new AltosLine(line));
209 public void add_bytes(byte[] bytes, int len) throws InterruptedException {
212 line = new String(bytes, 0, len, "UTF-8");
213 } catch (UnsupportedEncodingException ue) {
215 for (int i = 0; i < len; i++)
216 line = line + bytes[i];
219 System.out.printf("\t\t\t\t\t%s\n", line);
223 public void flush_output() {
224 for (String s : pending_output)
226 pending_output.clear();
229 public void flush_input(int timeout) throws InterruptedException {
234 Thread.sleep(timeout);
235 got_some = !reply_queue.isEmpty();
241 public void flush_input() throws InterruptedException {
250 * Various command-level operations on
253 public boolean monitor_mode = false;
254 public int telemetry = AltosLib.ao_telemetry_standard;
255 public double frequency;
256 public String callsign;
257 AltosConfigData config_data;
259 private Object config_data_lock = new Object();
261 private int telemetry_len() {
262 return AltosLib.telemetry_len(telemetry);
265 private void set_radio_freq(int frequency) {
267 printf("m 0\nc F %d\nm %x\n",
268 frequency, telemetry_len());
270 printf("c F %d\n", frequency);
274 public void set_radio_frequency(double frequency,
275 boolean has_frequency,
279 System.out.printf("set_radio_frequency %7.3f (freq %b) (set %b) %d\n", frequency, has_frequency, has_setting, cal);
283 set_radio_freq((int) Math.floor (frequency * 1000));
284 else if (has_setting)
285 set_radio_setting(AltosConvert.radio_frequency_to_setting(frequency, cal));
287 set_channel(AltosConvert.radio_frequency_to_channel(frequency));
290 public void set_radio_frequency(double in_frequency) throws InterruptedException, TimeoutException {
291 frequency = in_frequency;
293 set_radio_frequency(frequency,
294 config_data.radio_frequency > 0,
295 config_data.radio_setting > 0,
296 config_data.radio_calibration);
299 public void set_telemetry(int in_telemetry) {
300 telemetry = in_telemetry;
302 printf("m 0\nm %x\n", telemetry_len());
306 public void set_monitor(boolean monitor) {
307 monitor_mode = monitor;
309 printf("m %x\n", telemetry_len());
315 private void set_channel(int channel) {
317 printf("m 0\nc r %d\nm %x\n",
318 channel, telemetry_len());
320 printf("c r %d\n", channel);
324 private void set_radio_setting(int setting) {
326 printf("m 0\nc R %d\nm %x\n",
327 setting, telemetry_len());
329 printf("c R %d\n", setting);
333 public AltosConfigData config_data() throws InterruptedException, TimeoutException {
334 synchronized(config_data_lock) {
335 if (config_data == null)
336 config_data = new AltosConfigData(this);
341 public void set_callsign(String callsign) {
342 this.callsign = callsign;
343 printf ("c c %s\n", callsign);
347 public boolean remote;
351 public void start_remote() throws TimeoutException, InterruptedException {
352 if (frequency == 0.0)
353 frequency = AltosPreferences.frequency(serial);
355 System.out.printf("start remote %7.3f\n", frequency);
356 set_radio_frequency(frequency);
357 set_callsign(AltosPreferences.callsign());
363 public void stop_remote() throws InterruptedException {
365 System.out.printf("stop remote\n");
375 public int rssi() throws TimeoutException, InterruptedException {
379 String line = get_reply_no_dialog(5000);
381 throw new TimeoutException();
382 String[] items = line.split("\\s+");
383 if (items.length < 2)
385 if (!items[0].equals("RSSI:"))
387 int rssi = Integer.parseInt(items[1]);
391 public String[] adc() throws TimeoutException, InterruptedException {
394 String line = get_reply_no_dialog(5000);
396 throw new TimeoutException();
398 if (!line.startsWith("tick:"))
400 String[] items = line.split("\\s+");
405 public boolean has_monitor_battery() {
406 return config_data.has_monitor_battery();
409 public double monitor_battery() {
410 int monitor_batt = AltosLib.MISSING;
412 if (config_data.has_monitor_battery()) {
414 String[] items = adc();
415 for (int i = 0; i < items.length;) {
416 if (items[i].equals("batt")) {
417 monitor_batt = Integer.parseInt(items[i+1]);
423 } catch (InterruptedException ie) {
424 } catch (TimeoutException te) {
427 if (monitor_batt == AltosLib.MISSING)
428 return AltosLib.MISSING;
429 return AltosConvert.cc_battery_to_voltage(monitor_batt);