-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws_output.go
60 lines (52 loc) · 946 Bytes
/
ws_output.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
package main
import (
"github.com/e-asphyx/rpiws"
"log"
)
type WSOutput struct {
driver *rpiws.Driver
chnum int
ch chan []Color
notify chan int
}
func NewWSOutput(driver *rpiws.Driver, channel int) *WSOutput {
out := WSOutput{
driver: driver,
chnum: channel,
ch: make(chan []Color, 1),
notify: make(chan int),
}
go out.serve()
return &out
}
func (out *WSOutput) serve() {
for {
buf, ok := <-out.ch
if !ok {
break
}
leds := out.driver.Channel[out.chnum].Leds()
for i, led := range buf {
if i < len(leds) {
leds[i] = rpiws.RGB(led.R(), led.G(), led.B())
}
}
if err := out.driver.Render(); err != nil {
log.Println(err)
break
}
}
out.notify <- 1
}
func (out *WSOutput) Write(leds []Color) (n int, err error) {
if len(out.ch) < cap(out.ch) {
out.ch <- leds
return len(leds), nil
} else {
return 0, nil
}
}
func (out *WSOutput) Stop() {
close(out.ch)
<-out.notify
}