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