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.
24 import org.altusmetrum.AltosLib.*;
26 public class AltosDisplayThread extends Thread {
29 IdleThread idle_thread;
31 AltosFlightReader reader;
33 AltosFlightDisplay display;
35 void show_internal(AltosState state, int crc_errors) {
37 display.show(state, crc_errors);
40 void show_safely(AltosState in_state, int in_crc_errors) {
41 final AltosState state = in_state;
42 final int crc_errors = in_crc_errors;
43 Runnable r = new Runnable() {
46 show_internal(state, crc_errors);
47 } catch (Exception ex) {
51 SwingUtilities.invokeLater(r);
54 void reading_error_internal() {
55 JOptionPane.showMessageDialog(parent,
56 String.format("Error reading from \"%s\"", reader.name),
57 "Telemetry Read Error",
58 JOptionPane.ERROR_MESSAGE);
61 void reading_error_safely() {
62 Runnable r = new Runnable() {
65 reading_error_internal();
66 } catch (Exception ex) {
70 SwingUtilities.invokeLater(r);
73 class IdleThread extends Thread {
76 private AltosState state;
81 public synchronized void report(boolean last) {
85 /* reset the landing count once we hear about a new flight */
86 if (state.state < Altos.ao_flight_drogue)
89 /* Shut up once the rocket is on the ground */
90 if (reported_landing > 2) {
94 /* If the rocket isn't on the pad, then report height */
95 if (Altos.ao_flight_drogue <= state.state &&
96 state.state < Altos.ao_flight_landed &&
99 voice.speak("Height %s, bearing %s %d, elevation %d, range %s.\n",
100 AltosConvert.height.say(state.height),
101 state.from_pad.bearing_words(
102 AltosGreatCircle.BEARING_VOICE),
103 (int) (state.from_pad.bearing + 0.5),
104 (int) (state.elevation + 0.5),
105 AltosConvert.distance.say(state.range));
106 } else if (state.state > Altos.ao_flight_pad) {
107 voice.speak(AltosConvert.height.say_units(state.height));
109 reported_landing = 0;
112 /* If the rocket is coming down, check to see if it has landed;
113 * either we've got a landed report or we haven't heard from it in
116 if (state.state >= Altos.ao_flight_drogue &&
118 System.currentTimeMillis() - state.report_time >= 15000 ||
119 state.state == Altos.ao_flight_landed))
121 if (Math.abs(state.baro_speed) < 20 && state.height < 100)
122 voice.speak("rocket landed safely");
124 voice.speak("rocket may have crashed");
125 if (state.from_pad != null)
126 voice.speak("Bearing %d degrees, range %s.",
127 (int) (state.from_pad.bearing + 0.5),
128 AltosConvert.distance.say_units(state.from_pad.distance));
130 if (state.state != Altos.ao_flight_landed) {
131 state.state = Altos.ao_flight_landed;
132 show_safely(state, 0);
138 return System.currentTimeMillis();
141 void set_report_time() {
142 report_time = now() + report_interval;
151 synchronized (this) {
152 long sleep_time = report_time - now();
160 } catch (InterruptedException ie) {
163 } catch (InterruptedException iie) { }
167 public synchronized void notice(AltosState new_state, boolean spoken) {
168 AltosState old_state = state;
170 if (!started && state.state > Altos.ao_flight_pad) {
175 if (state.state < Altos.ao_flight_drogue)
176 report_interval = 10000;
178 report_interval = 20000;
179 if (old_state != null && old_state.state != state.state) {
186 public IdleThread() {
188 reported_landing = 0;
189 report_interval = 10000;
193 boolean tell(AltosState state, AltosState old_state) {
195 if (old_state == null || old_state.state != state.state) {
196 voice.speak(state.data.state());
197 if ((old_state == null || old_state.state <= Altos.ao_flight_boost) &&
198 state.state > Altos.ao_flight_boost) {
199 voice.speak("max speed: %s.",
200 AltosConvert.speed.say_units(state.max_accel_speed + 0.5));
202 } else if ((old_state == null || old_state.state < Altos.ao_flight_drogue) &&
203 state.state >= Altos.ao_flight_drogue) {
204 voice.speak("max height: %s.",
205 AltosConvert.height.say_units(state.max_height + 0.5));
209 if (old_state == null || old_state.gps_ready != state.gps_ready) {
210 if (state.gps_ready) {
211 voice.speak("GPS ready");
214 else if (old_state != null) {
215 voice.speak("GPS lost");
224 boolean interrupted = false;
226 AltosState state = null;
227 AltosState old_state = null;
230 idle_thread = new IdleThread();
235 AltosRecord record = reader.read();
239 state = new AltosState(record, state);
240 reader.update(state);
241 show_safely(state, crc_errors);
242 told = tell(state, old_state);
243 idle_thread.notice(state, told);
244 } catch (ParseException pp) {
245 System.out.printf("Parse error: %d \"%s\"\n", pp.getErrorOffset(), pp.getMessage());
246 } catch (AltosCRCException ce) {
248 show_safely(state, crc_errors);
251 } catch (InterruptedException ee) {
253 } catch (IOException ie) {
254 reading_error_safely();
257 idle_thread.report(true);
258 reader.close(interrupted);
259 idle_thread.interrupt();
262 } catch (InterruptedException ie) {}
266 public AltosDisplayThread(Frame in_parent, AltosVoice in_voice, AltosFlightDisplay in_display, AltosFlightReader in_reader) {
269 display = in_display;