Skip to content

Commit

Permalink
Merge pull request #8 from Pi4J/release/2.4.0
Browse files Browse the repository at this point in the history
Release v2.4.0
  • Loading branch information
mhashim6 authored Mar 1, 2023
2 parents ed1be0e + 1cfc0a3 commit 8e58a63
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 58 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
java
kotlin("jvm") version "1.7.10"
id("io.github.gradle-nexus.publish-plugin") version "1.1.0"
}

val libVersion by extra("2.3.0")
val libVersion by extra("2.4.0")

group = "com.pi4j"
version = libVersion
Expand Down
14 changes: 7 additions & 7 deletions example/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
plugins {
java
kotlin("jvm") version "1.7.10"
application
}
Expand All @@ -15,13 +16,12 @@ repositories {
}

dependencies {
// implementation(project(":lib"))
implementation("com.pi4j:pi4j-ktx:2.3.0")
implementation("com.pi4j:pi4j-core:2.2.1")
implementation("com.pi4j:pi4j-plugin-raspberrypi:2.2.1")
implementation("com.pi4j:pi4j-plugin-pigpio:2.2.1")
implementation("com.pi4j:pi4j-plugin-linuxfs:2.2.1")
implementation("com.pi4j:pi4j-plugin-mock:2.2.1")
implementation(project(":lib"))
implementation("com.pi4j:pi4j-core:2.3.0")
implementation("com.pi4j:pi4j-plugin-raspberrypi:2.3.0")
implementation("com.pi4j:pi4j-plugin-pigpio:2.3.0")
implementation("com.pi4j:pi4j-plugin-linuxfs:2.3.0")
implementation("com.pi4j:pi4j-plugin-mock:2.3.0")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.3")
implementation("org.slf4j:slf4j-api:1.7.32")
implementation("org.slf4j:slf4j-simple:1.7.32")
Expand Down
14 changes: 0 additions & 14 deletions example/src/main/kotlin/I2CExample.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,6 @@ import com.pi4j.ktx.pi4j
import com.pi4j.ktx.utils.binStr
import java.lang.Thread.sleep

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


private const val TCA9534_REG_ADDR_OUT_PORT: Int = 0x01
private const val TCA9534_REG_ADDR_CFG: Int = 0x03
Expand Down
73 changes: 73 additions & 0 deletions example/src/main/kotlin/SerialExample.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import com.pi4j.io.serial.FlowControl
import com.pi4j.io.serial.Parity
import com.pi4j.io.serial.StopBits
import com.pi4j.ktx.console
import com.pi4j.ktx.io.open
import com.pi4j.ktx.io.piGpioSerialProvider
import com.pi4j.ktx.io.serial
import com.pi4j.ktx.pi4j
import java.lang.Thread.sleep

/**
* @author Muhammad Hashim (mhashim6) (<a href="https://mhashim6.me">https://mhashim6.me</a>) on 26/02/2023
*/

fun main() {
pi4j {
serial("/dev/ttyS0") {
use_9600_N81()
dataBits_8()
parity(Parity.NONE)
stopBits(StopBits._1)
flowControl(FlowControl.NONE)
piGpioSerialProvider()
}.open {
console {
+"Waiting till serial port is open"
while (!isOpen) {
print(".")
sleep(250)
}
println()

+"Serial port is open"
startDaemon {
inputStream.bufferedReader().use {
while (true) {
if (available() != 0) sleep(10)
else buildString {
(0 until available()).forEach { _ ->
readByte().let { b ->
// All non-string bytes are handled as line breaks
if (b < 32) return@forEach
else append(b.toInt().toChar())
}
}
}.also { +"Data: '$it'" }
}
}
}
while (isOpen) sleep(500)
}
}
}
}

fun startDaemon(runnable: Runnable) = Thread(runnable).apply {
isDaemon = true
start()
}
8 changes: 4 additions & 4 deletions lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ repositories {
}

dependencies {
compileOnly("com.pi4j:pi4j-core:2.2.1")
testImplementation("com.pi4j:pi4j-core:2.2.1")
compileOnly("com.pi4j:pi4j-plugin-mock:2.2.1")
testImplementation("com.pi4j:pi4j-plugin-mock:2.2.1")
compileOnly("com.pi4j:pi4j-core:2.3.0")
testImplementation("com.pi4j:pi4j-core:2.3.0")
compileOnly("com.pi4j:pi4j-plugin-mock:2.3.0")
testImplementation("com.pi4j:pi4j-plugin-mock:2.3.0")
compileOnly("org.slf4j:slf4j-api:1.7.32")
testImplementation("org.slf4j:slf4j-api:1.7.32")
compileOnly("org.slf4j:slf4j-simple:1.7.32")
Expand Down
16 changes: 1 addition & 15 deletions lib/src/main/kotlin/com/pi4j/ktx/io/I2C.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,6 @@
* limitations under the License.
*/

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

package com.pi4j.ktx.io

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

fun I2CConfigBuilder.mockI2cProvider() {
fun I2CConfigBuilder.mockI2CProvider() {
provider(Provider.MOCK_I2C.id)
}

Expand Down
44 changes: 44 additions & 0 deletions lib/src/main/kotlin/com/pi4j/ktx/io/Serial.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.pi4j.ktx.io

import com.pi4j.context.Context
import com.pi4j.io.serial.Serial
import com.pi4j.io.serial.SerialConfigBuilder
import com.pi4j.ktx.utils.Provider

/**
* @author Muhammad Hashim (mhashim6) (<a href="https://mhashim6.me">https://mhashim6.me</a>) on 26/02/2023
*/

inline fun Context.serial(device: String, block: SerialConfigBuilder.() -> Unit): Serial =
create(Serial.newConfigBuilder(this).run {
device(device)
block()
build()
})

inline fun Serial.open(action: Serial.() -> Unit) = apply {
open()
action()
}

fun SerialConfigBuilder.mockSerialProvider() {
provider(Provider.MOCK_SERIAL.id)
}

fun SerialConfigBuilder.piGpioSerialProvider() {
provider(Provider.PI_GPIO_SERIAL.id)
}
2 changes: 2 additions & 0 deletions lib/src/main/kotlin/com/pi4j/ktx/utils/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ enum class Provider(val id: String) {
PI_GPIO_PWM("pigpio-pwm"),
MOCK_I2C("mock-i2c"),
LINUX_FS_I2C("linuxfs-i2c"),
MOCK_SERIAL("mock-serial"),
PI_GPIO_SERIAL("pigpio-serial")
}
20 changes: 3 additions & 17 deletions lib/src/test/kotlin/com/pi4j/ktx/io/I2CTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,6 @@
* limitations under the License.
*/

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

package com.pi4j.ktx.io

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

val kotlinI2C = i2c(2, 0x3f) {
mockI2cProvider()
mockI2CProvider()
}

assertEquals(javaI2C::class.java, kotlinI2C::class.java)
Expand All @@ -68,7 +54,7 @@ internal class I2CTest {
i2CProvider.create(I2C.newConfigBuilder(context).id("TCA9534").bus(1).device(0x4f).build())
i2c(1, 0x4f) {
id("TCA9534")
mockI2cProvider()
mockI2CProvider()
}
}
}
Expand Down
62 changes: 62 additions & 0 deletions lib/src/test/kotlin/com/pi4j/ktx/io/SerialTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.pi4j.ktx.io

import com.pi4j.Pi4J
import com.pi4j.context.Context
import com.pi4j.io.exception.IOAlreadyExistsException
import com.pi4j.plugin.mock.provider.serial.MockSerial

import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.assertEquals

/**
* @author Muhammad Hashim (mhashim6) (<a href="https://mhashim6.me">https://mhashim6.me</a>) on 26/02/2023
*/
internal class SerialTest {
private lateinit var context: Context

@BeforeTest
fun setup() {
context = Pi4J.newAutoContext()
}

@Test
fun `test serial creation`() {
context.run {
val kotlinSerial = serial("/dev/ttyS0") {
mockSerialProvider()
}

assertEquals(MockSerial::class, kotlinSerial::class)

assertThrows<IOAlreadyExistsException> {
serial("/dev/ttyS0") {
id("conflictingSerial")
mockSerialProvider()
}
}
}
}


@AfterTest
fun tearDown() {
context.shutdown()
}
}

0 comments on commit 8e58a63

Please sign in to comment.