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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altosuilib_14;
25 import org.altusmetrum.altoslib_14.*;
27 public class AltosDisplayThread extends Thread {
30 IdleThread idle_thread;
32 AltosFlightReader reader;
34 int old_state = AltosLib.ao_flight_invalid;
35 boolean old_gps_ready = false;
36 AltosListenerState listener_state;
37 AltosFlightDisplay display;
39 synchronized void show_safely() {
40 final AltosState my_state = state;
41 final AltosListenerState my_listener_state = listener_state;
42 Runnable r = new Runnable() {
45 display.show(my_state, my_listener_state);
46 } catch (Exception ex) {
50 SwingUtilities.invokeLater(r);
53 void reading_error_internal() {
54 JOptionPane.showMessageDialog(parent,
55 String.format("Error reading from \"%s\"", reader.name),
56 "Telemetry Read Error",
57 JOptionPane.ERROR_MESSAGE);
60 void reading_error_safely() {
61 Runnable r = new Runnable() {
64 reading_error_internal();
65 } catch (Exception ex) {
69 SwingUtilities.invokeLater(r);
72 class IdleThread extends Thread {
79 public synchronized void report(boolean last) {
83 /* reset the landing count once we hear about a new flight */
84 if (state.state() < AltosLib.ao_flight_drogue)
87 /* Shut up once the rocket is on the ground */
88 if (reported_landing > 2) {
92 /* If the rocket isn't on the pad, then report height */
93 if (AltosLib.ao_flight_drogue <= state.state() &&
94 state.state() < AltosLib.ao_flight_landed &&
95 state.from_pad != null &&
98 voice.speak("Height %s, bearing %s %d, elevation %d, distance %s.\n",
99 AltosConvert.height.say(state.height()),
100 state.from_pad.bearing_words(
101 AltosGreatCircle.BEARING_VOICE),
102 (int) (state.from_pad.bearing + 0.5),
103 (int) (state.elevation + 0.5),
104 AltosConvert.distance.say(state.distance));
105 } else if (state.state() > AltosLib.ao_flight_pad && state.height() != AltosLib.MISSING) {
106 voice.speak(AltosConvert.height.say_units(state.height()));
108 reported_landing = 0;
111 /* If the rocket is coming down, check to see if it has landed;
112 * either we've got a landed report or we haven't heard from it in
115 if (state.state() != AltosLib.ao_flight_stateless &&
116 state.state() >= AltosLib.ao_flight_drogue &&
118 System.currentTimeMillis() - state.received_time >= 15000 ||
119 state.state() == AltosLib.ao_flight_landed))
121 if (Math.abs(state.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, distance %s.",
127 (int) (state.from_pad.bearing + 0.5),
128 AltosConvert.distance.say_units(state.from_pad.distance));
134 return System.currentTimeMillis();
137 void set_report_time() {
138 report_time = now() + report_interval;
144 if (reader.has_monitor_battery()) {
145 listener_state.battery = reader.monitor_battery();
151 synchronized (this) {
152 long sleep_time = report_time - now();
161 } catch (InterruptedException ie) {
165 public synchronized void notice(boolean spoken) {
166 if (old_state != state.state()) {
173 public IdleThread() {
174 reported_landing = 0;
175 report_interval = 10000;
179 synchronized boolean tell() {
181 if (old_state != state.state()) {
182 if (state.state() != AltosLib.ao_flight_stateless)
183 voice.speak(state.state_name());
184 if ((old_state == AltosLib.ao_flight_invalid || old_state <= AltosLib.ao_flight_boost) &&
185 state.state() > AltosLib.ao_flight_boost) {
186 if (state.max_speed() != AltosLib.MISSING)
187 voice.speak("max speed: %s.",
188 AltosConvert.speed.say_units(state.max_speed() + 0.5));
190 } else if ((old_state == AltosLib.ao_flight_invalid || old_state < AltosLib.ao_flight_drogue) &&
191 state.state() >= AltosLib.ao_flight_drogue) {
192 if (state.max_height() != AltosLib.MISSING)
193 voice.speak("max height: %s.",
194 AltosConvert.height.say_units(state.max_height() + 0.5));
198 if (old_gps_ready != state.gps_ready) {
199 if (state.gps_ready) {
200 voice.speak("GPS ready");
203 else if (old_gps_ready) {
204 voice.speak("GPS lost");
208 old_state = state.state();
209 old_gps_ready = state.gps_ready;
214 boolean interrupted = false;
217 idle_thread = new IdleThread();
223 state = reader.read();
225 listener_state.running = false;
230 idle_thread.notice(told);
231 } catch (ParseException pp) {
232 System.out.printf("Parse error: %d \"%s\"\n", pp.getErrorOffset(), pp.getMessage());
233 } catch (AltosCRCException ce) {
234 ++listener_state.crc_errors;
238 } catch (InterruptedException ee) {
240 } catch (IOException ie) {
241 reading_error_safely();
244 idle_thread.report(true);
245 reader.close(interrupted);
246 idle_thread.interrupt();
249 } catch (InterruptedException ie) {}
253 public AltosDisplayThread(Frame in_parent, AltosVoice in_voice, AltosFlightDisplay in_display, AltosFlightReader in_reader) {
254 listener_state = new AltosListenerState();
257 display = in_display;