altoslib: Add imperial units conversion support
authorKeith Packard <keithp@keithp.com>
Mon, 10 Sep 2012 16:14:03 +0000 (09:14 -0700)
committerKeith Packard <keithp@keithp.com>
Mon, 10 Sep 2012 16:14:03 +0000 (09:14 -0700)
"Redneck" mode support

Signed-off-by: Keith Packard <keithp@keithp.com>
altoslib/AltosAccel.java [new file with mode: 0644]
altoslib/AltosConvert.java
altoslib/AltosDistance.java [new file with mode: 0644]
altoslib/AltosHeight.java [new file with mode: 0644]
altoslib/AltosPreferences.java
altoslib/AltosSpeed.java [new file with mode: 0644]
altoslib/AltosUnits.java [new file with mode: 0644]
altoslib/Makefile.am

diff --git a/altoslib/AltosAccel.java b/altoslib/AltosAccel.java
new file mode 100644 (file)
index 0000000..d14764a
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Copyright © 2012 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package org.altusmetrum.AltosLib;
+
+public class AltosAccel extends AltosUnits {
+
+       public double value(double v) {
+               if (AltosConvert.imperial_units)
+                       return AltosConvert.meters_to_feet(v);
+               return v;
+       }
+
+       public String show_units() {
+               if (AltosConvert.imperial_units)
+                       return "ft/s²";
+               return "m/s²";
+       }
+
+       public String say_units() {
+               if (AltosConvert.imperial_units)
+                       return "feet per second squared";
+               return "meters per second squared";
+       }
+
+       int show_fraction(int width) {
+               return width / 9;
+       }
+}
\ No newline at end of file
index 3527b575178a2291ceb62b5729c3987af756f618..acd6c5f481f331bcdd28fa4304668996e3db04c1 100644 (file)
@@ -242,6 +242,14 @@ public class AltosConvert {
                return meters * (100 / (2.54 * 12));
        }
 
+       public static double meters_to_miles(double meters) {
+               return meters_to_feet(meters) / 5280;
+       }
+
+       public static double meters_to_mph(double mps) {
+               return meters_to_miles(mps) * 3600;
+       }
+
        public static double meters_to_mach(double meters) {
                return meters / 343;            /* something close to mach at usual rocket sites */
        }
@@ -250,6 +258,26 @@ public class AltosConvert {
                return meters / 9.80665;
        }
 
+       public static boolean imperial_units = false;
+
+       public static AltosDistance distance = new AltosDistance();
+
+       public static AltosHeight height = new AltosHeight();
+
+       public static AltosSpeed speed = new AltosSpeed();
+
+       public static AltosAccel accel = new AltosAccel();
+
+       public static String show_gs(String format, double a) {
+               a = meters_to_g(a);
+               format = format.concat(" g");
+               return String.format(format, a);
+       }
+
+       public static String say_gs(double a) {
+               return String.format("%6.0 gees", meters_to_g(a));
+       }
+
        public static int checksum(int[] data, int start, int length) {
                int     csum = 0x5a;
                for (int i = 0; i < length; i++)
diff --git a/altoslib/AltosDistance.java b/altoslib/AltosDistance.java
new file mode 100644 (file)
index 0000000..a5e7331
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * Copyright © 2012 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package org.altusmetrum.AltosLib;
+
+public class AltosDistance extends AltosUnits {
+
+       public double value(double v) {
+               if (AltosConvert.imperial_units)
+                       return AltosConvert.meters_to_miles(v);
+               return v;
+       }
+
+       public String show_units() {
+               if (AltosConvert.imperial_units)
+                       return "miles";
+               return "m";
+       }
+
+       public String say_units() {
+               if (AltosConvert.imperial_units)
+                       return "miles";
+               return "meters";
+       }
+
+       int show_fraction(int width) {
+               if (AltosConvert.imperial_units)
+                       return width / 3;
+               return width / 9;
+       }
+
+       int say_fraction() {
+               return 1;
+       }
+}
\ No newline at end of file
diff --git a/altoslib/AltosHeight.java b/altoslib/AltosHeight.java
new file mode 100644 (file)
index 0000000..da7ffda
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Copyright © 2012 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package org.altusmetrum.AltosLib;
+
+public class AltosHeight extends AltosUnits {
+
+       public double value(double v) {
+               if (AltosConvert.imperial_units)
+                       return AltosConvert.meters_to_feet(v);
+               return v;
+       }
+
+       public String show_units() {
+               if (AltosConvert.imperial_units)
+                       return "ft";
+               return "m";
+       }
+
+       public String say_units() {
+               if (AltosConvert.imperial_units)
+                       return "feet";
+               return "meters";
+       }
+
+       int show_fraction(int width) {
+               return width / 9;
+       }
+}
\ No newline at end of file
index 13fee46de129d8c6e7f909790890e5b21bb194f8..065b6e997cadfc4d3cc5c00043494039bc735a20 100644 (file)
@@ -93,6 +93,10 @@ public class AltosPreferences {
        public final static String      frequency_format = "FREQUENCY-%d";
        public final static String      description_format = "DESCRIPTION-%d";
 
+       /* Units preference */
+
+       public final static String      unitsPreference = "IMPERIAL-UNITS";
+
        public static AltosFrequency[] load_common_frequencies() {
                AltosFrequency[] frequencies = null;
                boolean existing = false;
@@ -176,6 +180,7 @@ public class AltosPreferences {
 
                common_frequencies = load_common_frequencies();
 
+               AltosConvert.imperial_units = preferences.getBoolean(unitsPreference, false);
        }
 
        static { init(); }
@@ -356,4 +361,16 @@ public class AltosPreferences {
                        new_frequencies[i+1] = common_frequencies[i];
                set_common_frequencies(new_frequencies);
        }
+
+       public static boolean imperial_units() {
+               return AltosConvert.imperial_units;
+       }
+
+       public static void set_imperial_units(boolean imperial_units) {
+               AltosConvert.imperial_units = imperial_units;
+               synchronized (preferences) {
+                       preferences.putBoolean(unitsPreference, imperial_units);
+                       flush_preferences();
+               }
+       }
 }
diff --git a/altoslib/AltosSpeed.java b/altoslib/AltosSpeed.java
new file mode 100644 (file)
index 0000000..4e2daf5
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Copyright © 2012 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package org.altusmetrum.AltosLib;
+
+public class AltosSpeed extends AltosUnits {
+
+       public double value(double v) {
+               if (AltosConvert.imperial_units)
+                       return AltosConvert.meters_to_mph(v);
+               return v;
+       }
+
+       public String show_units() {
+               if (AltosConvert.imperial_units)
+                       return "mph";
+               return "m/s";
+       }
+
+       public String say_units() {
+               if (AltosConvert.imperial_units)
+                       return "miles per hour";
+               return "meters per second";
+       }
+
+       int show_fraction(int width) {
+               return width / 9;
+       }
+}
\ No newline at end of file
diff --git a/altoslib/AltosUnits.java b/altoslib/AltosUnits.java
new file mode 100644 (file)
index 0000000..47540c6
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * Copyright © 2012 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package org.altusmetrum.AltosLib;
+
+public abstract class AltosUnits {
+
+       public abstract double value(double v);
+
+       public abstract String show_units();
+
+       public abstract String say_units();
+
+       abstract int show_fraction(int width);
+
+       int say_fraction() {
+               return 0;
+       }
+
+       private String show_format(int width) {
+               return String.format("%%%d.%df %s", width, show_fraction(width), show_units());
+       }
+
+       private String say_format() {
+               return String.format("%%1.%df", say_fraction());
+       }
+
+       private String say_units_format() {
+               return String.format("%%1.%df %s", say_fraction(), say_units());
+       }
+
+       public String show(int width, double v) {
+               return String.format(show_format(width), value(v));
+       }
+
+       public String say(double v) {
+               return String.format(say_format(), value(v));
+       }
+
+       public String say_units(double v) {
+               return String.format(say_units_format(), value(v));
+       }
+}
\ No newline at end of file
index 39b43f3dea640e0d0ba8e6e90ec71959b37ec915..a9f810f9db0df531182b3c1cdcc1e98840e4d647 100644 (file)
@@ -68,7 +68,12 @@ AltosLib_JAVA = \
        $(SRC)/AltosTelemetryRecordMegaData.java \
        $(SRC)/AltosMs5607.java \
        $(SRC)/AltosIMU.java \
-       $(SRC)/AltosMag.java
+       $(SRC)/AltosMag.java \
+       $(SRC)/AltosUnits.java \
+       $(SRC)/AltosDistance.java \
+       $(SRC)/AltosHeight.java \
+       $(SRC)/AltosSpeed.java \
+       $(SRC)/AltosAccel.java
 
 JAR=AltosLib.jar