Skip to content

Commit

Permalink
Remove coroutine approach as it is not really helpful for performance
Browse files Browse the repository at this point in the history
  • Loading branch information
sknull committed Jan 1, 2024
1 parent 3690869 commit 76b626d
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,11 @@ class HybridScene() : Fadeable<HybridScene> {

fun getRgbColor(id: String): RGBColor? = fadeables[id]?.getRgbColor()

override suspend fun write(preferences: Preferences?, write: Boolean, transitionDuration: Long) {
coroutineScope {
// first collect all frame data for the dmx frame to avoid lots of costly write operations to a serial interface
fadeables().filterIsInstance<ParameterSet>().forEach { parameterSet ->
override fun write(preferences: Preferences?, write: Boolean, transitionDuration: Long) {
// first collect all frame data for the dmx frame to avoid lots of costly write operations to a serial interface
val parameterSets = fadeables().filterIsInstance<ParameterSet>()
if (parameterSets.isNotEmpty()) { // only write to dmx interface if needed
parameterSets.forEach { parameterSet ->
val baseChannel = parameterSet.baseChannel
preferences?.let{
val bytes = parameterSet.toBytes(it)
Expand All @@ -217,10 +218,13 @@ class HybridScene() : Fadeable<HybridScene> {
}
}
}

// call other fadeables
fadeables().filter { it !is ParameterSet }.forEach { it.write(preferences, true) }
}

// call twinkly interface which is not so fast
fadeables().filterIsInstance<XledFrameFadeable>().forEach { it.write(preferences, true) }

// call shelly interface which is pretty fast
fadeables().filterIsInstance<ShellyColor>().forEach { it.write(preferences, true) }
}

override fun fade(other: Any, factor: Double): HybridScene {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class DmxScene(
/**
* Writes the given scene into the internal dmx frame of the interface.
*/
override suspend fun write(preferences: Preferences?, write: Boolean, transitionDuration: Long) {
override fun write(preferences: Preferences?, write: Boolean, transitionDuration: Long) {
// first collect all frame data for the dmx frame to avoid lots of costly write operations to a serial interface
parameterSet
.sortedBy { it.baseChannel }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ interface Fadeable<T : Fadeable<T>> {

while (factor <= 1.0) {
val faded = fade(other, factor)
runBlocking { faded.write(preferences) }
faded.write(preferences)
factor += step
Thread.sleep(dmxFrameTime)
}
runBlocking { other.write(preferences) }
other.write(preferences)
}

suspend fun write(preferences: Preferences?, write: Boolean = true, transitionDuration: Long = 1) {
fun write(preferences: Preferences?, write: Boolean = true, transitionDuration: Long = 1) {
// nothing to do here
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class ParameterSet(
} ?: listOf()).toByteArray()
}

override suspend fun write(preferences: Preferences?, write: Boolean, transitionDuration: Long) {
override fun write(preferences: Preferences?, write: Boolean, transitionDuration: Long) {
preferences?.let {
val bytes = toBytes(it)
preferences.setDmxData(baseChannel, bytes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ShellyColor(
color = rgbColor.clone()
}

override suspend fun write(preferences: Preferences?, write: Boolean, transitionDuration: Long) {
override fun write(preferences: Preferences?, write: Boolean, transitionDuration: Long) {
if (write) {
log.debug("Set shelly color {}", color.ansiColor())
ShellyClient.setColor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class XledFrameFadeable(
xledFrame.setColor(TwinklyRGBColor(rgbColor.red, rgbColor.green, rgbColor.blue))
}

override suspend fun write(preferences: Preferences?, write: Boolean, transitionDuration: Long) {
override fun write(preferences: Preferences?, write: Boolean, transitionDuration: Long) {
val twinklyDevice = preferences?.getTwinklyConfiguration(deviceId)
if (twinklyDevice != null) {
val xledArray = twinklyDevice.xledArray
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class DmxInterfaceTest {
)
)
)
runBlocking { dmxScene.write(preferences) }
dmxScene.write(preferences)
preferences.writeDmxData()
}

Expand All @@ -53,7 +53,7 @@ class DmxInterfaceTest {
)
)
)
runBlocking { dmxScene.write(preferences) }
dmxScene.write(preferences)
preferences.writeDmxData()
}

Expand Down Expand Up @@ -157,7 +157,7 @@ class DmxInterfaceTest {
frame
)
)
runBlocking { dmxScene.write(preferences) }
dmxScene.write(preferences)
Thread.sleep(dmxFrameTime)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,8 @@ class SimpleTest {
)
)

runBlocking {
coroutineScope {
dmxScene.write(preferences)
dmxScene.write(preferences)
}
}
dmxScene.write(preferences)
dmxScene.write(preferences)
}

@Test
Expand All @@ -70,7 +66,7 @@ class SimpleTest {
),
)
)
runBlocking { dmxScene0.write(preferences) }
dmxScene0.write(preferences)
}

@Test
Expand Down Expand Up @@ -114,7 +110,7 @@ class SimpleTest {
),
)
)
runBlocking { dmxScene1.write(preferences) }
dmxScene1.write(preferences)

val dmxScene2 = DmxScene(
name = "JUnit Test",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ConfigHolder {
log.info("## klanglichtDirectory: " + klanglichtDirectory.absolutePath)
preferences = Preferences.load(klanglichtDirectory)
currentScene = preferences?.initialHybridScene()
runBlocking { currentScene?.write(preferences, true, 1000) }
currentScene?.write(preferences, true, 1000)
log.info("#### setUp - end")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,9 @@ class ShellyHandler {
.filter { it.trim().isNotEmpty() }
.map { it.trim() }

runBlocking {
coroutineScope {
lIds.forEach { id ->
configHolder?.getFadeable(id)?.write(configHolder.preferences!!, transitionDuration = transitionDuration)
}
lIds.forEach { id ->
configHolder?.getFadeable(id)?.write(configHolder.preferences!!, transitionDuration = transitionDuration)
}
}
}

fun power(
Expand Down

0 comments on commit 76b626d

Please sign in to comment.