From 0d4e344b5b9b0c7dbfd3c746a213a4555e347687 Mon Sep 17 00:00:00 2001 From: Matthew Lewis Date: Thu, 4 Apr 2024 21:39:58 +0100 Subject: [PATCH] Logging tweaks. Added resolution end inclusive/exclusive methods to RangeUtil. --- diozero-bom/pom.xml | 4 +- .../com/diozero/api/AnalogInputDevice.java | 17 +- .../devices/sandpit/HCSR04UsingWait.java | 15 +- .../internal/soc/rockchip/RK3288MmapGpio.java | 8 +- .../main/java/com/diozero/util/RangeUtil.java | 140 +++++++++-- .../java/com/diozero/devices/McpAdcTest.java | 18 +- .../java/com/diozero/util/RangeUtilTest.java | 218 +++++++++++++----- .../com/diozero/sampleapps/HCSR04Test.java | 2 +- .../com/diozero/sampleapps/McpAdcTest.java | 2 +- .../com/diozero/sampleapps/TMP36Test.java | 2 +- .../com/diozero/sampleapps/TSL2561Test.java | 2 +- .../sampleapps/perf/GpioReadPerfTest.java | 4 +- .../sampleapps/perf/MCP23017PerfTest.java | 12 +- .../sampleapps/perf/MCP23S17PerfTest.java | 12 +- .../sampleapps/perf/SpiGpioPerfTest.java | 4 +- .../sandpit/HCSR04UsingEventsTest.java | 14 +- 16 files changed, 343 insertions(+), 131 deletions(-) diff --git a/diozero-bom/pom.xml b/diozero-bom/pom.xml index a71f66ce..a06418e1 100644 --- a/diozero-bom/pom.xml +++ b/diozero-bom/pom.xml @@ -67,7 +67,7 @@ 3.6.3 3.3.0 3.5.2 - 3.3.0 + 3.3.1 3.1.1 3.1.1 3.2.2 @@ -88,7 +88,7 @@ 2.10.1 3.25.2 4.1.108.Final - 1.62.2 + 1.63.0 5.10.2 5.11.0 diff --git a/diozero-core/src/main/java/com/diozero/api/AnalogInputDevice.java b/diozero-core/src/main/java/com/diozero/api/AnalogInputDevice.java index c1b2d9ec..ebcf8dbc 100644 --- a/diozero-core/src/main/java/com/diozero/api/AnalogInputDevice.java +++ b/diozero-core/src/main/java/com/diozero/api/AnalogInputDevice.java @@ -44,11 +44,10 @@ /** *

- * The AnalogInputDevice base class encapsulates logic for interfacing with - * analog devices. This class provides access to unscaled (-1..1) and scaled - * (e.g. voltage, temperature, distance) readings. For scaled readings is - * important that the device factory is configured correctly - all raw analog - * readings are normalised (i.e. -1..1). + * The AnalogInputDevice base class encapsulates logic for interfacing with analog + * devices. This class provides access to unscaled (-1..1) and scaled (e.g. voltage, + * temperature, distance) readings. For scaled readings is important that the device + * factory is configured correctly - all raw analog readings are normalised (i.e. -1..1). *

*

* Note: The Raspberry Pi does not natively support analog input devices, see @@ -69,7 +68,7 @@ * try (McpAdc adc = new McpAdc(type, chipSelect); TMP36 tmp36 = new TMP36(adc, pin, vRef, tempOffset)) { * for (int i = 0; i < ITERATIONS; i++) { * double tmp = tmp36.getTemperature(); - * Logger.info("Temperature: {}", String.format("%.2f", Double.valueOf(tmp))); + * Logger.info("Temperature: {0.##}", Double.valueOf(tmp)); * SleepUtil.sleepSeconds(.5); * } * } @@ -225,8 +224,7 @@ public float getRange() { } /** - * Get the unscaled normalised value in the range 0..1 (if unsigned) or -1..1 - * (if signed) + * Get the unscaled normalised value in the range 0..1 (if unsigned) or -1..1 (if signed) * * @return the unscaled value * @throws RuntimeIOException if there was an I/O error @@ -236,8 +234,7 @@ public float getUnscaledValue() throws RuntimeIOException { } /** - * Get the scaled value in the range 0..range (if unsigned) or -range..range (if - * signed) + * Get the scaled value in the range 0..range (if unsigned) or -range..range (if signed) * * @return the scaled value (-range..range) * @throws RuntimeIOException if there was an I/O error diff --git a/diozero-core/src/main/java/com/diozero/devices/sandpit/HCSR04UsingWait.java b/diozero-core/src/main/java/com/diozero/devices/sandpit/HCSR04UsingWait.java index 15f96a10..42f60a36 100644 --- a/diozero-core/src/main/java/com/diozero/devices/sandpit/HCSR04UsingWait.java +++ b/diozero-core/src/main/java/com/diozero/devices/sandpit/HCSR04UsingWait.java @@ -46,13 +46,12 @@ * https://docs.google.com/document/d/1Y-yZnNhMYy7rwhAgyL_pfa39RsB-x2qR4vP8saG73rE/edit# * Product specification: http://www.micropik.com/PDF/HCSR04.pdf * - * Provides 2cm - 400cm non-contact measurement function, the ranging accuracy - * can reach to 3mm You only need to supply a short 10uS pulse to the trigger - * input to start the ranging, and then the module will send out an 8 cycle - * burst of ultrasound at 40 kHz and raise its echo. The Echo is a distance - * object that is pulse width and the range in proportion. We suggest to use - * over 60ms measurement cycle, in order to prevent trigger signal to the echo - * signal + * Provides 2cm - 400cm non-contact measurement function, the ranging accuracy can reach + * to 3mm You only need to supply a short 10uS pulse to the trigger input to start the + * ranging, and then the module will send out an 8 cycle burst of ultrasound at 40 kHz and + * raise its echo. The Echo is a distance object that is pulse width and the range in + * proportion. We suggest to use over 60ms measurement cycle, in order to prevent trigger + * signal to the echo signal */ public class HCSR04UsingWait implements DistanceSensorInterface { public static void main(String[] args) { @@ -65,7 +64,7 @@ public static void main(String[] args) { try (HCSR04UsingWait device = new HCSR04UsingWait(trigger_pin, echo_pin)) { while (true) { - Logger.info("Distance = {} cm", String.format("%.3f", Double.valueOf(device.getDistanceCm()))); + Logger.info("Distance = {0.###} cm", Double.valueOf(device.getDistanceCm())); SleepUtil.sleepMillis(1000); } } catch (RuntimeIOException ex) { diff --git a/diozero-core/src/main/java/com/diozero/internal/soc/rockchip/RK3288MmapGpio.java b/diozero-core/src/main/java/com/diozero/internal/soc/rockchip/RK3288MmapGpio.java index a9bad046..59804d7e 100644 --- a/diozero-core/src/main/java/com/diozero/internal/soc/rockchip/RK3288MmapGpio.java +++ b/diozero-core/src/main/java/com/diozero/internal/soc/rockchip/RK3288MmapGpio.java @@ -128,8 +128,8 @@ public synchronized void initialise() { if (gpioBanks == null) { gpioBanks = new MmapIntBuffer[9]; for (int i = 0; i < gpioBanks.length; i++) { - gpioBanks[i] = new MmapIntBuffer(GPIOMEM_DEVICE, GPIO_BASE + i * GPIO_LENGTH + (i > 0 ? GPIO_CHANNEL : 0), - PAGE_SIZE, ByteOrder.LITTLE_ENDIAN); + gpioBanks[i] = new MmapIntBuffer(GPIOMEM_DEVICE, + GPIO_BASE + i * GPIO_LENGTH + (i > 0 ? GPIO_CHANNEL : 0), PAGE_SIZE, ByteOrder.LITTLE_ENDIAN); } pmuMmapIntBuffer = new MmapIntBuffer(GPIOMEM_DEVICE, PMU_BASE, PAGE_SIZE, ByteOrder.LITTLE_ENDIAN); grfMmapIntBuffer = new MmapIntBuffer(GPIOMEM_DEVICE, GRF_BASE, PAGE_SIZE, ByteOrder.LITTLE_ENDIAN); @@ -545,8 +545,8 @@ public static void main(String[] args) { } long duration_ns = System.nanoTime() - start_nano; - Logger.info("Duration for {} iterations: {}s", Integer.valueOf(iterations), - String.format("%.4f", Float.valueOf(((float) duration_ns) / 1000 / 1000 / 1000))); + Logger.info("Duration for {} iterations: {0.####}s", Integer.valueOf(iterations), + Float.valueOf(((float) duration_ns) / 1000 / 1000 / 1000)); } } } diff --git a/diozero-core/src/main/java/com/diozero/util/RangeUtil.java b/diozero-core/src/main/java/com/diozero/util/RangeUtil.java index 2359b11b..221e3e50 100644 --- a/diozero-core/src/main/java/com/diozero/util/RangeUtil.java +++ b/diozero-core/src/main/java/com/diozero/util/RangeUtil.java @@ -32,8 +32,6 @@ */ public class RangeUtil { - private static final boolean DEFAULT_MAP_CONSTRAIN = true; - public static int constrain(int value, int min, int max) { return Math.min(max, Math.max(value, min)); } @@ -47,8 +45,68 @@ public static double constrain(double value, double min, double max) { } /** - * Map a number from one range to another. Based on Arduino's map(). Example: - * RangeUtil.map(500, 0, 1000, 0, 255); + * Map a number from one range to another and constrain the resulting value to be within + * the target range. Assumes both ranges start at 0. Based on Arduino's map(). Example: + * RangeUtil.map(500, 1000, 256); returns 128. + * + * @param value value to map + * @param fromHigh high end of originating range + * @param toHigh high end of target range + * @return mapped value + */ + public static int map(int value, int fromHigh, int toHigh) { + return map(value, 0, fromHigh, 0, toHigh, true); + } + + /** + * Map a number from one range to another. Assumes both ranges start at 0. Based on + * Arduino's map(). Example: RangeUtil.map(1500, 1000, 256, false); returns + * 384 (128+256). + * + * @param value value to map + * @param fromHigh high end of originating range + * @param toHigh high end of target range + * @param constrain whether to constrain the returned value to the specified range + * @return mapped value + */ + public static int map(int value, int fromHigh, int toHigh, boolean constrain) { + return map(value, 0, fromHigh, 0, toHigh, constrain); + } + + /** + * Map a number from one range to another and constrain the resulting value to be within + * the target range. Based on Arduino's map(). Example: + * RangeUtil.map(0, -1000, 1000, 0, 256); returns 128. + * + * @param value value to map + * @param fromLow low end of originating range + * @param fromHigh high end of originating range + * @param toLow low end of target range + * @param toHigh high end of target range + * @return mapped value + */ + public static int map(int value, int fromLow, int fromHigh, int toLow, int toHigh) { + return map(value, fromLow, fromHigh, toLow, toHigh, true); + } + + /** + * Map a number from one range to another and constrain the resulting value to be within + * the target range. Assumes both ranges start at 0. Based on Arduino's map(). Example: + * RangeUtil.map(0.5f, 1f, 256); returns 128. + * + * @param value value to map + * @param fromHigh high end of originating range + * @param toHigh high end of target range + * @return mapped value + */ + public static int map(float value, float fromHigh, int toHigh) { + return map(value, 0, fromHigh, 0, toHigh, true); + } + + /** + * Map a number from one range to another and constrain the resulting value to be within + * the target range. Based on Arduino's map(). Example: + * RangeUtil.map(1.5f, 1f, 2f, 0, 256); returns 128. * * @param value value to map * @param fromLow low end of originating range @@ -58,20 +116,19 @@ public static double constrain(double value, double min, double max) { * @return mapped value */ public static int map(float value, float fromLow, float fromHigh, int toLow, int toHigh) { - return map(value, fromLow, fromHigh, toLow, toHigh, DEFAULT_MAP_CONSTRAIN); + return map(value, fromLow, fromHigh, toLow, toHigh, true); } /** * Map a number from one range to another. Based on Arduino's map(). Example: - * RangeUtil.map(500, 0, 1000, 0, 255, true); + * RangeUtil.map(0.5f, 1f, 2f, 0, 256, false); returns -128. * * @param value value to map * @param fromLow low end of originating range * @param fromHigh high end of originating range * @param toLow low end of target range * @param toHigh high end of target range - * @param constrain whether to constrain the returned value to the speceified - * range + * @param constrain whether to constrain the returned value to the specified range * @return mapped value */ public static int map(float value, float fromLow, float fromHigh, int toLow, int toHigh, boolean constrain) { @@ -83,8 +140,23 @@ public static int map(float value, float fromLow, float fromHigh, int toLow, int } /** - * Map a number from one range to another. Based on Arduino's map(). Example: - * RangeUtil.map(500, 0, 1000, 0, 255); + * Map a number from one range to another and constrain the resulting value to be within + * the target range. Assumes both ranges start at zero. Based on Arduino's map(). Example: + * RangeUtil.map(0.5f, 1f, 256f); returns 128f. + * + * @param value value to map + * @param fromHigh high end of originating range + * @param toHigh high end of target range + * @return mapped value + */ + public static float map(float value, float fromHigh, float toHigh) { + return map(value, 0, fromHigh, 0, toHigh, true); + } + + /** + * Map a number from one range to another and constrain the resulting value to be within + * the target range. Based on Arduino's map(). Example: + * RangeUtil.map(1.5f, 1f, 2f, 0f, 256f); returns 128f. * * @param value value to map * @param fromLow low end of originating range @@ -94,20 +166,19 @@ public static int map(float value, float fromLow, float fromHigh, int toLow, int * @return mapped value */ public static float map(float value, float fromLow, float fromHigh, float toLow, float toHigh) { - return map(value, fromLow, fromHigh, toLow, toHigh, DEFAULT_MAP_CONSTRAIN); + return map(value, fromLow, fromHigh, toLow, toHigh, true); } /** * Map a number from one range to another. Based on Arduino's map(). Example: - * RangeUtil.map(500, 0, 1000, 0, 255); + * RangeUtil.map(500, 0, 1000, 0, 256); returns 128. * * @param value value to map * @param fromLow low end of originating range * @param fromHigh high end of originating range * @param toLow low end of target range * @param toHigh high end of target range - * @param constrain whether to constrain the returned value to the speceified - * range + * @param constrain whether to constrain the returned value to the specified range * @return mapped value */ public static float map(float value, float fromLow, float fromHigh, float toLow, float toHigh, boolean constrain) { @@ -119,8 +190,23 @@ public static float map(float value, float fromLow, float fromHigh, float toLow, } /** - * Map a number from one range to another. Based on Arduino's map(). Example: - * RangeUtil.map(500, 0, 1000, 0, 255); + * Map a number from one range to another and constrain the resulting value to be within + * the target range. Assumes both ranges start at zero. Based on Arduino's map(). Example: + * RangeUtil.map(500, 1000, 256); returns 128 + * + * @param value value to map + * @param fromHigh high end of originating range + * @param toHigh high end of target range + * @return mapped value + */ + public static double map(double value, double fromHigh, double toHigh) { + return map(value, 0, fromHigh, 0, toHigh, true); + } + + /** + * Map a number from one range to another and constrain the resulting value to be within + * the target range. Based on Arduino's map(). Example: + * RangeUtil.map(500, 0, 1000, 0, 256); returns 128. * * @param value value to map * @param fromLow low end of originating range @@ -130,9 +216,21 @@ public static float map(float value, float fromLow, float fromHigh, float toLow, * @return mapped value */ public static double map(double value, double fromLow, double fromHigh, double toLow, double toHigh) { - return map(value, fromLow, fromHigh, toLow, toHigh, DEFAULT_MAP_CONSTRAIN); + return map(value, fromLow, fromHigh, toLow, toHigh, true); } + /** + * Map a number from one range to another. Based on Arduino's map(). Example: + * RangeUtil.map(500, 0, 1000, 0, 256); returns 128. + * + * @param value value to map + * @param fromLow low end of originating range + * @param fromHigh high end of originating range + * @param toLow low end of target range + * @param toHigh high end of target range + * @param constrain whether to constrain the returned value to the specified range + * @return mapped value + */ public static double map(double value, double fromLow, double fromHigh, double toLow, double toHigh, boolean constrain) { double result = (value - fromLow) * (toHigh - toLow) / (fromHigh - fromLow) + toLow; @@ -141,4 +239,12 @@ public static double map(double value, double fromLow, double fromHigh, double t } return result; } + + public static int resolutionEndInclusive(int numBits) { + return 1 << numBits; + } + + public static int resolutionEndExclusive(int numBits) { + return resolutionEndInclusive(numBits) - 1; + } } diff --git a/diozero-core/src/test/java/com/diozero/devices/McpAdcTest.java b/diozero-core/src/test/java/com/diozero/devices/McpAdcTest.java index f6d2d500..a015fb72 100644 --- a/diozero-core/src/test/java/com/diozero/devices/McpAdcTest.java +++ b/diozero-core/src/test/java/com/diozero/devices/McpAdcTest.java @@ -52,7 +52,7 @@ public abstract class McpAdcTest { public static void beforeAll() { TestDeviceFactory.setSpiDeviceClass(TestMcpAdcSpiDevice.class); } - + public McpAdcTest(McpAdc.Type type) { this.type = type; } @@ -62,7 +62,7 @@ public void setup() { Logger.info("setup(), type=" + type); TestMcpAdcSpiDevice.setType(type); } - + @Test public void test() { int spi_chip_select = 0; @@ -72,31 +72,31 @@ public void test() { try (McpAdc adc = new McpAdc(type, spi_chip_select, voltage); AnalogInputDevice device = new AnalogInputDevice(adc, pin_number)) { - for (int i=0; i= -1 && unscaled_val < 1, "Unscaled range"); } else { Assertions.assertTrue(unscaled_val >= 0 && unscaled_val < 1, "Unscaled range"); } - + unscaled_val = device.getUnscaledValue(); - Logger.info("Unscaled value: {}", String.format("%.2f", Float.valueOf(unscaled_val))); + Logger.info("Unscaled value: {0.##}", Float.valueOf(unscaled_val)); if (type.isSigned()) { Assertions.assertTrue(unscaled_val >= -1 && unscaled_val < 1, "Unscaled range"); } else { Assertions.assertTrue(unscaled_val >= 0 && unscaled_val < 1, "Unscaled range"); } - + float scaled_val = device.getScaledValue(); - Logger.info("Scaled value: {}", String.format("%.2f", Float.valueOf(scaled_val))); + Logger.info("Scaled value: {0.##}", Float.valueOf(scaled_val)); if (type.isSigned()) { Assertions.assertTrue(scaled_val >= -voltage && scaled_val < voltage, "Scaled range"); } else { Assertions.assertTrue(scaled_val >= 0 && scaled_val < voltage, "Scaled range"); } - + SleepUtil.sleepMillis(100); } } diff --git a/diozero-core/src/test/java/com/diozero/util/RangeUtilTest.java b/diozero-core/src/test/java/com/diozero/util/RangeUtilTest.java index 2899ff3e..dbb05448 100644 --- a/diozero-core/src/test/java/com/diozero/util/RangeUtilTest.java +++ b/diozero-core/src/test/java/com/diozero/util/RangeUtilTest.java @@ -31,51 +31,162 @@ * #L% */ +import static com.diozero.util.RangeUtil.constrain; +import static com.diozero.util.RangeUtil.map; +import static com.diozero.util.RangeUtil.resolutionEndExclusive; +import static com.diozero.util.RangeUtil.resolutionEndInclusive; + import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import com.diozero.api.ServoTrim; + @SuppressWarnings("static-method") public class RangeUtilTest { private static final float DELTA_F = 0.001f; @Test - public void testMap() { - float from_low = 0f; - float from_high = 1f; + public void resolutionTest() { + final int _12bit_end_exclusive = resolutionEndExclusive(12); + final int _12bit_end_inclusive = resolutionEndInclusive(12); + + Assertions.assertEquals(4095, _12bit_end_exclusive); + Assertions.assertEquals(4096, _12bit_end_inclusive); + + Assertions.assertEquals(1f, map(_12bit_end_inclusive, _12bit_end_inclusive, 1f)); + Assertions.assertEquals(4095, map(1, 1f, _12bit_end_exclusive)); + Assertions.assertEquals(1, map(_12bit_end_exclusive, _12bit_end_exclusive, 1)); + Assertions.assertEquals(4095, map(1, 1, _12bit_end_exclusive)); + + final int MAX_VALUE = _12bit_end_exclusive; + final int[] on_off = { 0, MAX_VALUE }; + Assertions.assertEquals(1f, map(on_off[1] - on_off[0], MAX_VALUE, 1)); + Assertions.assertEquals(1f, (on_off[1] - on_off[0]) / (float) MAX_VALUE); + on_off[0] = MAX_VALUE / 2; + Assertions.assertEquals(0.5f, map(on_off[1] - on_off[0], MAX_VALUE, 1f), DELTA_F); + Assertions.assertEquals(0.5f, (on_off[1] - on_off[0]) / (float) MAX_VALUE, DELTA_F); + + float value = 0.5f; + // 2048 and not 2047 as 4095/2 gets rounded up + Assertions.assertEquals(2048, Math.round(value * MAX_VALUE)); + Assertions.assertEquals(2048, map(value, 1, MAX_VALUE)); + value = 1; + Assertions.assertEquals(MAX_VALUE, Math.round(value * MAX_VALUE)); + Assertions.assertEquals(MAX_VALUE, map(value, 1, MAX_VALUE)); + value = 0; + Assertions.assertEquals(0, Math.round(value * MAX_VALUE)); + Assertions.assertEquals(0, map(value, 1, MAX_VALUE)); + + final int CLOCK_FREQ = 25_000_000; + final int DEFAULT_PWM_FREQUENCY = 50; + int pwmFrequency = DEFAULT_PWM_FREQUENCY; + double prescale_flt = (((float) CLOCK_FREQ) / _12bit_end_exclusive / pwmFrequency) - 1; + System.out.println(prescale_flt); + prescale_flt = (((float) CLOCK_FREQ) / _12bit_end_inclusive / pwmFrequency) - 1; + System.out.println(); + + double periodUs = 1_000_000.0 / DEFAULT_PWM_FREQUENCY; + System.out.println(periodUs); + ServoTrim trim = ServoTrim.DEFAULT; + int off = (int) Math.round((trim.getMidPulseWidthUs() / periodUs) * MAX_VALUE); + int off_map = (int) Math.round(map(trim.getMidPulseWidthUs(), periodUs, MAX_VALUE)); + System.out.println("Mid: " + off + ", " + off_map); + + off = (int) Math.round((trim.getMaxPulseWidthUs() / periodUs) * MAX_VALUE); + off_map = (int) Math.round(map(trim.getMaxPulseWidthUs(), periodUs, MAX_VALUE)); + System.out.println("Max: " + off + ", " + off_map); + + off = (int) Math.round((trim.getMinPulseWidthUs() / periodUs) * MAX_VALUE); + off_map = (int) Math.round(map(trim.getMinPulseWidthUs(), periodUs, MAX_VALUE)); + System.out.println("Min: " + off + ", " + off_map); + } + + @Test + public void testMapFloat() { + float from_low = 0; + float from_high = 1; int to_low_int = 0; int to_high_int = 180; - Assertions.assertEquals(to_low_int, RangeUtil.map(from_low, from_low, from_high, to_low_int, to_high_int)); + Assertions.assertEquals(to_low_int, map(from_low, from_low, from_high, to_low_int, to_high_int)); Assertions.assertEquals((to_low_int + to_high_int) / 2, - RangeUtil.map((from_low + from_high) / 2, from_low, from_high, to_low_int, to_high_int)); - Assertions.assertEquals(to_high_int, RangeUtil.map(from_high, from_low, from_high, to_low_int, to_high_int)); - Assertions.assertEquals(to_low_int, RangeUtil.map(from_low - 10, from_low, from_high, to_low_int, to_high_int), - DELTA_F); - Assertions.assertEquals(to_high_int, RangeUtil.map(from_high + 10, from_low, from_high, to_low_int, to_high_int), + map((from_low + from_high) / 2, from_low, from_high, to_low_int, to_high_int)); + Assertions.assertEquals(to_high_int, map(from_high, from_low, from_high, to_low_int, to_high_int)); + Assertions.assertEquals(to_low_int, map(from_low - 10, from_low, from_high, to_low_int, to_high_int), DELTA_F); + Assertions.assertEquals(to_high_int, map(from_high + 10, from_low, from_high, to_low_int, to_high_int), DELTA_F); Assertions.assertEquals(-to_high_int, - RangeUtil.map(from_low - from_high, from_low, from_high, to_low_int, to_high_int, false), DELTA_F); + map(from_low - from_high, from_low, from_high, to_low_int, to_high_int, false), DELTA_F); Assertions.assertEquals(2 * to_high_int, - RangeUtil.map(2 * from_high, from_low, from_high, to_low_int, to_high_int, false), DELTA_F); + map(2 * from_high, from_low, from_high, to_low_int, to_high_int, false), DELTA_F); from_low = -90; from_high = 90; float to_low_float = 1f; float to_high_float = 2f; - Assertions.assertEquals(to_low_float, RangeUtil.map(from_low, from_low, from_high, to_low_float, to_high_float), - DELTA_F); + Assertions.assertEquals(to_low_float, map(from_low, from_low, from_high, to_low_float, to_high_float), DELTA_F); Assertions.assertEquals((to_low_float + to_high_float) / 2, - RangeUtil.map((from_low + from_high) / 2, from_low, from_high, to_low_float, to_high_float), DELTA_F); - Assertions.assertEquals(to_high_float, RangeUtil.map(from_high, from_low, from_high, to_low_float, to_high_float), + map((from_low + from_high) / 2, from_low, from_high, to_low_float, to_high_float), DELTA_F); + Assertions.assertEquals(to_high_float, map(from_high, from_low, from_high, to_low_float, to_high_float), + DELTA_F); + Assertions.assertEquals(to_low_float, map(from_low - 10, from_low, from_high, to_low_float, to_high_float), + DELTA_F); + Assertions.assertEquals(to_high_float, map(from_high + 10, from_low, from_high, to_low_float, to_high_float), DELTA_F); - Assertions.assertEquals(to_low_float, - RangeUtil.map(from_low - 10, from_low, from_high, to_low_float, to_high_float), DELTA_F); - Assertions.assertEquals(to_high_float, - RangeUtil.map(from_high + 10, from_low, from_high, to_low_float, to_high_float), DELTA_F); - Assertions.assertEquals(0.5, - RangeUtil.map(from_low - from_high, from_low, from_high, to_low_float, to_high_float, false), DELTA_F); - Assertions.assertEquals(2.5, RangeUtil.map(2 * from_high, from_low, from_high, to_low_float, to_high_float, false), + Assertions.assertEquals(0.5, map(from_low - from_high, from_low, from_high, to_low_float, to_high_float, false), DELTA_F); + Assertions.assertEquals(2.5, map(2 * from_high, from_low, from_high, to_low_float, to_high_float, false), + DELTA_F); + } + + @Test + public void testMapInt() { + Assertions.assertEquals(128, map(500, 1000, 256)); + Assertions.assertEquals(384, map(1500, 1000, 256, false)); + Assertions.assertEquals(128, map(0, -1000, 1000, 0, 256)); + Assertions.assertEquals(128, map(0.5f, 1f, 256f)); + Assertions.assertEquals(128, map(1.5f, 1f, 2f, 0, 256)); + Assertions.assertEquals(-128, map(0.5f, 1f, 2f, 0, 256, false)); + Assertions.assertEquals(128, map(0.5f, 1f, 256f)); + Assertions.assertEquals(128, map(1.5f, 1f, 2f, 0f, 256f)); + + int from_high = 90; + int to_high = 180; + + Assertions.assertEquals(0, map(0, from_high, to_high)); + Assertions.assertEquals(to_high / 2, map(from_high / 2, from_high, to_high)); + Assertions.assertEquals(to_high, map(from_high, from_high, to_high)); + Assertions.assertEquals(0, map(-10, from_high, to_high)); + Assertions.assertEquals(to_high, map(from_high + 10, from_high, to_high)); + Assertions.assertEquals(-to_high, map(-from_high, from_high, to_high, false)); + Assertions.assertEquals(2 * to_high, map(2 * from_high, from_high, to_high, false)); + + int from_low = 0; + from_high = 90; + int to_low = 0; + to_high = 180; + + Assertions.assertEquals(to_low, map(from_low, from_low, from_high, to_low, to_high)); + Assertions.assertEquals((to_low + to_high) / 2, + map((from_low + from_high) / 2, from_low, from_high, to_low, to_high)); + Assertions.assertEquals(to_high, map(from_high, from_low, from_high, to_low, to_high)); + Assertions.assertEquals(to_low, map(from_low - 10, from_low, from_high, to_low, to_high), DELTA_F); + Assertions.assertEquals(to_high, map(from_high + 10, from_low, from_high, to_low, to_high)); + Assertions.assertEquals(-to_high, map(from_low - from_high, from_low, from_high, to_low, to_high, false)); + Assertions.assertEquals(2 * to_high, map(2 * from_high, from_low, from_high, to_low, to_high, false)); + + from_low = -90; + from_high = 90; + to_low = 1000; + to_high = 2000; + Assertions.assertEquals(to_low, map(from_low, from_low, from_high, to_low, to_high), DELTA_F); + Assertions.assertEquals((to_low + to_high) / 2, + map((from_low + from_high) / 2, from_low, from_high, to_low, to_high)); + Assertions.assertEquals(to_high, map(from_high, from_low, from_high, to_low, to_high)); + Assertions.assertEquals(to_low, map(from_low - 10, from_low, from_high, to_low, to_high)); + Assertions.assertEquals(to_high, map(from_high + 10, from_low, from_high, to_low, to_high)); + Assertions.assertEquals(500, map(from_low - from_high, from_low, from_high, to_low, to_high, false)); + Assertions.assertEquals(2500, map(2 * from_high, from_low, from_high, to_low, to_high, false)); } @Test @@ -85,60 +196,59 @@ public void testServoRange() { float minus90_pulse_width_ms = 1f; float plus90_pulse_width_ms = 2f; + Assertions.assertEquals(map(min_pulse_width_ms, minus90_pulse_width_ms, plus90_pulse_width_ms, 0, 180, false), + -45); + Assertions.assertEquals(map(max_pulse_width_ms, minus90_pulse_width_ms, plus90_pulse_width_ms, 0, 180, false), + 225); Assertions.assertEquals( - RangeUtil.map(min_pulse_width_ms, minus90_pulse_width_ms, plus90_pulse_width_ms, 0, 180, false), -45); - Assertions.assertEquals( - RangeUtil.map(max_pulse_width_ms, minus90_pulse_width_ms, plus90_pulse_width_ms, 0, 180, false), 225); - Assertions.assertEquals( - RangeUtil.map(minus90_pulse_width_ms, minus90_pulse_width_ms, plus90_pulse_width_ms, 0, 180, false), 0); + map(minus90_pulse_width_ms, minus90_pulse_width_ms, plus90_pulse_width_ms, 0, 180, false), 0); Assertions.assertEquals( - RangeUtil.map(plus90_pulse_width_ms, minus90_pulse_width_ms, plus90_pulse_width_ms, 0, 180, false), - 180); - Assertions.assertEquals(RangeUtil.map((minus90_pulse_width_ms + plus90_pulse_width_ms) / 2, minus90_pulse_width_ms, + map(plus90_pulse_width_ms, minus90_pulse_width_ms, plus90_pulse_width_ms, 0, 180, false), 180); + Assertions.assertEquals(map((minus90_pulse_width_ms + plus90_pulse_width_ms) / 2, minus90_pulse_width_ms, plus90_pulse_width_ms, 0, 180, false), 90); - - Assertions.assertEquals(0, RangeUtil.map(-1, -1, 1, 0, 180)); - Assertions.assertEquals(90, RangeUtil.map(0, -1, 1, 0, 180)); - Assertions.assertEquals(180, RangeUtil.map(1, -1, 1, 0, 180)); - + + Assertions.assertEquals(0, map(-1, -1, 1, 0, 180)); + Assertions.assertEquals(90, map(0, -1, 1, 0, 180)); + Assertions.assertEquals(180, map(1, -1, 1, 0, 180)); + int servo_centre = 90; float value = -1; float pz_value = Math.round(servo_centre * value) + servo_centre; - Assertions.assertEquals(pz_value, RangeUtil.map(value, -1, 1, 0, 180), DELTA_F); + Assertions.assertEquals(pz_value, map(value, -1, 1, 0, 180), DELTA_F); value = 0; pz_value = Math.round(servo_centre * value) + servo_centre; - Assertions.assertEquals(pz_value, RangeUtil.map(value, -1, 1, 0, 180), DELTA_F); + Assertions.assertEquals(pz_value, map(value, -1, 1, 0, 180), DELTA_F); value = 1; pz_value = Math.round(servo_centre * value) + servo_centre; - Assertions.assertEquals(pz_value, RangeUtil.map(value, -1, 1, 0, 180), DELTA_F); + Assertions.assertEquals(pz_value, map(value, -1, 1, 0, 180), DELTA_F); } @Test public void testConstrain() { int min = -90; int max = 90; - Assertions.assertEquals(min, RangeUtil.constrain(min, min, max)); - Assertions.assertEquals(min, RangeUtil.constrain(min - 1, min, max)); - Assertions.assertEquals(max, RangeUtil.constrain(max, min, max)); - Assertions.assertEquals(max, RangeUtil.constrain(max + 1, min, max)); - Assertions.assertEquals(max / 2, RangeUtil.constrain(max / 2, min, max)); + Assertions.assertEquals(min, constrain(min, min, max)); + Assertions.assertEquals(min, constrain(min - 1, min, max)); + Assertions.assertEquals(max, constrain(max, min, max)); + Assertions.assertEquals(max, constrain(max + 1, min, max)); + Assertions.assertEquals(max / 2, constrain(max / 2, min, max)); } - + @Test public void testMax30102Map() { float min_current = 0f; float max_current = 51f; int min_val = 0; int max_val = 255; - - Assertions.assertEquals(0x00, RangeUtil.map(0.0f, min_current, max_current, min_val, max_val)); - Assertions.assertEquals(0x01, RangeUtil.map(0.2f, min_current, max_current, min_val, max_val)); - Assertions.assertEquals(0x02, RangeUtil.map(0.4f, min_current, max_current, min_val, max_val)); - Assertions.assertEquals(0x0f, RangeUtil.map(3.0f, min_current, max_current, min_val, max_val)); - Assertions.assertEquals(0x1f, RangeUtil.map(6.2f, min_current, max_current, min_val, max_val)); - Assertions.assertEquals(0x24, RangeUtil.map(7.1f, min_current, max_current, min_val, max_val)); - Assertions.assertEquals(0x3f, RangeUtil.map(12.6f, min_current, max_current, min_val, max_val)); - Assertions.assertEquals(0x7f, RangeUtil.map(25.4f, min_current, max_current, min_val, max_val)); - Assertions.assertEquals(0xff, RangeUtil.map(51f, min_current, max_current, min_val, max_val)); + + Assertions.assertEquals(0x00, map(0.0f, min_current, max_current, min_val, max_val)); + Assertions.assertEquals(0x01, map(0.2f, min_current, max_current, min_val, max_val)); + Assertions.assertEquals(0x02, map(0.4f, min_current, max_current, min_val, max_val)); + Assertions.assertEquals(0x0f, map(3.0f, min_current, max_current, min_val, max_val)); + Assertions.assertEquals(0x1f, map(6.2f, min_current, max_current, min_val, max_val)); + Assertions.assertEquals(0x24, map(7.1f, min_current, max_current, min_val, max_val)); + Assertions.assertEquals(0x3f, map(12.6f, min_current, max_current, min_val, max_val)); + Assertions.assertEquals(0x7f, map(25.4f, min_current, max_current, min_val, max_val)); + Assertions.assertEquals(0xff, map(51f, min_current, max_current, min_val, max_val)); } } diff --git a/diozero-sampleapps/src/main/java/com/diozero/sampleapps/HCSR04Test.java b/diozero-sampleapps/src/main/java/com/diozero/sampleapps/HCSR04Test.java index d1540da1..42bf2b50 100644 --- a/diozero-sampleapps/src/main/java/com/diozero/sampleapps/HCSR04Test.java +++ b/diozero-sampleapps/src/main/java/com/diozero/sampleapps/HCSR04Test.java @@ -65,7 +65,7 @@ public static void test(int triggerPin, int echoPin) { try (HCSR04 device = new HCSR04(triggerPin, echoPin)) { for (int i = 0; i < ITERATIONS; i++) { double distance_cm = device.getDistanceCm(); - Logger.info("Distance = {} cm", String.format("%.3f", Double.valueOf(distance_cm))); + Logger.info("Distance = {0.###} cm", Double.valueOf(distance_cm)); SleepUtil.sleepSeconds(0.5); } } catch (RuntimeIOException ex) { diff --git a/diozero-sampleapps/src/main/java/com/diozero/sampleapps/McpAdcTest.java b/diozero-sampleapps/src/main/java/com/diozero/sampleapps/McpAdcTest.java index 1a6afcaf..b1d04bdf 100644 --- a/diozero-sampleapps/src/main/java/com/diozero/sampleapps/McpAdcTest.java +++ b/diozero-sampleapps/src/main/java/com/diozero/sampleapps/McpAdcTest.java @@ -75,7 +75,7 @@ public static void test(McpAdc.Type type, int controller, int chipSelect, int pi try (McpAdc adc = new McpAdc(type, controller, chipSelect)) { for (int i = 0; i < ITERATIONS; i++) { float v = adc.getValue(pin); - Logger.info("Value: {}", String.format("%.2f", Float.valueOf(v))); + Logger.info("Value: {0.##}", Float.valueOf(v)); SleepUtil.sleepSeconds(0.5); } } catch (RuntimeIOException ioe) { diff --git a/diozero-sampleapps/src/main/java/com/diozero/sampleapps/TMP36Test.java b/diozero-sampleapps/src/main/java/com/diozero/sampleapps/TMP36Test.java index 77f2d2ca..9f383f9a 100644 --- a/diozero-sampleapps/src/main/java/com/diozero/sampleapps/TMP36Test.java +++ b/diozero-sampleapps/src/main/java/com/diozero/sampleapps/TMP36Test.java @@ -77,7 +77,7 @@ public static void test(McpAdc.Type type, int chipSelect, int pin, float tempOff try (McpAdc adc = new McpAdc(type, chipSelect, vRef); TMP36 tmp36 = new TMP36(adc, pin, tempOffset)) { for (int i = 0; i < ITERATIONS; i++) { double tmp = tmp36.getTemperature(); - Logger.info("Temperature: {}", String.format("%.2f", Double.valueOf(tmp))); + Logger.info("Temperature: {0.##}", Double.valueOf(tmp)); SleepUtil.sleepSeconds(.5); } } catch (RuntimeIOException ioe) { diff --git a/diozero-sampleapps/src/main/java/com/diozero/sampleapps/TSL2561Test.java b/diozero-sampleapps/src/main/java/com/diozero/sampleapps/TSL2561Test.java index 75bf639c..6eeb13e2 100644 --- a/diozero-sampleapps/src/main/java/com/diozero/sampleapps/TSL2561Test.java +++ b/diozero-sampleapps/src/main/java/com/diozero/sampleapps/TSL2561Test.java @@ -65,7 +65,7 @@ public static void test(int controller) { for (int i = 0; i < ITERATIONS; i++) { double lux = tsl2561.getLuminosity(); - Logger.info("Luminosity={} Lux", String.format("%.2f", Double.valueOf(lux))); + Logger.info("Luminosity={0.##} Lux", Double.valueOf(lux)); SleepUtil.sleepSeconds(0.5); } diff --git a/diozero-sampleapps/src/main/java/com/diozero/sampleapps/perf/GpioReadPerfTest.java b/diozero-sampleapps/src/main/java/com/diozero/sampleapps/perf/GpioReadPerfTest.java index f28e6481..58a4e941 100644 --- a/diozero-sampleapps/src/main/java/com/diozero/sampleapps/perf/GpioReadPerfTest.java +++ b/diozero-sampleapps/src/main/java/com/diozero/sampleapps/perf/GpioReadPerfTest.java @@ -75,8 +75,8 @@ public static void test(int pin, int iterations) { } long duration_ns = System.nanoTime() - start_nano; - Logger.info("Duration for {} iterations: {}s", Integer.valueOf(iterations), - String.format("%.4f", Float.valueOf(((float) duration_ns) / 1000 / 1000 / 1000))); + Logger.info("Duration for {} iterations: {0.###}s", Integer.valueOf(iterations), + Float.valueOf(((float) duration_ns) / 1000 / 1000 / 1000)); } } catch (RuntimeIOException e) { Logger.error(e, "Error: {}", e); diff --git a/diozero-sampleapps/src/main/java/com/diozero/sampleapps/perf/MCP23017PerfTest.java b/diozero-sampleapps/src/main/java/com/diozero/sampleapps/perf/MCP23017PerfTest.java index d4083e11..3acbd02d 100644 --- a/diozero-sampleapps/src/main/java/com/diozero/sampleapps/perf/MCP23017PerfTest.java +++ b/diozero-sampleapps/src/main/java/com/diozero/sampleapps/perf/MCP23017PerfTest.java @@ -38,8 +38,8 @@ import com.diozero.devices.mcp23xxx.MCP23x17; /** - * MCP23017 performance test application. To run (note this hangs the Pi when - * using wiringPi provider): + * MCP23017 performance test application. To run (note this hangs the Pi when using + * wiringPi provider): *