forked from BaaaZen/go-busylight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
busylight.go
89 lines (78 loc) · 2.1 KB
/
busylight.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package led
import (
"image/color"
"time"
"github.com/baaazen/go-hid"
)
// Device type: BusyLight UC
var BusyLightUC DeviceType
// Device type: BusyLight Lync
var BusyLightLync DeviceType
func init() {
BusyLightUC = addDriver(usbDriver{
Name: "BusyLight UC",
Type: &BusyLightUC,
VendorId: 0x27BB,
ProductId: 0x3BCB,
Open: func(d hid.Device) (Device, error) {
return newBusyLight(d, func(c color.Color) {
r, g, b, _ := c.RGBA()
d.Write([]byte{0x00, 0x00, 0x00, byte(r >> 8), byte(g >> 8), byte(b >> 8), 0x00, 0x00, 0x00})
}), nil
},
})
BusyLightLync = addDriver(usbDriver{
Name: "BusyLight Lync",
Type: &BusyLightLync,
VendorId: 0x04D8,
ProductId: 0xF848,
Open: func(d hid.Device) (Device, error) {
return newBusyLight(d, func(c color.Color) {
r, g, b, _ := c.RGBA()
d.Write([]byte{0x00, 0x00, 0x00, byte(r >> 8), byte(g >> 8), byte(b >> 8), 0x00, 0x00, 0x00})
}), nil
},
})
}
type busylightDev struct {
closeChan chan<- struct{}
colorChan chan<- color.Color
closed *bool
}
func newBusyLight(d hid.Device, setcolorFn func(c color.Color)) *busylightDev {
closeChan := make(chan struct{})
colorChan := make(chan color.Color)
ticker := time.NewTicker(20 * time.Second) // If nothing is send after 30 seconds the device turns off.
closed := false
go func() {
var curColor color.Color = color.Black
for !closed {
select {
case <-ticker.C:
setcolorFn(curColor)
case col := <-colorChan:
curColor = col
setcolorFn(curColor)
case <-closeChan:
ticker.Stop()
setcolorFn(color.Black) // turn off device
d.Close()
closed = true
}
}
}()
return &busylightDev{closeChan: closeChan, colorChan: colorChan, closed: &closed}
}
func (d *busylightDev) SetKeepActive(v bool) error {
return ErrKeepActiveNotSupported
}
func (d *busylightDev) SetColor(c color.Color) error {
d.colorChan <- c
return nil
}
func (d *busylightDev) Close() {
d.closeChan <- struct{}{}
}
func (d *busylightDev) IsClosed() bool {
return *d.closed
}