altoslib: Finish AltosState changes. Update version number.
[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_2;
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         AltosState      state = null;
30
31         LinkedBlockingQueue<AltosLine> telem;
32
33         public AltosState read() throws InterruptedException, ParseException, AltosCRCException, IOException {
34                 AltosLine l = telem.take();
35                 if (l.line == null)
36                         throw new IOException("IO error");
37                 AltosTelemetry  telem = AltosTelemetryLegacy.parse(l.line);
38                 if (state == null)
39                         state = new AltosState();
40                 else
41                         state = state.clone();
42                 telem.update_state(state);
43                 return state;
44         }
45
46         public void flush() {
47                 telem.clear();
48         }
49
50         public void reset() {
51                 flush();
52         }
53
54         public void close(boolean interrupted) {
55                 link.remove_monitor(telem);
56                 log.close();
57                 link.close();
58         }
59
60         public void set_frequency(double in_frequency) throws InterruptedException, TimeoutException {
61                 frequency = in_frequency;
62                 link.set_radio_frequency(frequency);
63         }
64
65         public boolean supports_telemetry(int telemetry) {
66
67                 try {
68                         /* Version 1.0 or later firmware supports all telemetry formats */
69                         if (link.config_data().compare_version("1.0") >= 0)
70                                 return true;
71
72                         /* Version 0.9 firmware only supports 0.9 telemetry */
73                         if (link.config_data().compare_version("0.9") >= 0) {
74                                 if (telemetry == AltosLib.ao_telemetry_0_9)
75                                         return true;
76                                 else
77                                         return false;
78                         }
79
80                         /* Version 0.8 firmware only supports 0.8 telemetry */
81                         if (telemetry == AltosLib.ao_telemetry_0_8)
82                                 return true;
83                         else
84                                 return false;
85                 } catch (InterruptedException ie) {
86                         return true;
87                 } catch (TimeoutException te) {
88                         return true;
89                 }
90         }
91
92         public void save_frequency() {
93                 AltosPreferences.set_frequency(link.serial, frequency);
94         }
95
96         public void set_telemetry(int in_telemetry) {
97                 telemetry = in_telemetry;
98                 link.set_telemetry(telemetry);
99         }
100
101         public void save_telemetry() {
102                 AltosPreferences.set_telemetry(link.serial, telemetry);
103         }
104
105         public void set_monitor(boolean monitor) {
106                 link.set_monitor(monitor);
107         }
108
109         public File backing_file() {
110                 return log.file();
111         }
112
113         public boolean has_monitor_battery() {
114                 return link.has_monitor_battery();
115         }
116
117         public double monitor_battery() {
118                 return link.monitor_battery();
119         }
120
121         public AltosTelemetryReader (AltosLink in_link)
122                 throws IOException, InterruptedException, TimeoutException {
123                 link = in_link;
124                 try {
125                         log = new AltosLog(link);
126                         name = link.name;
127                         telem = new LinkedBlockingQueue<AltosLine>();
128                         frequency = AltosPreferences.frequency(link.serial);
129                         set_frequency(frequency);
130                         telemetry = AltosPreferences.telemetry(link.serial);
131                         set_telemetry(telemetry);
132                         link.add_monitor(telem);
133                 } catch (TimeoutException e) {
134                         close(true);
135                         throw(e);
136                 } catch (InterruptedException e) {
137                         close(true);
138                         throw(e);
139                 }
140         }
141 }