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;
22 import java.util.concurrent.*;
26 public abstract class AltosLink {
27 public abstract void print(String data);
28 public abstract void close();
30 public static boolean debug = false;
31 public static void set_debug(boolean in_debug) { debug = in_debug; }
32 LinkedList<String> pending_output = new LinkedList<String>();
34 public LinkedList<LinkedBlockingQueue<AltosLine>> monitors = new LinkedList<LinkedBlockingQueue<AltosLine>> ();;
35 public LinkedBlockingQueue<AltosLine> reply_queue = new LinkedBlockingQueue<AltosLine>();
37 public void add_monitor(LinkedBlockingQueue<AltosLine> q) {
42 public void remove_monitor(LinkedBlockingQueue<AltosLine> q) {
44 if (monitors.isEmpty())
48 public void printf(String format, Object ... arguments) {
49 String line = String.format(format, arguments);
51 pending_output.add(line);
55 public String get_reply_no_dialog(int timeout) throws InterruptedException, TimeoutException {
57 AltosLine line = reply_queue.poll(timeout, TimeUnit.MILLISECONDS);
63 public String get_reply(int timeout) throws InterruptedException {
65 return get_reply_no_dialog(timeout);
66 } catch (TimeoutException te) {
71 public String get_reply() throws InterruptedException {
72 return get_reply(5000);
75 public void add_telem(AltosLine line) throws InterruptedException {
76 for (int e = 0; e < monitors.size(); e++) {
77 LinkedBlockingQueue<AltosLine> q = monitors.get(e);
82 public void add_reply(AltosLine line) throws InterruptedException {
83 reply_queue.put (line);
86 public void add_string(String line) throws InterruptedException {
87 if (line.startsWith("TELEM") || line.startsWith("VERSION") || line.startsWith("CRC")) {
88 add_telem(new AltosLine(line));
90 add_reply(new AltosLine(line));
94 public void add_bytes(byte[] bytes, int len) throws InterruptedException {
97 line = new String(bytes, 0, len, "UTF-8");
98 } catch (UnsupportedEncodingException ue) {
100 for (int i = 0; i < len; i++)
101 line = line + bytes[i];
104 System.out.printf("\t\t\t\t\t%s\n", line);
108 public void flush_output() {
109 for (String s : pending_output)
111 pending_output.clear();
114 public void flush_input(int timeout) throws InterruptedException {
119 Thread.sleep(timeout);
120 got_some = !reply_queue.isEmpty();
126 public void flush_input() throws InterruptedException {
132 * Various command-level operations on
135 public boolean monitor_mode = false;
136 public int telemetry = AltosLib.ao_telemetry_standard;
137 public double frequency;
138 AltosConfigData config_data;
140 private int telemetry_len() {
141 return AltosLib.telemetry_len(telemetry);
144 public void set_telemetry(int in_telemetry) {
145 telemetry = in_telemetry;
147 printf("m 0\nm %x\n", telemetry_len());
151 public void set_monitor(boolean monitor) {
152 monitor_mode = monitor;
154 printf("m %x\n", telemetry_len());
160 private void set_channel(int channel) {
162 printf("m 0\nc r %d\nm %x\n",
163 channel, telemetry_len());
165 printf("c r %d\n", channel);
169 private void set_radio_setting(int setting) {
171 printf("m 0\nc R %d\nm %x\n",
172 setting, telemetry_len());
174 printf("c R %d\n", setting);
178 public void set_radio_frequency(double frequency,
182 System.out.printf("set_radio_frequency %7.3f %b %d\n", frequency, has_setting, cal);
184 set_radio_setting(AltosConvert.radio_frequency_to_setting(frequency, cal));
186 set_channel(AltosConvert.radio_frequency_to_channel(frequency));
189 public AltosConfigData config_data() throws InterruptedException, TimeoutException {
190 if (config_data == null)
191 config_data = new AltosConfigData(this);
195 public void set_radio_frequency(double in_frequency) throws InterruptedException, TimeoutException {
196 frequency = in_frequency;
198 set_radio_frequency(frequency,
199 config_data.radio_setting != 0,
200 config_data.radio_calibration);
203 public void set_callsign(String callsign) {
204 printf ("c c %s\n", callsign);
208 public boolean remote;
212 public void start_remote() throws TimeoutException, InterruptedException {
214 System.out.printf("start remote %7.3f\n", frequency);
215 if (frequency == 0.0)
216 frequency = AltosPreferences.frequency(serial);
217 set_radio_frequency(frequency);
218 set_callsign(AltosPreferences.callsign());
224 public void stop_remote() throws InterruptedException {
226 System.out.printf("stop remote\n");