altosui/telegps: Reduce CPU time needed for flight displays
[fw/altos] / altosui / AltosAscent.java
index c8ce2caa1feac5f9cfc91810916ae69e0d3e9253..c3225709c21f651fd10423ecfbcef902698c9009 100644 (file)
 package altosui;
 
 import java.awt.*;
+import java.awt.event.*;
 import javax.swing.*;
-import org.altusmetrum.altoslib_3.*;
+import org.altusmetrum.altoslib_4.*;
+import org.altusmetrum.altosuilib_2.*;
 
-public class AltosAscent extends JComponent implements AltosFlightDisplay {
+public class AltosAscent extends JComponent implements AltosFlightDisplay, HierarchyListener {
        GridBagLayout   layout;
        JLabel                  cur, max;
 
-       public class AscentStatus {
+       private AltosState              last_state;
+       private AltosListenerState      last_listener_state;
+
+       public class AscentStatus implements AltosFontListener, AltosUnitsListener {
                JLabel          label;
                JTextField      value;
                AltosLights     lights;
+               double          v;
+               AltosUnits      units;
 
                void show() {
                        value.setVisible(true);
@@ -48,8 +55,9 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay {
                        show();
                        value.setText(s);
                }
-               
-               void show(AltosUnits units, double v) {
+
+               void show(double v) {
+                       this.v = v;
                        show(units.show(8, v));
                }
 
@@ -62,12 +70,18 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay {
                        lights.set(false);
                }
 
-               void set_font() {
+               public void font_size_changed(int font_size) {
                        label.setFont(Altos.label_font);
                        value.setFont(Altos.value_font);
                }
 
-               public AscentStatus (GridBagLayout layout, int y, String text) {
+               public void units_changed(boolean imperial_units) {
+                       if (units != null)
+                               show(v);
+               }
+
+               public AscentStatus (GridBagLayout layout, int y, AltosUnits units, String text) {
+                       this.units = units;
                        GridBagConstraints      c = new GridBagConstraints();
                        c.weighty = 1;
 
@@ -91,6 +105,7 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay {
                        add(label);
 
                        value = new JTextField(Altos.text_width);
+                       value.setEditable(false);
                        value.setFont(Altos.value_font);
                        value.setHorizontalAlignment(SwingConstants.RIGHT);
                        c.gridx = 2; c.gridy = y;
@@ -104,10 +119,13 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay {
                }
        }
 
-       public class AscentValue {
+       public abstract class AscentValue implements AltosFontListener, AltosUnitsListener {
                JLabel          label;
                JTextField      value;
-               void show(AltosState state, AltosListenerState listener_state) {}
+               double          v;
+               AltosUnits      units;
+
+               abstract void show(AltosState state, AltosListenerState listener_state);
 
                void reset() {
                        value.setText("");
@@ -122,8 +140,9 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay {
                        show();
                        value.setText(s);
                }
-               
-               void show(AltosUnits units, double v) {
+
+               void show(double v) {
+                       this.v = v;
                        show(units.show(8, v));
                }
 
@@ -135,12 +154,18 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay {
                        label.setVisible(false);
                        value.setVisible(false);
                }
-               void set_font() {
+
+               public void font_size_changed(int font_size) {
                        label.setFont(Altos.label_font);
                        value.setFont(Altos.value_font);
                }
 
-               public AscentValue (GridBagLayout layout, int y, String text) {
+               public void units_changed(boolean imperial_units) {
+                       if (units != null)
+                               show(v);
+               }
+
+               public AscentValue (GridBagLayout layout, int y, AltosUnits units, String text) {
                        GridBagConstraints      c = new GridBagConstraints();
                        c.weighty = 1;
 
@@ -156,6 +181,7 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay {
                        add(label);
 
                        value = new JTextField(Altos.text_width);
+                       value.setEditable(false);
                        value.setFont(Altos.value_font);
                        value.setHorizontalAlignment(SwingConstants.RIGHT);
                        c.gridx = 2; c.gridy = y;
@@ -166,13 +192,19 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay {
                        layout.setConstraints(value, c);
                        add(value);
                }
+
+               public AscentValue (GridBagLayout layout, int y, String text) {
+                       this(layout, y, null, text);
+               }
        }
 
-       public class AscentValueHold {
+       public class AscentValueHold implements AltosFontListener, AltosUnitsListener {
                JLabel          label;
                JTextField      value;
                JTextField      max_value;
                double          max;
+               AltosUnits      units;
+               double          v;
 
                void show(AltosState state, AltosListenerState listener_state) {}
 
@@ -182,25 +214,38 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay {
                        max = AltosLib.MISSING;
                }
 
-               void set_font() {
+               public void font_size_changed(int font_size) {
                        label.setFont(Altos.label_font);
                        value.setFont(Altos.value_font);
                        max_value.setFont(Altos.value_font);
                }
 
-               void show(AltosUnits units, double v) {
+               public void units_changed(boolean imperial_units) {
+                       show(v, max);
+               }
+
+               void show(double v, double max) {
+                       this.v = v;
+                       this.max = max;
                        if (v == AltosLib.MISSING) {
                                value.setText("Missing");
-                               max_value.setText("Missing");
                        } else {
                                value.setText(units.show(8, v));
-                               if (v > max || max == AltosLib.MISSING) {
-                                       max_value.setText(units.show(8, v));
-                                       max = v;
-                               }
                        }
+                       if (max == AltosLib.MISSING)
+                               max_value.setText("Missing");
+                       else
+                               max_value.setText(units.show(8, max));
                }
-               public AscentValueHold (GridBagLayout layout, int y, String text) {
+
+               void hide() {
+                       label.setVisible(false);
+                       value.setVisible(false);
+                       max_value.setVisible(false);
+               }
+
+               public AscentValueHold (GridBagLayout layout, int y, AltosUnits units, String text) {
+                       this.units = units;
                        GridBagConstraints      c = new GridBagConstraints();
                        c.weighty = 1;
 
@@ -216,6 +261,7 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay {
                        add(label);
 
                        value = new JTextField(Altos.text_width);
+                       value.setEditable(false);
                        value.setFont(Altos.value_font);
                        value.setHorizontalAlignment(SwingConstants.RIGHT);
                        c.gridx = 2; c.gridy = y;
@@ -226,6 +272,7 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay {
                        add(value);
 
                        max_value = new JTextField(Altos.text_width);
+                       max_value.setEditable(false);
                        max_value.setFont(Altos.value_font);
                        max_value.setHorizontalAlignment(SwingConstants.RIGHT);
                        c.gridx = 3; c.gridy = y;
@@ -237,13 +284,12 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay {
                }
        }
 
-
        class Height extends AscentValueHold {
                void show (AltosState state, AltosListenerState listener_state) {
-                       show(AltosConvert.height, state.height());
+                       show(state.height(), state.max_height());
                }
                public Height (GridBagLayout layout, int y) {
-                       super (layout, y, "Height");
+                       super (layout, y, AltosConvert.height, "Height");
                }
        }
 
@@ -251,10 +297,10 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay {
 
        class Speed extends AscentValueHold {
                void show (AltosState state, AltosListenerState listener_state) {
-                       show(AltosConvert.speed, state.speed());
+                       show(state.speed(), state.max_speed());
                }
                public Speed (GridBagLayout layout, int y) {
-                       super (layout, y, "Speed");
+                       super (layout, y, AltosConvert.speed, "Speed");
                }
        }
 
@@ -262,15 +308,26 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay {
 
        class Accel extends AscentValueHold {
                void show (AltosState state, AltosListenerState listener_state) {
-                       show(AltosConvert.accel, state.acceleration());
+                       show(state.acceleration(), state.max_acceleration());
                }
                public Accel (GridBagLayout layout, int y) {
-                       super (layout, y, "Acceleration");
+                       super (layout, y, AltosConvert.accel, "Acceleration");
                }
        }
 
        Accel   accel;
 
+       class Orient extends AscentValueHold {
+               void show (AltosState state, AltosListenerState listener_state) {
+                       show(state.orient(), state.max_orient());
+               }
+               public Orient (GridBagLayout layout, int y) {
+                       super (layout, y, AltosConvert.orient, "Tilt Angle");
+               }
+       }
+
+       Orient  orient;
+
        String pos(double p, String pos, String neg) {
                String  h = pos;
                if (p < 0) {
@@ -288,7 +345,7 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay {
                        lights.set(state.apogee_voltage >= AltosLib.ao_igniter_good);
                }
                public Apogee (GridBagLayout layout, int y) {
-                       super(layout, y, "Apogee Igniter Voltage");
+                       super(layout, y, null, "Apogee Igniter Voltage");
                }
        }
 
@@ -300,7 +357,7 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay {
                        lights.set(state.main_voltage >= AltosLib.ao_igniter_good);
                }
                public Main (GridBagLayout layout, int y) {
-                       super(layout, y, "Main Igniter Voltage");
+                       super(layout, y, null, "Main Igniter Voltage");
                }
        }
 
@@ -342,21 +399,40 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay {
                height.reset();
                speed.reset();
                accel.reset();
+               orient.reset();
        }
 
-       public void set_font() {
+       public void font_size_changed(int font_size) {
                cur.setFont(Altos.label_font);
                max.setFont(Altos.label_font);
-               lat.set_font();
-               lon.set_font();
-               main.set_font();
-               apogee.set_font();
-               height.set_font();
-               speed.set_font();
-               accel.set_font();
+               lat.font_size_changed(font_size);
+               lon.font_size_changed(font_size);
+               main.font_size_changed(font_size);
+               apogee.font_size_changed(font_size);
+               height.font_size_changed(font_size);
+               speed.font_size_changed(font_size);
+               accel.font_size_changed(font_size);
+               orient.font_size_changed(font_size);
+       }
+
+       public void units_changed(boolean imperial_units) {
+               lat.units_changed(imperial_units);
+               lon.units_changed(imperial_units);
+               main.units_changed(imperial_units);
+               apogee.units_changed(imperial_units);
+               height.units_changed(imperial_units);
+               speed.units_changed(imperial_units);
+               accel.units_changed(imperial_units);
+               orient.units_changed(imperial_units);
        }
 
        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) {
                        lat.show(state, listener_state);
                        lon.show(state, listener_state);
@@ -375,6 +451,10 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay {
                        apogee.hide();
                speed.show(state, listener_state);
                accel.show(state, listener_state);
+               if (state.orient() != AltosLib.MISSING)
+                       orient.show(state, listener_state);
+               else
+                       orient.hide();
        }
 
        public void labels(GridBagLayout layout, int y) {
@@ -399,6 +479,17 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay {
                return "Ascent";
        }
 
+       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 AltosAscent() {
                layout = new GridBagLayout();
 
@@ -410,13 +501,16 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay {
                 * lon
                 * height
                 */
-               labels(layout, 0);
-               height = new Height(layout, 1);
-               speed = new Speed(layout, 2);
-               accel = new Accel(layout, 3);
-               lat = new Lat(layout, 4);
-               lon = new Lon(layout, 5);
-               apogee = new Apogee(layout, 6);
-               main = new Main(layout, 7);
+               int y = 0;
+               labels(layout, y++);
+               height = new Height(layout, y++);
+               speed = new Speed(layout, y++);
+               accel = new Accel(layout, y++);
+               orient = new Orient(layout, y++);
+               lat = new Lat(layout, y++);
+               lon = new Lon(layout, y++);
+               apogee = new Apogee(layout, y++);
+               main = new Main(layout, y++);
+               addHierarchyListener(this);
        }
 }