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