5ed501348b07174bb0debee09d499ada26bffb56
[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; version 2 of the License.
7  *
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.
12  *
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.
16  */
17
18 package org.altusmetrum.altoslib_5;
19
20 import java.text.*;
21 import java.io.*;
22 import java.util.concurrent.*;
23
24 public class AltosTelemetryReader extends AltosFlightReader {
25         AltosLink       link;
26         AltosLog        log;
27         double          frequency;
28         int             telemetry;
29         int             telemetry_rate;
30         AltosState      state = null;
31
32         LinkedBlockingQueue<AltosLine> telem;
33
34         public AltosState read() throws InterruptedException, ParseException, AltosCRCException, IOException {
35                 AltosLine l = telem.take();
36                 if (l.line == null)
37                         throw new IOException("IO error");
38                 AltosTelemetry  telem = AltosTelemetry.parse(l.line);
39                 if (state == null)
40                         state = new AltosState();
41                 else
42                         state = state.clone();
43                 telem.update_state(state);
44                 return state;
45         }
46
47         public void flush() {
48                 telem.clear();
49         }
50
51         public void reset() {
52                 flush();
53         }
54
55         public void close(boolean interrupted) {
56                 link.remove_monitor(telem);
57                 log.close();
58                 try {
59                         link.close();
60                 } catch (InterruptedException ie) {
61                 }
62         }
63
64         public void set_frequency(double in_frequency) throws InterruptedException, TimeoutException {
65                 frequency = in_frequency;
66                 link.set_radio_frequency(frequency);
67         }
68
69         public boolean supports_telemetry(int telemetry) {
70
71                 try {
72                         /* Version 1.0 or later firmware supports all telemetry formats */
73                         if (link.config_data().compare_version("1.0") >= 0)
74                                 return true;
75
76                         /* Version 0.9 firmware only supports 0.9 telemetry */
77                         if (link.config_data().compare_version("0.9") >= 0) {
78                                 if (telemetry == AltosLib.ao_telemetry_0_9)
79                                         return true;
80                                 else
81                                         return false;
82                         }
83
84                         /* Version 0.8 firmware only supports 0.8 telemetry */
85                         if (telemetry == AltosLib.ao_telemetry_0_8)
86                                 return true;
87                         else
88                                 return false;
89                 } catch (InterruptedException ie) {
90                         return false;
91                 } catch (TimeoutException te) {
92                         return true;
93                 }
94         }
95
96         public boolean supports_telemetry_rate(int telemetry_rate) {
97                 try {
98                         /* Version 1.4.1.1 supports all rates, older versions don't */
99                         if (link.config_data().compare_version("1.4.1.1") >= 0)
100                                 return true;
101
102                         if (telemetry_rate == AltosLib.ao_telemetry_rate_38400)
103                                 return true;
104                         else
105                                 return false;
106                 } catch (InterruptedException ie) {
107                         return false;
108                 } catch (TimeoutException te) {
109                         return true;
110                 }
111         }
112
113         public void save_frequency() {
114                 AltosPreferences.set_frequency(link.serial, frequency);
115         }
116
117         public void set_telemetry(int in_telemetry) {
118                 telemetry = in_telemetry;
119                 link.set_telemetry(telemetry);
120         }
121
122         public void save_telemetry() {
123                 AltosPreferences.set_telemetry(link.serial, telemetry);
124         }
125
126         public void set_telemetry_rate(int in_telemetry_rate) {
127                 telemetry_rate = in_telemetry_rate;
128                 link.set_telemetry_rate(telemetry_rate);
129         }
130
131         public void save_telemetry_rate() {
132                 AltosPreferences.set_telemetry_rate(link.serial, telemetry_rate);
133         }
134
135         public void set_monitor(boolean monitor) {
136                 link.set_monitor(monitor);
137         }
138
139         public File backing_file() {
140                 return log.file();
141         }
142
143         public boolean has_monitor_battery() {
144                 return link.has_monitor_battery();
145         }
146
147         public double monitor_battery() throws InterruptedException {
148                 return link.monitor_battery();
149         }
150
151         public AltosTelemetryReader (AltosLink in_link)
152                 throws IOException, InterruptedException, TimeoutException {
153                 link = in_link;
154                 boolean success = false;
155                 try {
156                         log = new AltosLog(link);
157                         name = link.name;
158                         telem = new LinkedBlockingQueue<AltosLine>();
159                         frequency = AltosPreferences.frequency(link.serial);
160                         set_frequency(frequency);
161                         telemetry = AltosPreferences.telemetry(link.serial);
162                         set_telemetry(telemetry);
163                         telemetry_rate = AltosPreferences.telemetry_rate(link.serial);
164                         set_telemetry_rate(telemetry_rate);
165                         link.add_monitor(telem);
166                         success = true;
167                 } finally {
168                         if (!success)
169                                 close(true);
170                 }
171         }
172 }