b1cc009c12814f336bf56b9b3d86be1a460fc4ef
[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_1;
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         AltosRecord     previous;
28         double          frequency;
29         int             telemetry;
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 = AltosTelemetryLegacy.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                 previous = null;
53                 flush();
54         }
55
56         public void close(boolean interrupted) {
57                 link.remove_monitor(telem);
58                 log.close();
59                 link.close();
60         }
61
62         public void set_frequency(double in_frequency) throws InterruptedException, TimeoutException {
63                 frequency = in_frequency;
64                 link.set_radio_frequency(frequency);
65         }
66
67         public boolean supports_telemetry(int telemetry) {
68
69                 try {
70                         /* Version 1.0 or later firmware supports all telemetry formats */
71                         if (link.config_data().compare_version("1.0") >= 0)
72                                 return true;
73
74                         /* Version 0.9 firmware only supports 0.9 telemetry */
75                         if (link.config_data().compare_version("0.9") >= 0) {
76                                 if (telemetry == AltosLib.ao_telemetry_0_9)
77                                         return true;
78                                 else
79                                         return false;
80                         }
81
82                         /* Version 0.8 firmware only supports 0.8 telemetry */
83                         if (telemetry == AltosLib.ao_telemetry_0_8)
84                                 return true;
85                         else
86                                 return false;
87                 } catch (InterruptedException ie) {
88                         return true;
89                 } catch (TimeoutException te) {
90                         return true;
91                 }
92         }
93
94         public void save_frequency() {
95                 AltosPreferences.set_frequency(link.serial, frequency);
96         }
97
98         public void set_telemetry(int in_telemetry) {
99                 telemetry = in_telemetry;
100                 link.set_telemetry(telemetry);
101         }
102
103         public void save_telemetry() {
104                 AltosPreferences.set_telemetry(link.serial, telemetry);
105         }
106
107         public void set_monitor(boolean monitor) {
108                 link.set_monitor(monitor);
109         }
110
111         public File backing_file() {
112                 return log.file();
113         }
114
115         public boolean has_monitor_battery() {
116                 return link.has_monitor_battery();
117         }
118
119         public double monitor_battery() {
120                 return link.monitor_battery();
121         }
122
123         public AltosTelemetryReader (AltosLink in_link)
124                 throws IOException, InterruptedException, TimeoutException {
125                 link = in_link;
126                 try {
127                         log = new AltosLog(link);
128                         name = link.name;
129                         previous = null;
130                         telem = new LinkedBlockingQueue<AltosLine>();
131                         frequency = AltosPreferences.frequency(link.serial);
132                         set_frequency(frequency);
133                         telemetry = AltosPreferences.telemetry(link.serial);
134                         set_telemetry(telemetry);
135                         link.add_monitor(telem);
136                 } catch (TimeoutException e) {
137                         close(true);
138                         throw(e);
139                 } catch (InterruptedException e) {
140                         close(true);
141                         throw(e);
142                 }
143         }
144 }