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.
18 package org.altusmetrum.altoslib_5;
22 import java.util.concurrent.*;
24 public class AltosTelemetryReader extends AltosFlightReader {
30 AltosState state = null;
31 AltosFlightReader stacked;
33 LinkedBlockingQueue<AltosLine> telem;
35 public AltosState read() throws InterruptedException, ParseException, AltosCRCException, IOException {
36 if (stacked != null) {
37 state = stacked.read();
42 AltosLine l = telem.take();
44 throw new IOException("IO error");
45 AltosTelemetry telem = AltosTelemetry.parse(l.line);
47 state = new AltosState();
49 state = state.clone();
50 telem.update_state(state);
62 public void close(boolean interrupted) {
64 if (stacked != null) {
65 stacked.close(interrupted);
69 link.remove_monitor(telem);
73 } catch (InterruptedException ie) {
77 public void set_frequency(double in_frequency) throws InterruptedException, TimeoutException {
78 frequency = in_frequency;
79 link.set_radio_frequency(frequency);
82 public boolean supports_telemetry(int telemetry) {
85 /* Version 1.0 or later firmware supports all telemetry formats */
86 if (link.config_data().compare_version("1.0") >= 0)
89 /* Version 0.9 firmware only supports 0.9 telemetry */
90 if (link.config_data().compare_version("0.9") >= 0) {
91 if (telemetry == AltosLib.ao_telemetry_0_9)
97 /* Version 0.8 firmware only supports 0.8 telemetry */
98 if (telemetry == AltosLib.ao_telemetry_0_8)
102 } catch (InterruptedException ie) {
104 } catch (TimeoutException te) {
109 public boolean supports_telemetry_rate(int telemetry_rate) {
111 /* Version 1.4.1.1 supports all rates, older versions don't */
112 if (link.config_data().compare_version("1.4.1.1") >= 0)
115 if (telemetry_rate == AltosLib.ao_telemetry_rate_38400)
119 } catch (InterruptedException ie) {
121 } catch (TimeoutException te) {
126 public void save_frequency() {
127 AltosPreferences.set_frequency(link.serial, frequency);
130 public void set_telemetry(int in_telemetry) {
131 telemetry = in_telemetry;
132 link.set_telemetry(telemetry);
135 public void save_telemetry() {
136 AltosPreferences.set_telemetry(link.serial, telemetry);
139 public void set_telemetry_rate(int in_telemetry_rate) {
140 telemetry_rate = in_telemetry_rate;
141 link.set_telemetry_rate(telemetry_rate);
144 public void save_telemetry_rate() {
145 AltosPreferences.set_telemetry_rate(link.serial, telemetry_rate);
148 public void set_monitor(boolean monitor) {
149 link.set_monitor(monitor);
152 public File backing_file() {
156 public boolean has_monitor_battery() {
157 return link.has_monitor_battery();
160 public double monitor_battery() throws InterruptedException {
161 return link.monitor_battery();
164 public AltosTelemetryReader (AltosLink in_link, AltosFlightReader in_stacked)
165 throws IOException, InterruptedException, TimeoutException {
167 stacked = in_stacked;
168 boolean success = false;
170 log = new AltosLog(link);
172 telem = new LinkedBlockingQueue<AltosLine>();
173 frequency = AltosPreferences.frequency(link.serial);
174 set_frequency(frequency);
175 telemetry = AltosPreferences.telemetry(link.serial);
176 set_telemetry(telemetry);
177 telemetry_rate = AltosPreferences.telemetry_rate(link.serial);
178 set_telemetry_rate(telemetry_rate);
179 link.add_monitor(telem);
187 private static AltosFlightReader existing_data(AltosLink link) {
191 File file = AltosPreferences.logfile(link.serial);
193 AltosStateIterable iterable = AltosStateIterable.iterable(file);
194 if (iterable != null)
195 return new AltosReplayReader(iterable.iterator(), file, false);
200 public AltosTelemetryReader(AltosLink link)
201 throws IOException, InterruptedException, TimeoutException {