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;
30 import org.altusmetrum.AltosLib.*;
32 public class AltosDisplayThread extends Thread {
35 IdleThread idle_thread;
37 AltosFlightReader reader;
39 AltosFlightDisplay display;
41 void show_internal(AltosState state, int crc_errors) {
43 display.show(state, crc_errors);
46 void show_safely(AltosState in_state, int in_crc_errors) {
47 final AltosState state = in_state;
48 final int crc_errors = in_crc_errors;
49 Runnable r = new Runnable() {
52 show_internal(state, crc_errors);
53 } catch (Exception ex) {
57 SwingUtilities.invokeLater(r);
60 void reading_error_internal() {
61 JOptionPane.showMessageDialog(parent,
62 String.format("Error reading from \"%s\"", reader.name),
63 "Telemetry Read Error",
64 JOptionPane.ERROR_MESSAGE);
67 void reading_error_safely() {
68 Runnable r = new Runnable() {
71 reading_error_internal();
72 } catch (Exception ex) {
76 SwingUtilities.invokeLater(r);
79 class IdleThread extends Thread {
82 private AltosState state;
87 public synchronized void report(boolean last) {
91 /* reset the landing count once we hear about a new flight */
92 if (state.state < Altos.ao_flight_drogue)
95 /* Shut up once the rocket is on the ground */
96 if (reported_landing > 2) {
100 /* If the rocket isn't on the pad, then report height */
101 if (Altos.ao_flight_drogue <= state.state &&
102 state.state < Altos.ao_flight_landed &&
105 voice.speak("Height %d, bearing %s %d, elevation %d, range %d.\n",
106 (int) (state.height + 0.5),
107 state.from_pad.bearing_words(
108 AltosGreatCircle.BEARING_VOICE),
109 (int) (state.from_pad.bearing + 0.5),
110 (int) (state.elevation + 0.5),
111 (int) (state.range + 0.5));
112 } else if (state.state > Altos.ao_flight_pad) {
113 voice.speak("%d meters", (int) (state.height + 0.5));
115 reported_landing = 0;
118 /* If the rocket is coming down, check to see if it has landed;
119 * either we've got a landed report or we haven't heard from it in
122 if (state.state >= Altos.ao_flight_drogue &&
124 System.currentTimeMillis() - state.report_time >= 15000 ||
125 state.state == Altos.ao_flight_landed))
127 if (Math.abs(state.baro_speed) < 20 && state.height < 100)
128 voice.speak("rocket landed safely");
130 voice.speak("rocket may have crashed");
131 if (state.from_pad != null)
132 voice.speak("Bearing %d degrees, range %d meters.",
133 (int) (state.from_pad.bearing + 0.5),
134 (int) (state.from_pad.distance + 0.5));
136 if (state.state != Altos.ao_flight_landed) {
137 state.state = Altos.ao_flight_landed;
138 show_safely(state, 0);
144 return System.currentTimeMillis();
147 void set_report_time() {
148 report_time = now() + report_interval;
157 synchronized (this) {
158 long sleep_time = report_time - now();
166 } catch (InterruptedException ie) {
169 } catch (InterruptedException iie) { }
173 public synchronized void notice(AltosState new_state, boolean spoken) {
174 AltosState old_state = state;
176 if (!started && state.state > Altos.ao_flight_pad) {
181 if (state.state < Altos.ao_flight_drogue)
182 report_interval = 10000;
184 report_interval = 20000;
185 if (old_state != null && old_state.state != state.state) {
192 public IdleThread() {
194 reported_landing = 0;
195 report_interval = 10000;
199 boolean tell(AltosState state, AltosState old_state) {
201 if (old_state == null || old_state.state != state.state) {
202 voice.speak(state.data.state());
203 if ((old_state == null || old_state.state <= Altos.ao_flight_boost) &&
204 state.state > Altos.ao_flight_boost) {
205 voice.speak("max speed: %d meters per second.",
206 (int) (state.max_speed + 0.5));
208 } else if ((old_state == null || old_state.state < Altos.ao_flight_drogue) &&
209 state.state >= Altos.ao_flight_drogue) {
210 voice.speak("max height: %d meters.",
211 (int) (state.max_height + 0.5));
215 if (old_state == null || old_state.gps_ready != state.gps_ready) {
216 if (state.gps_ready) {
217 voice.speak("GPS ready");
220 else if (old_state != null) {
221 voice.speak("GPS lost");
230 boolean interrupted = false;
232 AltosState state = null;
233 AltosState old_state = null;
236 idle_thread = new IdleThread();
241 AltosRecord record = reader.read();
245 state = new AltosState(record, state);
246 reader.update(state);
247 show_safely(state, crc_errors);
248 told = tell(state, old_state);
249 idle_thread.notice(state, told);
250 } catch (ParseException pp) {
251 System.out.printf("Parse error: %d \"%s\"\n", pp.getErrorOffset(), pp.getMessage());
252 } catch (AltosCRCException ce) {
254 show_safely(state, crc_errors);
257 } catch (InterruptedException ee) {
259 } catch (IOException ie) {
260 reading_error_safely();
263 idle_thread.report(true);
264 reader.close(interrupted);
265 idle_thread.interrupt();
268 } catch (InterruptedException ie) {}
272 public AltosDisplayThread(Frame in_parent, AltosVoice in_voice, AltosFlightDisplay in_display, AltosFlightReader in_reader) {
275 display = in_display;