Code Monkey home page Code Monkey logo

pi4j-kotlin's Introduction

Pi4J - Kotlin

Kotlin Interface & DSL for Pi4J V2
For Pi4J V1 Kotlin Bindings, check Pi4K

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))
        }
      }
    }
  }
}

pi4j-kotlin's People

Contributors

mhashim6 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

pi4j-kotlin's Issues

Pi5 and gpiod

Pi4j doesn't load gpiod when used with this dependencies :

implementation("com.pi4j:pi4j-ktx:2.4.0")
implementation("com.pi4j:pi4j-core:2.6.0")
implementation("com.pi4j:pi4j-plugin-raspberrypi:2.6.0")
implementation("com.pi4j:pi4j-plugin-gpiod:2.6.0")
implementation("com.pi4j:pi4j-library-gpiod:2.6.0")

This seems to always try to load pigpio and tell me that it's not available.

I can Run Hello_World Using Kotlin and Java_17 But I can't run CoroutinesExample

Hi,

I have Rpi4 8gb with Java-17.0.11
I can run a hello world remotely in Kotlin but I get the error below when I try to run the CoroutinesExample example from github

[main] INFO com.pi4j.util.Console - 
[main] WARN com.pi4j.library.pigpio.impl.PiGpioNativeImpl - PIGPIO ERROR: PI_INIT_FAILED; pigpio initialisation failed
Exception in thread "main" java.lang.reflect.UndeclaredThrowableException
	at jdk.proxy1/jdk.proxy1.$Proxy20.create(Unknown Source)
	at com.pi4j.context.Context.create(Context.java:317)
	at com.pi4j.internal.IOCreator.create(IOCreator.java:58)
	at com.pi4j.internal.IOCreator.create(IOCreator.java:106)
	at MinimalExampleKt.main(MinimalExample.kt:159)
	at MinimalExampleKt.main(MinimalExample.kt)
Caused by: java.lang.reflect.InvocationTargetException
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at com.pi4j.provider.impl.ProviderProxyHandler.invoke(ProviderProxyHandler.java:100)
	... 6 more
Caused by: com.pi4j.library.pigpio.PiGpioException: PIGPIO ERROR: PI_INIT_FAILED; pigpio initialisation failed
	at com.pi4j.library.pigpio.impl.PiGpioBase.validateResult(PiGpioBase.java:265)
	at com.pi4j.library.pigpio.impl.PiGpioBase.validateResult(PiGpioBase.java:251)
	at com.pi4j.library.pigpio.impl.PiGpioNativeImpl.gpioInitialise(PiGpioNativeImpl.java:98)
	at com.pi4j.library.pigpio.PiGpio.initialize(PiGpio.java:159)
	at com.pi4j.plugin.pigpio.provider.gpio.digital.PiGpioDigitalInputProviderImpl.create(PiGpioDigitalInputProviderImpl.java:60)
	at com.pi4j.plugin.pigpio.provider.gpio.digital.PiGpioDigitalInputProviderImpl.create(PiGpioDigitalInputProviderImpl.java:41)
	... 11 more

Process finished with exit code 1

I can't put java_11 in rpi4
Captura de tela 2024-07-17 225436

Coroutines Support

Some users on reddit thought it would be nice to integrate coroutines in the DSL.
I was thinking of introducing a pi4jAsync sister DSL in which user can call suspended functions.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.