update changelogs for Debian build
[fw/altos] / altosui / AltosDisplayThread.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package altosui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import java.io.*;
26 import java.util.*;
27 import java.text.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.LinkedBlockingQueue;
30
31 public class AltosDisplayThread extends Thread {
32
33         Frame                   parent;
34         IdleThread              idle_thread;
35         AltosVoice              voice;
36         String                  name;
37         AltosFlightReader       reader;
38         int                     crc_errors;
39         AltosFlightDisplay      display;
40
41         void show_internal(AltosState state, int crc_errors) {
42                 if (state != null)
43                         display.show(state, crc_errors);
44         }
45
46         void show_safely(AltosState in_state, int in_crc_errors) {
47                 final AltosState state = in_state;
48                 final int crc_errors = in_crc_errors;
49                 Runnable r = new Runnable() {
50                                 public void run() {
51                                         try {
52                                                 show_internal(state, crc_errors);
53                                         } catch (Exception ex) {
54                                         }
55                                 }
56                         };
57                 SwingUtilities.invokeLater(r);
58         }
59
60         void reading_error_internal(String name) {
61                 JOptionPane.showMessageDialog(parent,
62                                               String.format("Error reading from \"%s\"", name),
63                                               "Telemetry Read Error",
64                                               JOptionPane.ERROR_MESSAGE);
65         }
66
67         void reading_error_safely(String in_name) {
68                 final String name = in_name;
69                 Runnable r = new Runnable() {
70                                 public void run() {
71                                         try {
72                                                 reading_error_internal(name);
73                                         } catch (Exception ex) {
74                                         }
75                                 }
76                         };
77                 SwingUtilities.invokeLater(r);
78         }
79
80         class IdleThread extends Thread {
81
82                 boolean started;
83                 private AltosState state;
84                 int     reported_landing;
85                 int     report_interval;
86                 long    report_time;
87
88                 public synchronized void report(boolean last) {
89                         if (state == null)
90                                 return;
91
92                         /* reset the landing count once we hear about a new flight */
93                         if (state.state < Altos.ao_flight_drogue)
94                                 reported_landing = 0;
95
96                         /* Shut up once the rocket is on the ground */
97                         if (reported_landing > 2) {
98                                 return;
99                         }
100
101                         /* If the rocket isn't on the pad, then report height */
102                         if (Altos.ao_flight_drogue <= state.state &&
103                             state.state < Altos.ao_flight_landed &&
104                             state.range >= 0)
105                         {
106                                 voice.speak("Height %d, bearing %s %d, elevation %d, range %d.\n",
107                                             (int) (state.height + 0.5),
108                         state.from_pad.bearing_words(
109                             AltosGreatCircle.BEARING_VOICE),
110                                             (int) (state.from_pad.bearing + 0.5),
111                                             (int) (state.elevation + 0.5),
112                                             (int) (state.range + 0.5));
113                         } else if (state.state > Altos.ao_flight_pad) {
114                                 voice.speak("%d meters", (int) (state.height + 0.5));
115                         } else {
116                                 reported_landing = 0;
117                         }
118
119                         /* If the rocket is coming down, check to see if it has landed;
120                          * either we've got a landed report or we haven't heard from it in
121                          * a long time
122                          */
123                         if (state.state >= Altos.ao_flight_drogue &&
124                             (last ||
125                              System.currentTimeMillis() - state.report_time >= 15000 ||
126                              state.state == Altos.ao_flight_landed))
127                         {
128                                 if (Math.abs(state.baro_speed) < 20 && state.height < 100)
129                                         voice.speak("rocket landed safely");
130                                 else
131                                         voice.speak("rocket may have crashed");
132                                 if (state.from_pad != null)
133                                         voice.speak("Bearing %d degrees, range %d meters.",
134                                                     (int) (state.from_pad.bearing + 0.5),
135                                                     (int) (state.from_pad.distance + 0.5));
136                                 ++reported_landing;
137                                 if (state.state != Altos.ao_flight_landed) {
138                                         state.state = Altos.ao_flight_landed;
139                                         show_safely(state, 0);
140                                 }
141                         }
142                 }
143
144                 long now () {
145                         return System.currentTimeMillis();
146                 }
147
148                 void set_report_time() {
149                         report_time = now() + report_interval;
150                 }
151
152                 public void run () {
153                         try {
154                                 for (;;) {
155                                         set_report_time();
156                                         for (;;) {
157                                                 voice.drain();
158                                                 synchronized (this) {
159                                                         long    sleep_time = report_time - now();
160                                                         if (sleep_time <= 0)
161                                                                 break;
162                                                         wait(sleep_time);
163                                                 }
164                                         }
165                                         report(false);
166                                 }
167                         } catch (InterruptedException ie) {
168                                 try {
169                                         voice.drain();
170                                 } catch (InterruptedException iie) { }
171                         }
172                 }
173
174                 public synchronized void notice(AltosState new_state, boolean spoken) {
175                         AltosState old_state = state;
176                         state = new_state;
177                         if (!started && state.state > Altos.ao_flight_pad) {
178                                 started = true;
179                                 start();
180                         }
181
182                         if (state.state < Altos.ao_flight_drogue)
183                                 report_interval = 10000;
184                         else
185                                 report_interval = 20000;
186                         if (old_state != null && old_state.state != state.state) {
187                                 report_time = now();
188                                 this.notify();
189                         } else if (spoken)
190                                 set_report_time();
191                 }
192
193                 public IdleThread() {
194                         state = null;
195                         reported_landing = 0;
196                         report_interval = 10000;
197                 }
198         }
199
200         boolean tell(AltosState state, AltosState old_state) {
201                 boolean ret = false;
202                 if (old_state == null || old_state.state != state.state) {
203                         voice.speak(state.data.state());
204                         if ((old_state == null || old_state.state <= Altos.ao_flight_boost) &&
205                             state.state > Altos.ao_flight_boost) {
206                                 voice.speak("max speed: %d meters per second.",
207                                             (int) (state.max_speed + 0.5));
208                                 ret = true;
209                         } else if ((old_state == null || old_state.state < Altos.ao_flight_drogue) &&
210                                    state.state >= Altos.ao_flight_drogue) {
211                                 voice.speak("max height: %d meters.",
212                                             (int) (state.max_height + 0.5));
213                                 ret = true;
214                         }
215                 }
216                 if (old_state == null || old_state.gps_ready != state.gps_ready) {
217                         if (state.gps_ready) {
218                                 voice.speak("GPS ready");
219                                 ret = true;
220                         }
221                         else if (old_state != null) {
222                                 voice.speak("GPS lost");
223                                 ret = true;
224                         }
225                 }
226                 old_state = state;
227                 return ret;
228         }
229
230         public void run() {
231                 boolean         interrupted = false;
232                 String          line;
233                 AltosState      state = null;
234                 AltosState      old_state = null;
235                 boolean         told;
236
237                 idle_thread = new IdleThread();
238
239                 try {
240                         for (;;) {
241                                 try {
242                                         AltosRecord record = reader.read();
243                                         if (record == null)
244                                                 break;
245                                         old_state = state;
246                                         state = new AltosState(record, state);
247                                         reader.update(state);
248                                         show_safely(state, crc_errors);
249                                         told = tell(state, old_state);
250                                         idle_thread.notice(state, told);
251                                 } catch (ParseException pp) {
252                                         System.out.printf("Parse error: %d \"%s\"\n", pp.getErrorOffset(), pp.getMessage());
253                                 } catch (AltosCRCException ce) {
254                                         ++crc_errors;
255                                         show_safely(state, crc_errors);
256                                 }
257                         }
258                 } catch (InterruptedException ee) {
259                         interrupted = true;
260                 } catch (IOException ie) {
261                         reading_error_safely(name);
262                 } finally {
263                         if (!interrupted)
264                                 idle_thread.report(true);
265                         reader.close(interrupted);
266                         idle_thread.interrupt();
267                         try {
268                                 idle_thread.join();
269                         } catch (InterruptedException ie) {}
270                 }
271         }
272
273         public AltosDisplayThread(Frame in_parent, AltosVoice in_voice, AltosFlightDisplay in_display, AltosFlightReader in_reader) {
274                 parent = in_parent;
275                 voice = in_voice;
276                 display = in_display;
277                 reader = in_reader;
278                 display.reset();
279         }
280 }