reverse working and alternating pattern added

Signed-off-by: Martyn Ranyard <m@rtyn.berlin>
This commit is contained in:
Martyn 2020-05-16 11:18:07 +02:00
parent 1c5124b78a
commit 2202ba2fc0
1 changed files with 153 additions and 109 deletions

262
main.go
View File

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