From 2abd2f252f00df756294c97dbbd7a675d816bec1 Mon Sep 17 00:00:00 2001 From: Stephan Knull Date: Fri, 5 Jan 2024 20:58:51 +0100 Subject: [PATCH] Fix bug in HybridScene regarding writing dmx frames --- .../klanglicht/model/hybrid/HybridScene.kt | 30 +++++++++++-------- .../model/shelly/ShellyColorTest.kt | 11 +++++++ 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/klanglicht-module-dmx/src/main/kotlin/de/visualdigits/kotlin/klanglicht/model/hybrid/HybridScene.kt b/klanglicht-module-dmx/src/main/kotlin/de/visualdigits/kotlin/klanglicht/model/hybrid/HybridScene.kt index 78e066c..e20d435 100644 --- a/klanglicht-module-dmx/src/main/kotlin/de/visualdigits/kotlin/klanglicht/model/hybrid/HybridScene.kt +++ b/klanglicht-module-dmx/src/main/kotlin/de/visualdigits/kotlin/klanglicht/model/hybrid/HybridScene.kt @@ -8,7 +8,6 @@ import de.visualdigits.kotlin.klanglicht.model.preferences.Preferences import de.visualdigits.kotlin.klanglicht.model.shelly.ShellyColor import de.visualdigits.kotlin.klanglicht.model.twinkly.XledFrameFadeable import de.visualdigits.kotlin.twinkly.model.playable.XledFrame -import kotlinx.coroutines.coroutineScope import org.slf4j.Logger import org.slf4j.LoggerFactory import kotlin.math.min @@ -213,27 +212,32 @@ class HybridScene() : Fadeable { fun getRgbColor(id: String): RGBColor? = fadeables[id]?.getRgbColor() 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() if (parameterSets.isNotEmpty()) { // only write to dmx interface if needed - parameterSets.forEach { parameterSet -> - val baseChannel = parameterSet.baseChannel - preferences?.let{ - val bytes = parameterSet.toBytes(it) - preferences.setDmxData(baseChannel, bytes) - if (write) { - log.debug("Writing hybrid scene {}", this) - preferences.writeDmxData() - } + preferences?.let { + // first collect all frame data for the dmx frame to avoid lots of costly write operations to a serial interface + parameterSets.forEach { parameterSet -> + preferences.setDmxData( + baseChannel = parameterSet.baseChannel, + bytes = parameterSet.toBytes(it) + ) + } + if (write) { + log.debug("Writing hybrid scene {}", this) + preferences.writeDmxData() } } } // call twinkly interface which is not so fast - fadeables().filterIsInstance().forEach { it.write(preferences, true) } + fadeables().filterIsInstance().forEach { + it.write(preferences, true) + } // call shelly interface which is pretty fast - fadeables().filterIsInstance().forEach { it.write(preferences, true) } + fadeables().filterIsInstance().forEach { + it.write(preferences, true) + } } override fun clone(): HybridScene { diff --git a/klanglicht-module-dmx/src/test/kotlin/de/visualdigits/kotlin/klanglicht/model/shelly/ShellyColorTest.kt b/klanglicht-module-dmx/src/test/kotlin/de/visualdigits/kotlin/klanglicht/model/shelly/ShellyColorTest.kt index 354b81d..93b4097 100644 --- a/klanglicht-module-dmx/src/test/kotlin/de/visualdigits/kotlin/klanglicht/model/shelly/ShellyColorTest.kt +++ b/klanglicht-module-dmx/src/test/kotlin/de/visualdigits/kotlin/klanglicht/model/shelly/ShellyColorTest.kt @@ -14,6 +14,17 @@ class ShellyColorTest { preferencesFileName = "preferences_livingroom_dummy.json" ) + @Test + fun testTiming() { + val shellyDevice = preferences.getShellyDevice("Starwars")!! + val red = ShellyColor("Starwars", shellyDevice.ipAddress, RGBColor(255, 0, 0), 1.0f, true) + val green = ShellyColor("Starwars", shellyDevice.ipAddress, RGBColor(0, 255, 0), 1.0f, true) + val t = System.currentTimeMillis() + red.write(preferences, transitionDuration = 3000) + val d = System.currentTimeMillis() - t + println(d) + } + @Test fun testFade() { val shellyDevice = preferences.getShellyDevice("Starwars")