altoslib: Hide 'state' member and use accessor function
authorKeith Packard <keithp@keithp.com>
Sun, 13 Sep 2015 02:20:49 +0000 (19:20 -0700)
committerKeith Packard <keithp@keithp.com>
Tue, 13 Oct 2015 20:54:28 +0000 (13:54 -0700)
Someone was smashing the state to 'landed' when no packets had been
received for a while. Found that by making it impossible for anyone
outside of AltosState to change the value.

Signed-off-by: Keith Packard <keithp@keithp.com>
16 files changed:
altoslib/AltosCSV.java
altoslib/AltosEepromDownload.java
altoslib/AltosEepromFile.java
altoslib/AltosFlightStats.java
altoslib/AltosKML.java
altoslib/AltosMap.java
altoslib/AltosReplayReader.java
altoslib/AltosState.java
altoslib/AltosTelemetry.java
altoslib/AltosTelemetryFile.java
altosui/AltosFlightStatus.java
altosui/AltosFlightUI.java
altosui/AltosPad.java
altosui/AltosUI.java
altosuilib/AltosDisplayThread.java
altosuilib/AltosGraphDataPoint.java

index edb23e6..150f5a6 100644 (file)
@@ -129,7 +129,7 @@ public class AltosCSV implements AltosWriter {
        }
 
        void write_flight(AltosState state) {
-               out.printf("%d,%8s", state.state, state.state_name());
+               out.printf("%d,%8s", state.state(), state.state_name());
        }
 
        void write_basic_header() {
@@ -326,14 +326,14 @@ public class AltosCSV implements AltosWriter {
        }
 
        private void write(AltosState state) {
-               if (state.state == AltosLib.ao_flight_startup)
+               if (state.state() == AltosLib.ao_flight_startup)
                        return;
                if (!header_written) {
                        write_header();
                        header_written = true;
                }
                if (!seen_boost) {
-                       if (state.state >= AltosLib.ao_flight_boost) {
+                       if (state.state() >= AltosLib.ao_flight_boost) {
                                seen_boost = true;
                                boost_tick = state.tick;
                                flush_pad();
@@ -368,7 +368,7 @@ public class AltosCSV implements AltosWriter {
                has_gps_sat = false;
                has_companion = false;
                for (AltosState state : states) {
-                       if (state.state != AltosLib.ao_flight_stateless && state.state != AltosLib.ao_flight_invalid && state.state != AltosLib.ao_flight_startup)
+                       if (state.state() != AltosLib.ao_flight_stateless && state.state() != AltosLib.ao_flight_invalid && state.state() != AltosLib.ao_flight_startup)
                                has_flight_state = true;
                        if (state.acceleration() != AltosLib.MISSING || state.pressure() != AltosLib.MISSING)
                                has_basic = true;
index baaeb99..5e035db 100644 (file)
@@ -111,12 +111,12 @@ public class AltosEepromDownload implements Runnable {
                                monitor.set_flight(state.flight);
 
                        /* Monitor state transitions to update display */
-                       if (state.state != AltosLib.ao_flight_invalid &&
-                           state.state <= AltosLib.ao_flight_landed)
+                       if (state.state() != AltosLib.ao_flight_invalid &&
+                           state.state() <= AltosLib.ao_flight_landed)
                        {
-                               if (state.state > AltosLib.ao_flight_pad)
+                               if (state.state() > AltosLib.ao_flight_pad)
                                        want_file = true;
-                               if (state.state == AltosLib.ao_flight_landed)
+                               if (state.state() == AltosLib.ao_flight_landed)
                                        done = true;
                        }
 
@@ -174,13 +174,13 @@ public class AltosEepromDownload implements Runnable {
 
                        CaptureEeprom (eechunk, log_format);
 
-                       if (state.state != prev_state && state.state != AltosLib.ao_flight_invalid) {
+                       if (state.state() != prev_state && state.state() != AltosLib.ao_flight_invalid) {
                                state_block = block;
-                               prev_state = state.state;
+                               prev_state = state.state();
                        }
 
                        monitor.set_value(state.state_name(),
-                                         state.state,
+                                         state.state(),
                                          block - state_block,
                                          block - log.start_block);
                }
index 33d4f63..002b7ff 100644 (file)
@@ -72,7 +72,7 @@ public class AltosEepromFile extends AltosStateIterable {
                headers = new AltosEepromIterable(AltosEepromHeader.read(input));
 
                start = headers.state();
-               if (start.state != AltosLib.ao_flight_stateless)
+               if (start.state() != AltosLib.ao_flight_stateless)
                        start.set_state(AltosLib.ao_flight_pad);
 
                if (start.log_format == AltosLib.MISSING) {
@@ -117,7 +117,7 @@ public class AltosEepromFile extends AltosStateIterable {
                for (AltosEeprom eeprom : body) {
                        eeprom.update_state(state);
                        state.finish_update();
-                       if (state.state >= AltosLib.ao_flight_boost) {
+                       if (state.state() >= AltosLib.ao_flight_boost) {
                                start.set_boost_tick(state.tick);
                                break;
                        }
index b837ba8..fdf79fa 100644 (file)
@@ -52,7 +52,7 @@ public class AltosFlightStats {
 
                for (AltosState s : states) {
                        state = s;
-                       if (state.state == AltosLib.ao_flight_landed)
+                       if (state.state() == AltosLib.ao_flight_landed)
                                break;
                }
 
@@ -92,7 +92,7 @@ public class AltosFlightStats {
                        state = s;
                        if (state.acceleration() < 1)
                                boost_time = state.time;
-                       if (state.state >= AltosLib.ao_flight_boost && state.state <= AltosLib.ao_flight_landed)
+                       if (state.state() >= AltosLib.ao_flight_boost && state.state() <= AltosLib.ao_flight_landed)
                                break;
                }
                if (state == null)
@@ -138,7 +138,7 @@ public class AltosFlightStats {
                        if (state.pressure() != AltosLib.MISSING)
                                has_flight_data = true;
 
-                       int state_id = state.state;
+                       int state_id = state.state();
                        if (state.time >= boost_time && state_id < AltosLib.ao_flight_boost)
                                state_id = AltosLib.ao_flight_boost;
                        if (state.time >= landed_time && state_id < AltosLib.ao_flight_landed)
index 8143395..1a36410 100644 (file)
@@ -112,8 +112,8 @@ public class AltosKML implements AltosWriter {
        boolean started = false;
 
        void state_start(AltosState state) {
-               String  state_name = AltosLib.state_name(state.state);
-               String  state_color = state_color(state.state);
+               String  state_name = AltosLib.state_name(state.state());
+               String  state_color = state_color(state.state());
                out.printf(kml_style_start, state_name, state_color);
                out.printf("\tState: %s\n", state_name);
                out.printf("%s", kml_style_end);
@@ -171,8 +171,8 @@ public class AltosKML implements AltosWriter {
                }
                if (prev != null && prev.gps_sequence == state.gps_sequence)
                        return;
-               if (state.state != flight_state) {
-                       flight_state = state.state;
+               if (state.state() != flight_state) {
+                       flight_state = state.state();
                        if (prev != null) {
                                coord(state);
                                state_end(prev);
index 8a3266c..a285519 100644 (file)
@@ -230,23 +230,23 @@ public class AltosMap implements AltosMapTileListener, AltosMapStoreListener {
                if (!gps.locked && gps.nsat < 4)
                        return;
 
-               switch (state.state) {
+               switch (state.state()) {
                case AltosLib.ao_flight_boost:
                        if (!have_boost) {
-                               add_mark(gps.lat, gps.lon, state.state);
+                               add_mark(gps.lat, gps.lon, state.state());
                                have_boost = true;
                        }
                        break;
                case AltosLib.ao_flight_landed:
                        if (!have_landed) {
-                               add_mark(gps.lat, gps.lon, state.state);
+                               add_mark(gps.lat, gps.lon, state.state());
                                have_landed = true;
                        }
                        break;
                }
 
                if (path != null) {
-                       AltosMapRectangle       damage = path.add(gps.lat, gps.lon, state.state);
+                       AltosMapRectangle       damage = path.add(gps.lat, gps.lon, state.state());
 
                        if (damage != null)
                                repaint(damage, AltosMapPath.stroke_width);
@@ -324,6 +324,7 @@ public class AltosMap implements AltosMapTileListener, AltosMapStoreListener {
                                if (!tiles.containsKey(point)) {
                                        AltosLatLon     ul = transform.lat_lon(point);
                                        AltosLatLon     center = transform.lat_lon(new AltosPointDouble(x + AltosMap.px_size/2, y + AltosMap.px_size/2));
+                                       debug("make tile %g,%g\n", center.lat, center.lon);
                                        AltosMapTile tile = map_interface.new_tile(this, ul, center, zoom, maptype, px_size);
                                        tiles.put(point, tile);
                                }
index 9419ec9..55faa17 100644 (file)
@@ -39,7 +39,7 @@ public class AltosReplayReader extends AltosFlightReader {
 
        public void update(AltosState state) throws InterruptedException {
                /* Make it run in realtime after the rocket leaves the pad */
-               if (state.state > AltosLib.ao_flight_pad && state.time_change > 0)
+               if (state.state() > AltosLib.ao_flight_pad && state.time_change > 0)
                        Thread.sleep((int) (Math.min(state.time_change,10) * 1000));
                state.set_received_time(System.currentTimeMillis());
        }
index cf4fb9b..12cd5ad 100644 (file)
@@ -284,7 +284,7 @@ public class AltosState implements Cloneable, Serializable {
                }
        }
 
-       public int      state;
+       private int     state;
        public int      flight;
        public int      serial;
        public int      altitude_32;
@@ -1056,6 +1056,10 @@ public class AltosState implements Cloneable, Serializable {
                }
        }
 
+       public int state() {
+               return state;
+       }
+
        public void set_device_type(int device_type) {
                this.device_type = device_type;
                switch (device_type) {
index 4c973cd..821adc8 100644 (file)
@@ -44,7 +44,7 @@ public abstract class AltosTelemetry implements AltosStateUpdate {
 
        public void update_state(AltosState state) {
                state.set_serial(serial);
-               if (state.state == AltosLib.ao_flight_invalid)
+               if (state.state() == AltosLib.ao_flight_invalid)
                        state.set_state(AltosLib.ao_flight_startup);
                state.set_tick(tick);
                state.set_rssi(rssi, status);
index d2275f5..d87939f 100644 (file)
@@ -75,7 +75,7 @@ public class AltosTelemetryFile extends AltosStateIterable {
                for (AltosTelemetry telem : telems) {
                        telem.update_state(state);
                        state.finish_update();
-                       if (state.state != AltosLib.ao_flight_invalid && state.state >= AltosLib.ao_flight_boost) {
+                       if (state.state() != AltosLib.ao_flight_invalid && state.state() >= AltosLib.ao_flight_boost) {
                                start.set_boost_tick(state.tick);
                                break;
                        }
@@ -93,4 +93,4 @@ public class AltosTelemetryFile extends AltosStateIterable {
                }
                return new AltosTelemetryIterator(state, i);
        }
-}
\ No newline at end of file
+}
index 815a6fa..f44e261 100644 (file)
@@ -173,14 +173,14 @@ public class AltosFlightStatus extends JComponent implements AltosFlightDisplay
                int     last_state = -1;
 
                void show(AltosState state, AltosListenerState listener_state) {
-                       if (state.state != last_state) {
-                               if (state.state == AltosLib.ao_flight_stateless)
+                       if (state.state() != last_state) {
+                               if (state.state() == AltosLib.ao_flight_stateless)
                                        hide();
                                else {
                                        show();
                                        value.setText(state.state_name());
                                }
-                               last_state = state.state;
+                               last_state = state.state();
                        }
                }
 
index db33e0d..939c468 100644 (file)
@@ -53,13 +53,13 @@ public class AltosFlightUI extends AltosUIFrame implements AltosFlightDisplay {
 
        JComponent cur_tab = null;
        JComponent which_tab(AltosState state) {
-               if (state.state < Altos.ao_flight_boost)
+               if (state.state() < Altos.ao_flight_boost)
                        return pad;
-               if (state.state <= Altos.ao_flight_coast)
+               if (state.state() <= Altos.ao_flight_coast)
                        return ascent;
-               if (state.state <= Altos.ao_flight_main)
+               if (state.state() <= Altos.ao_flight_main)
                        return descent;
-               if (state.state == AltosLib.ao_flight_stateless)
+               if (state.state() == AltosLib.ao_flight_stateless)
                        return descent;
                return landed;
        }
@@ -102,7 +102,7 @@ public class AltosFlightUI extends AltosUIFrame implements AltosFlightDisplay {
                if (state == null)
                        state = new AltosState();
 
-               if (state.state != Altos.ao_flight_startup) {
+               if (state.state() != Altos.ao_flight_startup) {
                        if (!has_state) {
                                pane.setTitleAt(0, "Launch Pad");
                                pane.add(ascent, 1);
index 2f87b68..d056e25 100644 (file)
@@ -49,10 +49,10 @@ public class AltosPad extends AltosUIFlightTab {
                                hide();
                        } else {
                                if (state.flight != 0) {
-                                       if (state.state <= Altos.ao_flight_pad)
+                                       if (state.state() <= Altos.ao_flight_pad)
                                                show("Ready to record");
-                                       else if (state.state < Altos.ao_flight_landed ||
-                                                state.state == AltosLib.ao_flight_stateless)
+                                       else if (state.state() < Altos.ao_flight_landed ||
+                                                state.state() == AltosLib.ao_flight_stateless)
                                                show("Recording data");
                                        else
                                                show("Recorded data");
@@ -121,8 +121,8 @@ public class AltosPad extends AltosUIFlightTab {
        }
 
        boolean report_pad(AltosState state) {
-               if ((state.state == AltosLib.ao_flight_stateless ||
-                    state.state < AltosLib.ao_flight_pad) &&
+               if ((state.state() == AltosLib.ao_flight_stateless ||
+                    state.state() < AltosLib.ao_flight_pad) &&
                    state.gps != null &&
                    state.gps.lat != AltosLib.MISSING)
                {
index f6caa4e..c29f0db 100644 (file)
@@ -539,7 +539,7 @@ public class AltosUI extends AltosUIFrame {
                        System.out.printf ("process cat\n");
                        for (AltosState state : eef) {
                                System.out.printf ("tick %d state %d height %g\n",
-                                                  state.tick, state.state, state.height());
+                                                  state.tick, state.state(), state.height());
                                if ((state.set & AltosState.set_gps) != 0)
                                        System.out.printf ("time %g lat %g lon %g alt %g\n",
                                                           state.time_since_boost(),
index 73d8569..a773022 100644 (file)
@@ -78,7 +78,7 @@ public class AltosDisplayThread extends Thread {
                                return;
 
                        /* reset the landing count once we hear about a new flight */
-                       if (state.state < AltosLib.ao_flight_drogue)
+                       if (state.state() < AltosLib.ao_flight_drogue)
                                reported_landing = 0;
 
                        /* Shut up once the rocket is on the ground */
@@ -87,8 +87,8 @@ public class AltosDisplayThread extends Thread {
                        }
 
                        /* If the rocket isn't on the pad, then report height */
-                       if (AltosLib.ao_flight_drogue <= state.state &&
-                           state.state < AltosLib.ao_flight_landed &&
+                       if (AltosLib.ao_flight_drogue <= state.state() &&
+                           state.state() < AltosLib.ao_flight_landed &&
                            state.from_pad != null &&
                            state.range >= 0)
                        {
@@ -99,7 +99,7 @@ public class AltosDisplayThread extends Thread {
                                            (int) (state.from_pad.bearing + 0.5),
                                            (int) (state.elevation + 0.5),
                                            AltosConvert.distance.say(state.range));
-                       } else if (state.state > AltosLib.ao_flight_pad && state.height() != AltosLib.MISSING) {
+                       } else if (state.state() > AltosLib.ao_flight_pad && state.height() != AltosLib.MISSING) {
                                voice.speak(AltosConvert.height.say_units(state.height()));
                        } else {
                                reported_landing = 0;
@@ -109,11 +109,11 @@ public class AltosDisplayThread extends Thread {
                         * either we've got a landed report or we haven't heard from it in
                         * a long time
                         */
-                       if (state.state != AltosLib.ao_flight_stateless &&
-                           state.state >= AltosLib.ao_flight_drogue &&
+                       if (state.state() != AltosLib.ao_flight_stateless &&
+                           state.state() >= AltosLib.ao_flight_drogue &&
                            (last ||
                             System.currentTimeMillis() - state.received_time >= 15000 ||
-                            state.state == AltosLib.ao_flight_landed))
+                            state.state() == AltosLib.ao_flight_landed))
                        {
                                if (Math.abs(state.speed()) < 20 && state.height() < 100)
                                        voice.speak("rocket landed safely");
@@ -124,10 +124,6 @@ public class AltosDisplayThread extends Thread {
                                                    (int) (state.from_pad.bearing + 0.5),
                                                    AltosConvert.distance.say_units(state.from_pad.distance));
                                ++reported_landing;
-                               if (state.state != AltosLib.ao_flight_landed) {
-                                       state.state = AltosLib.ao_flight_landed;
-                                       show_safely();
-                               }
                        }
                }
 
@@ -167,7 +163,7 @@ public class AltosDisplayThread extends Thread {
                }
 
                public synchronized void notice(boolean spoken) {
-                       if (old_state != null && old_state.state != state.state) {
+                       if (old_state != null && old_state.state() != state.state()) {
                                report_time = now();
                                this.notify();
                        } else if (spoken)
@@ -182,17 +178,17 @@ public class AltosDisplayThread extends Thread {
 
        synchronized boolean tell() {
                boolean ret = false;
-               if (old_state == null || old_state.state != state.state) {
-                       if (state.state != AltosLib.ao_flight_stateless)
+               if (old_state == null || old_state.state() != state.state()) {
+                       if (state.state() != AltosLib.ao_flight_stateless)
                                voice.speak(state.state_name());
-                       if ((old_state == null || old_state.state <= AltosLib.ao_flight_boost) &&
-                           state.state > AltosLib.ao_flight_boost) {
+                       if ((old_state == null || old_state.state() <= AltosLib.ao_flight_boost) &&
+                           state.state() > AltosLib.ao_flight_boost) {
                                if (state.max_speed() != AltosLib.MISSING)
                                        voice.speak("max speed: %s.",
                                                    AltosConvert.speed.say_units(state.max_speed() + 0.5));
                                ret = true;
-                       } else if ((old_state == null || old_state.state < AltosLib.ao_flight_drogue) &&
-                                  state.state >= AltosLib.ao_flight_drogue) {
+                       } else if ((old_state == null || old_state.state() < AltosLib.ao_flight_drogue) &&
+                                  state.state() >= AltosLib.ao_flight_drogue) {
                                if (state.max_height() != AltosLib.MISSING)
                                        voice.speak("max height: %s.",
                                                    AltosConvert.height.say_units(state.max_height() + 0.5));
index 30d436a..ce76e90 100644 (file)
@@ -217,7 +217,7 @@ public class AltosGraphDataPoint implements AltosUIDataPoint {
 
        public int id(int index) {
                if (index == data_state) {
-                       int s = state.state;
+                       int s = state.state();
                        if (AltosLib.ao_flight_boost <= s && s <= AltosLib.ao_flight_landed)
                                return s;
                } else if (data_ignitor_fired_0 <= index && index <= data_ignitor_fired_max) {