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;
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(String name) {
61 JOptionPane.showMessageDialog(parent,
62 String.format("Error reading from \"%s\"", name),
63 "Telemetry Read Error",
64 JOptionPane.ERROR_MESSAGE);
67 void reading_error_safely(String in_name) {
68 final String name = in_name;
69 Runnable r = new Runnable() {
72 reading_error_internal(name);
73 } catch (Exception ex) {
77 SwingUtilities.invokeLater(r);
80 class IdleThread extends Thread {
83 private AltosState state;
88 public synchronized void report(boolean last) {
92 /* reset the landing count once we hear about a new flight */
93 if (state.state < Altos.ao_flight_drogue)
96 /* Shut up once the rocket is on the ground */
97 if (reported_landing > 2) {
101 /* If the rocket isn't on the pad, then report height */
102 if (Altos.ao_flight_drogue <= state.state &&
103 state.state < Altos.ao_flight_landed &&
106 voice.speak("Height %d, bearing %s %d, elevation %d, range %d.\n",
107 (int) (state.height + 0.5),
108 state.from_pad.bearing_words(
109 AltosGreatCircle.BEARING_VOICE),
110 (int) (state.from_pad.bearing + 0.5),
111 (int) (state.elevation + 0.5),
112 (int) (state.range + 0.5));
113 } else if (state.state > Altos.ao_flight_pad) {
114 voice.speak("%d meters", (int) (state.height + 0.5));
116 reported_landing = 0;
119 /* If the rocket is coming down, check to see if it has landed;
120 * either we've got a landed report or we haven't heard from it in
123 if (state.state >= Altos.ao_flight_drogue &&
125 System.currentTimeMillis() - state.report_time >= 15000 ||
126 state.state == Altos.ao_flight_landed))
128 if (Math.abs(state.baro_speed) < 20 && state.height < 100)
129 voice.speak("rocket landed safely");
131 voice.speak("rocket may have crashed");
132 if (state.from_pad != null)
133 voice.speak("Bearing %d degrees, range %d meters.",
134 (int) (state.from_pad.bearing + 0.5),
135 (int) (state.from_pad.distance + 0.5));
137 if (state.state != Altos.ao_flight_landed) {
138 state.state = Altos.ao_flight_landed;
139 show_safely(state, 0);
145 return System.currentTimeMillis();
148 void set_report_time() {
149 report_time = now() + report_interval;
158 synchronized (this) {
159 long sleep_time = report_time - now();
167 } catch (InterruptedException ie) {
170 } catch (InterruptedException iie) { }
174 public synchronized void notice(AltosState new_state, boolean spoken) {
175 AltosState old_state = state;
177 if (!started && state.state > Altos.ao_flight_pad) {
182 if (state.state < Altos.ao_flight_drogue)
183 report_interval = 10000;
185 report_interval = 20000;
186 if (old_state != null && old_state.state != state.state) {
193 public IdleThread() {
195 reported_landing = 0;
196 report_interval = 10000;
200 boolean tell(AltosState state, AltosState old_state) {
202 if (old_state == null || old_state.state != state.state) {
203 voice.speak(state.data.state());
204 if ((old_state == null || old_state.state <= Altos.ao_flight_boost) &&
205 state.state > Altos.ao_flight_boost) {
206 voice.speak("max speed: %d meters per second.",
207 (int) (state.max_speed + 0.5));
209 } else if ((old_state == null || old_state.state < Altos.ao_flight_drogue) &&
210 state.state >= Altos.ao_flight_drogue) {
211 voice.speak("max height: %d meters.",
212 (int) (state.max_height + 0.5));
216 if (old_state == null || old_state.gps_ready != state.gps_ready) {
217 if (state.gps_ready) {
218 voice.speak("GPS ready");
221 else if (old_state != null) {
222 voice.speak("GPS lost");
231 boolean interrupted = false;
233 AltosState state = null;
234 AltosState old_state = null;
237 idle_thread = new IdleThread();
242 AltosRecord record = reader.read();
246 state = new AltosState(record, state);
247 reader.update(state);
248 show_safely(state, crc_errors);
249 told = tell(state, old_state);
250 idle_thread.notice(state, told);
251 } catch (ParseException pp) {
252 System.out.printf("Parse error: %d \"%s\"\n", pp.getErrorOffset(), pp.getMessage());
253 } catch (AltosCRCException ce) {
255 show_safely(state, crc_errors);
258 } catch (InterruptedException ee) {
260 } catch (IOException ie) {
261 reading_error_safely(name);
264 idle_thread.report(true);
265 reader.close(interrupted);
266 idle_thread.interrupt();
269 } catch (InterruptedException ie) {}
273 public AltosDisplayThread(Frame in_parent, AltosVoice in_voice, AltosFlightDisplay in_display, AltosFlightReader in_reader) {
276 display = in_display;