Kotlin DSL for Pi4J V2
Find a file
2024-07-25 15:09:58 +02:00
.github/workflows ci gradle version 7.4.2 2022-11-08 21:07:44 +02:00
.run default test config 2022-12-28 21:30:47 +02:00
example Move version definition to root level 2024-07-25 15:09:58 +02:00
gradle/wrapper project structure 2022-09-11 00:06:39 +02:00
lib Move version definition to root level 2024-07-25 15:09:58 +02:00
.gitignore ignore .idea 2022-09-11 13:00:26 +02:00
build.gradle.kts Move version definition to root level 2024-07-25 15:09:58 +02:00
gradle.properties restore project level gradle.properties 2022-09-14 19:19:31 +02:00
gradlew project structure 2022-09-11 00:06:39 +02:00
gradlew.bat project structure 2022-09-11 00:06:39 +02:00
LICENSE.txt License 2022-09-11 13:23:53 +02:00
README.md Fix failing tests 2024-07-25 14:40:53 +02:00
settings.gradle.kts lib identity: com.pi4j.ktx 2022-09-12 19:13:26 +02:00

Pi4J - Kotlin

Kotlin Interface & DSL for Pi4J V2
For Pi4J V1 Kotlin Bindings, check Pi4K (no longer supported).

Awesome Kotlin Badge Maven Central License GitHub Actions build state

Documentation

Full documentation can be found on the website

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)
        piGpioProvider()
      }.onLow {
        pressCount++
        +"Button was pressed for the ${pressCount}th time"
      }
    }
    
    digitalOutput(PIN_LED) {
        id("led")
        name("LED Flasher")
        shutdown(DigitalState.LOW)
        initial(DigitalState.LOW)
        piGpioProvider()
      }.run {
        while (pressCount < 5) {
          +"LED ${state()}"
          toggle()
          sleep(500L / (pressCount + 1))
        }
      }
    }
  }
}