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 synchronized void show(AltosState state, int crc_errors) {
43 display.show(state, crc_errors);
46 class IdleThread extends Thread {
49 private AltosState state;
54 public synchronized void report(boolean last) {
58 /* reset the landing count once we hear about a new flight */
59 if (state.state < Altos.ao_flight_drogue)
62 /* Shut up once the rocket is on the ground */
63 if (reported_landing > 2) {
67 /* If the rocket isn't on the pad, then report height */
68 if (Altos.ao_flight_drogue <= state.state &&
69 state.state < Altos.ao_flight_landed &&
72 voice.speak("Height %d, bearing %s %d, elevation %d, range %d.\n",
73 (int) (state.height + 0.5),
74 state.from_pad.bearing_words(
75 AltosGreatCircle.BEARING_VOICE),
76 (int) (state.from_pad.bearing + 0.5),
77 (int) (state.elevation + 0.5),
78 (int) (state.range + 0.5));
79 } else if (state.state > Altos.ao_flight_pad) {
80 voice.speak("%d meters", (int) (state.height + 0.5));
85 /* If the rocket is coming down, check to see if it has landed;
86 * either we've got a landed report or we haven't heard from it in
89 if (state.state >= Altos.ao_flight_drogue &&
91 System.currentTimeMillis() - state.report_time >= 15000 ||
92 state.state == Altos.ao_flight_landed))
94 if (Math.abs(state.baro_speed) < 20 && state.height < 100)
95 voice.speak("rocket landed safely");
97 voice.speak("rocket may have crashed");
98 if (state.from_pad != null)
99 voice.speak("Bearing %d degrees, range %d meters.",
100 (int) (state.from_pad.bearing + 0.5),
101 (int) (state.from_pad.distance + 0.5));
103 if (state.state != Altos.ao_flight_landed) {
104 state.state = Altos.ao_flight_landed;
111 return System.currentTimeMillis();
114 void set_report_time() {
115 report_time = now() + report_interval;
124 synchronized (this) {
125 long sleep_time = report_time - now();
133 } catch (InterruptedException ie) {
136 } catch (InterruptedException iie) { }
140 public synchronized void notice(AltosState new_state, boolean spoken) {
141 AltosState old_state = state;
143 if (!started && state.state > Altos.ao_flight_pad) {
148 if (state.state < Altos.ao_flight_drogue)
149 report_interval = 10000;
151 report_interval = 20000;
152 if (old_state != null && old_state.state != state.state) {
159 public IdleThread() {
161 reported_landing = 0;
162 report_interval = 10000;
166 boolean tell(AltosState state, AltosState old_state) {
168 if (old_state == null || old_state.state != state.state) {
169 voice.speak(state.data.state());
170 if ((old_state == null || old_state.state <= Altos.ao_flight_boost) &&
171 state.state > Altos.ao_flight_boost) {
172 voice.speak("max speed: %d meters per second.",
173 (int) (state.max_speed + 0.5));
175 } else if ((old_state == null || old_state.state < Altos.ao_flight_drogue) &&
176 state.state >= Altos.ao_flight_drogue) {
177 voice.speak("max height: %d meters.",
178 (int) (state.max_height + 0.5));
182 if (old_state == null || old_state.gps_ready != state.gps_ready) {
183 if (state.gps_ready) {
184 voice.speak("GPS ready");
187 else if (old_state != null) {
188 voice.speak("GPS lost");
197 boolean interrupted = false;
199 AltosState state = null;
200 AltosState old_state = null;
203 idle_thread = new IdleThread();
209 AltosRecord record = reader.read();
213 state = new AltosState(record, state);
214 reader.update(state);
215 show(state, crc_errors);
216 told = tell(state, old_state);
217 idle_thread.notice(state, told);
218 } catch (ParseException pp) {
219 System.out.printf("Parse error: %d \"%s\"\n", pp.getErrorOffset(), pp.getMessage());
220 } catch (AltosCRCException ce) {
222 show(state, crc_errors);
225 } catch (InterruptedException ee) {
227 } catch (IOException ie) {
228 JOptionPane.showMessageDialog(parent,
229 String.format("Error reading from \"%s\"", name),
230 "Telemetry Read Error",
231 JOptionPane.ERROR_MESSAGE);
234 idle_thread.report(true);
235 reader.close(interrupted);
236 idle_thread.interrupt();
239 } catch (InterruptedException ie) {}
243 public AltosDisplayThread(Frame in_parent, AltosVoice in_voice, AltosFlightDisplay in_display, AltosFlightReader in_reader) {
246 display = in_display;