Base working

Signed-off-by: Martyn Ranyard <m@rtyn.berlin>
This commit is contained in:
Martyn 2020-05-16 10:39:18 +02:00
parent 7d9ebd206e
commit 1c5124b78a
1 changed files with 110 additions and 0 deletions

110
main.go Executable file
View File

@ -0,0 +1,110 @@
package main
import (
"log"
"time"
"fmt"
"github.com/Hundemeier/go-sacn/sacn"
)
func slice_rearrange(rowwidth int, rows int, alternaterows bool, inslice []byte) [][]byte {
if alternaterows {
panic("Alternating rows not yet implemented")
}
currentUniverse := 0
currentUniversePosition := 0
universes := make([][]byte, (len(inslice)/510)+1)
currentUniverseSlice := make([]byte, 511)
currentRowReverse := false;
for i := range(inslice) {
fmt.Println(i)
if currentUniversePosition >= 510 {
fmt.Println("Reached end of universe!")
universes[currentUniverse] = currentUniverseSlice
if (!currentRowReverse) {
currentRowReverse = true
} else {
currentRowReverse = false
}
currentUniverse += 1
currentUniversePosition = 0
currentUniverseSlice = make([]byte, 511)
}
currentUniverseSlice[currentUniversePosition] = inslice[i]
currentUniversePosition += 1
}
universes[currentUniverse] = currentUniverseSlice
return universes
}
// gradient returns from fromX to toX fading from (fromR,fromG,fromB) to (toR,toG,toB)
// at least that's the idea.
func gradient(fromR byte, fromG byte, fromB byte, toR byte, toG byte, toB byte, fromX int, toX int) []byte {
ret := make([]byte, toX*3);
var stepR float32 = (float32(toR) - float32(fromR)) / (float32(toX) - float32(fromX))
var stepG float32 = (float32(toG) - float32(fromG)) / (float32(toX) - float32(fromX))
var stepB float32 = (float32(toB) - float32(fromB)) / (float32(toX) - float32(fromX))
for i := fromX; i < toX*3; i+=3 {
ret[i] = fromR+byte(float32(i/3)*stepR)
ret[i+1] = fromG+byte(float32(i/3)*stepG)
ret[i+2] = fromB+byte(float32(i/3)*stepB)
}
return ret
}
func slice512(s []byte) [512]byte {
var ret [512]byte
for i := range(s) {
if i < 512 {
ret[i] = s[i]
}
}
return ret
}
func sliceUnlenthed(s [512]byte) []byte {
ret := make([]byte, 512);
for i := range(s) {
ret[i] = s[i]
}
return ret
}
func main() {
//instead of "" you could provide an ip-address that the socket should bind to
trans, err := sacn.NewTransmitter("", [16]byte{1, 2, 3}, "test")
if err != nil {
log.Fatal(err)
}
//activates the first universe
ch1, err := trans.Activate(1)
if err != nil {
log.Fatal(err)
}
ch2, err := trans.Activate(2)
if err != nil {
log.Fatal(err)
}
//deactivate the channel on exit
defer close(ch1)
defer close(ch2)
//set a unicast destination, and/or use multicast
trans.SetMulticast(1, false)//this specific setup will not multicast on windows,
//because no bind address was provided
//set some example ip-addresses
trans.SetDestinations(1, []string{"192.168.1.139"})
trans.SetMulticast(2, false)//this specific setup will not multicast on windows,
trans.SetDestinations(2, []string{"192.168.1.139"})
//send some random data for 10 seconds
for i := 0; i < 20; i++ {
channels := slice_rearrange(68,4,false,gradient(0,0,255,255,0,0,0,272))
ch1 <- slice512(channels[0])
ch2 <- slice512(channels[1])
time.Sleep(500 * time.Millisecond)
}
}