Merge remote-tracking branch 'origin/master'
[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_2.*;
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 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) {
62                 if (!tts_enabled) return;
63
64                 boolean spoke = false;
65                 if (old_state == null || old_state.state != state.state) {
66                         speak(state.state_name());
67                         if ((old_state == null || old_state.state <= AltosLib.ao_flight_boost) &&
68                             state.state > AltosLib.ao_flight_boost) {
69                                 speak(String.format("max speed: %d meters per second.", (int) (state.max_speed() + 0.5)));
70                                 spoke = true;
71                         } else if ((old_state == null || old_state.state < AltosLib.ao_flight_drogue) &&
72                                    state.state >= AltosLib.ao_flight_drogue) {
73                                 speak(String.format("max height: %d meters.", (int) (state.max_height() + 0.5)));
74                                 spoke = true;
75                         }
76                 }
77                 if (old_state == null || old_state.gps_ready != state.gps_ready) {
78                         if (state.gps_ready) {
79                                 speak("GPS ready");
80                                 spoke = true;
81                         } else if (old_state != null) {
82                                 speak("GPS lost");
83                                 spoke = true;
84                         }
85                 }
86                 old_state = state;
87                 idle_thread.notice(state, spoke);
88         }
89
90
91         class IdleThread extends Thread {
92                 boolean            started;
93                 private AltosState state;
94                 int                reported_landing;
95                 int                report_interval;
96                 long               report_time;
97
98                 public synchronized void report(boolean last) {
99                         if (state == null)
100                                 return;
101
102                         /* reset the landing count once we hear about a new flight */
103                         if (state.state < AltosLib.ao_flight_drogue)
104                                 reported_landing = 0;
105
106                         /* Shut up once the rocket is on the ground */
107                         if (reported_landing > 2) {
108                                 return;
109                         }
110
111                         /* If the rocket isn't on the pad, then report height */
112                         if (AltosLib.ao_flight_drogue <= state.state &&
113                             state.state < AltosLib.ao_flight_landed &&
114                             state.range >= 0)
115                         {
116                                 speak(String.format("Height %d, bearing %s %d, elevation %d, range %d.\n",
117                                                     (int) (state.height() + 0.5),
118                                         state.from_pad.bearing_words(
119                                               AltosGreatCircle.BEARING_VOICE),
120                                                     (int) (state.from_pad.bearing + 0.5),
121                                                     (int) (state.elevation + 0.5),
122                                                     (int) (state.range + 0.5)));
123                         } else if (state.state > AltosLib.ao_flight_pad) {
124                                 speak(String.format("%d meters", (int) (state.height() + 0.5)));
125                         } else {
126                                 reported_landing = 0;
127                         }
128
129                         /* If the rocket is coming down, check to see if it has landed;
130                          * either we've got a landed report or we haven't heard from it in
131                          * a long time
132                          */
133                         if (state.state >= AltosLib.ao_flight_drogue &&
134                             (last ||
135                              System.currentTimeMillis() - state.received_time >= 15000 ||
136                              state.state == AltosLib.ao_flight_landed))
137                         {
138                                 if (Math.abs(state.speed()) < 20 && state.height() < 100)
139                                         speak("rocket landed safely");
140                                 else
141                                         speak("rocket may have crashed");
142                                 if (state.from_pad != null)
143                                         speak(String.format("Bearing %d degrees, range %d meters.",
144                                                             (int) (state.from_pad.bearing + 0.5),
145                                                             (int) (state.from_pad.distance + 0.5)));
146                                 ++reported_landing;
147                         }
148                 }
149
150                 long now () {
151                         return System.currentTimeMillis();
152                 }
153
154                 void set_report_time() {
155                         report_time = now() + report_interval;
156                 }
157
158                 public void run () {
159                         try {
160                                 for (;;) {
161                                         set_report_time();
162                                         for (;;) {
163                                                 synchronized (this) {
164                                                         long sleep_time = report_time - now();
165                                                         if (sleep_time <= 0)
166                                                                 break;
167                                                         wait(sleep_time);
168                                                 }
169                                         }
170                                         report(false);
171                                 }
172                         } catch (InterruptedException ie) {
173                         }
174                 }
175
176                 public synchronized void notice(AltosState new_state, boolean spoken) {
177                         AltosState old_state = state;
178                         state = new_state;
179                         if (!started && state.state > AltosLib.ao_flight_pad) {
180                                 started = true;
181                                 start();
182                         }
183
184                         if (state.state < AltosLib.ao_flight_drogue)
185                                 report_interval = 10000;
186                         else
187                                 report_interval = 20000;
188                         if (old_state != null && old_state.state != state.state) {
189                                 report_time = now();
190                                 this.notify();
191                         } else if (spoken)
192                                 set_report_time();
193                 }
194
195                 public IdleThread() {
196                         state = null;
197                         reported_landing = 0;
198                         report_interval = 10000;
199                 }
200         }
201
202 }