simplified rgb handling
This commit is contained in:
86
ledd.go
86
ledd.go
@@ -76,9 +76,9 @@ type Client struct {
|
||||
}
|
||||
|
||||
type LED struct {
|
||||
name string
|
||||
channel []int32
|
||||
backend string
|
||||
Name string
|
||||
Channel []int32
|
||||
Backend string
|
||||
color chan colorful.Color
|
||||
}
|
||||
|
||||
@@ -115,11 +115,11 @@ func setupSocket(host string, port int, logTag string, backend bool) (func(), er
|
||||
}
|
||||
log.Infof("[%s] New connection from %s", logTag, conn.RemoteAddr())
|
||||
if backend {
|
||||
backend := &Backend{socket: conn, data: make(chan []byte)}
|
||||
backend := &Backend{socket: conn, data: make(chan []byte, 20)}
|
||||
go backManager.receive(backend)
|
||||
go backManager.send(backend)
|
||||
} else {
|
||||
client := &Client{socket: conn, data: make(chan []byte)}
|
||||
client := &Client{socket: conn, data: make(chan []byte, 20)}
|
||||
go clientManager.receive(client)
|
||||
go clientManager.send(client)
|
||||
}
|
||||
@@ -133,24 +133,29 @@ func (manager *LEDManager) start() {
|
||||
for {
|
||||
select {
|
||||
case led := <-manager.add:
|
||||
log.Debugf("[%s] Request to add LED: %s (%s)", led.backend, led.name, led.channel)
|
||||
log.Debugf("[%s] Request to add LED: %s (%s)", led.Backend, led.Name, led.Channel)
|
||||
|
||||
if led.name == "" || len(led.channel) == 0 || led.backend == "" {
|
||||
if led.Name == "" || len(led.Channel) == 0 || led.Backend == "" {
|
||||
log.Warningf("[%s] Can't add LED without required information! (%s)", LOG_CLIENTS, led)
|
||||
continue
|
||||
}
|
||||
|
||||
manager.leds[led.name] = led
|
||||
if _, ok := manager.leds[led.Name]; ok {
|
||||
log.Warningf("[%s] Can't add LED: already existent! (%s)", LOG_CLIENTS, led.Name)
|
||||
continue
|
||||
}
|
||||
|
||||
manager.leds[led.Name] = led
|
||||
go manager.color(led)
|
||||
err := LEDCollection.Insert(led)
|
||||
if err != nil {
|
||||
log.Warning("[%s] Error while adding LED to database: %s", LOG_BACKEND, err)
|
||||
}
|
||||
case led := <-manager.remove:
|
||||
if _, ok := manager.leds[led.name]; ok {
|
||||
log.Debugf("[%s] Request to remove %s", led.backend, led.name)
|
||||
LEDCollection.RemoveAll(bson.M{"name": led.name})
|
||||
delete(manager.leds, led.name)
|
||||
if _, ok := manager.leds[led.Name]; ok {
|
||||
log.Debugf("[%s] Request to remove %s", led.Backend, led.Name)
|
||||
LEDCollection.RemoveAll(bson.M{"name": led.Name})
|
||||
delete(manager.leds, led.Name)
|
||||
}
|
||||
case color := <-manager.broadcast:
|
||||
for _, led := range manager.leds {
|
||||
@@ -166,18 +171,17 @@ func (manager *LEDManager) color(led *LED) {
|
||||
for {
|
||||
select {
|
||||
case color := <-led.color:
|
||||
if backend, ok := backManager.backends[led.backend]; ok {
|
||||
if len(led.channel) != 3 {
|
||||
log.Warningf("[%s] Currently only RGB LEDs are supported", led.name)
|
||||
if backend, ok := backManager.backends[led.Backend]; ok {
|
||||
if len(led.Channel) != 3 {
|
||||
log.Warningf("[%s] Currently only RGB LEDs are supported", led.Name)
|
||||
return
|
||||
}
|
||||
|
||||
cMap := make(map[int32]int32)
|
||||
r, g, b := color.Clamped().RGB255()
|
||||
|
||||
cMap[led.channel[0]] = int32((float64(r) / 255) * float64(backend.resolution))
|
||||
cMap[led.channel[1]] = int32((float64(g) / 255) * float64(backend.resolution))
|
||||
cMap[led.channel[2]] = int32((float64(b) / 255) * float64(backend.resolution))
|
||||
cMap[led.Channel[0]] = int32(color.Clamped().R * float64(backend.resolution))
|
||||
cMap[led.Channel[1]] = int32(color.Clamped().G * float64(backend.resolution))
|
||||
cMap[led.Channel[2]] = int32(color.Clamped().B * float64(backend.resolution))
|
||||
|
||||
wrapperMsg := &ledd.BackendWrapperMessage{
|
||||
Msg: &ledd.BackendWrapperMessage_MSetChannel{
|
||||
@@ -186,12 +190,12 @@ func (manager *LEDManager) color(led *LED) {
|
||||
|
||||
data, err := proto.Marshal(wrapperMsg)
|
||||
if err != nil {
|
||||
log.Warningf("[%s] Failed to encode protobuf msg to %s: %s", led.name, backend.name, err)
|
||||
log.Warningf("[%s] Failed to encode protobuf msg to %s: %s", led.Name, backend.name, err)
|
||||
}
|
||||
|
||||
backend.data <- prepareProtobuf(data)
|
||||
} else {
|
||||
log.Warningf("[LM] Failed to set color for %s: backend %s not found", led.name, led.backend)
|
||||
log.Warningf("[LM] Failed to set color for %s: backend %s not found", led.Name, led.Backend)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -272,7 +276,7 @@ func (manager *BackendManager) receive(backend *Backend) {
|
||||
if length > 0 {
|
||||
msgLen := binary.BigEndian.Uint32(message[0:4])
|
||||
|
||||
// log.Debugf("[%s] Read %d bytes, first protobuf is %d long", backend.niceName(), length, msgLen)
|
||||
// log.Debugf("[%s] Read %d bytes, first protobuf is %d long", Backend.niceName(), length, msgLen)
|
||||
|
||||
backendMsg := &ledd.BackendWrapperMessage{}
|
||||
err = proto.Unmarshal(message[4:msgLen+4], backendMsg)
|
||||
@@ -310,7 +314,7 @@ func (manager *ClientManager) start() {
|
||||
|
||||
for _, led := range ledManager.leds {
|
||||
leds = append(leds, &ledd.LED{
|
||||
Name: led.name,
|
||||
Name: led.Name,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -407,7 +411,7 @@ func (manager *ClientManager) receive(client *Client) {
|
||||
allLED := make([]*ledd.LED, 0)
|
||||
|
||||
for _, led := range ledManager.leds {
|
||||
allLED = append(allLED, &ledd.LED{Name: led.name})
|
||||
allLED = append(allLED, &ledd.LED{Name: led.Name})
|
||||
}
|
||||
|
||||
data, err := proto.Marshal(&ledd.ClientWrapperMessage{Leds: allLED})
|
||||
@@ -430,10 +434,10 @@ func (manager *ClientManager) receive(client *Client) {
|
||||
}
|
||||
|
||||
nLED := &LED{
|
||||
name: msg.MAddLed.Name,
|
||||
channel: msg.MAddLed.Channel,
|
||||
backend: backend.name,
|
||||
color: make(chan colorful.Color),
|
||||
Name: msg.MAddLed.Name,
|
||||
Channel: msg.MAddLed.Channel,
|
||||
Backend: backend.name,
|
||||
color: make(chan colorful.Color, 20),
|
||||
}
|
||||
|
||||
ledManager.add <- nLED
|
||||
@@ -451,9 +455,13 @@ func (manager *ClientManager) receive(client *Client) {
|
||||
log.Warningf("[%s] Failed to set %s: not found", client.socket.RemoteAddr(), pLED.Name)
|
||||
break
|
||||
}
|
||||
// log.Debugf("[%s] Set %s to %s", client.socket.RemoteAddr(), led.name, colorful.Hcl(msg.MSetLed.Colour.Hue, msg.MSetLed.Colour.Chroma, msg.MSetLed.Colour.Light))
|
||||
// log.Debugf("[%s] Set %s to %s", client.socket.RemoteAddr(), led.Name, colorful.Hcl(msg.MSetLed.Colour.Hue, msg.MSetLed.Colour.Chroma, msg.MSetLed.Colour.Light))
|
||||
|
||||
led.color <- colorful.Hcl(msg.MSetLed.Colour.Hue, msg.MSetLed.Colour.Chroma, msg.MSetLed.Colour.Light)
|
||||
if pLED.Color == nil {
|
||||
led.color <- colorful.Hcl(msg.MSetLed.Colour.Hue, msg.MSetLed.Colour.Chroma, msg.MSetLed.Colour.Light)
|
||||
} else {
|
||||
led.color <- colorful.Hcl(pLED.Color.Hue, pLED.Color.Chroma, pLED.Color.Light)
|
||||
}
|
||||
}
|
||||
case *ledd.ClientWrapperMessage_MSetDirect:
|
||||
backend, ok := backManager.backends[msg.MSetDirect.Backend]
|
||||
@@ -539,27 +547,27 @@ func main() {
|
||||
|
||||
backManager = BackendManager{
|
||||
backends: make(map[string]*Backend),
|
||||
broadcast: make(chan []byte),
|
||||
register: make(chan *Backend),
|
||||
unregister: make(chan *Backend),
|
||||
broadcast: make(chan []byte, 20),
|
||||
register: make(chan *Backend, 10),
|
||||
unregister: make(chan *Backend, 10),
|
||||
}
|
||||
|
||||
go backManager.start()
|
||||
|
||||
clientManager = ClientManager{
|
||||
clients: make(map[*Client]bool),
|
||||
broadcast: make(chan []byte),
|
||||
register: make(chan *Client),
|
||||
unregister: make(chan *Client),
|
||||
broadcast: make(chan []byte, 20),
|
||||
register: make(chan *Client, 10),
|
||||
unregister: make(chan *Client, 10),
|
||||
}
|
||||
|
||||
go clientManager.start()
|
||||
|
||||
ledManager = LEDManager{
|
||||
leds: make(map[string]*LED),
|
||||
broadcast: make(chan colorful.Color),
|
||||
add: make(chan *LED),
|
||||
remove: make(chan *LED),
|
||||
broadcast: make(chan colorful.Color, 10),
|
||||
add: make(chan *LED, 10),
|
||||
remove: make(chan *LED, 10),
|
||||
}
|
||||
|
||||
go ledManager.start()
|
||||
|
Reference in New Issue
Block a user