-
Notifications
You must be signed in to change notification settings - Fork 1
/
blinkstick.go
84 lines (70 loc) · 1.83 KB
/
blinkstick.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
package blinkstick
import (
"fmt"
"image/color"
"github.com/yesnault/hid"
)
// Version of Blinkstick
// One Line for this, used by release.sh script
// Keep "const Version on one line"
const Version = "0.1.7"
// vendorID blinkstick
const vendorID = 0x20a0
// productID blinkstick
const productID = 0x41e5
// usbDevice ...
type usbDevice struct {
DeviceInfo *hid.DeviceInfo
Device *hid.Device
}
// Blinkstick represents a blinkstick device
type Blinkstick interface {
List() []Blinkstick
SetColor(color.Color) error
Blink(color.Color, int, int) error
GetDeviceInfo() *hid.DeviceInfo
ListFilter(hid *hid.DeviceInfo) (bool, Blinkstick)
getUSBDevice() *usbDevice
}
// SetColor set color
func (usbDevice *usbDevice) setColor(c color.Color, index byte) error {
if usbDevice.Device == nil {
if err := usbDevice.Open(); err != nil {
return err
}
}
r, g, b, _ := c.RGBA()
d := *usbDevice.Device
return d.WriteFeature([]byte{0x05, 0x00, index, byte(r >> 8), byte(g >> 8), byte(b >> 8)})
}
// Open open a device
func (usbDevice *usbDevice) Open() error {
device, err := usbDevice.DeviceInfo.Open()
if err != nil {
return fmt.Errorf("Error while opening device: %s", err)
}
usbDevice.Device = &device
return nil
}
// ListFilter is used to filter device on List
type ListFilter func(*hid.DeviceInfo) (bool, Blinkstick)
// List gets all blinkstick device
func List(opts ...ListFilter) []Blinkstick {
out := []Blinkstick{}
if len(opts) == 0 {
opts = append(opts, Nano{}.ListFilter)
opts = append(opts, Flex{}.ListFilter)
opts = append(opts, Strip{}.ListFilter)
}
for di := range hid.Devices() {
if di.VendorId == vendorID && di.ProductId == productID {
// fmt.Printf("di: %+v", di)
for _, o := range opts {
if toKeep, blinkstick := o(di); toKeep {
out = append(out, blinkstick)
}
}
}
}
return out
}