altosui: Don't try to report bearing/elevation without GPS
[fw/altos] / altosui / AltosDisplayThread.java
index cf69c414580e27bbbd56e05880a82017c93e1568..4b4cc3b9ac2aee901b5e633228eb2142b15ad4e9 100644 (file)
 package altosui;
 
 import java.awt.*;
-import java.awt.event.*;
 import javax.swing.*;
-import javax.swing.filechooser.FileNameExtensionFilter;
-import javax.swing.table.*;
 import java.io.*;
-import java.util.*;
 import java.text.*;
-import java.util.prefs.*;
-import java.util.concurrent.LinkedBlockingQueue;
-import org.altusmetrum.AltosLib.*;
+import org.altusmetrum.altoslib_2.*;
 
 public class AltosDisplayThread extends Thread {
 
@@ -35,21 +29,17 @@ public class AltosDisplayThread extends Thread {
        IdleThread              idle_thread;
        AltosVoice              voice;
        AltosFlightReader       reader;
-       int                     crc_errors;
+       AltosState              old_state, state;
+       AltosListenerState      listener_state;
        AltosFlightDisplay      display;
 
-       void show_internal(AltosState state, int crc_errors) {
-               if (state != null)
-                       display.show(state, crc_errors);
-       }
-
-       void show_safely(AltosState in_state, int in_crc_errors) {
-               final AltosState state = in_state;
-               final int crc_errors = in_crc_errors;
+       synchronized void show_safely() {
+               final AltosState my_state = state;
+               final AltosListenerState my_listener_state = listener_state;
                Runnable r = new Runnable() {
                                public void run() {
                                        try {
-                                               show_internal(state, crc_errors);
+                                               display.show(my_state, my_listener_state);
                                        } catch (Exception ex) {
                                        }
                                }
@@ -79,7 +69,6 @@ public class AltosDisplayThread extends Thread {
        class IdleThread extends Thread {
 
                boolean started;
-               private AltosState state;
                int     reported_landing;
                int     report_interval;
                long    report_time;
@@ -100,17 +89,18 @@ public class AltosDisplayThread extends Thread {
                        /* If the rocket isn't on the pad, then report height */
                        if (Altos.ao_flight_drogue <= state.state &&
                            state.state < Altos.ao_flight_landed &&
+                           state.from_pad != null &&
                            state.range >= 0)
                        {
                                voice.speak("Height %s, bearing %s %d, elevation %d, range %s.\n",
-                                           AltosConvert.height.say(state.height),
+                                           AltosConvert.height.say(state.height()),
                                            state.from_pad.bearing_words(
                                                    AltosGreatCircle.BEARING_VOICE),
                                            (int) (state.from_pad.bearing + 0.5),
                                            (int) (state.elevation + 0.5),
                                            AltosConvert.distance.say(state.range));
                        } else if (state.state > Altos.ao_flight_pad) {
-                               voice.speak(AltosConvert.height.say_units(state.height));
+                               voice.speak(AltosConvert.height.say_units(state.height()));
                        } else {
                                reported_landing = 0;
                        }
@@ -121,10 +111,10 @@ public class AltosDisplayThread extends Thread {
                         */
                        if (state.state >= Altos.ao_flight_drogue &&
                            (last ||
-                            System.currentTimeMillis() - state.report_time >= 15000 ||
+                            System.currentTimeMillis() - state.received_time >= 15000 ||
                             state.state == Altos.ao_flight_landed))
                        {
-                               if (Math.abs(state.baro_speed) < 20 && state.height < 100)
+                               if (Math.abs(state.speed()) < 20 && state.height() < 100)
                                        voice.speak("rocket landed safely");
                                else
                                        voice.speak("rocket may have crashed");
@@ -135,7 +125,7 @@ public class AltosDisplayThread extends Thread {
                                ++reported_landing;
                                if (state.state != Altos.ao_flight_landed) {
                                        state.state = Altos.ao_flight_landed;
-                                       show_safely(state, 0);
+                                       show_safely();
                                }
                        }
                }
@@ -151,6 +141,10 @@ public class AltosDisplayThread extends Thread {
                public void run () {
                        try {
                                for (;;) {
+                                       if (reader.has_monitor_battery()) {
+                                               listener_state.battery = reader.monitor_battery();
+                                               show_safely();
+                                       }
                                        set_report_time();
                                        for (;;) {
                                                voice.drain();
@@ -161,6 +155,7 @@ public class AltosDisplayThread extends Thread {
                                                        wait(sleep_time);
                                                }
                                        }
+                                       
                                        report(false);
                                }
                        } catch (InterruptedException ie) {
@@ -170,18 +165,7 @@ public class AltosDisplayThread extends Thread {
                        }
                }
 
-               public synchronized void notice(AltosState new_state, boolean spoken) {
-                       AltosState old_state = state;
-                       state = new_state;
-                       if (!started && state.state > Altos.ao_flight_pad) {
-                               started = true;
-                               start();
-                       }
-
-                       if (state.state < Altos.ao_flight_drogue)
-                               report_interval = 10000;
-                       else
-                               report_interval = 20000;
+               public synchronized void notice(boolean spoken) {
                        if (old_state != null && old_state.state != state.state) {
                                report_time = now();
                                this.notify();
@@ -190,25 +174,24 @@ public class AltosDisplayThread extends Thread {
                }
 
                public IdleThread() {
-                       state = null;
                        reported_landing = 0;
                        report_interval = 10000;
                }
        }
 
-       boolean tell(AltosState state, AltosState old_state) {
+       synchronized boolean tell() {
                boolean ret = false;
                if (old_state == null || old_state.state != state.state) {
-                       voice.speak(state.data.state());
+                       voice.speak(state.state_name());
                        if ((old_state == null || old_state.state <= Altos.ao_flight_boost) &&
                            state.state > Altos.ao_flight_boost) {
                                voice.speak("max speed: %s.",
-                                           AltosConvert.speed.say_units(state.max_speed + 0.5));
+                                           AltosConvert.speed.say_units(state.max_speed() + 0.5));
                                ret = true;
                        } else if ((old_state == null || old_state.state < Altos.ao_flight_drogue) &&
                                   state.state >= Altos.ao_flight_drogue) {
                                voice.speak("max height: %s.",
-                                           AltosConvert.height.say_units(state.max_height + 0.5));
+                                           AltosConvert.height.say_units(state.max_height() + 0.5));
                                ret = true;
                        }
                }
@@ -228,30 +211,26 @@ public class AltosDisplayThread extends Thread {
 
        public void run() {
                boolean         interrupted = false;
-               String          line;
-               AltosState      state = null;
-               AltosState      old_state = null;
                boolean         told;
 
                idle_thread = new IdleThread();
+               idle_thread.start();
 
                try {
                        for (;;) {
                                try {
-                                       AltosRecord record = reader.read();
-                                       if (record == null)
+                                       state = reader.read();
+                                       if (state == null)
                                                break;
-                                       old_state = state;
-                                       state = new AltosState(record, state);
                                        reader.update(state);
-                                       show_safely(state, crc_errors);
-                                       told = tell(state, old_state);
-                                       idle_thread.notice(state, told);
+                                       show_safely();
+                                       told = tell();
+                                       idle_thread.notice(told);
                                } catch (ParseException pp) {
                                        System.out.printf("Parse error: %d \"%s\"\n", pp.getErrorOffset(), pp.getMessage());
                                } catch (AltosCRCException ce) {
-                                       ++crc_errors;
-                                       show_safely(state, crc_errors);
+                                       ++listener_state.crc_errors;
+                                       show_safely();
                                }
                        }
                } catch (InterruptedException ee) {
@@ -270,6 +249,7 @@ public class AltosDisplayThread extends Thread {
        }
 
        public AltosDisplayThread(Frame in_parent, AltosVoice in_voice, AltosFlightDisplay in_display, AltosFlightReader in_reader) {
+               listener_state = new AltosListenerState();
                parent = in_parent;
                voice = in_voice;
                display = in_display;