-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (61 loc) · 1.64 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"fmt"
"github.com/adwirawien/go-opc-server/dao"
"github.com/adwirawien/go-opc-server/domain"
"net"
"os"
)
func main() {
// setup domain object
driver := domain.Ws281xDriver{}
err := driver.Setup(domain.Ws281xDefaultOptions)
if err != nil {
panic(err)
}
defer driver.Unregister()
// listen on all interfaces
address := "0.0.0.0:7890"
l, err := net.Listen("tcp", address)
if err != nil {
fmt.Println("Error listening:", err.Error())
os.Exit(1)
}
fmt.Printf("Listening on %s", address)
defer l.Close()
for {
// accept new connections and parse them
conn, err := l.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
os.Exit(1)
}
go handleOPCRequest(&driver, conn)
}
}
// handleOPCRequest is responsible for handling an incoming connection and the transmitted data respectively
func handleOPCRequest(driver dao.Driver, conn net.Conn) {
for {
buf := make([]byte, 65539)
_, err := conn.Read(buf)
if err != nil {
fmt.Println("Connection closing:", err.Error())
return
}
// parse header bytes with opc information
channel := int(buf[0])
command := int(buf[1]) // FIXME: currently only command 0 for 8-bit pixel colors is supported!
length := (int(buf[2]) << 8) + int(buf[3])
// parse pixel information
pixels := make([][]int, 0, 21845)
fmt.Printf("Chan: %d, Cmd: %d, Len: %d \n", channel, command, length)
for i := 0; i < length; i += 3 {
// use 4 byte offset due to the header bytes at the buffer start
r := int(buf[4+i+0])
g := int(buf[4+i+1])
b := int(buf[4+i+2])
pixels = append(pixels, []int{r, g, b})
}
go driver.Display(channel, pixels)
}
}