2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
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.
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.
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.
18 package org.altusmetrum.AltosLib;
23 import java.util.prefs.*;
24 import java.util.concurrent.*;
35 public AltosADCTM(AltosLink link) throws InterruptedException, TimeoutException {
38 String line = link.get_reply_no_dialog(5000);
40 throw new TimeoutException();
42 if (!line.startsWith("tick:"))
44 String[] items = line.split("\\s+");
45 for (int i = 0; i < items.length;) {
46 if (items[i].equals("tick:")) {
47 tick = Integer.parseInt(items[i+1]);
51 if (items[i].equals("accel:")) {
52 accel = Integer.parseInt(items[i+1]);
56 if (items[i].equals("pres:")) {
57 pres = Integer.parseInt(items[i+1]);
61 if (items[i].equals("temp:")) {
62 temp = Integer.parseInt(items[i+1]);
66 if (items[i].equals("batt:")) {
67 batt = Integer.parseInt(items[i+1]);
71 if (items[i].equals("drogue:")) {
72 drogue = Integer.parseInt(items[i+1]);
76 if (items[i].equals("main:")) {
77 main = Integer.parseInt(items[i+1]);
88 class AltosGPSQuery extends AltosGPS {
89 public AltosGPSQuery (AltosLink link, AltosConfigData config_data)
90 throws TimeoutException, InterruptedException {
91 boolean says_done = config_data.compare_version("1.0") >= 0;
94 String line = link.get_reply_no_dialog(5000);
96 throw new TimeoutException();
97 String[] bits = line.split("\\s+");
100 if (line.startsWith("Date:")) {
103 String[] d = bits[1].split(":");
106 year = Integer.parseInt(d[0]) + 2000;
107 month = Integer.parseInt(d[1]);
108 day = Integer.parseInt(d[2]);
111 if (line.startsWith("Time:")) {
114 String[] d = bits[1].split("/");
117 hour = Integer.parseInt(d[0]);
118 minute = Integer.parseInt(d[1]);
119 second = Integer.parseInt(d[2]);
122 if (line.startsWith("Lat/Lon:")) {
125 lat = Integer.parseInt(bits[1]) * 1.0e-7;
126 lon = Integer.parseInt(bits[2]) * 1.0e-7;
129 if (line.startsWith("Alt:")) {
132 alt = Integer.parseInt(bits[1]);
135 if (line.startsWith("Flags:")) {
138 int status = Integer.decode(bits[1]);
139 connected = (status & AltosLib.AO_GPS_RUNNING) != 0;
140 locked = (status & AltosLib.AO_GPS_VALID) != 0;
145 if (line.startsWith("Sats:")) {
148 nsat = Integer.parseInt(bits[1]);
149 cc_gps_sat = new AltosGPSSat[nsat];
150 for (int i = 0; i < nsat; i++) {
151 int svid = Integer.parseInt(bits[2+i*2]);
152 int cc_n0 = Integer.parseInt(bits[3+i*2]);
153 cc_gps_sat[i] = new AltosGPSSat(svid, cc_n0);
156 if (line.startsWith("done"))
158 if (line.startsWith("Syntax error"))
164 public class AltosIdleMonitor extends Thread {
166 AltosIdleMonitorListener listener;
170 AltosState previous_state;
171 AltosConfigData config_data;
175 int AltosRSSI() throws TimeoutException, InterruptedException {
177 String line = link.get_reply_no_dialog(5000);
179 throw new TimeoutException();
180 String[] items = line.split("\\s+");
181 if (items.length < 2)
183 if (!items[0].equals("RSSI:"))
185 int rssi = Integer.parseInt(items[1]);
189 void update_state() throws InterruptedException, TimeoutException {
190 AltosRecordTM record = new AltosRecordTM();
195 link.set_radio_frequency(frequency);
199 config_data = new AltosConfigData(link);
200 adc = new AltosADC(link);
201 gps = new AltosGPSQuery(link, config_data);
211 record.callsign = config_data.callsign;
212 record.serial = config_data.serial;
213 record.flight = config_data.log_available() > 0 ? 255 : 0;
216 record.state = AltosLib.ao_flight_idle;
218 record.tick = adc.tick;
220 record.accel = adc.accel;
221 record.pres = adc.pres;
222 record.batt = adc.batt;
223 record.temp = adc.temp;
224 record.drogue = adc.drogue;
225 record.main = adc.main;
227 record.ground_accel = record.accel;
228 record.ground_pres = record.pres;
229 record.accel_plus_g = config_data.accel_cal_plus;
230 record.accel_minus_g = config_data.accel_cal_minus;
231 record.acceleration = 0;
235 state = new AltosState (record, state);
238 public void set_frequency(double in_frequency) {
239 frequency = in_frequency;
242 public void post_state() {
243 listener.update(state);
252 } catch (TimeoutException te) {
256 } catch (InterruptedException ie) {
261 public AltosIdleMonitor(AltosIdleMonitorListener in_listener, AltosLink in_link, boolean in_remote)
262 throws FileNotFoundException, InterruptedException, TimeoutException {
263 listener = in_listener;