altosui/telegps: Reduce CPU time needed for flight displays
[fw/altos] / altosui / AltosAscent.java
index de6c90a1098e97a9503860f82e22971b4ef1637f..c3225709c21f651fd10423ecfbcef902698c9009 100644 (file)
  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  */
 
-package AltosUI;
+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.*;
-
-public class AltosAscent extends JComponent implements AltosFlightDisplay {
+import org.altusmetrum.altoslib_4.*;
+import org.altusmetrum.altosuilib_2.*;
+
+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);
@@ -50,18 +49,39 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay {
                        label.setVisible(false);
                }
 
-               void show(AltosState state, int crc_errors) {}
+               void show(AltosState state, AltosListenerState listener_state) {}
+
+               void show(String s) {
+                       show();
+                       value.setText(s);
+               }
+
+               void show(double v) {
+                       this.v = v;
+                       show(units.show(8, v));
+               }
+
+               void show(String format, double v) {
+                       show(String.format(format, v));
+               }
+
                void reset() {
                        value.setText("");
                        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;
 
@@ -85,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;
@@ -98,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, int crc_errors) {}
+               double          v;
+               AltosUnits      units;
+
+               abstract void show(AltosState state, AltosListenerState listener_state);
 
                void reset() {
                        value.setText("");
@@ -112,16 +136,36 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay {
                        value.setVisible(true);
                }
 
+               void show(String s) {
+                       show();
+                       value.setText(s);
+               }
+
+               void show(double v) {
+                       this.v = v;
+                       show(units.show(8, v));
+               }
+
+               void show(String format, double v) {
+                       show(String.format(format, v));
+               }
+
                void hide() {
                        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;
 
@@ -137,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;
@@ -147,41 +192,60 @@ 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, int crc_errors) {}
+               void show(AltosState state, AltosListenerState listener_state) {}
 
                void reset() {
                        value.setText("");
                        max_value.setText("");
-                       max = AltosRecord.MISSING;
+                       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) {
-                       if (v == AltosRecord.MISSING) {
+               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 == AltosRecord.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));
+               }
+
+               void hide() {
+                       label.setVisible(false);
+                       value.setVisible(false);
+                       max_value.setVisible(false);
                }
-               public AscentValueHold (GridBagLayout layout, int y, String text) {
+
+               public AscentValueHold (GridBagLayout layout, int y, AltosUnits units, String text) {
+                       this.units = units;
                        GridBagConstraints      c = new GridBagConstraints();
                        c.weighty = 1;
 
@@ -197,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;
@@ -207,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;
@@ -218,43 +284,50 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay {
                }
        }
 
-
        class Height extends AscentValueHold {
-               void show (AltosState state, int crc_errors) {
-                       show(AltosConvert.height, state.height);
+               void show (AltosState state, AltosListenerState listener_state) {
+                       show(state.height(), state.max_height());
                }
                public Height (GridBagLayout layout, int y) {
-                       super (layout, y, "Height");
+                       super (layout, y, AltosConvert.height, "Height");
                }
        }
 
        Height  height;
 
        class Speed extends AscentValueHold {
-               void show (AltosState state, int crc_errors) {
-                       double speed = state.speed;
-                       if (!state.ascent)
-                               speed = state.baro_speed;
-                       show(AltosConvert.speed, speed);
+               void show (AltosState state, AltosListenerState listener_state) {
+                       show(state.speed(), state.max_speed());
                }
                public Speed (GridBagLayout layout, int y) {
-                       super (layout, y, "Speed");
+                       super (layout, y, AltosConvert.speed, "Speed");
                }
        }
 
        Speed   speed;
 
        class Accel extends AscentValueHold {
-               void show (AltosState state, int crc_errors) {
-                       show(AltosConvert.accel, state.acceleration);
+               void show (AltosState state, AltosListenerState listener_state) {
+                       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) {
@@ -267,38 +340,35 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay {
        }
 
        class Apogee extends AscentStatus {
-               void show (AltosState state, int crc_errors) {
-                       show();
-                       value.setText(String.format("%4.2f V", state.drogue_sense));
-                       lights.set(state.drogue_sense > 3.2);
+               void show (AltosState state, AltosListenerState listener_state) {
+                       show("%4.2f V", state.apogee_voltage);
+                       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");
                }
        }
 
        Apogee apogee;
 
        class Main extends AscentStatus {
-               void show (AltosState state, int crc_errors) {
-                       show();
-                       value.setText(String.format("%4.2f V", state.main_sense));
-                       lights.set(state.main_sense > 3.2);
+               void show (AltosState state, AltosListenerState listener_state) {
+                       show("%4.2f V", state.main_voltage);
+                       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");
                }
        }
 
        Main main;
 
        class Lat extends AscentValue {
-               void show (AltosState state, int crc_errors) {
-                       show();
-                       if (state.gps != null)
-                               value.setText(pos(state.gps.lat,"N", "S"));
+               void show (AltosState state, AltosListenerState listener_state) {
+                       if (state.gps != null && state.gps.connected && state.gps.lat != AltosLib.MISSING)
+                               show(pos(state.gps.lat,"N", "S"));
                        else
-                               value.setText("???");
+                               show("???");
                }
                public Lat (GridBagLayout layout, int y) {
                        super (layout, y, "Latitude");
@@ -308,12 +378,11 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay {
        Lat lat;
 
        class Lon extends AscentValue {
-               void show (AltosState state, int crc_errors) {
-                       show();
-                       if (state.gps != null)
-                               value.setText(pos(state.gps.lon,"E", "W"));
+               void show (AltosState state, AltosListenerState listener_state) {
+                       if (state.gps != null && state.gps.connected && state.gps.lon != AltosLib.MISSING)
+                               show(pos(state.gps.lon,"E", "W"));
                        else
-                               value.setText("???");
+                               show("???");
                }
                public Lon (GridBagLayout layout, int y) {
                        super (layout, y, "Longitude");
@@ -330,39 +399,62 @@ 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 show(AltosState state, int crc_errors) {
+       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, crc_errors);
-                       lon.show(state, crc_errors);
+                       lat.show(state, listener_state);
+                       lon.show(state, listener_state);
                } else {
                        lat.hide();
                        lon.hide();
                }
-               height.show(state, crc_errors);
-               if (state.main_sense != AltosRecord.MISSING)
-                       main.show(state, crc_errors);
+               height.show(state, listener_state);
+               if (state.main_voltage != AltosLib.MISSING)
+                       main.show(state, listener_state);
                else
                        main.hide();
-               if (state.drogue_sense != AltosRecord.MISSING)
-                       apogee.show(state, crc_errors);
+               if (state.apogee_voltage != AltosLib.MISSING)
+                       apogee.show(state, listener_state);
                else
                        apogee.hide();
-               speed.show(state, crc_errors);
-               accel.show(state, crc_errors);
+               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) {
@@ -383,6 +475,21 @@ public class AltosAscent extends JComponent implements AltosFlightDisplay {
                add(max);
        }
 
+       public String getName() {
+               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();
 
@@ -394,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);
        }
 }