Switch from GPLv2 to GPLv2+
[fw/altos] / telegps / TeleGPSDisplayThread.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.telegps;
20
21 import java.awt.*;
22 import javax.swing.*;
23 import java.io.*;
24 import java.text.*;
25 import org.altusmetrum.altoslib_11.*;
26 import org.altusmetrum.altosuilib_11.*;
27
28 public class TeleGPSDisplayThread extends Thread {
29
30         Frame                   parent;
31         IdleThread              idle_thread;
32         AltosVoice              voice;
33         AltosFlightReader       reader;
34         AltosState              old_state, state;
35         AltosListenerState      listener_state;
36         AltosFlightDisplay      display;
37
38         synchronized void show_safely() {
39                 final AltosState my_state = state;
40                 final AltosListenerState my_listener_state = listener_state;
41                 Runnable r = new Runnable() {
42                                 public void run() {
43                                         try {
44                                                 display.show(my_state, my_listener_state);
45                                         } catch (Exception ex) {
46                                         }
47                                 }
48                         };
49                 SwingUtilities.invokeLater(r);
50         }
51
52         void reading_error_internal() {
53                 JOptionPane.showMessageDialog(parent,
54                                               String.format("Error reading from \"%s\"", reader.name),
55                                               "Telemetry Read Error",
56                                               JOptionPane.ERROR_MESSAGE);
57         }
58
59         void reading_error_safely() {
60                 Runnable r = new Runnable() {
61                                 public void run() {
62                                         try {
63                                                 reading_error_internal();
64                                         } catch (Exception ex) {
65                                         }
66                                 }
67                         };
68                 SwingUtilities.invokeLater(r);
69         }
70
71         class IdleThread extends Thread {
72
73                 boolean started;
74                 int     report_interval;
75                 long    report_time;
76
77                 public synchronized void report(boolean last) {
78                         if (state == null)
79                                 return;
80
81                         if (state.height() != AltosLib.MISSING) {
82                                 if (state.from_pad != null) {
83                                         voice.speak("Height %s, bearing %s %d, elevation %d, range %s, .\n",
84                                                     AltosConvert.height.say(state.gps_height()),
85                                                     state.from_pad.bearing_words(
86                                                             AltosGreatCircle.BEARING_VOICE),
87                                                     (int) (state.from_pad.bearing + 0.5),
88                                                     (int) (state.elevation + 0.5),
89                                                     AltosConvert.distance.say(state.range));
90                                 } else {
91                                         voice.speak("Height %s.\n",
92                                                     AltosConvert.height.say(state.height()));
93                                 }
94                         }
95                 }
96
97                 long now () {
98                         return System.currentTimeMillis();
99                 }
100
101                 void set_report_time() {
102                         report_time = now() + report_interval;
103                 }
104
105                 public void run () {
106                         try {
107                                 for (;;) {
108                                         if (reader.has_monitor_battery()) {
109                                                 listener_state.battery = reader.monitor_battery();
110                                                 show_safely();
111                                         }
112                                         set_report_time();
113                                         for (;;) {
114                                                 voice.drain();
115                                                 synchronized (this) {
116                                                         long    sleep_time = report_time - now();
117                                                         if (sleep_time <= 0)
118                                                                 break;
119                                                         wait(sleep_time);
120                                                 }
121                                         }
122
123                                         report(false);
124                                 }
125                         } catch (InterruptedException ie) {
126                                 try {
127                                         voice.drain();
128                                 } catch (InterruptedException iie) { }
129                         }
130                 }
131
132                 public synchronized void notice(boolean spoken) {
133                         if (old_state != null && old_state.state() != state.state()) {
134                                 report_time = now();
135                                 this.notify();
136                         } else if (spoken)
137                                 set_report_time();
138                 }
139
140                 public IdleThread() {
141                         report_interval = 10000;
142                 }
143         }
144
145         synchronized boolean tell() {
146                 boolean ret = false;
147                 if (old_state == null || old_state.gps_ready != state.gps_ready) {
148                         if (state.gps_ready) {
149                                 voice.speak("GPS ready");
150                                 ret = true;
151                         }
152                         else if (old_state != null) {
153                                 voice.speak("GPS lost");
154                                 ret = true;
155                         }
156                 }
157                 old_state = state;
158                 return ret;
159         }
160
161         public void run() {
162                 boolean         interrupted = false;
163                 boolean         told;
164
165                 idle_thread = new IdleThread();
166                 idle_thread.start();
167
168                 try {
169                         for (;;) {
170                                 try {
171                                         state = reader.read();
172                                         if (state == null) {
173                                                 listener_state.running = false;
174                                                 break;
175                                         }
176                                         reader.update(state);
177                                         show_safely();
178                                         told = tell();
179                                         idle_thread.notice(told);
180                                 } catch (ParseException pp) {
181                                         System.out.printf("Parse error: %d \"%s\"\n", pp.getErrorOffset(), pp.getMessage());
182                                 } catch (AltosCRCException ce) {
183                                         ++listener_state.crc_errors;
184                                         show_safely();
185                                 }
186                         }
187                 } catch (InterruptedException ee) {
188                         interrupted = true;
189                 } catch (IOException ie) {
190                         reading_error_safely();
191                 } finally {
192                         if (!interrupted)
193                                 idle_thread.report(true);
194                         reader.close(interrupted);
195                         idle_thread.interrupt();
196                         try {
197                                 idle_thread.join();
198                         } catch (InterruptedException ie) {}
199                 }
200         }
201
202         public TeleGPSDisplayThread(Frame in_parent, AltosVoice in_voice, AltosFlightDisplay in_display, AltosFlightReader in_reader) {
203                 listener_state = new AltosListenerState();
204                 parent = in_parent;
205                 voice = in_voice;
206                 display = in_display;
207                 reader = in_reader;
208                 display.reset();
209         }
210 }