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 AltosStatusTable flightStatus;
40 AltosInfoTable flightInfo;
42 class IdleThread extends Thread {
45 private AltosState state;
50 public synchronized void report(boolean last) {
54 /* reset the landing count once we hear about a new flight */
55 if (state.state < Altos.ao_flight_drogue)
58 /* Shut up once the rocket is on the ground */
59 if (reported_landing > 2) {
63 /* If the rocket isn't on the pad, then report height */
64 if (Altos.ao_flight_drogue <= state.state &&
65 state.state < Altos.ao_flight_landed &&
68 voice.speak("Height %d, bearing %d, elevation %d, range %d.\n",
69 (int) (state.height + 0.5),
70 (int) (state.from_pad.bearing + 0.5),
71 (int) (state.elevation + 0.5),
72 (int) (state.range + 0.5));
73 } else if (state.state > Altos.ao_flight_pad) {
74 voice.speak("%d meters", (int) (state.height + 0.5));
79 /* If the rocket is coming down, check to see if it has landed;
80 * either we've got a landed report or we haven't heard from it in
83 if (state.state >= Altos.ao_flight_drogue &&
85 System.currentTimeMillis() - state.report_time >= 15000 ||
86 state.state == Altos.ao_flight_landed))
88 if (Math.abs(state.baro_speed) < 20 && state.height < 100)
89 voice.speak("rocket landed safely");
91 voice.speak("rocket may have crashed");
92 if (state.from_pad != null)
93 voice.speak("Bearing %d degrees, range %d meters.",
94 (int) (state.from_pad.bearing + 0.5),
95 (int) (state.from_pad.distance + 0.5));
101 return System.currentTimeMillis();
104 void set_report_time() {
105 report_time = now() + report_interval;
110 reported_landing = 0;
112 report_interval = 10000;
118 synchronized (this) {
119 long sleep_time = report_time - now();
127 } catch (InterruptedException ie) {
130 } catch (InterruptedException iie) { }
134 public synchronized void notice(AltosState new_state, boolean spoken) {
135 AltosState old_state = state;
137 if (!started && state.state > Altos.ao_flight_pad) {
142 if (state.state < Altos.ao_flight_drogue)
143 report_interval = 10000;
145 report_interval = 20000;
146 if (old_state != null && old_state.state != state.state) {
154 boolean tell(AltosState state, AltosState old_state) {
156 if (old_state == null || old_state.state != state.state) {
157 voice.speak(state.data.state());
158 if ((old_state == null || old_state.state <= Altos.ao_flight_boost) &&
159 state.state > Altos.ao_flight_boost) {
160 voice.speak("max speed: %d meters per second.",
161 (int) (state.max_speed + 0.5));
163 } else if ((old_state == null || old_state.state < Altos.ao_flight_drogue) &&
164 state.state >= Altos.ao_flight_drogue) {
165 voice.speak("max height: %d meters.",
166 (int) (state.max_height + 0.5));
170 if (old_state == null || old_state.gps_ready != state.gps_ready) {
171 if (state.gps_ready) {
172 voice.speak("GPS ready");
175 else if (old_state != null) {
176 voice.speak("GPS lost");
184 void show(AltosState state, int crc_errors) {
186 flightStatus.set(state);
187 flightInfo.show(state, crc_errors);
192 boolean interrupted = false;
194 AltosState state = null;
195 AltosState old_state = null;
198 idle_thread = new IdleThread();
204 AltosRecord record = reader.read();
208 state = new AltosState(record, state);
209 reader.update(state);
210 show(state, crc_errors);
211 told = tell(state, old_state);
212 idle_thread.notice(state, told);
213 } catch (ParseException pp) {
214 System.out.printf("Parse error: %d \"%s\"\n", pp.getErrorOffset(), pp.getMessage());
215 } catch (AltosCRCException ce) {
217 show(state, crc_errors);
220 } catch (InterruptedException ee) {
222 } catch (IOException ie) {
223 JOptionPane.showMessageDialog(parent,
224 String.format("Error reading from \"%s\"", name),
225 "Telemetry Read Error",
226 JOptionPane.ERROR_MESSAGE);
229 idle_thread.report(true);
230 reader.close(interrupted);
231 idle_thread.interrupt();
234 } catch (InterruptedException ie) {}
238 public AltosDisplayThread(Frame in_parent, AltosVoice in_voice, AltosStatusTable in_status, AltosInfoTable in_info, AltosFlightReader in_reader) {
241 flightStatus = in_status;
242 flightInfo = in_info;