3915927c5df0666714af79a3a90700eb15f02f69
[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                 AltosRecord     next = AltosTelemetry.parse(l.line, previous);
39                 previous = next;
40                 state = new AltosState (next, state);
41                 return state;
42         }
43
44         public void flush() {
45                 telem.clear();
46         }
47
48         public void reset() {
49                 previous = null;
50                 flush();
51         }
52
53         public void close(boolean interrupted) {
54                 link.remove_monitor(telem);
55                 log.close();
56                 link.close();
57         }
58
59         public void set_frequency(double in_frequency) throws InterruptedException, TimeoutException {
60                 frequency = in_frequency;
61                 link.set_radio_frequency(frequency);
62         }
63
64         public boolean supports_telemetry(int telemetry) {
65
66                 try {
67                         /* Version 1.0 or later firmware supports all telemetry formats */
68                         if (link.config_data().compare_version("1.0") >= 0)
69                                 return true;
70
71                         /* Version 0.9 firmware only supports 0.9 telemetry */
72                         if (link.config_data().compare_version("0.9") >= 0) {
73                                 if (telemetry == AltosLib.ao_telemetry_0_9)
74                                         return true;
75                                 else
76                                         return false;
77                         }
78
79                         /* Version 0.8 firmware only supports 0.8 telemetry */
80                         if (telemetry == AltosLib.ao_telemetry_0_8)
81                                 return true;
82                         else
83                                 return false;
84                 } catch (InterruptedException ie) {
85                         return true;
86                 } catch (TimeoutException te) {
87                         return true;
88                 }
89         }
90
91         public void save_frequency() {
92                 AltosPreferences.set_frequency(link.serial, frequency);
93         }
94
95         public void set_telemetry(int in_telemetry) {
96                 telemetry = in_telemetry;
97                 link.set_telemetry(telemetry);
98         }
99
100         public void save_telemetry() {
101                 AltosPreferences.set_telemetry(link.serial, telemetry);
102         }
103
104         public void set_monitor(boolean monitor) {
105                 link.set_monitor(monitor);
106         }
107
108         public File backing_file() {
109                 return log.file();
110         }
111
112         public boolean has_monitor_battery() {
113                 return link.has_monitor_battery();
114         }
115
116         public double monitor_battery() {
117                 return link.monitor_battery();
118         }
119
120         public AltosTelemetryReader (AltosLink in_link)
121                 throws IOException, InterruptedException, TimeoutException {
122                 link = in_link;
123                 try {
124                         log = new AltosLog(link);
125                         name = link.name;
126                         previous = null;
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 }