Skip to content

Commit

Permalink
Fix bug in HybridScene regarding writing dmx frames
Browse files Browse the repository at this point in the history
  • Loading branch information
sknull committed Jan 5, 2024
1 parent 3338cde commit 2abd2f2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -213,27 +212,32 @@ class HybridScene() : Fadeable<HybridScene> {
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<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)
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<XledFrameFadeable>().forEach { it.write(preferences, true) }
fadeables().filterIsInstance<XledFrameFadeable>().forEach {
it.write(preferences, true)
}

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

override fun clone(): HybridScene {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 2abd2f2

Please sign in to comment.