telegps: use new eeprom reading code
[fw/altos] / altosuilib / 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
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.
13  *
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.
17  */
18
19 package org.altusmetrum.altosuilib_11;
20
21 import java.awt.*;
22 import javax.swing.*;
23 import java.io.*;
24 import java.text.*;
25 import org.altusmetrum.altoslib_11.*;
26
27 public class AltosDisplayThread extends Thread {
28
29         Frame                   parent;
30         IdleThread              idle_thread;
31         AltosVoice              voice;
32         AltosFlightReader       reader;
33         AltosState              old_state, state;
34         AltosListenerState      listener_state;
35         AltosFlightDisplay      display;
36
37         synchronized void show_safely() {
38                 final AltosState my_state = state;
39                 final AltosListenerState my_listener_state = listener_state;
40                 Runnable r = new Runnable() {
41                                 public void run() {
42                                         try {
43                                                 display.show(my_state, my_listener_state);
44                                         } catch (Exception ex) {
45                                         }
46                                 }
47                         };
48                 SwingUtilities.invokeLater(r);
49         }
50
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);
56         }
57
58         void reading_error_safely() {
59                 Runnable r = new Runnable() {
60                                 public void run() {
61                                         try {
62                                                 reading_error_internal();
63                                         } catch (Exception ex) {
64                                         }
65                                 }
66                         };
67                 SwingUtilities.invokeLater(r);
68         }
69
70         class IdleThread extends Thread {
71
72                 boolean started;
73                 int     reported_landing;
74                 int     report_interval;
75                 long    report_time;
76
77                 public synchronized void report(boolean last) {
78                         if (state == null)
79                                 return;
80
81                         /* reset the landing count once we hear about a new flight */
82                         if (state.state() < AltosLib.ao_flight_drogue)
83                                 reported_landing = 0;
84
85                         /* Shut up once the rocket is on the ground */
86                         if (reported_landing > 2) {
87                                 return;
88                         }
89
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 &&
94                             state.range >= 0)
95                         {
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()));
105                         } else {
106                                 reported_landing = 0;
107                         }
108
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
111                          * a long time
112                          */
113                         if (state.state() != AltosLib.ao_flight_stateless &&
114                             state.state() >= AltosLib.ao_flight_drogue &&
115                             (last ||
116                              System.currentTimeMillis() - state.received_time >= 15000 ||
117                              state.state() == AltosLib.ao_flight_landed))
118                         {
119                                 if (Math.abs(state.speed()) < 20 && state.height() < 100)
120                                         voice.speak("rocket landed safely");
121                                 else
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));
127                                 ++reported_landing;
128                         }
129                 }
130
131                 long now () {
132                         return System.currentTimeMillis();
133                 }
134
135                 void set_report_time() {
136                         report_time = now() + report_interval;
137                 }
138
139                 public void run () {
140                         try {
141                                 for (;;) {
142                                         if (reader.has_monitor_battery()) {
143                                                 listener_state.battery = reader.monitor_battery();
144                                                 show_safely();
145                                         }
146                                         set_report_time();
147                                         for (;;) {
148                                                 voice.drain();
149                                                 synchronized (this) {
150                                                         long    sleep_time = report_time - now();
151                                                         if (sleep_time <= 0)
152                                                                 break;
153                                                         wait(sleep_time);
154                                                 }
155                                         }
156
157                                         report(false);
158                                 }
159                         } catch (InterruptedException ie) {
160                                 try {
161                                         voice.drain();
162                                 } catch (InterruptedException iie) { }
163                         }
164                 }
165
166                 public synchronized void notice(boolean spoken) {
167                         if (old_state != null && old_state.state() != state.state()) {
168                                 report_time = now();
169                                 this.notify();
170                         } else if (spoken)
171                                 set_report_time();
172                 }
173
174                 public IdleThread() {
175                         reported_landing = 0;
176                         report_interval = 10000;
177                 }
178         }
179
180         synchronized boolean tell() {
181                 boolean ret = false;
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));
190                                 ret = true;
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));
196                                 ret = true;
197                         }
198                 }
199                 if (old_state == null || old_state.gps_ready != state.gps_ready) {
200                         if (state.gps_ready) {
201                                 voice.speak("GPS ready");
202                                 ret = true;
203                         }
204                         else if (old_state != null) {
205                                 voice.speak("GPS lost");
206                                 ret = true;
207                         }
208                 }
209                 old_state = state;
210                 return ret;
211         }
212
213         public void run() {
214                 boolean         interrupted = false;
215                 boolean         told;
216
217                 idle_thread = new IdleThread();
218                 idle_thread.start();
219
220                 try {
221                         for (;;) {
222                                 try {
223                                         AltosState new_state = reader.read();
224                                         if (new_state == null) {
225                                                 state = null;
226                                                 listener_state.running = false;
227                                                 break;
228                                         }
229                                         reader.update(new_state);
230                                         state = new_state;
231                                         show_safely();
232                                         told = tell();
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;
238                                         show_safely();
239                                 }
240                         }
241                 } catch (InterruptedException ee) {
242                         interrupted = true;
243                 } catch (IOException ie) {
244                         reading_error_safely();
245                 } finally {
246                         if (!interrupted)
247                                 idle_thread.report(true);
248                         reader.close(interrupted);
249                         idle_thread.interrupt();
250                         try {
251                                 idle_thread.join();
252                         } catch (InterruptedException ie) {}
253                 }
254         }
255
256         public AltosDisplayThread(Frame in_parent, AltosVoice in_voice, AltosFlightDisplay in_display, AltosFlightReader in_reader) {
257                 listener_state = new AltosListenerState();
258                 parent = in_parent;
259                 voice = in_voice;
260                 display = in_display;
261                 reader = in_reader;
262                 display.reset();
263         }
264 }