bd94ee363a0ceb0c1555dc1ec185c96d13eae758
[fw/altos] / altosui / altoslib / src / org / altusmetrum / 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 class AltosTelemetryReader extends AltosFlightReader {
26         AltosDevice     device;
27         AltosSerial     serial;
28         AltosLog        log;
29         AltosRecord     previous;
30         double          frequency;
31         int             telemetry;
32
33         LinkedBlockingQueue<AltosLine> telem;
34
35         AltosRecord read() throws InterruptedException, ParseException, AltosCRCException, IOException {
36                 AltosLine l = telem.take();
37                 if (l.line == null)
38                         throw new IOException("IO error");
39                 AltosRecord     next = AltosTelemetry.parse(l.line, previous);
40                 previous = next;
41                 return next;
42         }
43
44         void flush() {
45                 telem.clear();
46         }
47
48         void close(boolean interrupted) {
49                 serial.remove_monitor(telem);
50                 log.close();
51                 serial.close();
52         }
53
54         public void set_frequency(double in_frequency) throws InterruptedException, TimeoutException {
55                 frequency = in_frequency;
56                 serial.set_radio_frequency(frequency);
57         }
58
59         public boolean supports_telemetry(int telemetry) {
60
61                 try {
62                         /* Version 1.0 or later firmware supports all telemetry formats */
63                         if (serial.config_data().compare_version("1.0") >= 0)
64                                 return true;
65
66                         /* Version 0.9 firmware only supports 0.9 telemetry */
67                         if (serial.config_data().compare_version("0.9") >= 0) {
68                                 if (telemetry == Altos.ao_telemetry_0_9)
69                                         return true;
70                                 else
71                                         return false;
72                         }
73
74                         /* Version 0.8 firmware only supports 0.8 telemetry */
75                         if (telemetry == Altos.ao_telemetry_0_8)
76                                 return true;
77                         else
78                                 return false;
79                 } catch (InterruptedException ie) {
80                         return true;
81                 } catch (TimeoutException te) {
82                         return true;
83                 }
84         }
85
86         void save_frequency() {
87                 AltosUIPreferences.set_frequency(device.getSerial(), frequency);
88         }
89
90         void set_telemetry(int in_telemetry) {
91                 telemetry = in_telemetry;
92                 serial.set_telemetry(telemetry);
93         }
94
95         void save_telemetry() {
96                 AltosUIPreferences.set_telemetry(device.getSerial(), telemetry);
97         }
98
99         File backing_file() {
100                 return log.file();
101         }
102
103         public AltosTelemetryReader (AltosDevice in_device)
104                 throws FileNotFoundException, AltosSerialInUseException, IOException, InterruptedException, TimeoutException {
105                 device = in_device;
106                 serial = new AltosSerial(device);
107                 log = new AltosLog(serial);
108                 name = device.toShortString();
109                 previous = null;
110
111                 telem = new LinkedBlockingQueue<AltosLine>();
112                 frequency = AltosUIPreferences.frequency(device.getSerial());
113                 set_frequency(frequency);
114                 telemetry = AltosUIPreferences.telemetry(device.getSerial());
115                 set_telemetry(telemetry);
116                 serial.set_callsign(AltosUIPreferences.callsign());
117                 serial.add_monitor(telem);
118         }
119 }