altoslib: Remove spurious debug printf in AltosCalData
[fw/altos] / altoslib / AltosTelemetryReader.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altoslib_13;
20
21 import java.text.*;
22 import java.io.*;
23 import java.util.concurrent.*;
24
25 public class AltosTelemetryReader extends AltosFlightReader {
26         AltosLink               link;
27         AltosLog                log;
28         double                  frequency;
29         int                     telemetry;
30         int                     telemetry_rate;
31         private AltosState      state = null;
32         private AltosCalData    cal_data = null;
33
34         LinkedBlockingQueue<AltosLine> telem;
35
36         public AltosState read() throws InterruptedException, ParseException, AltosCRCException, IOException {
37                 AltosLine l;
38                 do {
39                         l = telem.take();
40                         if (l.line == null)
41                                 throw new IOException("IO error");
42                 } while (!link.get_monitor());
43                 AltosTelemetry  telem = AltosTelemetry.parse(l.line);
44                 if (state == null)
45                         state = new AltosState(cal_data());
46                 telem.provide_data(state);
47                 return state;
48         }
49
50         public AltosCalData cal_data() {
51                 if (cal_data == null)
52                         cal_data = new AltosCalData();
53                 return cal_data;
54         }
55
56         public void flush() {
57                 telem.clear();
58         }
59
60         public void reset() {
61                 flush();
62                 state = null;
63                 cal_data = null;
64         }
65
66         public void close(boolean interrupted) {
67
68                 link.remove_monitor(telem);
69                 log.close();
70                 try {
71                         link.close();
72                 } catch (InterruptedException ie) {
73                 }
74         }
75
76         public void set_frequency(double in_frequency) throws InterruptedException, TimeoutException {
77                 frequency = in_frequency;
78                 link.set_radio_frequency(frequency);
79         }
80
81         public boolean supports_telemetry(int telemetry) {
82
83                 try {
84                         /* Version 1.0 or later firmware supports all telemetry formats */
85                         if (link.config_data().compare_version("1.0") >= 0)
86                                 return true;
87
88                         /* Version 0.9 firmware only supports 0.9 telemetry */
89                         if (link.config_data().compare_version("0.9") >= 0) {
90                                 if (telemetry == AltosLib.ao_telemetry_0_9)
91                                         return true;
92                                 else
93                                         return false;
94                         }
95
96                         /* Version 0.8 firmware only supports 0.8 telemetry */
97                         if (telemetry == AltosLib.ao_telemetry_0_8)
98                                 return true;
99                         else
100                                 return false;
101                 } catch (InterruptedException ie) {
102                         return false;
103                 } catch (TimeoutException te) {
104                         return true;
105                 }
106         }
107
108         public boolean supports_telemetry_rate(int telemetry_rate) {
109                 try {
110                         /* Version 1.4.1.1 supports all rates, older versions don't */
111                         if (link.config_data().compare_version("1.4.1.1") >= 0)
112                                 return true;
113
114                         if (telemetry_rate == AltosLib.ao_telemetry_rate_38400)
115                                 return true;
116                         else
117                                 return false;
118                 } catch (InterruptedException ie) {
119                         return false;
120                 } catch (TimeoutException te) {
121                         return true;
122                 }
123         }
124
125         public void save_frequency() {
126                 AltosPreferences.set_frequency(link.serial, frequency);
127         }
128
129         public void set_telemetry(int in_telemetry) {
130                 telemetry = in_telemetry;
131                 link.set_telemetry(telemetry);
132         }
133
134         public void save_telemetry() {
135                 AltosPreferences.set_telemetry(link.serial, telemetry);
136         }
137
138         public void set_telemetry_rate(int in_telemetry_rate) {
139                 telemetry_rate = in_telemetry_rate;
140                 link.set_telemetry_rate(telemetry_rate);
141         }
142
143         public void save_telemetry_rate() {
144                 AltosPreferences.set_telemetry_rate(link.serial, telemetry_rate);
145         }
146
147         public void set_monitor(boolean monitor) {
148                 link.set_monitor(monitor);
149         }
150
151         public File backing_file() {
152                 return log.file();
153         }
154
155         public boolean has_monitor_battery() {
156                 return link.has_monitor_battery();
157         }
158
159         public double monitor_battery() throws InterruptedException {
160                 return link.monitor_battery();
161         }
162
163         public AltosTelemetryReader (AltosLink in_link)
164                 throws IOException, InterruptedException, TimeoutException {
165                 link = in_link;
166                 boolean success = false;
167                 try {
168                         log = new AltosLog(link);
169                         name = link.name;
170                         telem = new LinkedBlockingQueue<AltosLine>();
171                         frequency = AltosPreferences.frequency(link.serial);
172                         set_frequency(frequency);
173                         telemetry = AltosPreferences.telemetry(link.serial);
174                         set_telemetry(telemetry);
175                         telemetry_rate = AltosPreferences.telemetry_rate(link.serial);
176                         set_telemetry_rate(telemetry_rate);
177                         link.add_monitor(telem);
178                         success = true;
179                 } finally {
180                         if (!success)
181                                 close(true);
182                 }
183         }
184 }