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