6abe95d8f3b917680bdc0a91317f3727c8a03eb9
[fw/altos] / altosui / 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 altosui;
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         void save_frequency() {
60                 AltosPreferences.set_frequency(device.getSerial(), frequency);
61         }
62
63         void set_telemetry(int in_telemetry) {
64                 telemetry = in_telemetry;
65                 serial.set_telemetry(telemetry);
66         }
67
68         void save_telemetry() {
69                 AltosPreferences.set_telemetry(device.getSerial(), telemetry);
70         }
71
72         public AltosTelemetryReader (AltosDevice in_device)
73                 throws FileNotFoundException, AltosSerialInUseException, IOException, InterruptedException, TimeoutException {
74                 device = in_device;
75                 serial = new AltosSerial(device);
76                 log = new AltosLog(serial);
77                 name = device.toShortString();
78                 previous = null;
79
80                 telem = new LinkedBlockingQueue<AltosLine>();
81                 frequency = AltosPreferences.frequency(device.getSerial());
82                 set_frequency(frequency);
83                 telemetry = AltosPreferences.telemetry(device.getSerial());
84                 set_telemetry(telemetry);
85                 serial.set_callsign(AltosPreferences.callsign());
86                 serial.add_monitor(telem);
87         }
88 }