Java 22+ library for interacting with GPIO and hardware interfaces
MIT License
This Java library is intended to be used with any kind of hardware, that exposes GPIO interface to user (e.g. RaspberryPi, OrangePi, etc.). With new FFM API that were released in a recent versions of Java, you can work with low level stuff (like linux kernel syscalls) within your app. It is fast and reliable native interface provided by JVM. No more JNI and JNA!
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.DigitalSmile:gpio:{version}'
}
var spiBus = GPIOBoard.ofSPI(0, SPIMode.MODE_0, 20_000_000);
var rst = GPIOBoard.ofPin(17, Direction.OUTPUT);
var busy = GPIOBoard.ofPin(24, Direction.INPUT);
var dc = GPIOBoard.ofPin(25, Direction.OUTPUT);
var pwr = GPIOBoard.ofPin(18, Direction.OUTPUT);
pwr.write(State.HIGH);
// direct lookup for state change
while(busy.read().equals(State.HIGH)){
Thread.sleep(10);
}
// or usage of edge event detection
pwr.startEventDetection(PinEvent.FALLING, (eventList ->{
// do stuff on events captured
}));
dc.write(State.LOW);
spiBus.sendByteData(new byte[] { 1 },false);
var i2c = GPIOBoard.ofI2C(0);
var scanned = i2c.scan();
var status = scanned.get(0x55);
if (status.equals(I2CStatus.AVAILABLE)) {
i2c.selectAddress(0x55);
var read = i2c.read(0x55);
//do stuff with data read
}
var pwmPin = GPIOBoard.ofPWMBus(0);
pwmPin.configure(25_000, 40, PWMPolarity.NORMAL);
pwmPin.enable();
// set rotation speed, e.g. fan
var percentages = new int[]{100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0};
for (int percent : percentages) {
pwmPin.setSpeed(percent);
Thread.sleep(1_000);
}