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;
20 import java.util.concurrent.*;
31 public AltosADCTM(AltosLink link) throws InterruptedException, TimeoutException {
34 String line = link.get_reply_no_dialog(5000);
36 throw new TimeoutException();
38 if (!line.startsWith("tick:"))
40 String[] items = line.split("\\s+");
41 for (int i = 0; i < items.length;) {
42 if (items[i].equals("tick:")) {
43 tick = Integer.parseInt(items[i+1]);
47 if (items[i].equals("accel:")) {
48 accel = Integer.parseInt(items[i+1]);
52 if (items[i].equals("pres:")) {
53 pres = Integer.parseInt(items[i+1]);
57 if (items[i].equals("temp:")) {
58 temp = Integer.parseInt(items[i+1]);
62 if (items[i].equals("batt:")) {
63 batt = Integer.parseInt(items[i+1]);
67 if (items[i].equals("drogue:")) {
68 drogue = Integer.parseInt(items[i+1]);
72 if (items[i].equals("main:")) {
73 main = Integer.parseInt(items[i+1]);
84 class AltosGPSQuery extends AltosGPS {
85 public AltosGPSQuery (AltosLink link, AltosConfigData config_data)
86 throws TimeoutException, InterruptedException {
87 boolean says_done = config_data.compare_version("1.0") >= 0;
90 String line = link.get_reply_no_dialog(5000);
92 throw new TimeoutException();
93 String[] bits = line.split("\\s+");
96 if (line.startsWith("Date:")) {
99 String[] d = bits[1].split(":");
102 year = Integer.parseInt(d[0]) + 2000;
103 month = Integer.parseInt(d[1]);
104 day = Integer.parseInt(d[2]);
107 if (line.startsWith("Time:")) {
110 String[] d = bits[1].split("/");
113 hour = Integer.parseInt(d[0]);
114 minute = Integer.parseInt(d[1]);
115 second = Integer.parseInt(d[2]);
118 if (line.startsWith("Lat/Lon:")) {
121 lat = Integer.parseInt(bits[1]) * 1.0e-7;
122 lon = Integer.parseInt(bits[2]) * 1.0e-7;
125 if (line.startsWith("Alt:")) {
128 alt = Integer.parseInt(bits[1]);
131 if (line.startsWith("Flags:")) {
134 int status = Integer.decode(bits[1]);
135 connected = (status & AltosLib.AO_GPS_RUNNING) != 0;
136 locked = (status & AltosLib.AO_GPS_VALID) != 0;
141 if (line.startsWith("Sats:")) {
144 nsat = Integer.parseInt(bits[1]);
145 cc_gps_sat = new AltosGPSSat[nsat];
146 for (int i = 0; i < nsat; i++) {
147 int svid = Integer.parseInt(bits[2+i*2]);
148 int cc_n0 = Integer.parseInt(bits[3+i*2]);
149 cc_gps_sat[i] = new AltosGPSSat(svid, cc_n0);
152 if (line.startsWith("done"))
154 if (line.startsWith("Syntax error"))
160 public class AltosIdleMonitor extends Thread {
162 AltosIdleMonitorListener listener;
166 AltosState previous_state;
167 AltosConfigData config_data;
171 int AltosRSSI() throws TimeoutException, InterruptedException {
173 String line = link.get_reply_no_dialog(5000);
175 throw new TimeoutException();
176 String[] items = line.split("\\s+");
177 if (items.length < 2)
179 if (!items[0].equals("RSSI:"))
181 int rssi = Integer.parseInt(items[1]);
185 void update_state() throws InterruptedException, TimeoutException {
186 AltosRecordTM record = new AltosRecordTM();
191 link.set_radio_frequency(frequency);
195 config_data = new AltosConfigData(link);
196 adc = new AltosADC(link);
197 gps = new AltosGPSQuery(link, config_data);
207 record.callsign = config_data.callsign;
208 record.serial = config_data.serial;
209 record.flight = config_data.log_available() > 0 ? 255 : 0;
212 record.state = AltosLib.ao_flight_idle;
214 record.tick = adc.tick;
216 record.accel = adc.accel;
217 record.pres = adc.pres;
218 record.batt = adc.batt;
219 record.temp = adc.temp;
220 record.drogue = adc.drogue;
221 record.main = adc.main;
223 record.ground_accel = record.accel;
224 record.ground_pres = record.pres;
225 record.accel_plus_g = config_data.accel_cal_plus;
226 record.accel_minus_g = config_data.accel_cal_minus;
227 record.acceleration = 0;
231 state = new AltosState (record, state);
234 public void set_frequency(double in_frequency) {
235 frequency = in_frequency;
238 public void post_state() {
239 listener.update(state);
248 } catch (TimeoutException te) {
252 } catch (InterruptedException ie) {
257 public AltosIdleMonitor(AltosIdleMonitorListener in_listener, AltosLink in_link, boolean in_remote)
258 throws FileNotFoundException, InterruptedException, TimeoutException {
259 listener = in_listener;