Add sACN input/output support and fix multi-source merging

- Add sACN/E1.31 protocol support for both input and output
- from_proto = "sacn" to receive from sACN multicast
- proto = "sacn" to output via sACN multicast
- Fix remap engine to maintain persistent state per output universe
- Multiple inputs targeting same output now merge correctly
- Prevents flickering when multiple universes feed same output

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Ian Gulliver
2025-12-22 12:32:41 -08:00
parent cdb769d059
commit b0e9ecdee7
9 changed files with 580 additions and 80 deletions

View File

@@ -1,6 +1,8 @@
package remap
import (
"sync"
"github.com/gopatchy/artmap/artnet"
"github.com/gopatchy/artmap/config"
)
@@ -8,63 +10,95 @@ import (
// Output represents a remapped DMX output
type Output struct {
Universe artnet.Universe
Protocol config.Protocol
Data [512]byte
}
// outputKey uniquely identifies an output destination
type outputKey struct {
Universe artnet.Universe
Protocol config.Protocol
}
// sourceKey uniquely identifies an input source
type sourceKey struct {
Universe artnet.Universe
Protocol config.Protocol
}
// Engine handles DMX channel remapping
type Engine struct {
mappings []config.NormalizedMapping
// Index mappings by source universe for faster lookup
bySource map[artnet.Universe][]config.NormalizedMapping
// Index mappings by source universe and protocol for faster lookup
bySource map[sourceKey][]config.NormalizedMapping
// Persistent state for each output universe (merged from all sources)
state map[outputKey]*[512]byte
stateMu sync.Mutex
}
// NewEngine creates a new remapping engine
func NewEngine(mappings []config.NormalizedMapping) *Engine {
bySource := make(map[artnet.Universe][]config.NormalizedMapping)
bySource := make(map[sourceKey][]config.NormalizedMapping)
for _, m := range mappings {
bySource[m.FromUniverse] = append(bySource[m.FromUniverse], m)
key := sourceKey{Universe: m.FromUniverse, Protocol: m.FromProto}
bySource[key] = append(bySource[key], m)
}
// Initialize state for all output universes
state := make(map[outputKey]*[512]byte)
for _, m := range mappings {
key := outputKey{Universe: m.ToUniverse, Protocol: m.Protocol}
if _, ok := state[key]; !ok {
state[key] = &[512]byte{}
}
}
return &Engine{
mappings: mappings,
bySource: bySource,
state: state,
}
}
// Remap applies mappings to incoming DMX data and returns outputs
func (e *Engine) Remap(srcUniverse artnet.Universe, srcData [512]byte) []Output {
mappings, ok := e.bySource[srcUniverse]
func (e *Engine) Remap(srcProto config.Protocol, srcUniverse artnet.Universe, srcData [512]byte) []Output {
key := sourceKey{Universe: srcUniverse, Protocol: srcProto}
mappings, ok := e.bySource[key]
if !ok {
return nil
}
// Group outputs by destination universe
outputs := make(map[artnet.Universe]*Output)
e.stateMu.Lock()
defer e.stateMu.Unlock()
// Track which outputs are affected by this input
affected := make(map[outputKey]bool)
for _, m := range mappings {
// Get or create output for this destination universe
out, ok := outputs[m.ToUniverse]
if !ok {
out = &Output{
Universe: m.ToUniverse,
}
outputs[m.ToUniverse] = out
}
outKey := outputKey{Universe: m.ToUniverse, Protocol: m.Protocol}
affected[outKey] = true
// Copy channels
// Update state for this output
outState := e.state[outKey]
// Copy channels into persistent state
for i := 0; i < m.Count; i++ {
srcChan := m.FromChannel + i
dstChan := m.ToChannel + i
if srcChan < 512 && dstChan < 512 {
out.Data[dstChan] = srcData[srcChan]
outState[dstChan] = srcData[srcChan]
}
}
}
// Convert map to slice
result := make([]Output, 0, len(outputs))
for _, out := range outputs {
result = append(result, *out)
// Return outputs for all affected universes
result := make([]Output, 0, len(affected))
for outKey := range affected {
result = append(result, Output{
Universe: outKey.Universe,
Protocol: outKey.Protocol,
Data: *e.state[outKey],
})
}
return result
@@ -72,18 +106,24 @@ func (e *Engine) Remap(srcUniverse artnet.Universe, srcData [512]byte) []Output
// SourceUniverses returns all universes that have mappings
func (e *Engine) SourceUniverses() []artnet.Universe {
result := make([]artnet.Universe, 0, len(e.bySource))
for u := range e.bySource {
seen := make(map[artnet.Universe]bool)
for key := range e.bySource {
seen[key.Universe] = true
}
result := make([]artnet.Universe, 0, len(seen))
for u := range seen {
result = append(result, u)
}
return result
}
// DestUniverses returns all destination universes
// DestUniverses returns all destination universes (for ArtNet discovery)
func (e *Engine) DestUniverses() []artnet.Universe {
seen := make(map[artnet.Universe]bool)
for _, m := range e.mappings {
seen[m.ToUniverse] = true
if m.Protocol == config.ProtocolArtNet {
seen[m.ToUniverse] = true
}
}
result := make([]artnet.Universe, 0, len(seen))