-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Most are the same, only varying parts should be varying.
- Loading branch information
1 parent
157d1da
commit 1db60de
Showing
4 changed files
with
108 additions
and
108 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
85 changes: 85 additions & 0 deletions
85
zeapp/badge/src/jvmMain/kotlin/de/berlindroid/zekompanion/BadgeManager.kt
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,85 @@ | ||
package de.berlindroid.zekompanion | ||
|
||
import com.fazecast.jSerialComm.SerialPort | ||
import java.lang.Thread.sleep | ||
import kotlin.IllegalStateException | ||
|
||
abstract class JvmBadgeManager : BadgeManager { | ||
override suspend fun sendPayload(payload: BadgePayload): Result<Int> = if (isConnected()) { | ||
val badgers = getBadger2040s() | ||
val command = payload.toBadgeCommand() | ||
val commandBytes = command.toByteArray(Charsets.UTF_8) | ||
|
||
var written = 0 | ||
for (badger in badgers) { | ||
try { | ||
if (!badger.openPort(300)) { | ||
// couldn't open, try next one | ||
continue | ||
} | ||
setupPort(badger) | ||
|
||
// write as much as you can and then write some more | ||
var offset = 0 | ||
while (offset < commandBytes.size) { | ||
val length = commandBytes.size - offset | ||
offset += badger.writeBytes(commandBytes, length, offset) | ||
} | ||
|
||
written = offset | ||
|
||
// wait some more time for the buffers to recover | ||
sleep(300) | ||
|
||
if (written > 0) { | ||
break | ||
} | ||
} catch (e: RuntimeException) { | ||
written = -1 | ||
break | ||
} finally { | ||
badger.flushIOBuffers() | ||
if (badger.isOpen) { | ||
badger.closePort() | ||
} | ||
badger.clearDTR() | ||
} | ||
} | ||
|
||
if (written > 0) { | ||
Result.success(written) | ||
} else { | ||
Result.failure(RuntimeException("Couldn't write to badge.")) | ||
} | ||
} else { | ||
Result.failure(RuntimeException("No badge connected.")) | ||
} | ||
|
||
private fun setupPort(badger: SerialPort) { | ||
if (!badger.setDTR()) { | ||
throw IllegalStateException("Could not set dtr on $badger.") | ||
} | ||
|
||
if (!badger.setBaudRate(115200)) { | ||
throw IllegalStateException("Could not set baud rate on $badger.") | ||
} | ||
|
||
if (!badger.setNumDataBits(8)) { | ||
throw IllegalStateException("Could not data bit on $badger.") | ||
} | ||
|
||
if (!badger.setNumStopBits(1)) { | ||
throw IllegalStateException("Could not set num stop bits on $badger.") | ||
} | ||
|
||
if (!badger.setParity(0)) { | ||
throw IllegalStateException("Could not set parity bit on $badger.") | ||
} | ||
} | ||
|
||
override fun isConnected(): Boolean { | ||
return getBadger2040s().isNotEmpty() | ||
} | ||
|
||
abstract fun getBadger2040s() : List<SerialPort> | ||
} |
31 changes: 7 additions & 24 deletions
31
zeapp/badge/src/linuxMain/kotlin/de/berlindroid/zekompanion/BadgeManager.kt
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 |
---|---|---|
@@ -1,30 +1,13 @@ | ||
package de.berlindroid.zekompanion | ||
|
||
import java.io.File | ||
import java.lang.RuntimeException | ||
import com.fazecast.jSerialComm.SerialPort | ||
|
||
class AppleBadgeManager : BadgeManager { | ||
override suspend fun sendPayload(payload: BadgePayload): Result<Int> = if (isConnected()) { | ||
val serial = getUsbModems().last() | ||
val bytes = payload.toBadgeCommand().toByteArray() | ||
serial.writeBytes(bytes) | ||
Result.success(bytes.size) | ||
} else { | ||
Result.failure(RuntimeException("No badge connected.")) | ||
} | ||
|
||
override fun isConnected(): Boolean { | ||
return getUsbModems().size == 2 | ||
} | ||
|
||
private fun getUsbModems() = File("/dev/") | ||
.listFiles() | ||
.orEmpty() | ||
.filter { | ||
it.name.startsWith("ttyACM") | ||
} | ||
} | ||
|
||
actual typealias Environment = Any | ||
|
||
actual fun buildBadgeManager(environment: Environment): BadgeManager = AppleBadgeManager() | ||
actual fun buildBadgeManager(environment: Environment): BadgeManager = object : JvmBadgeManager() { | ||
override fun getBadger2040s() = SerialPort.getCommPorts().filter { | ||
it.descriptivePortName.contains("Badger") && | ||
it.systemPortPath.contains("tty") | ||
}.toList() | ||
} |
90 changes: 6 additions & 84 deletions
90
zeapp/badge/src/macMain/kotlin/de/berlindroid/zekompanion/BadgeManager.kt
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 |
---|---|---|
@@ -1,92 +1,14 @@ | ||
package de.berlindroid.zekompanion | ||
|
||
import com.fazecast.jSerialComm.SerialPort | ||
import java.lang.Thread.sleep | ||
import kotlin.IllegalStateException | ||
|
||
class AppleBadgeManager : BadgeManager { | ||
override suspend fun sendPayload(payload: BadgePayload): Result<Int> = if (isConnected()) { | ||
val badgers = getBadger2040s() | ||
val command = payload.toBadgeCommand() | ||
val commandBytes = command.toByteArray(Charsets.UTF_8) | ||
|
||
var written = 0 | ||
for (badger in badgers) { | ||
try { | ||
if (!badger.openPort(300)) { | ||
// couldn't open, try next one | ||
continue | ||
} | ||
setupPort(badger) | ||
|
||
// write as much as you can and then write some more | ||
var offset = 0 | ||
while (offset < commandBytes.size) { | ||
val length = commandBytes.size - offset | ||
offset += badger.writeBytes(commandBytes, length, offset) | ||
} | ||
|
||
written = offset | ||
|
||
// wait some more time for the buffers to recover | ||
sleep(300) | ||
|
||
if (written > 0) { | ||
break | ||
} | ||
} catch (e: RuntimeException) { | ||
written = -1 | ||
break | ||
} finally { | ||
if (badger.isOpen) { | ||
badger.closePort() | ||
} | ||
badger.clearDTR() | ||
} | ||
} | ||
|
||
if (written > 0) { | ||
Result.success(written) | ||
} else { | ||
Result.failure(RuntimeException("Couldn't write to badge.")) | ||
} | ||
} else { | ||
Result.failure(RuntimeException("No badge connected.")) | ||
} | ||
|
||
private fun setupPort(badger: SerialPort) { | ||
if (!badger.setDTR()) { | ||
throw IllegalStateException("Could not set dtr on $badger.") | ||
} | ||
|
||
if (!badger.setBaudRate(115200)) { | ||
throw IllegalStateException("Could not set baud rate on $badger.") | ||
} | ||
|
||
if (!badger.setNumDataBits(8)) { | ||
throw IllegalStateException("Could not data bit on $badger.") | ||
} | ||
|
||
if (!badger.setNumStopBits(1)) { | ||
throw IllegalStateException("Could not set num stop bits on $badger.") | ||
} | ||
|
||
if (!badger.setParity(0)) { | ||
throw IllegalStateException("Could not set parity bit on $badger.") | ||
} | ||
} | ||
|
||
override fun isConnected(): Boolean { | ||
return getBadger2040s().isNotEmpty() | ||
} | ||
actual typealias Environment = Any | ||
|
||
private fun getBadger2040s() = SerialPort.getCommPorts().filter { | ||
actual fun buildBadgeManager(environment: Environment): BadgeManager = object : JvmBadgeManager() { | ||
override fun getBadger2040s() = SerialPort.getCommPorts().filter { | ||
it.descriptivePortName.contains("Badger") && | ||
it.systemPortPath.contains("cu") && | ||
!it.systemPortPath.contains("tty") | ||
} | ||
!it.systemPortPath.contains("tty") && | ||
it.systemPortPath.contains("cu") | ||
}.toList() | ||
} | ||
|
||
actual typealias Environment = Any | ||
|
||
actual fun buildBadgeManager(environment: Environment): BadgeManager = AppleBadgeManager() |