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 %d, elevation %d, range %d.\n",
73 (int) (state.height + 0.5),
74 (int) (state.from_pad.bearing + 0.5),
75 (int) (state.elevation + 0.5),
76 (int) (state.range + 0.5));
77 } else if (state.state > Altos.ao_flight_pad) {
78 voice.speak("%d meters", (int) (state.height + 0.5));
83 /* If the rocket is coming down, check to see if it has landed;
84 * either we've got a landed report or we haven't heard from it in
87 if (state.state >= Altos.ao_flight_drogue &&
89 System.currentTimeMillis() - state.report_time >= 15000 ||
90 state.state == Altos.ao_flight_landed))
92 if (Math.abs(state.baro_speed) < 20 && state.height < 100)
93 voice.speak("rocket landed safely");
95 voice.speak("rocket may have crashed");
96 if (state.from_pad != null)
97 voice.speak("Bearing %d degrees, range %d meters.",
98 (int) (state.from_pad.bearing + 0.5),
99 (int) (state.from_pad.distance + 0.5));
101 if (state.state != Altos.ao_flight_landed) {
102 state.state = Altos.ao_flight_landed;
109 return System.currentTimeMillis();
112 void set_report_time() {
113 report_time = now() + report_interval;
118 reported_landing = 0;
120 report_interval = 10000;
126 synchronized (this) {
127 long sleep_time = report_time - now();
135 } catch (InterruptedException ie) {
138 } catch (InterruptedException iie) { }
142 public synchronized void notice(AltosState new_state, boolean spoken) {
143 AltosState old_state = state;
145 if (!started && state.state > Altos.ao_flight_pad) {
150 if (state.state < Altos.ao_flight_drogue)
151 report_interval = 10000;
153 report_interval = 20000;
154 if (old_state != null && old_state.state != state.state) {
162 boolean tell(AltosState state, AltosState old_state) {
164 if (old_state == null || old_state.state != state.state) {
165 voice.speak(state.data.state());
166 if ((old_state == null || old_state.state <= Altos.ao_flight_boost) &&
167 state.state > Altos.ao_flight_boost) {
168 voice.speak("max speed: %d meters per second.",
169 (int) (state.max_speed + 0.5));
171 } else if ((old_state == null || old_state.state < Altos.ao_flight_drogue) &&
172 state.state >= Altos.ao_flight_drogue) {
173 voice.speak("max height: %d meters.",
174 (int) (state.max_height + 0.5));
178 if (old_state == null || old_state.gps_ready != state.gps_ready) {
179 if (state.gps_ready) {
180 voice.speak("GPS ready");
183 else if (old_state != null) {
184 voice.speak("GPS lost");
193 boolean interrupted = false;
195 AltosState state = null;
196 AltosState old_state = null;
199 idle_thread = new IdleThread();
205 AltosRecord record = reader.read();
209 state = new AltosState(record, state);
210 reader.update(state);
211 show(state, crc_errors);
212 told = tell(state, old_state);
213 idle_thread.notice(state, told);
214 } catch (ParseException pp) {
215 System.out.printf("Parse error: %d \"%s\"\n", pp.getErrorOffset(), pp.getMessage());
216 } catch (AltosCRCException ce) {
218 show(state, crc_errors);
221 } catch (InterruptedException ee) {
223 } catch (IOException ie) {
224 JOptionPane.showMessageDialog(parent,
225 String.format("Error reading from \"%s\"", name),
226 "Telemetry Read Error",
227 JOptionPane.ERROR_MESSAGE);
230 idle_thread.report(true);
231 reader.close(interrupted);
232 idle_thread.interrupt();
235 } catch (InterruptedException ie) {}
239 public AltosDisplayThread(Frame in_parent, AltosVoice in_voice, AltosFlightDisplay in_display, AltosFlightReader in_reader) {
242 display = in_display;