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