Merge branch 'buttonbox' of git://git.gag.com/fw/altos into buttonbox
[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         synchronized void show(AltosState state, int crc_errors) {
42                 if (state != null)
43                         display.show(state, crc_errors);
44         }
45
46         class IdleThread extends Thread {
47
48                 boolean started;
49                 private AltosState state;
50                 int     reported_landing;
51                 int     report_interval;
52                 long    report_time;
53
54                 public synchronized void report(boolean last) {
55                         if (state == null)
56                                 return;
57
58                         /* reset the landing count once we hear about a new flight */
59                         if (state.state < Altos.ao_flight_drogue)
60                                 reported_landing = 0;
61
62                         /* Shut up once the rocket is on the ground */
63                         if (reported_landing > 2) {
64                                 return;
65                         }
66
67                         /* If the rocket isn't on the pad, then report height */
68                         if (Altos.ao_flight_drogue <= state.state &&
69                             state.state < Altos.ao_flight_landed &&
70                             state.range >= 0)
71                         {
72                                 voice.speak("Height %d, bearing %s %d, elevation %d, range %d.\n",
73                                             (int) (state.height + 0.5),
74                         state.from_pad.bearing_words(
75                             AltosGreatCircle.BEARING_VOICE),
76                                             (int) (state.from_pad.bearing + 0.5),
77                                             (int) (state.elevation + 0.5),
78                                             (int) (state.range + 0.5));
79                         } else if (state.state > Altos.ao_flight_pad) {
80                                 voice.speak("%d meters", (int) (state.height + 0.5));
81                         } else {
82                                 reported_landing = 0;
83                         }
84
85                         /* If the rocket is coming down, check to see if it has landed;
86                          * either we've got a landed report or we haven't heard from it in
87                          * a long time
88                          */
89                         if (state.state >= Altos.ao_flight_drogue &&
90                             (last ||
91                              System.currentTimeMillis() - state.report_time >= 15000 ||
92                              state.state == Altos.ao_flight_landed))
93                         {
94                                 if (Math.abs(state.baro_speed) < 20 && state.height < 100)
95                                         voice.speak("rocket landed safely");
96                                 else
97                                         voice.speak("rocket may have crashed");
98                                 if (state.from_pad != null)
99                                         voice.speak("Bearing %d degrees, range %d meters.",
100                                                     (int) (state.from_pad.bearing + 0.5),
101                                                     (int) (state.from_pad.distance + 0.5));
102                                 ++reported_landing;
103                                 if (state.state != Altos.ao_flight_landed) {
104                                         state.state = Altos.ao_flight_landed;
105                                         show(state, 0);
106                                 }
107                         }
108                 }
109
110                 long now () {
111                         return System.currentTimeMillis();
112                 }
113
114                 void set_report_time() {
115                         report_time = now() + report_interval;
116                 }
117
118                 public void run () {
119                         try {
120                                 for (;;) {
121                                         set_report_time();
122                                         for (;;) {
123                                                 voice.drain();
124                                                 synchronized (this) {
125                                                         long    sleep_time = report_time - now();
126                                                         if (sleep_time <= 0)
127                                                                 break;
128                                                         wait(sleep_time);
129                                                 }
130                                         }
131                                         report(false);
132                                 }
133                         } catch (InterruptedException ie) {
134                                 try {
135                                         voice.drain();
136                                 } catch (InterruptedException iie) { }
137                         }
138                 }
139
140                 public synchronized void notice(AltosState new_state, boolean spoken) {
141                         AltosState old_state = state;
142                         state = new_state;
143                         if (!started && state.state > Altos.ao_flight_pad) {
144                                 started = true;
145                                 start();
146                         }
147
148                         if (state.state < Altos.ao_flight_drogue)
149                                 report_interval = 10000;
150                         else
151                                 report_interval = 20000;
152                         if (old_state != null && old_state.state != state.state) {
153                                 report_time = now();
154                                 this.notify();
155                         } else if (spoken)
156                                 set_report_time();
157                 }
158
159                 public IdleThread() {
160                         state = null;
161                         reported_landing = 0;
162                         report_interval = 10000;
163                 }
164         }
165
166         boolean tell(AltosState state, AltosState old_state) {
167                 boolean ret = false;
168                 if (old_state == null || old_state.state != state.state) {
169                         voice.speak(state.data.state());
170                         if ((old_state == null || old_state.state <= Altos.ao_flight_boost) &&
171                             state.state > Altos.ao_flight_boost) {
172                                 voice.speak("max speed: %d meters per second.",
173                                             (int) (state.max_speed + 0.5));
174                                 ret = true;
175                         } else if ((old_state == null || old_state.state < Altos.ao_flight_drogue) &&
176                                    state.state >= Altos.ao_flight_drogue) {
177                                 voice.speak("max height: %d meters.",
178                                             (int) (state.max_height + 0.5));
179                                 ret = true;
180                         }
181                 }
182                 if (old_state == null || old_state.gps_ready != state.gps_ready) {
183                         if (state.gps_ready) {
184                                 voice.speak("GPS ready");
185                                 ret = true;
186                         }
187                         else if (old_state != null) {
188                                 voice.speak("GPS lost");
189                                 ret = true;
190                         }
191                 }
192                 old_state = state;
193                 return ret;
194         }
195
196         public void run() {
197                 boolean         interrupted = false;
198                 String          line;
199                 AltosState      state = null;
200                 AltosState      old_state = null;
201                 boolean         told;
202
203                 idle_thread = new IdleThread();
204
205                 display.reset();
206                 try {
207                         for (;;) {
208                                 try {
209                                         AltosRecord record = reader.read();
210                                         if (record == null)
211                                                 break;
212                                         old_state = state;
213                                         state = new AltosState(record, state);
214                                         reader.update(state);
215                                         show(state, crc_errors);
216                                         told = tell(state, old_state);
217                                         idle_thread.notice(state, told);
218                                 } catch (ParseException pp) {
219                                         System.out.printf("Parse error: %d \"%s\"\n", pp.getErrorOffset(), pp.getMessage());
220                                 } catch (AltosCRCException ce) {
221                                         ++crc_errors;
222                                         show(state, crc_errors);
223                                 }
224                         }
225                 } catch (InterruptedException ee) {
226                         interrupted = true;
227                 } catch (IOException ie) {
228                         JOptionPane.showMessageDialog(parent,
229                                                       String.format("Error reading from \"%s\"", name),
230                                                       "Telemetry Read Error",
231                                                       JOptionPane.ERROR_MESSAGE);
232                 } finally {
233                         if (!interrupted)
234                                 idle_thread.report(true);
235                         reader.close(interrupted);
236                         idle_thread.interrupt();
237                         try {
238                                 idle_thread.join();
239                         } catch (InterruptedException ie) {}
240                 }
241         }
242
243         public AltosDisplayThread(Frame in_parent, AltosVoice in_voice, AltosFlightDisplay in_display, AltosFlightReader in_reader) {
244                 parent = in_parent;
245                 voice = in_voice;
246                 display = in_display;
247                 reader = in_reader;
248         }
249 }