Skip to content

Commit 0fe2ae4

Browse files
author
Muhammad Hashim
committed
Updated pi4j to v4.0.0 with Java25. New artefact wrapping jSerialComm
Signed-off-by: Muhammad Hashim <muhammad.hashim@celfocus.com>
1 parent 2e4db07 commit 0fe2ae4

29 files changed

Lines changed: 534 additions & 382 deletions

File tree

.github/workflows/build.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ jobs:
77
build-and-test:
88
runs-on: ubuntu-latest
99
steps:
10-
- uses: actions/checkout@v2
11-
- uses: gradle/gradle-build-action@v2
10+
- uses: actions/checkout@v4
11+
- uses: actions/setup-java@v4
1212
with:
13-
gradle-version: 7.4.2
13+
distribution: 'temurin'
14+
java-version: '25'
15+
- uses: gradle/actions/setup-gradle@v4
1416
- name: build project
1517
run: ./gradlew build
1618
- name: Run tests
17-
run: ./gradlew lib:check
19+
run: ./gradlew lib:check lib-serial:check

.github/workflows/release.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ jobs:
66
release-pi4j-ktx:
77
runs-on: ubuntu-latest
88
steps:
9-
- uses: actions/checkout@v2
10-
- uses: gradle/gradle-build-action@v2
9+
- uses: actions/checkout@v4
10+
- uses: actions/setup-java@v4
1111
with:
12-
gradle-version: 7.4.2
12+
distribution: 'temurin'
13+
java-version: '25'
14+
- uses: gradle/actions/setup-gradle@v4
1315
- name: release project
1416
env:
1517
ORG_GRADLE_PROJECT_sonatypePassword: ${{ secrets.SONATYPE_PASSWORD }}
@@ -18,4 +20,4 @@ jobs:
1820
ORG_GRADLE_PROJECT_signingPassphrase: ${{ secrets.GPG_PASSPHRASE }}
1921
run: |
2022
./gradlew build
21-
./gradlew publishPi4j-ktxPublicationToSonatypeRepository closeAndReleaseSonatypeStagingRepository
23+
./gradlew publishPi4j-ktxPublicationToSonatypeRepository publishPi4j-ktx-serialPublicationToSonatypeRepository closeAndReleaseSonatypeStagingRepository

README.md

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Pi4J - Kotlin
22

3-
Kotlin Interface & DSL for [Pi4J V2](https://github.com/Pi4J/pi4j-v2)
3+
Kotlin Interface & DSL for [Pi4J](https://github.com/Pi4J/pi4j)
44
For Pi4J V1 Kotlin Bindings, check [Pi4K](https://github.com/mhashim6/Pi4K) (no longer supported).
55

66
[![Awesome Kotlin Badge](https://kotlin.link/awesome-kotlin.svg)](https://github.com/KotlinBy/awesome-kotlin)
@@ -9,7 +9,14 @@ For Pi4J V1 Kotlin Bindings, check [Pi4K](https://github.com/mhashim6/Pi4K) (no
99
![GitHub Actions build state](https://github.com/pi4j/pi4j-kotlin/workflows/Build%20pi4j-kotlin/badge.svg)
1010

1111
## Documentation
12-
Full documentation can be found on the [website](https://pi4j.com/kotlin/kotlin-api-docs/)
12+
Full documentation can be found on the [website](https://www.pi4j.com/kotlin/)
13+
14+
## Modules
15+
16+
| Module | Artifact | Description |
17+
|--------|----------|-------------|
18+
| `lib` | `pi4j-ktx` | Core Kotlin DSL for Pi4J (digital/analog GPIO, PWM, I2C) |
19+
| `lib-serial` | `pi4j-ktx-serial` | Serial communication DSL wrapping [jSerialComm](https://fazecast.github.io/jSerialComm/) |
1320

1421
## Example
1522

@@ -26,32 +33,58 @@ console {
2633
title("<-- The Pi4J Project -->", "Minimal Example project")
2734
pi4j {
2835
digitalInput(PIN_BUTTON) {
29-
id("button")
30-
name("Press button")
31-
pull(PullResistance.PULL_DOWN)
32-
debounce(3000L)
33-
piGpioProvider()
34-
}.onLow {
35-
pressCount++
36-
+"Button was pressed for the ${pressCount}th time"
37-
}
36+
id("button")
37+
name("Press button")
38+
pull(PullResistance.PULL_DOWN)
39+
debounce(3000L)
40+
}.onLow {
41+
pressCount++
42+
+"Button was pressed for the ${pressCount}th time"
3843
}
39-
44+
4045
digitalOutput(PIN_LED) {
41-
id("led")
42-
name("LED Flasher")
43-
shutdown(DigitalState.LOW)
44-
initial(DigitalState.LOW)
45-
piGpioProvider()
46-
}.run {
47-
while (pressCount < 5) {
48-
+"LED ${state()}"
49-
toggle()
50-
sleep(500L / (pressCount + 1))
51-
}
46+
id("led")
47+
name("LED Flasher")
48+
shutdown(DigitalState.LOW)
49+
initial(DigitalState.LOW)
50+
}.run {
51+
while (pressCount < 5) {
52+
+"LED ${state()}"
53+
toggle()
54+
sleep(500L / (pressCount + 1))
5255
}
5356
}
5457
}
5558
}
5659
```
5760

61+
### Serial
62+
63+
Serial communication is provided by the `pi4j-ktx-serial` module, which wraps [jSerialComm](https://fazecast.github.io/jSerialComm/) (independent of Pi4J core since serial was removed in Pi4J 4.0.0).
64+
65+
**Gradle setup:**
66+
67+
``` kotlin
68+
dependencies {
69+
implementation("com.pi4j:pi4j-ktx-serial:4.0.0")
70+
implementation("com.fazecast:jSerialComm:2.11.0")
71+
}
72+
```
73+
74+
**Usage:**
75+
76+
``` kotlin
77+
import com.pi4j.ktx.io.serial.serial
78+
import com.pi4j.ktx.io.serial.open
79+
80+
serial("/dev/ttyS0") {
81+
baudRate = 115200
82+
dataBits = 8
83+
// stopBits, parity, flowControl also available
84+
}.open {
85+
// `this` is a jSerialComm SerialPort
86+
// port is automatically closed when the block exits
87+
outputStream.write("Hello\n".toByteArray())
88+
val response = inputStream.bufferedReader().readLine()
89+
}
90+
```

build.gradle.kts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
2-
31
plugins {
42
java
5-
kotlin("jvm") version "1.7.10"
6-
id("io.github.gradle-nexus.publish-plugin") version "1.1.0"
3+
kotlin("jvm") version "2.3.0"
4+
id("io.github.gradle-nexus.publish-plugin") version "2.0.0"
75
}
86

97
// Set a property
10-
extra["pi4jVersion"] = "2.6.0"
11-
extra["slf4jVersion"] = "2.0.12"
12-
extra["kotlinCoroutinesVersion"] = "1.6.4"
8+
extra["pi4jVersion"] = "4.0.0"
9+
extra["slf4jVersion"] = "2.0.16"
10+
extra["kotlinCoroutinesVersion"] = "1.10.1"
1311

1412
group = "com.pi4j"
15-
version = "2.6.0"
13+
version = "4.0.0"
1614

1715
repositories {
1816
mavenCentral()
@@ -26,12 +24,12 @@ tasks.test {
2624
useJUnitPlatform()
2725
}
2826

29-
tasks.withType<KotlinCompile> {
30-
kotlinOptions.jvmTarget = "11"
27+
kotlin {
28+
jvmToolchain(25)
3129
}
3230

3331
nexusPublishing {
3432
repositories {
3533
sonatype()
3634
}
37-
}
35+
}

example/build.gradle.kts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
plugins {
22
java
3-
kotlin("jvm") version "1.7.10"
3+
kotlin("jvm") version "2.3.0"
44
application
55
}
66

@@ -20,22 +20,24 @@ dependencies {
2020
val slf4jVersion: String by rootProject.extra
2121
val kotlinCoroutinesVersion: String by rootProject.extra
2222
implementation(project(":lib"))
23+
implementation(project(":lib-serial"))
2324
implementation("com.pi4j:pi4j-core:$pi4jVersion")
2425
implementation("com.pi4j:pi4j-plugin-raspberrypi:$pi4jVersion")
25-
implementation("com.pi4j:pi4j-plugin-pigpio:$pi4jVersion")
26+
implementation("com.pi4j:pi4j-plugin-ffm:$pi4jVersion")
2627
implementation("com.pi4j:pi4j-plugin-gpiod:$pi4jVersion")
2728
implementation("com.pi4j:pi4j-plugin-linuxfs:$pi4jVersion")
2829
implementation("com.pi4j:pi4j-plugin-mock:$pi4jVersion")
30+
implementation("com.fazecast:jSerialComm:2.11.0")
2931
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinCoroutinesVersion")
3032
implementation("org.slf4j:slf4j-api:$slf4jVersion")
3133
implementation("org.slf4j:slf4j-simple:$slf4jVersion")
3234
testImplementation(kotlin("test"))
3335
}
3436

37+
kotlin {
38+
jvmToolchain(25)
39+
}
40+
3541
tasks.getByName<Test>("test") {
3642
useJUnitPlatform()
3743
}
38-
39-
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
40-
kotlinOptions.jvmTarget = "11"
41-
}

example/src/main/kotlin/SerialExample.kt

Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,62 +12,36 @@
1212
* limitations under the License.
1313
*/
1414

15-
import com.pi4j.io.serial.FlowControl
16-
import com.pi4j.io.serial.Parity
17-
import com.pi4j.io.serial.StopBits
1815
import com.pi4j.ktx.console
19-
import com.pi4j.ktx.io.open
20-
import com.pi4j.ktx.io.piGpioSerialProvider
21-
import com.pi4j.ktx.io.serial
22-
import com.pi4j.ktx.pi4j
16+
import com.pi4j.ktx.io.serial.open
17+
import com.pi4j.ktx.io.serial.serial
2318
import java.lang.Thread.sleep
2419

2520
/**
26-
* @author Muhammad Hashim (mhashim6) (<a href="https://mhashim6.me">https://mhashim6.me</a>) on 26/02/2023
21+
* @author Muhammad Hashim (mhashim6) (<a href="https://mhashim6.me">https://mhashim6.me</a>)
2722
*/
2823

2924
fun main() {
30-
pi4j {
31-
serial("/dev/ttyS0") {
32-
use_9600_N81()
33-
dataBits_8()
34-
parity(Parity.NONE)
35-
stopBits(StopBits._1)
36-
flowControl(FlowControl.NONE)
37-
piGpioSerialProvider()
38-
}.open {
39-
console {
40-
+"Waiting till serial port is open"
41-
while (!isOpen) {
42-
print(".")
43-
sleep(250)
44-
}
45-
println()
46-
47-
+"Serial port is open"
48-
startDaemon {
49-
inputStream.bufferedReader().use {
50-
while (true) {
51-
if (available() != 0) sleep(10)
52-
else buildString {
53-
(0 until available()).forEach { _ ->
54-
readByte().let { b ->
55-
// All non-string bytes are handled as line breaks
56-
if (b < 32) return@forEach
57-
else append(b.toInt().toChar())
58-
}
59-
}
60-
}.also { +"Data: '$it'" }
61-
}
25+
serial("/dev/ttyS0") {
26+
baudRate = 9600
27+
dataBits = 8
28+
}.open {
29+
console {
30+
+"Serial port is open"
31+
startDaemon {
32+
inputStream.bufferedReader().use { reader ->
33+
while (true) {
34+
val line = reader.readLine() ?: break
35+
+"Data: '$line'"
6236
}
6337
}
64-
while (isOpen) sleep(500)
6538
}
39+
while (isOpen) sleep(500)
6640
}
6741
}
6842
}
6943

7044
fun startDaemon(runnable: Runnable) = Thread(runnable).apply {
7145
isDaemon = true
7246
start()
73-
}
47+
}

gradle/wrapper/gradle-wrapper.jar

-15.6 KB
Binary file not shown.
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-bin.zip
4+
networkTimeout=10000
5+
validateDistributionUrl=true
46
zipStoreBase=GRADLE_USER_HOME
5-
zipStorePath=wrapper/dists
7+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)