2d32dc071ffdc5240443cf72a0433279c7036ea2
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / AltosVoice.java
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  * Copyright © 2012 Mike Beattie <mike@ethernal.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; version 2 of the License.
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.AltosDroid;
20
21 import android.speech.tts.TextToSpeech;
22 import android.speech.tts.TextToSpeech.OnInitListener;
23
24 import org.altusmetrum.altoslib_6.*;
25
26 public class AltosVoice {
27
28         private TextToSpeech tts         = null;
29         private boolean      tts_enabled = false;
30
31         private IdleThread   idle_thread = null;
32
33         private AltosState   old_state   = null;
34
35         public AltosVoice(AltosDroid a) {
36
37                 tts = new TextToSpeech(a, new OnInitListener() {
38                         public void onInit(int status) {
39                                 if (status == TextToSpeech.SUCCESS) tts_enabled = true;
40                                 if (tts_enabled) {
41                                         idle_thread = new IdleThread();
42                                 }
43                         }
44                 });
45
46         }
47
48         public synchronized void speak(String s) {
49                 if (!tts_enabled) return;
50                 tts.speak(s, TextToSpeech.QUEUE_ADD, null);
51         }
52
53         public void stop() {
54                 if (tts != null) tts.shutdown();
55                 if (idle_thread != null) {
56                         idle_thread.interrupt();
57                         idle_thread = null;
58                 }
59         }
60
61         public void tell(AltosState state, AltosGreatCircle from_receiver) {
62                 if (!tts_enabled) return;
63
64                 boolean spoke = false;
65                 if (old_state == null || old_state.state != state.state) {
66                         if (state.state != AltosLib.ao_flight_stateless)
67                                 speak(state.state_name());
68                         if ((old_state == null || old_state.state <= AltosLib.ao_flight_boost) &&
69                             state.state > AltosLib.ao_flight_boost) {
70                                 if (state.max_speed() != AltosLib.MISSING)
71                                         speak(String.format("Max speed: %s.",
72                                                             AltosConvert.speed.say_units(state.max_speed())));
73                                 spoke = true;
74                         } else if ((old_state == null || old_state.state < AltosLib.ao_flight_drogue) &&
75                                    state.state >= AltosLib.ao_flight_drogue) {
76                                 if (state.max_height() != AltosLib.MISSING)
77                                         speak(String.format("Max height: %s.",
78                                                             AltosConvert.height.say_units(state.max_height())));
79                                 spoke = true;
80                         }
81                 }
82                 if (old_state == null || old_state.gps_ready != state.gps_ready) {
83                         if (state.gps_ready) {
84                                 speak("GPS ready");
85                                 spoke = true;
86                         } else if (old_state != null) {
87                                 speak("GPS lost");
88                                 spoke = true;
89                         }
90                 }
91                 old_state = state;
92                 if (idle_thread != null)
93                         idle_thread.notice(state, from_receiver, spoke);
94         }
95
96
97         class IdleThread extends Thread {
98                 boolean            started;
99                 private AltosState state;
100                 private AltosGreatCircle from_receiver;
101                 int                reported_landing;
102                 int                report_interval;
103                 long               report_time;
104
105                 public synchronized void report(boolean last) {
106                         if (state == null)
107                                 return;
108
109                         /* reset the landing count once we hear about a new flight */
110                         if (state.state < AltosLib.ao_flight_drogue)
111                                 reported_landing = 0;
112
113                         /* Shut up once the rocket is on the ground */
114                         if (reported_landing > 2) {
115                                 return;
116                         }
117
118                         /* If the rocket isn't on the pad, then report location */
119                         if ((AltosLib.ao_flight_drogue <= state.state &&
120                               state.state < AltosLib.ao_flight_landed) ||
121                              state.state == AltosLib.ao_flight_stateless)
122                         {
123                                 AltosGreatCircle        position;
124
125                                 if (from_receiver != null)
126                                         position = from_receiver;
127                                 else
128                                         position = state.from_pad;
129
130                                 if (position != null) {
131                                         speak(String.format("Height %s, bearing %s %d, elevation %d, range %s.\n",
132                                                             AltosConvert.height.say_units(state.height()),
133                                                             position.bearing_words(
134                                                                     AltosGreatCircle.BEARING_VOICE),
135                                                             (int) (position.bearing + 0.5),
136                                                             (int) (position.elevation + 0.5),
137                                                             AltosConvert.distance.say_units(position.range)));
138                                 }
139                         } else if (state.state > AltosLib.ao_flight_pad) {
140                                 if (state.height() != AltosLib.MISSING)
141                                         speak(AltosConvert.height.say_units(state.height()));
142                         } else {
143                                 reported_landing = 0;
144                         }
145
146                         /* If the rocket is coming down, check to see if it has landed;
147                          * either we've got a landed report or we haven't heard from it in
148                          * a long time
149                          */
150                         if (state.state >= AltosLib.ao_flight_drogue &&
151                             (last ||
152                              System.currentTimeMillis() - state.received_time >= 15000 ||
153                              state.state == AltosLib.ao_flight_landed))
154                         {
155                                 if (Math.abs(state.speed()) < 20 && state.height() < 100)
156                                         speak("rocket landed safely");
157                                 else
158                                         speak("rocket may have crashed");
159                                 if (state.from_pad != null)
160                                         speak(String.format("Bearing %d degrees, range %s.",
161                                                             (int) (state.from_pad.bearing + 0.5),
162                                                             AltosConvert.distance.say_units(state.from_pad.distance)));
163                                 ++reported_landing;
164                         }
165                 }
166
167                 long now () {
168                         return System.currentTimeMillis();
169                 }
170
171                 void set_report_time() {
172                         report_time = now() + report_interval;
173                 }
174
175                 public void run () {
176                         try {
177                                 for (;;) {
178                                         set_report_time();
179                                         for (;;) {
180                                                 synchronized (this) {
181                                                         long sleep_time = report_time - now();
182                                                         if (sleep_time <= 0)
183                                                                 break;
184                                                         wait(sleep_time);
185                                                 }
186                                         }
187                                         report(false);
188                                 }
189                         } catch (InterruptedException ie) {
190                         }
191                 }
192
193                 public synchronized void notice(AltosState new_state, AltosGreatCircle new_from_receiver, boolean spoken) {
194                         AltosState old_state = state;
195                         state = new_state;
196                         from_receiver = new_from_receiver;
197                         if (!started && state.state > AltosLib.ao_flight_pad) {
198                                 started = true;
199                                 start();
200                         }
201
202                         if (state.state < AltosLib.ao_flight_drogue)
203                                 report_interval = 10000;
204                         else
205                                 report_interval = 20000;
206                         if (old_state != null && old_state.state != state.state) {
207                                 report_time = now();
208                                 this.notify();
209                         } else if (spoken)
210                                 set_report_time();
211                 }
212
213                 public IdleThread() {
214                         state = null;
215                         reported_landing = 0;
216                         report_interval = 10000;
217                 }
218         }
219
220 }