Skip to content

Commit

Permalink
Abstracting desktop sources
Browse files Browse the repository at this point in the history
Most are the same, only varying parts should be varying.
  • Loading branch information
mariobodemann authored and Mario Bodemann committed Sep 14, 2023
1 parent 157d1da commit 1db60de
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 108 deletions.
10 changes: 10 additions & 0 deletions zeapp/badge/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,22 @@ kotlin {
}
}

val jvmMain by creating {
sourceSets {
dependsOn (sourceSets.getAt("commonMain"))
}
}

when {
os.startsWith("Mac OS") -> {
val macMain by getting {
dependencies {
implementation(libs.jSerialComm)
}

sourceSets {
dependsOn (sourceSets.getAt("jvmMain"))
}
}
}

Expand Down
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>
}
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()
}
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()

0 comments on commit 1db60de

Please sign in to comment.