altosui/telegps: Reduce CPU time needed for flight displays
[fw/altos] / altosui / AltosLanded.java
index d03e5ac56da3562a34ba9aa56b62ee6ccb221f8c..760b2d64e1456a6ebb4ddccf68414b4621eac4c8 100644 (file)
@@ -21,15 +21,23 @@ import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
 import java.io.*;
-import org.altusmetrum.altoslib.*;
+import org.altusmetrum.altoslib_4.*;
+import org.altusmetrum.altosuilib_2.*;
 
-public class AltosLanded extends JComponent implements AltosFlightDisplay, ActionListener {
+public class AltosLanded extends JComponent implements AltosFlightDisplay, ActionListener, HierarchyListener {
        GridBagLayout   layout;
 
-       public class LandedValue {
+       private AltosState              last_state;
+       private AltosListenerState      last_listener_state;
+
+       public abstract class LandedValue implements AltosFontListener, AltosUnitsListener {
                JLabel          label;
                JTextField      value;
-               void show(AltosState state, int crc_errors) {}
+               AltosUnits      units;
+               double          v;
+               String          last_value = "";
+
+               abstract void show(AltosState state, AltosListenerState listener_state);
 
                void reset() {
                        value.setText("");
@@ -42,28 +50,42 @@ public class AltosLanded extends JComponent implements AltosFlightDisplay, Actio
 
                void show(String s) {
                        show();
-                       value.setText(s);
+                       if (!last_value.equals(s)) {
+                               value.setText(s);
+                               last_value = s;
+                       }
                }
-               
-               void show(AltosUnits units, double v) {
-                       show(units.show(8, v));
+
+               void show(double v) {
+                       this.v = v;
+                       if (v == AltosLib.MISSING)
+                               show("Missing");
+                       else
+                               show(units.show(8, v));
                }
 
                void show(String format, double v) {
                        show(String.format(format, v));
                }
 
-               public void set_font() {
+               public void font_size_changed(int font_size) {
                        label.setFont(Altos.label_font);
                        value.setFont(Altos.value_font);
                }
 
+               public void units_changed(boolean imperial_units) {
+                       if (units != null)
+                               show(v);
+               }
+
                void hide() {
                        label.setVisible(false);
                        value.setVisible(false);
                }
 
-               public LandedValue (GridBagLayout layout, int y, String text) {
+               public LandedValue (GridBagLayout layout, int y, AltosUnits units, String text) {
+                       this.units = units;
+
                        GridBagConstraints      c = new GridBagConstraints();
                        c.weighty = 1;
 
@@ -79,6 +101,7 @@ public class AltosLanded extends JComponent implements AltosFlightDisplay, Actio
                        add(label);
 
                        value = new JTextField(Altos.text_width);
+                       value.setEditable(false);
                        value.setFont(Altos.value_font);
                        value.setHorizontalAlignment(SwingConstants.RIGHT);
                        c.gridx = 1; c.gridy = y;
@@ -88,6 +111,10 @@ public class AltosLanded extends JComponent implements AltosFlightDisplay, Actio
                        layout.setConstraints(value, c);
                        add(value);
                }
+
+               public LandedValue (GridBagLayout layout, int y, String text) {
+                       this(layout, y, null, text);
+               }
        }
 
        String pos(double p, String pos, String neg) {
@@ -102,8 +129,9 @@ public class AltosLanded extends JComponent implements AltosFlightDisplay, Actio
        }
 
        class Lat extends LandedValue {
-               void show (AltosState state, int crc_errors) {
-                       if (state.gps != null && state.gps.connected)
+               void show (AltosState state, AltosListenerState listener_state) {
+                       show();
+                       if (state.gps != null && state.gps.connected && state.gps.lat != AltosLib.MISSING)
                                show(pos(state.gps.lat,"N", "S"));
                        else
                                show("???");
@@ -116,9 +144,9 @@ public class AltosLanded extends JComponent implements AltosFlightDisplay, Actio
        Lat lat;
 
        class Lon extends LandedValue {
-               void show (AltosState state, int crc_errors) {
+               void show (AltosState state, AltosListenerState listener_state) {
                        show();
-                       if (state.gps != null && state.gps.connected)
+                       if (state.gps != null && state.gps.connected && state.gps.lon != AltosLib.MISSING)
                                show(pos(state.gps.lon,"E", "W"));
                        else
                                show("???");
@@ -131,7 +159,7 @@ public class AltosLanded extends JComponent implements AltosFlightDisplay, Actio
        Lon lon;
 
        class Bearing extends LandedValue {
-               void show (AltosState state, int crc_errors) {
+               void show (AltosState state, AltosListenerState listener_state) {
                        show();
                        if (state.from_pad != null)
                                show("%3.0f°", state.from_pad.bearing);
@@ -146,48 +174,48 @@ public class AltosLanded extends JComponent implements AltosFlightDisplay, Actio
        Bearing bearing;
 
        class Distance extends LandedValue {
-               void show (AltosState state, int crc_errors) {
+               void show (AltosState state, AltosListenerState listener_state) {
                        show();
                        if (state.from_pad != null)
-                               show(AltosConvert.distance, state.from_pad.distance);
+                               show(state.from_pad.distance);
                        else
                                show("???");
                }
                public Distance (GridBagLayout layout, int y) {
-                       super (layout, y, "Distance");
+                       super (layout, y, AltosConvert.distance, "Distance");
                }
        }
 
        Distance distance;
 
        class Height extends LandedValue {
-               void show (AltosState state, int crc_errors) {
-                       show(AltosConvert.height, state.max_height);
+               void show (AltosState state, AltosListenerState listener_state) {
+                       show(state.max_height());
                }
                public Height (GridBagLayout layout, int y) {
-                       super (layout, y, "Maximum Height");
+                       super (layout, y, AltosConvert.height, "Maximum Height");
                }
        }
 
        Height  height;
 
        class Speed extends LandedValue {
-               void show (AltosState state, int crc_errors) {
-                       show(AltosConvert.speed, state.max_speed());
+               void show (AltosState state, AltosListenerState listener_state) {
+                       show(state.max_speed());
                }
                public Speed (GridBagLayout layout, int y) {
-                       super (layout, y, "Maximum Speed");
+                       super (layout, y, AltosConvert.speed, "Maximum Speed");
                }
        }
 
        Speed   speed;
 
        class Accel extends LandedValue {
-               void show (AltosState state, int crc_errors) {
-                       show(AltosConvert.accel, state.max_acceleration);
+               void show (AltosState state, AltosListenerState listener_state) {
+                       show(state.max_acceleration());
                }
                public Accel (GridBagLayout layout, int y) {
-                       super (layout, y, "Maximum Acceleration");
+                       super (layout, y, AltosConvert.accel, "Maximum Acceleration");
                }
        }
 
@@ -203,31 +231,47 @@ public class AltosLanded extends JComponent implements AltosFlightDisplay, Actio
                accel.reset();
        }
 
-       public void set_font() {
-               lat.set_font();
-               lon.set_font();
-               bearing.set_font();
-               distance.set_font();
-               height.set_font();
-               speed.set_font();
-               accel.set_font();
+       public void font_size_changed(int font_size) {
+               lat.font_size_changed(font_size);
+               lon.font_size_changed(font_size);
+               bearing.font_size_changed(font_size);
+               distance.font_size_changed(font_size);
+               height.font_size_changed(font_size);
+               speed.font_size_changed(font_size);
+               accel.font_size_changed(font_size);
+       }
+
+       public void units_changed(boolean imperial_units) {
+               lat.units_changed(imperial_units);
+               lon.units_changed(imperial_units);
+               bearing.units_changed(imperial_units);
+               distance.units_changed(imperial_units);
+               height.units_changed(imperial_units);
+               speed.units_changed(imperial_units);
+               accel.units_changed(imperial_units);
        }
 
-       public void show(AltosState state, int crc_errors) {
+       public void show(AltosState state, AltosListenerState listener_state) {
+               if (!isShowing()) {
+                       last_state = state;
+                       last_listener_state = listener_state;
+                       return;
+               }
+
                if (state.gps != null && state.gps.connected) {
-                       bearing.show(state, crc_errors);
-                       distance.show(state, crc_errors);
-                       lat.show(state, crc_errors);
-                       lon.show(state, crc_errors);
+                       bearing.show(state, listener_state);
+                       distance.show(state, listener_state);
+                       lat.show(state, listener_state);
+                       lon.show(state, listener_state);
                } else {
                        bearing.hide();
                        distance.hide();
                        lat.hide();
                        lon.hide();
                }
-               height.show(state, crc_errors);
-               speed.show(state, crc_errors);
-               accel.show(state, crc_errors);
+               height.show(state, listener_state);
+               speed.show(state, listener_state);
+               accel.show(state, listener_state);
                if (reader.backing_file() != null)
                        graph.setEnabled(true);
        }
@@ -243,21 +287,18 @@ public class AltosLanded extends JComponent implements AltosFlightDisplay, Actio
                        if (file != null) {
                                String  filename = file.getName();
                                try {
-                                       AltosRecordIterable records = null;
+                                       AltosStateIterable states = null;
                                        if (filename.endsWith("eeprom")) {
                                                FileInputStream in = new FileInputStream(file);
-                                               records = new AltosEepromIterable(in);
+                                               states = new AltosEepromFile(in);
                                        } else if (filename.endsWith("telem")) {
                                                FileInputStream in = new FileInputStream(file);
-                                               records = new AltosTelemetryIterable(in);
-                                       } else if (filename.endsWith("mega")) {
-                                               FileInputStream in = new FileInputStream(file);
-                                               records = new AltosEepromMegaIterable(in);
+                                               states = new AltosTelemetryFile(in);
                                        } else {
                                                throw new FileNotFoundException(filename);
                                        }
                                        try {
-                                               new AltosGraphUI(records, filename);
+                                               new AltosGraphUI(states, file);
                                        } catch (InterruptedException ie) {
                                        } catch (IOException ie) {
                                        }
@@ -271,6 +312,21 @@ public class AltosLanded extends JComponent implements AltosFlightDisplay, Actio
                }
        }
 
+       public String getName() {
+               return "Landed";
+       }
+
+       public void hierarchyChanged(HierarchyEvent e) {
+               if (last_state != null && isShowing()) {
+                       AltosState              state = last_state;
+                       AltosListenerState      listener_state = last_listener_state;
+
+                       last_state = null;
+                       last_listener_state = null;
+                       show(state, listener_state);
+               }
+       }
+
        public AltosLanded(AltosFlightReader in_reader) {
                layout = new GridBagLayout();
 
@@ -301,5 +357,6 @@ public class AltosLanded extends JComponent implements AltosFlightDisplay, Actio
                c.weighty = 0;
                c.fill = GridBagConstraints.VERTICAL;
                add(graph, c);
+               addHierarchyListener(this);
        }
 }