Skip to content

Commit

Permalink
Should now adhere to system level configuration changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mores committed Sep 16, 2024
1 parent 20d71f2 commit 738a493
Showing 1 changed file with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 738a493

Please sign in to comment.