LEDController/main.go

154 lines
4.1 KiB
Go
Executable File

package main
import (
"log"
"time"
"fmt"
"github.com/Hundemeier/go-sacn/sacn"
)
func even(number int) bool {
return number%2 == 0
}
func slice_rearrange(rowwidth int, rows int, alternaterows bool, inslice []byte) [][]byte {
var flippedslice []byte
if alternaterows {
flippedslice = make([]byte, len(inslice)+3)
for r := 0; r < rows; r++ {
rowzero := (r*rowwidth*3)
if even(r) {
for c := 0; c < (rowwidth)*3; c+=3 {
flippedslice[rowzero+c] = inslice[rowzero+c]
flippedslice[rowzero+c+1] = inslice[rowzero+c+1]
flippedslice[rowzero+c+2] = inslice[rowzero+c+2]
}
} else {
x := rowwidth*3
for c := 0; c < (rowwidth)*3; c+=3 {
x = x-3
fmt.Println(x)
flippedslice[rowzero+x] = inslice[rowzero+c]
flippedslice[rowzero+x+1] = inslice[rowzero+c+1]
flippedslice[rowzero+x+2] = inslice[rowzero+c+2]/*
flippedslice[rowzero+c] = inslice[rowzero+(((rowwidth-1)*3)-c)]
flippedslice[rowzero+c+1] = inslice[rowzero+(((rowwidth-1)*3)-c)-1]
flippedslice[rowzero+c+2] = inslice[rowzero+(((rowwidth-1)*3)-c)-2]*/
}
}
}
} else {
flippedslice = inslice
}
currentUniverse := 0
currentUniversePosition := 0
universes := make([][]byte, (len(flippedslice)/510)+1)
currentUniverseSlice := make([]byte, 511)
currentRowReverse := false;
for i := range(flippedslice) {
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] = flippedslice[i]
currentUniversePosition += 1
}
universes[currentUniverse] = currentUniverseSlice
return universes
}
func alternate(R0 byte, G0 byte, B0 byte, R1 byte, G1 byte, B1 byte, fromX int, toX int) []byte {
ret := make([]byte, toX*3);
for i := fromX; i < toX*3; i+=3 {
if even(i) {
ret[i] = R0
ret[i+1] = G0
ret[i+2] = B0
} else {
ret[i] = R1
ret[i+1] = G1
ret[i+2] = B1
}
}
return ret
}
// 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,true,gradient(255,255,255,0,0,0,0,272))
ch1 <- slice512(channels[0])
ch2 <- slice512(channels[1])
time.Sleep(500 * time.Millisecond)
}
}