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