From: Keith Packard Date: Mon, 10 Sep 2012 16:14:03 +0000 (-0700) Subject: altoslib: Add imperial units conversion support X-Git-Tag: 1.1~17 X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=commitdiff_plain;h=66a1e07efcac9324d33a1eca0dfb58a2724b667a;ds=sidebyside altoslib: Add imperial units conversion support "Redneck" mode support Signed-off-by: Keith Packard --- diff --git a/altoslib/AltosAccel.java b/altoslib/AltosAccel.java new file mode 100644 index 00000000..d14764a2 --- /dev/null +++ b/altoslib/AltosAccel.java @@ -0,0 +1,43 @@ +/* + * Copyright © 2012 Keith Packard + * + * 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 diff --git a/altoslib/AltosConvert.java b/altoslib/AltosConvert.java index 3527b575..acd6c5f4 100644 --- a/altoslib/AltosConvert.java +++ b/altoslib/AltosConvert.java @@ -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 index 00000000..a5e7331c --- /dev/null +++ b/altoslib/AltosDistance.java @@ -0,0 +1,49 @@ +/* + * Copyright © 2012 Keith Packard + * + * 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 index 00000000..da7ffdae --- /dev/null +++ b/altoslib/AltosHeight.java @@ -0,0 +1,43 @@ +/* + * Copyright © 2012 Keith Packard + * + * 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 diff --git a/altoslib/AltosPreferences.java b/altoslib/AltosPreferences.java index 13fee46d..065b6e99 100644 --- a/altoslib/AltosPreferences.java +++ b/altoslib/AltosPreferences.java @@ -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 index 00000000..4e2daf5a --- /dev/null +++ b/altoslib/AltosSpeed.java @@ -0,0 +1,43 @@ +/* + * Copyright © 2012 Keith Packard + * + * 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 index 00000000..47540c61 --- /dev/null +++ b/altoslib/AltosUnits.java @@ -0,0 +1,57 @@ +/* + * Copyright © 2012 Keith Packard + * + * 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 diff --git a/altoslib/Makefile.am b/altoslib/Makefile.am index 39b43f3d..a9f810f9 100644 --- a/altoslib/Makefile.am +++ b/altoslib/Makefile.am @@ -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