-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Pi4J/release/2.4.0
Release v2.4.0
- Loading branch information
Showing
10 changed files
with
198 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |