Pi4J - Kotlin
Kotlin Interface & DSL for Pi4J.
For Pi4J V1 Kotlin Bindings, check Pi4K (no longer supported).
Documentation
Full documentation can be found on the website
Modules
| Module | Artifact | Description |
|---|---|---|
lib |
pi4j-ktx |
Core Kotlin DSL for Pi4J (digital/analog GPIO, PWM, I2C) |
lib-serial |
pi4j-ktx-serial |
Serial communication DSL wrapping jSerialComm |
Example
This is a minimal working example, make sure to check it out for full introduction and comments.
const val PIN_BUTTON = 24 // PIN 18 = BCM 24
const val PIN_LED = 22 // PIN 15 = BCM 22
var pressCount = 0
console {
title("<-- The Pi4J Project -->", "Minimal Example project")
pi4j {
digitalInput(PIN_BUTTON) {
id("button")
name("Press button")
pull(PullResistance.PULL_DOWN)
debounce(3000L)
}.onLow {
pressCount++
+"Button was pressed for the ${pressCount}th time"
}
digitalOutput(PIN_LED) {
id("led")
name("LED Flasher")
shutdown(DigitalState.LOW)
initial(DigitalState.LOW)
}.run {
while (pressCount < 5) {
+"LED ${state()}"
toggle()
sleep(500L / (pressCount + 1))
}
}
}
}
Serial
Serial communication is provided by the pi4j-ktx-serial module, which wraps jSerialComm (independent of Pi4J core since serial was removed in Pi4J 4.0.0).
Gradle setup:
dependencies {
implementation("com.pi4j:pi4j-ktx-serial:4.0.0")
implementation("com.fazecast:jSerialComm:2.11.0")
}
Usage:
import com.pi4j.ktx.io.serial.serial
import com.pi4j.ktx.io.serial.open
serial("/dev/ttyS0") {
baudRate = 115200
dataBits = 8
// stopBits, parity, flowControl also available
}.open {
// `this` is a jSerialComm SerialPort
// port is automatically closed when the block exits
outputStream.write("Hello\n".toByteArray())
val response = inputStream.bufferedReader().readLine()
}