Skip to content

Commit 8e58a63

Browse files
authored
Merge pull request #8 from Pi4J/release/2.4.0
Release v2.4.0
2 parents ed1be0e + 1cfc0a3 commit 8e58a63

File tree

10 files changed

+198
-58
lines changed

10 files changed

+198
-58
lines changed

build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
22

33
plugins {
4+
java
45
kotlin("jvm") version "1.7.10"
56
id("io.github.gradle-nexus.publish-plugin") version "1.1.0"
67
}
78

8-
val libVersion by extra("2.3.0")
9+
val libVersion by extra("2.4.0")
910

1011
group = "com.pi4j"
1112
version = libVersion

example/build.gradle.kts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
plugins {
2+
java
23
kotlin("jvm") version "1.7.10"
34
application
45
}
@@ -15,13 +16,12 @@ repositories {
1516
}
1617

1718
dependencies {
18-
// implementation(project(":lib"))
19-
implementation("com.pi4j:pi4j-ktx:2.3.0")
20-
implementation("com.pi4j:pi4j-core:2.2.1")
21-
implementation("com.pi4j:pi4j-plugin-raspberrypi:2.2.1")
22-
implementation("com.pi4j:pi4j-plugin-pigpio:2.2.1")
23-
implementation("com.pi4j:pi4j-plugin-linuxfs:2.2.1")
24-
implementation("com.pi4j:pi4j-plugin-mock:2.2.1")
19+
implementation(project(":lib"))
20+
implementation("com.pi4j:pi4j-core:2.3.0")
21+
implementation("com.pi4j:pi4j-plugin-raspberrypi:2.3.0")
22+
implementation("com.pi4j:pi4j-plugin-pigpio:2.3.0")
23+
implementation("com.pi4j:pi4j-plugin-linuxfs:2.3.0")
24+
implementation("com.pi4j:pi4j-plugin-mock:2.3.0")
2525
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.3")
2626
implementation("org.slf4j:slf4j-api:1.7.32")
2727
implementation("org.slf4j:slf4j-simple:1.7.32")

example/src/main/kotlin/I2CExample.kt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,6 @@ import com.pi4j.ktx.pi4j
2020
import com.pi4j.ktx.utils.binStr
2121
import java.lang.Thread.sleep
2222

23-
/*
24-
* Licensed under the Apache License, Version 2.0 (the "License");
25-
* you may not use this file except in compliance with the License.
26-
* You may obtain a copy of the License at
27-
*
28-
* http://www.apache.org/licenses/LICENSE-2.0
29-
*
30-
* Unless required by applicable law or agreed to in writing, software
31-
* distributed under the License is distributed on an "AS IS" BASIS,
32-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33-
* See the License for the specific language governing permissions and
34-
* limitations under the License.
35-
*/
36-
3723

3824
private const val TCA9534_REG_ADDR_OUT_PORT: Int = 0x01
3925
private const val TCA9534_REG_ADDR_CFG: Int = 0x03
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
import com.pi4j.io.serial.FlowControl
16+
import com.pi4j.io.serial.Parity
17+
import com.pi4j.io.serial.StopBits
18+
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
23+
import java.lang.Thread.sleep
24+
25+
/**
26+
* @author Muhammad Hashim (mhashim6) (<a href="https://mhashim6.me">https://mhashim6.me</a>) on 26/02/2023
27+
*/
28+
29+
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+
}
62+
}
63+
}
64+
while (isOpen) sleep(500)
65+
}
66+
}
67+
}
68+
}
69+
70+
fun startDaemon(runnable: Runnable) = Thread(runnable).apply {
71+
isDaemon = true
72+
start()
73+
}

lib/build.gradle.kts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ repositories {
1515
}
1616

1717
dependencies {
18-
compileOnly("com.pi4j:pi4j-core:2.2.1")
19-
testImplementation("com.pi4j:pi4j-core:2.2.1")
20-
compileOnly("com.pi4j:pi4j-plugin-mock:2.2.1")
21-
testImplementation("com.pi4j:pi4j-plugin-mock:2.2.1")
18+
compileOnly("com.pi4j:pi4j-core:2.3.0")
19+
testImplementation("com.pi4j:pi4j-core:2.3.0")
20+
compileOnly("com.pi4j:pi4j-plugin-mock:2.3.0")
21+
testImplementation("com.pi4j:pi4j-plugin-mock:2.3.0")
2222
compileOnly("org.slf4j:slf4j-api:1.7.32")
2323
testImplementation("org.slf4j:slf4j-api:1.7.32")
2424
compileOnly("org.slf4j:slf4j-simple:1.7.32")

lib/src/main/kotlin/com/pi4j/ktx/io/I2C.kt

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,6 @@
1212
* limitations under the License.
1313
*/
1414

15-
/*
16-
* Licensed under the Apache License, Version 2.0 (the "License");
17-
* you may not use this file except in compliance with the License.
18-
* You may obtain a copy of the License at
19-
*
20-
* http://www.apache.org/licenses/LICENSE-2.0
21-
*
22-
* Unless required by applicable law or agreed to in writing, software
23-
* distributed under the License is distributed on an "AS IS" BASIS,
24-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25-
* See the License for the specific language governing permissions and
26-
* limitations under the License.
27-
*/
28-
2915
package com.pi4j.ktx.io
3016

3117
import com.pi4j.context.Context
@@ -57,7 +43,7 @@ fun I2C.setPin(currentState: Int, pin: Int, regOutPort: Int, high: Boolean = tru
5743
return newState
5844
}
5945

60-
fun I2CConfigBuilder.mockI2cProvider() {
46+
fun I2CConfigBuilder.mockI2CProvider() {
6147
provider(Provider.MOCK_I2C.id)
6248
}
6349

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package com.pi4j.ktx.io
16+
17+
import com.pi4j.context.Context
18+
import com.pi4j.io.serial.Serial
19+
import com.pi4j.io.serial.SerialConfigBuilder
20+
import com.pi4j.ktx.utils.Provider
21+
22+
/**
23+
* @author Muhammad Hashim (mhashim6) (<a href="https://mhashim6.me">https://mhashim6.me</a>) on 26/02/2023
24+
*/
25+
26+
inline fun Context.serial(device: String, block: SerialConfigBuilder.() -> Unit): Serial =
27+
create(Serial.newConfigBuilder(this).run {
28+
device(device)
29+
block()
30+
build()
31+
})
32+
33+
inline fun Serial.open(action: Serial.() -> Unit) = apply {
34+
open()
35+
action()
36+
}
37+
38+
fun SerialConfigBuilder.mockSerialProvider() {
39+
provider(Provider.MOCK_SERIAL.id)
40+
}
41+
42+
fun SerialConfigBuilder.piGpioSerialProvider() {
43+
provider(Provider.PI_GPIO_SERIAL.id)
44+
}

lib/src/main/kotlin/com/pi4j/ktx/utils/Constants.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ enum class Provider(val id: String) {
3030
PI_GPIO_PWM("pigpio-pwm"),
3131
MOCK_I2C("mock-i2c"),
3232
LINUX_FS_I2C("linuxfs-i2c"),
33+
MOCK_SERIAL("mock-serial"),
34+
PI_GPIO_SERIAL("pigpio-serial")
3335
}

lib/src/test/kotlin/com/pi4j/ktx/io/I2CTest.kt

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,6 @@
1212
* limitations under the License.
1313
*/
1414

15-
/*
16-
* Licensed under the Apache License, Version 2.0 (the "License");
17-
* you may not use this file except in compliance with the License.
18-
* You may obtain a copy of the License at
19-
*
20-
* http://www.apache.org/licenses/LICENSE-2.0
21-
*
22-
* Unless required by applicable law or agreed to in writing, software
23-
* distributed under the License is distributed on an "AS IS" BASIS,
24-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25-
* See the License for the specific language governing permissions and
26-
* limitations under the License.
27-
*/
28-
2915
package com.pi4j.ktx.io
3016

3117
import com.pi4j.Pi4J
@@ -53,12 +39,12 @@ internal class I2CTest {
5339
@Test
5440
fun `test i2c creation`() {
5541
context.run {
56-
val i2CProvider: I2CProvider = context.provider("mock-i2c")
42+
val i2CProvider: I2CProvider = context.provider<I2CProvider>("mock-i2c")
5743
val javaI2C =
5844
i2CProvider.create(I2C.newConfigBuilder(context).id("TCA9534").bus(1).device(0x3f).build())
5945

6046
val kotlinI2C = i2c(2, 0x3f) {
61-
mockI2cProvider()
47+
mockI2CProvider()
6248
}
6349

6450
assertEquals(javaI2C::class.java, kotlinI2C::class.java)
@@ -68,7 +54,7 @@ internal class I2CTest {
6854
i2CProvider.create(I2C.newConfigBuilder(context).id("TCA9534").bus(1).device(0x4f).build())
6955
i2c(1, 0x4f) {
7056
id("TCA9534")
71-
mockI2cProvider()
57+
mockI2CProvider()
7258
}
7359
}
7460
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package com.pi4j.ktx.io
16+
17+
import com.pi4j.Pi4J
18+
import com.pi4j.context.Context
19+
import com.pi4j.io.exception.IOAlreadyExistsException
20+
import com.pi4j.plugin.mock.provider.serial.MockSerial
21+
22+
import org.junit.jupiter.api.Test
23+
import org.junit.jupiter.api.assertThrows
24+
import kotlin.test.AfterTest
25+
import kotlin.test.BeforeTest
26+
import kotlin.test.assertEquals
27+
28+
/**
29+
* @author Muhammad Hashim (mhashim6) (<a href="https://mhashim6.me">https://mhashim6.me</a>) on 26/02/2023
30+
*/
31+
internal class SerialTest {
32+
private lateinit var context: Context
33+
34+
@BeforeTest
35+
fun setup() {
36+
context = Pi4J.newAutoContext()
37+
}
38+
39+
@Test
40+
fun `test serial creation`() {
41+
context.run {
42+
val kotlinSerial = serial("/dev/ttyS0") {
43+
mockSerialProvider()
44+
}
45+
46+
assertEquals(MockSerial::class, kotlinSerial::class)
47+
48+
assertThrows<IOAlreadyExistsException> {
49+
serial("/dev/ttyS0") {
50+
id("conflictingSerial")
51+
mockSerialProvider()
52+
}
53+
}
54+
}
55+
}
56+
57+
58+
@AfterTest
59+
fun tearDown() {
60+
context.shutdown()
61+
}
62+
}

0 commit comments

Comments
 (0)