Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sknull committed Apr 13, 2023
1 parent 107f13c commit a624853
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package de.visualdigits.kotlin.klanglicht.dmx
import jssc.SerialPort
import jssc.SerialPortException
import org.apache.commons.lang3.StringUtils
import java.lang.IllegalArgumentException


open class DMXInterface {
Expand All @@ -13,20 +12,12 @@ open class DMXInterface {
private var serialPort: SerialPort? = null

companion object {

private var dmxInterface: DMXInterface? = null

fun instance(): DMXInterface = dmxInterface!!

fun load(type: DMXInterfaceType): DMXInterface {
if (dmxInterface == null) {
dmxInterface = when (type) {
DMXInterfaceType.Serial -> DMXInterface()
DMXInterfaceType.Dummy -> DMXInterfaceDummy()
DMXInterfaceType.Rest -> DMXInterfaceRest()
}
return when (type) {
DMXInterfaceType.Serial -> DMXInterface()
DMXInterfaceType.Dummy -> DMXInterfaceDummy()
DMXInterfaceType.Rest -> DMXInterfaceRest()
}
return dmxInterface!!
}
}

Expand All @@ -50,21 +41,17 @@ open class DMXInterface {
open fun open(portName: String) {
if (serialPort == null && !StringUtils.isEmpty(portName)) {
try {
synchronized(this) {
serialPort = SerialPort(portName)
serialPort?.openPort()
serialPort?.setParams(9600, 8, 1, 0)
}
serialPort = SerialPort(portName)
serialPort?.openPort()
serialPort?.setParams(9600, 8, 1, 0)
} catch (e: Exception) {
System.err.println("Could not open DMX port '$portName'")
}
}
}

open fun isOpen(): Boolean {
synchronized(this) {
return serialPort?.isOpened == true
}
return serialPort?.isOpened == true
}

/**
Expand All @@ -73,9 +60,7 @@ open class DMXInterface {
open fun close() {
if (isOpen()) {
try {
synchronized(this) {
serialPort?.closePort()
}
serialPort?.closePort()
} catch (e: SerialPortException) {
throw IllegalStateException("Could not close port", e)
}
Expand Down Expand Up @@ -108,9 +93,7 @@ open class DMXInterface {
open fun write() {
if (isOpen()) {
try {
synchronized(this) {
serialPort?.writeBytes(dmxFrame.frame())
}
serialPort?.writeBytes(dmxFrame.frame())
} catch (e: SerialPortException) {
throw IllegalStateException("Could write dmxFrame to port", e)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import java.nio.file.Paths

data class Preferences(
val dividerPositions: List<Double> = listOf(),
val dmxFrameTime: Int? = null,
val dmxFrameTime: Long? = null,
val dmxPort: String? = null,
val dmxInterfaceType: DMXInterfaceType? = null,
val fonts: List<Font> = listOf(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import de.visualdigits.kotlin.klanglicht.model.color.RGBColor
import de.visualdigits.kotlin.klanglicht.model.parameter.ParameterSet
import de.visualdigits.kotlin.klanglicht.model.parameter.Scene
import de.visualdigits.kotlin.klanglicht.model.preferences.Preferences
import kotlinx.coroutines.*
import org.junit.jupiter.api.Test
import java.io.File
import kotlin.math.ceil
Expand Down Expand Up @@ -66,6 +67,27 @@ internal class DMXInterfaceTest {
prefs.write()
}

@Test
fun testMovinghead() {
// val repeater = Repeater.instance(prefs)
val repeater = Repeater(prefs)
repeater.start()

val color1 = RGBColor(
red = 255,
green = 0,
blue = 0
)
val color2 = RGBColor(
red = 0,
green = 255,
blue = 0
)
fade(48, color1, color2, 2000)

repeater.join()
}

@Test
fun testInterfaceFromModel2() {
val color1 = RGBColor(
Expand All @@ -80,9 +102,17 @@ internal class DMXInterfaceTest {
)
val fadeDuration = 2000 // millis

fade(15, color1, color2, fadeDuration)
}

private fun fade(
baseChannel: Int,
color1: RGBColor,
color2: RGBColor,
fadeDuration: Int
) {
val dmxFrameTime = prefs.dmxFrameTime!! // millis
val steps = ceil(fadeDuration.toDouble() / dmxFrameTime.toDouble()).toInt()
val start = System.currentTimeMillis()
val step = 1.0 / steps
for (f in 0..steps + 1) {
val factor = step * f
Expand All @@ -91,7 +121,7 @@ internal class DMXInterfaceTest {
name = "test",
parameterSet = setOf(
ParameterSet(
baseChannel = 15,
baseChannel = baseChannel,
parameterObjects = setOf(
NamedParameter("MasterDimmer", 255),
NamedParameter("Stroboscope", 0),
Expand All @@ -101,11 +131,9 @@ internal class DMXInterfaceTest {
)
)
prefs.setScene(scene)
prefs.write()
Thread.sleep(dmxFrameTime)
// prefs.write()
}
val stop = System.currentTimeMillis()

println("### durations: ${stop - start} millis")
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package de.visualdigits.kotlin.klanglicht.dmx

import de.visualdigits.kotlin.klanglicht.model.preferences.Preferences

class Repeater(
val preferences: Preferences
) : Thread("DMX Repeater") {

private val dmxFrameTime: Long = preferences.dmxFrameTime!! / 2

private var loop = false

private var running = false

companion object {

var theOne: Repeater? = null

fun instance(preferences: Preferences): Repeater {
if (theOne == null) {
theOne = Repeater(preferences)
theOne?.start()
}
return theOne!!
}
}

override fun run() {
running = true
loop = true
println("### repeater started")
while (loop) {
if (running) {
preferences.write()
sleep(dmxFrameTime)
}
}
}

fun play() {
running = true
}

fun pause() {
running = false
}

fun end() {
loop = false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@
"model" : "Flat PAR Can RGB 10 IR",
"mode" : "6-Channel Mode",
"baseChannel" : 15
}, {
"manufacturer": "Renkforce",
"model": "Moving Head GM107",
"mode": "13-Channel Mode",
"baseChannel": 48
} ]
}
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
<artifactId>kotlin-stdlib</artifactId>
<version>${version.kotlin}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-core-jvm</artifactId>
<version>1.5.0</version>
</dependency>

<!-- logging -->
<dependency>
Expand Down

0 comments on commit a624853

Please sign in to comment.