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