Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[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;
19
20 import java.lang.*;
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         AltosRecord     previous;
29         double          frequency;
30         int             telemetry;
31
32         LinkedBlockingQueue<AltosLine> telem;
33
34         public AltosRecord 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                 return next;
41         }
42
43         public void flush() {
44                 telem.clear();
45         }
46
47         public void reset() {
48                 previous = null;
49                 flush();
50         }
51
52         public void close(boolean interrupted) {
53                 link.remove_monitor(telem);
54                 log.close();
55                 link.close();
56         }
57
58         public void set_frequency(double in_frequency) throws InterruptedException, TimeoutException {
59                 frequency = in_frequency;
60                 link.set_radio_frequency(frequency);
61         }
62
63         public boolean supports_telemetry(int telemetry) {
64
65                 try {
66                         /* Version 1.0 or later firmware supports all telemetry formats */
67                         if (link.config_data().compare_version("1.0") >= 0)
68                                 return true;
69
70                         /* Version 0.9 firmware only supports 0.9 telemetry */
71                         if (link.config_data().compare_version("0.9") >= 0) {
72                                 if (telemetry == AltosLib.ao_telemetry_0_9)
73                                         return true;
74                                 else
75                                         return false;
76                         }
77
78                         /* Version 0.8 firmware only supports 0.8 telemetry */
79                         if (telemetry == AltosLib.ao_telemetry_0_8)
80                                 return true;
81                         else
82                                 return false;
83                 } catch (InterruptedException ie) {
84                         return true;
85                 } catch (TimeoutException te) {
86                         return true;
87                 }
88         }
89
90         public void save_frequency() {
91                 AltosPreferences.set_frequency(link.serial, frequency);
92         }
93
94         public void set_telemetry(int in_telemetry) {
95                 telemetry = in_telemetry;
96                 link.set_telemetry(telemetry);
97         }
98
99         public void save_telemetry() {
100                 AltosPreferences.set_telemetry(link.serial, telemetry);
101         }
102
103         public void set_monitor(boolean monitor) {
104                 link.set_monitor(monitor);
105         }
106
107         public File backing_file() {
108                 return log.file();
109         }
110
111         public AltosTelemetryReader (AltosLink in_link)
112                 throws IOException, InterruptedException, TimeoutException {
113                 link = in_link;
114                 try {
115                         log = new AltosLog(link);
116                         name = link.name;
117                         previous = null;
118                         telem = new LinkedBlockingQueue<AltosLine>();
119                         frequency = AltosPreferences.frequency(link.serial);
120                         set_frequency(frequency);
121                         telemetry = AltosPreferences.telemetry(link.serial);
122                         set_telemetry(telemetry);
123                         link.add_monitor(telem);
124                 } catch (TimeoutException e) {
125                         close(true);
126                         throw(e);
127                 } catch (InterruptedException e) {
128                         close(true);
129                         throw(e);
130                 }
131         }
132 }