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