Popularity
3.9
Growing
Activity
0.0
-
50
4
0

Programming language: Kotlin
Tags: Raspberry Pi    
Latest version: v0.1
Add another 'Raspberry Pi' Library

README

Pi4K

Pi4J Kotlin bindings.
Awesome Kotlin Badge

Demo

Make sure to check Examples.kt.

fun blink() = digitalOutput(GPIO_01) {
    while (true) {
        toggle()
        delay(500)
    }
}
  • Pulse:

    digitalOutput(GPIO_01).pulse(600)
    
  • Triggers:

    val input = digitalInput(GPIO_04)
    val output = digitalOutput(GPIO_05)
    input.trigger(output) //output is HIGH when input is HIGH.
    // input.trigger(output, whenState = PinState.LOW, targetState = PinState.HIGH) //output is HIGH when input is LOW.
    
  • Pulse triggers:

    val input = digitalInput(GPIO_04)
    val output = digitalOutput(GPIO_05)
    input.triggerPulse(output, PinState.HIGH, millis = 1000) //when input is HIGH, output is HIGH for 1 second
    
  • State change listeners:

    digitalInput(GPIO_06).onStateChange { event, edge, state -> println(state) }
    
  • PWM

    pwm(clockDivisor = 500, mode = PWM_MODE_MS, range = 1000)
    pwmOutput(GPIO_01, 400)
    
  • Software PWM

    softPwmOutput(GPIO_01, 400)
    
  • Analog counterparts are also available.

  • When done with gpio, housekeeping is necessary;

gpioShutdown()

Testing

Tests were simulated on my machine using the PI4J_PLATFORM=Simulated Environment variable.
However, I was not able to mock/test AnalogPins bindings. Contributions are always welcome.

Dependency

  • Gradle

Add it in your root build.gradle at the end of repositories:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Add the dependency:

dependencies {
    implementation 'com.github.mhashim6:Pi4K:0.1'
}

  • Maven xml <repositories> <repository> <id>jitpack.io</id> <url>https://jitpack.io</url> </repository> </repositories> Add the dependency: xml <dependency> <groupId>com.github.mhashim6</groupId> <artifactId>Pi4K</artifactId> <version>0.1</version> </dependency>