From 7dc367528d959d219bd3056b7c26f36018128326 Mon Sep 17 00:00:00 2001 From: Tom Aarts Date: Sat, 7 Nov 2026 18:25:12 -0600 Subject: [PATCH] bmp280 i2c already used the new cmd path, I migrateed bmp280 spi to the new path. Also updated bmp280s and sg90 servo expanded parm, -a became -address --- .../pi4j/devices/bmp280/BMP280I2cExample.java | 25 +-- .../pi4j/devices/bmp280/BMP280SpiExample.java | 155 ++++++++---------- .../java/com/pi4j/devices/bmp280/README.md | 37 ++++- .../java/com/pi4j/devices/sg90/Sg90App.java | 2 +- 4 files changed, 109 insertions(+), 110 deletions(-) diff --git a/src/main/java/com/pi4j/devices/bmp280/BMP280I2cExample.java b/src/main/java/com/pi4j/devices/bmp280/BMP280I2cExample.java index 0a45268..58a99f3 100644 --- a/src/main/java/com/pi4j/devices/bmp280/BMP280I2cExample.java +++ b/src/main/java/com/pi4j/devices/bmp280/BMP280I2cExample.java @@ -2,9 +2,9 @@ import com.pi4j.Pi4J; import com.pi4j.context.Context; +import com.pi4j.drivers.sensor.environment.bmx280.Bmx280Driver; import com.pi4j.examples.utils.commandhandler.DeviceCommandHandler; import com.pi4j.examples.utils.commandhandler.IntegerParameter; -import com.pi4j.drivers.sensor.environment.bmx280.Bmx280Driver; import com.pi4j.io.i2c.I2C; import java.text.DecimalFormat; @@ -29,35 +29,36 @@ static void main(String[] args) { DeviceCommandHandler commandHandler = new DeviceCommandHandler<>(settings -> new Bmx280Driver(pi4j.create(I2C.newConfigBuilder() - .device((Integer) settings.get("a")) - .bus((Integer) settings.get("b"))))); + .device((Integer) settings.get("address")) + .bus((Integer) settings.get("bus"))))); commandHandler.configuration( - "a", + "address", "Set the BMx280 I2C Device Address", new IntegerParameter(0, 127), Bmx280Driver.ADDRESS_BMP_280); commandHandler.configuration( - "b", + "bus", "Set the BMx280 I2C Bus", new IntegerParameter(0, 127), DEFAULT_BUS); - commandHandler.action("r", "Read measurements (default)", device -> { + commandHandler.action("reset", "Reset the sensor", device -> { + device.reset(); + }); + + commandHandler.action("read", "Read measurements (default)", device -> { Bmx280Driver.Measurement measurement = device.readMeasurement(); - System.out.println("Temperature °C: " + FORMAT.format(measurement.getTemperature()) ); - System.out.println("Temperature °F: " + FORMAT.format(measurement.getTemperature() * 1.8 + 32) ); + System.out.println("Temperature °C: " + FORMAT.format(measurement.getTemperature())); + System.out.println("Temperature °F: " + FORMAT.format(measurement.getTemperature() * 1.8 + 32)); System.out.println("Pressure Pa: " + FORMAT.format(measurement.getPressure())); System.out.println("Pressure mbar: " + FORMAT.format(measurement.getPressure() / 100)); System.out.println("Pressure InHg: " + FORMAT.format(measurement.getPressure() / 3386)); - System.out.println("Humidity: " + FORMAT.format(measurement.getHumidity()) + " %"); }); - - - commandHandler.setDefault("r"); + commandHandler.setDefault("read"); commandHandler.process(args); // Shutdown Pi4J diff --git a/src/main/java/com/pi4j/devices/bmp280/BMP280SpiExample.java b/src/main/java/com/pi4j/devices/bmp280/BMP280SpiExample.java index 66120e9..ff3bd0e 100644 --- a/src/main/java/com/pi4j/devices/bmp280/BMP280SpiExample.java +++ b/src/main/java/com/pi4j/devices/bmp280/BMP280SpiExample.java @@ -3,16 +3,19 @@ import com.pi4j.Pi4J; import com.pi4j.context.Context; import com.pi4j.drivers.sensor.environment.bmx280.Bmx280Driver; +import com.pi4j.examples.utils.commandhandler.DeviceCommandHandler; +import com.pi4j.examples.utils.commandhandler.IntegerParameter; import com.pi4j.io.spi.Spi; import com.pi4j.io.spi.SpiBus; import com.pi4j.io.spi.SpiMode; -import com.pi4j.util.Console; import java.text.DecimalFormat; - +import java.text.NumberFormat; public class BMP280SpiExample { + private static final int DEFAULT_BUS = 0x0; + private static final NumberFormat FORMAT = new DecimalFormat("0.###"); /** * Sample application using BMP280 sensor chip. @@ -23,101 +26,71 @@ public class BMP280SpiExample { * otherwise provider setup is used * @throws java.lang.Exception if any. */ - public static void main(String[] args) throws Exception - { + static void main(String[] args) throws Exception { + System.out.println("BMP 280 I2C Example"); + System.out.println(); - int chipSelect = 0; - SpiBus spiBus = SpiBus.BUS_0; Context pi4j = Pi4J.newAutoContext(); - - // ------------------------------------------------------------ - // Initialize the Pi4J Runtime Context - // ------------------------------------------------------------ - // Before you can use Pi4J you must initialize a new runtime - // context. - // - // The 'Pi4J' static class includes a few helper context - // creators for the most common use cases. The 'newAutoContext()' - // method will automatically load all available Pi4J - // extensions found in the application's classpath which - // may include 'Platforms' and 'I/O Providers' - - - // print installed providers - System.out.println("----------------------------------------------------------"); - System.out.println("PI4J PROVIDERS"); - System.out.println("----------------------------------------------------------"); - pi4j.providers().describe().print(System.out); - System.out.println("----------------------------------------------------------"); - - - final Console console = new Console(); - console.print("=============================================================="); - console.print("startup BMP280SpiExample "); - console.print("=============================================================="); - - - String helpString = " parms: NONE \n " ; - for (int i = 0; i < args.length; i++) { - String o = args[i]; - if (o.contentEquals("-h")) { - console.println(helpString); - System.exit(39); - } else { - console.println(" !!! Invalid Parm " + args); - console.println(helpString); - System.exit(42); - } - } - - Spi spiDev = createSPIDevice(pi4j, spiBus, chipSelect); - - Bmx280Driver bmpDev = new Bmx280Driver(spiDev); - - - - bmpDev.reset(); - - DecimalFormat df = new DecimalFormat("0.###"); - - Bmx280Driver.Measurement measurement = bmpDev.readMeasurement(); - - - console.println(" Setup ----------------------------------------------------------"); - - console.println(" Temperature C = " + df.format(measurement.getTemperature()) ); - - console.println(" Temperature F = " + df.format(measurement.getTemperature() * 1.8 + 32) ); - - console.println(" Pressure Pa = " + df.format(measurement.getPressure())); - - console.println(" Pressure mbar = " + df.format(measurement.getPressure() / 100)); - - console.println(" Pressure InHg = " + df.format(measurement.getPressure() / 3386)); - - console.println(" Dev Spi detail SpiBus : " + spiBus + " chipSelect : " + chipSelect); + DeviceCommandHandler commandHandler = new DeviceCommandHandler<>(settings -> + new Bmx280Driver(pi4j.create(Spi.newConfigBuilder() + .id("SPI" + settings.get("bus") + "_BMP280") + .name("SENSOR") + .baud(Spi.DEFAULT_BAUD) // Max 10MHz + .baud(Spi.DEFAULT_BAUD) // Max 10MHz + .mode(SpiMode.MODE_0) + .bus((Integer) settings.get("bus")) + .channel((Integer) settings.get("chipdelect"))))); + + commandHandler.configuration( + "chipselect", + "Set the BMx280 SPI chip select", + new IntegerParameter(0, 8), + Spi.DEFAULT_CHANNEL); + + commandHandler.configuration( + "bus", + "Set the BMx280 SPI Bus", + new IntegerParameter(0, 6), + DEFAULT_BUS); + + commandHandler.action("reset", "Reset the sensor", device -> { + device.reset(); + }); + + commandHandler.action("read", "Read measurements (default)", device -> { + Bmx280Driver.Measurement measurement = device.readMeasurement(); + + System.out.println("Temperature °C: " + FORMAT.format(measurement.getTemperature())); + System.out.println("Temperature °F: " + FORMAT.format(measurement.getTemperature() * 1.8 + 32)); + System.out.println("Pressure Pa: " + FORMAT.format(measurement.getPressure())); + System.out.println("Pressure mbar: " + FORMAT.format(measurement.getPressure() / 100)); + System.out.println("Pressure InHg: " + FORMAT.format(measurement.getPressure() / 3386)); + }); + + commandHandler.setDefault("read"); + commandHandler.process(args); // Shutdown Pi4J pi4j.shutdown(); } -/** - * Use the state from the Sensor config object and the state pi4j to create - * a BMP280 device instance - */ -private static Spi createSPIDevice(Context pi4j, SpiBus spiBus, int channel) { - var spiConfig = Spi.newConfigBuilder(pi4j) - .id("SPI" + spiBus + "_BMP280") - .name("D/A converter") - .bus(spiBus) - .channel(channel) - //1 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 - //b b b b b b R T n n n n W A u2 u1 u0 p2 p1 p0 m m - // .flags(0b0000000000000000100000L) // MODE0, ux GPIO not used for chip select - .baud(Spi.DEFAULT_BAUD) // Max 10MHz - .mode(SpiMode.MODE_0) - .build(); - return pi4j.create(spiConfig); -} + /** + * Use the state from the Sensor config object and the state pi4j to create + * a BMP280 device instance + */ + private static Spi createSPIDevice(Context pi4j, SpiBus spiBus, int channel) { + var spiConfig = Spi.newConfigBuilder(pi4j) + .id("SPI" + spiBus + "_BMP280") + .name("D/A converter") + .bus(spiBus) + .channel(channel) + //1 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 + //b b b b b b R T n n n n W A u2 u1 u0 p2 p1 p0 m m + // .flags(0b0000000000000000100000L) // MODE0, ux GPIO not used for chip select + .baud(Spi.DEFAULT_BAUD) // Max 10MHz + .mode(SpiMode.MODE_0) + .build(); + return pi4j.create(spiConfig); + } } \ No newline at end of file diff --git a/src/main/java/com/pi4j/devices/bmp280/README.md b/src/main/java/com/pi4j/devices/bmp280/README.md index 1cdeaec..8d48b4e 100644 --- a/src/main/java/com/pi4j/devices/bmp280/README.md +++ b/src/main/java/com/pi4j/devices/bmp280/README.md @@ -18,23 +18,48 @@ I2C 1. ./mvnw clean package 2. cd target/distribution 3. sudo ./runBMP280I2C.sh - Args if bus and or address must be set - -b 0x01 -a 0x77 + Args + -bus + -address + - application will start and wait for input + + sudo ./runBMP280I2C.sh -bus 1 -address 0x77 - ( or -address 119) + +Commands +| Input | | +|---------|-------------------------------| +| read | read sensor and print results | +| help | display help text | +| exit | program exit | +| reset | reset chip | + The SPI connection per the Datasheet supports MODE0 or MODE3. SPI 1. ./mvnw clean package 2. cd target/distribution -3. sudo ./runBMP280SPI.sh +3. sudo ./runBMP280SPI.sh args + + Args + -chipselect + -bus 0..6 + - application will start and wait for input + +sudo ./runBMP280SPI.sh -bus 0 -chipselect 0 - ( or -address 119) +Commands +| Input | | +|---------|-------------------------------| +| read | read sensor and print results | +| help | display help text | +| exit | program exit | +| reset | reset chip | -Will create the BMP280 device to create a BMP280Device instance Call the various temperature and pressure methods +Will create the BMP280 device driver then Call the various temperature and pressure methods defined in the interface. -No parameters are required. However, if 'any' parameter value is to be supplied: -parms: -b hex value bus -a hex value address The file BMP280.pdf documents a method of connecting an Adafruit BMP280 to a Pi. diff --git a/src/main/java/com/pi4j/devices/sg90/Sg90App.java b/src/main/java/com/pi4j/devices/sg90/Sg90App.java index c87bb8a..423049d 100644 --- a/src/main/java/com/pi4j/devices/sg90/Sg90App.java +++ b/src/main/java/com/pi4j/devices/sg90/Sg90App.java @@ -116,7 +116,7 @@ static void waitChange(long c) { if ((nextDegree < 0.0) || (nextDegree > 180.0)) { console.println(" degree must be in the range 0..180"); } else { - sg90.setServoAngle(nextDegree); + sg90.setAngle(nextDegree); } } else { break;