2 * Copyright © 2010 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.
21 import java.awt.event.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.LinkedBlockingQueue;
31 public class AltosDisplayThread extends Thread {
34 IdleThread idle_thread;
38 AltosStatusTable flightStatus;
39 AltosInfoTable flightInfo;
41 class IdleThread extends Thread {
44 private AltosState state;
49 public synchronized void report(boolean last) {
53 /* reset the landing count once we hear about a new flight */
54 if (state.state < Altos.ao_flight_drogue)
57 /* Shut up once the rocket is on the ground */
58 if (reported_landing > 2) {
62 /* If the rocket isn't on the pad, then report height */
63 if (Altos.ao_flight_drogue <= state.state &&
64 state.state < Altos.ao_flight_landed &&
67 voice.speak("Height %d, bearing %d, elevation %d, range %d.\n",
68 (int) (state.height + 0.5),
69 (int) (state.from_pad.bearing + 0.5),
70 (int) (state.elevation + 0.5),
71 (int) (state.range + 0.5));
72 } else if (state.state > Altos.ao_flight_pad) {
73 voice.speak("%d meters", (int) (state.height + 0.5));
78 /* If the rocket is coming down, check to see if it has landed;
79 * either we've got a landed report or we haven't heard from it in
82 if (state.state >= Altos.ao_flight_drogue &&
84 System.currentTimeMillis() - state.report_time >= 15000 ||
85 state.state == Altos.ao_flight_landed))
87 if (Math.abs(state.baro_speed) < 20 && state.height < 100)
88 voice.speak("rocket landed safely");
90 voice.speak("rocket may have crashed");
91 if (state.from_pad != null)
92 voice.speak("Bearing %d degrees, range %d meters.",
93 (int) (state.from_pad.bearing + 0.5),
94 (int) (state.from_pad.distance + 0.5));
100 return System.currentTimeMillis();
103 void set_report_time() {
104 report_time = now() + report_interval;
109 reported_landing = 0;
111 report_interval = 10000;
117 synchronized (this) {
118 long sleep_time = report_time - now();
126 } catch (InterruptedException ie) {
129 } catch (InterruptedException iie) { }
133 public synchronized void notice(AltosState new_state, boolean spoken) {
134 AltosState old_state = state;
136 if (!started && state.state > Altos.ao_flight_pad) {
141 if (state.state < Altos.ao_flight_drogue)
142 report_interval = 10000;
144 report_interval = 20000;
145 if (old_state != null && old_state.state != state.state) {
155 AltosRecord read() throws InterruptedException, ParseException, AltosCRCException, IOException { return null; }
157 void close(boolean interrupted) { }
159 void update(AltosState state) throws InterruptedException { }
161 boolean tell(AltosState state, AltosState old_state) {
163 if (old_state == null || old_state.state != state.state) {
164 voice.speak(state.data.state());
165 if ((old_state == null || old_state.state <= Altos.ao_flight_boost) &&
166 state.state > Altos.ao_flight_boost) {
167 voice.speak("max speed: %d meters per second.",
168 (int) (state.max_speed + 0.5));
170 } else if ((old_state == null || old_state.state < Altos.ao_flight_drogue) &&
171 state.state >= Altos.ao_flight_drogue) {
172 voice.speak("max height: %d meters.",
173 (int) (state.max_height + 0.5));
177 if (old_state == null || old_state.gps_ready != state.gps_ready) {
178 if (state.gps_ready) {
179 voice.speak("GPS ready");
182 else if (old_state != null) {
183 voice.speak("GPS lost");
191 void show(AltosState state, int crc_errors) {
193 flightStatus.set(state);
194 flightInfo.show(state, crc_errors);
199 boolean interrupted = false;
201 AltosState state = null;
202 AltosState old_state = null;
205 idle_thread = new IdleThread();
211 AltosRecord record = read();
215 state = new AltosState(record, state);
217 show(state, crc_errors);
218 told = tell(state, old_state);
219 idle_thread.notice(state, told);
220 } catch (ParseException pp) {
221 System.out.printf("Parse error: %d \"%s\"\n", pp.getErrorOffset(), pp.getMessage());
222 } catch (AltosCRCException ce) {
224 show(state, crc_errors);
227 } catch (InterruptedException ee) {
229 } catch (IOException ie) {
230 JOptionPane.showMessageDialog(parent,
231 String.format("Error reading from \"%s\"", name),
232 "Telemetry Read Error",
233 JOptionPane.ERROR_MESSAGE);
236 idle_thread.interrupt();
239 } catch (InterruptedException ie) {}
243 public AltosDisplayThread(Frame in_parent, AltosVoice in_voice, AltosStatusTable in_status, AltosInfoTable in_info) {
246 flightStatus = in_status;
247 flightInfo = in_info;
250 public void report() {
251 if (idle_thread != null)
252 idle_thread.report(true);