2 * Copyright © 2011 Keith Packard <keithp@keithp.com>
3 * Copyright © 2012 Mike Beattie <mike@ethernal.org>
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.
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.
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.
19 package org.altusmetrum.AltosDroid;
21 import android.speech.tts.TextToSpeech;
22 import android.speech.tts.TextToSpeech.OnInitListener;
24 import org.altusmetrum.altoslib_5.*;
26 public class AltosVoice {
28 private TextToSpeech tts = null;
29 private boolean tts_enabled = false;
31 private IdleThread idle_thread = null;
33 private AltosState old_state = null;
35 public AltosVoice(AltosDroid a) {
37 tts = new TextToSpeech(a, new OnInitListener() {
38 public void onInit(int status) {
39 if (status == TextToSpeech.SUCCESS) tts_enabled = true;
41 idle_thread = new IdleThread();
48 public void speak(String s) {
49 if (!tts_enabled) return;
50 tts.speak(s, TextToSpeech.QUEUE_ADD, null);
54 if (tts != null) tts.shutdown();
55 if (idle_thread != null) {
56 idle_thread.interrupt();
61 public void tell(AltosState state) {
62 if (!tts_enabled) return;
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: %d meters per second.", (int) (state.max_speed() + 0.5)));
73 } else if ((old_state == null || old_state.state < AltosLib.ao_flight_drogue) &&
74 state.state >= AltosLib.ao_flight_drogue) {
75 if (state.max_height() != AltosLib.MISSING)
76 speak(String.format("max height: %d meters.", (int) (state.max_height() + 0.5)));
80 if (old_state == null || old_state.gps_ready != state.gps_ready) {
81 if (state.gps_ready) {
84 } else if (old_state != null) {
90 idle_thread.notice(state, spoke);
94 class IdleThread extends Thread {
96 private AltosState state;
101 public synchronized void report(boolean last) {
105 /* reset the landing count once we hear about a new flight */
106 if (state.state < AltosLib.ao_flight_drogue)
107 reported_landing = 0;
109 /* Shut up once the rocket is on the ground */
110 if (reported_landing > 2) {
114 /* If the rocket isn't on the pad, then report height */
115 if (((AltosLib.ao_flight_drogue <= state.state &&
116 state.state < AltosLib.ao_flight_landed) ||
117 state.state == AltosLib.ao_flight_stateless) &&
120 speak(String.format("Height %d, bearing %s %d, elevation %d, range %d.\n",
121 (int) (state.height() + 0.5),
122 state.from_pad.bearing_words(
123 AltosGreatCircle.BEARING_VOICE),
124 (int) (state.from_pad.bearing + 0.5),
125 (int) (state.elevation + 0.5),
126 (int) (state.range + 0.5)));
127 } else if (state.state > AltosLib.ao_flight_pad) {
128 if (state.height() != AltosLib.MISSING)
129 speak(String.format("%d meters", (int) (state.height() + 0.5)));
131 reported_landing = 0;
134 /* If the rocket is coming down, check to see if it has landed;
135 * either we've got a landed report or we haven't heard from it in
138 if (state.state >= AltosLib.ao_flight_drogue &&
140 System.currentTimeMillis() - state.received_time >= 15000 ||
141 state.state == AltosLib.ao_flight_landed))
143 if (Math.abs(state.speed()) < 20 && state.height() < 100)
144 speak("rocket landed safely");
146 speak("rocket may have crashed");
147 if (state.from_pad != null)
148 speak(String.format("Bearing %d degrees, range %d meters.",
149 (int) (state.from_pad.bearing + 0.5),
150 (int) (state.from_pad.distance + 0.5)));
156 return System.currentTimeMillis();
159 void set_report_time() {
160 report_time = now() + report_interval;
168 synchronized (this) {
169 long sleep_time = report_time - now();
177 } catch (InterruptedException ie) {
181 public synchronized void notice(AltosState new_state, boolean spoken) {
182 AltosState old_state = state;
184 if (!started && state.state > AltosLib.ao_flight_pad) {
189 if (state.state < AltosLib.ao_flight_drogue)
190 report_interval = 10000;
192 report_interval = 20000;
193 if (old_state != null && old_state.state != state.state) {
200 public IdleThread() {
202 reported_landing = 0;
203 report_interval = 10000;