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_11;
25 import org.altusmetrum.altoslib_11.*;
27 public class AltosDisplayThread extends Thread {
30 IdleThread idle_thread;
32 AltosFlightReader reader;
33 AltosState old_state, state;
34 AltosListenerState listener_state;
35 AltosFlightDisplay display;
37 synchronized void show_safely() {
38 final AltosState my_state = state;
39 final AltosListenerState my_listener_state = listener_state;
40 Runnable r = new Runnable() {
43 display.show(my_state, my_listener_state);
44 } catch (Exception ex) {
48 SwingUtilities.invokeLater(r);
51 void reading_error_internal() {
52 JOptionPane.showMessageDialog(parent,
53 String.format("Error reading from \"%s\"", reader.name),
54 "Telemetry Read Error",
55 JOptionPane.ERROR_MESSAGE);
58 void reading_error_safely() {
59 Runnable r = new Runnable() {
62 reading_error_internal();
63 } catch (Exception ex) {
67 SwingUtilities.invokeLater(r);
70 class IdleThread extends Thread {
77 public synchronized void report(boolean last) {
81 /* reset the landing count once we hear about a new flight */
82 if (state.state() < AltosLib.ao_flight_drogue)
85 /* Shut up once the rocket is on the ground */
86 if (reported_landing > 2) {
90 /* If the rocket isn't on the pad, then report height */
91 if (AltosLib.ao_flight_drogue <= state.state() &&
92 state.state() < AltosLib.ao_flight_landed &&
93 state.from_pad != null &&
96 voice.speak("Height %s, bearing %s %d, elevation %d, range %s.\n",
97 AltosConvert.height.say(state.height()),
98 state.from_pad.bearing_words(
99 AltosGreatCircle.BEARING_VOICE),
100 (int) (state.from_pad.bearing + 0.5),
101 (int) (state.elevation + 0.5),
102 AltosConvert.distance.say(state.range));
103 } else if (state.state() > AltosLib.ao_flight_pad && state.height() != AltosLib.MISSING) {
104 voice.speak(AltosConvert.height.say_units(state.height()));
106 reported_landing = 0;
109 /* If the rocket is coming down, check to see if it has landed;
110 * either we've got a landed report or we haven't heard from it in
113 if (state.state() != AltosLib.ao_flight_stateless &&
114 state.state() >= AltosLib.ao_flight_drogue &&
116 System.currentTimeMillis() - state.received_time >= 15000 ||
117 state.state() == AltosLib.ao_flight_landed))
119 if (Math.abs(state.speed()) < 20 && state.height() < 100)
120 voice.speak("rocket landed safely");
122 voice.speak("rocket may have crashed");
123 if (state.from_pad != null)
124 voice.speak("Bearing %d degrees, range %s.",
125 (int) (state.from_pad.bearing + 0.5),
126 AltosConvert.distance.say_units(state.from_pad.distance));
132 return System.currentTimeMillis();
135 void set_report_time() {
136 report_time = now() + report_interval;
142 if (reader.has_monitor_battery()) {
143 listener_state.battery = reader.monitor_battery();
149 synchronized (this) {
150 long sleep_time = report_time - now();
159 } catch (InterruptedException ie) {
162 } catch (InterruptedException iie) { }
166 public synchronized void notice(boolean spoken) {
167 if (old_state != null && old_state.state() != state.state()) {
174 public IdleThread() {
175 reported_landing = 0;
176 report_interval = 10000;
180 synchronized boolean tell() {
182 if (old_state == null || old_state.state() != state.state()) {
183 if (state.state() != AltosLib.ao_flight_stateless)
184 voice.speak(state.state_name());
185 if ((old_state == null || old_state.state() <= AltosLib.ao_flight_boost) &&
186 state.state() > AltosLib.ao_flight_boost) {
187 if (state.max_speed() != AltosLib.MISSING)
188 voice.speak("max speed: %s.",
189 AltosConvert.speed.say_units(state.max_speed() + 0.5));
191 } else if ((old_state == null || old_state.state() < AltosLib.ao_flight_drogue) &&
192 state.state() >= AltosLib.ao_flight_drogue) {
193 if (state.max_height() != AltosLib.MISSING)
194 voice.speak("max height: %s.",
195 AltosConvert.height.say_units(state.max_height() + 0.5));
199 if (old_state == null || old_state.gps_ready != state.gps_ready) {
200 if (state.gps_ready) {
201 voice.speak("GPS ready");
204 else if (old_state != null) {
205 voice.speak("GPS lost");
214 boolean interrupted = false;
217 idle_thread = new IdleThread();
223 AltosState new_state = reader.read();
224 if (new_state == null) {
226 listener_state.running = false;
229 reader.update(new_state);
233 idle_thread.notice(told);
234 } catch (ParseException pp) {
235 System.out.printf("Parse error: %d \"%s\"\n", pp.getErrorOffset(), pp.getMessage());
236 } catch (AltosCRCException ce) {
237 ++listener_state.crc_errors;
241 } catch (InterruptedException ee) {
243 } catch (IOException ie) {
244 reading_error_safely();
247 idle_thread.report(true);
248 reader.close(interrupted);
249 idle_thread.interrupt();
252 } catch (InterruptedException ie) {}
256 public AltosDisplayThread(Frame in_parent, AltosVoice in_voice, AltosFlightDisplay in_display, AltosFlightReader in_reader) {
257 listener_state = new AltosListenerState();
260 display = in_display;