diff --git a/libraries/pi4j-library-pigpio/src/main/java/com/pi4j/library/pigpio/impl/PiGpioNativeImpl.java b/libraries/pi4j-library-pigpio/src/main/java/com/pi4j/library/pigpio/impl/PiGpioNativeImpl.java index 23ed9e6a..23435c38 100644 --- a/libraries/pi4j-library-pigpio/src/main/java/com/pi4j/library/pigpio/impl/PiGpioNativeImpl.java +++ b/libraries/pi4j-library-pigpio/src/main/java/com/pi4j/library/pigpio/impl/PiGpioNativeImpl.java @@ -33,8 +33,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.File; import java.util.Arrays; import java.util.Objects; +import java.util.Scanner; import static com.pi4j.library.pigpio.PiGpioConst.PI_IF_DISABLE_FIFO; import static com.pi4j.library.pigpio.PiGpioConst.PI_IF_DISABLE_SOCK; @@ -49,6 +51,8 @@ public class PiGpioNativeImpl extends PiGpioBase implements PiGpio { protected Logger logger = LoggerFactory.getLogger(this.getClass()); + private int SPI_BUFFSIZ = 4096; + private static final PiGpioNativeImpl instance; static { instance = new PiGpioNativeImpl(); @@ -105,6 +109,16 @@ public int gpioInitialise() { result = PIGPIO.gpioInitialise(); validateResult(result); + try { + Scanner scanner = new Scanner(new File("/sys/module/spidev/parameters/bufsiz")); + if (scanner.hasNextInt()) { + SPI_BUFFSIZ = scanner.nextInt(); + } + logger.trace("[INITIALIZE] -> SPI_BUFFSIZ={}", SPI_BUFFSIZ); + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + // initialization successful this.initialized = true; logger.debug("[INITIALIZE] -- INITIALIZED SUCCESSFULLY"); @@ -1456,14 +1470,13 @@ public int spiWrite(int handle, byte[] data, int offset, int length) { // write data array to SPI bus/channel int result = 0; byte[] someData = Arrays.copyOfRange(data, offset, length); - int chunksize = 4096; int start = 0; while (start < someData.length) { - int end = Math.min(someData.length, start + chunksize); + int end = Math.min(someData.length, start + SPI_BUFFSIZ); byte[] chunk = Arrays.copyOfRange(someData, start, end); result += PIGPIO.spiWrite(handle, chunk, 0, chunk.length ); logger.trace("[SPI::WRITE] <- HANDLE={}; SUCCESS={}", handle, (result>=0)); - start += chunksize; + start += SPI_BUFFSIZ; } validateResult(result, false); return result;