-
Notifications
You must be signed in to change notification settings - Fork 1
/
driver.go
103 lines (86 loc) · 2.39 KB
/
driver.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package luxafor
import (
"time"
"github.com/karalabe/hid"
"github.com/pkg/errors"
)
// Luxafor is used to access the devices.
type Luxafor struct {
deviceInfo hid.DeviceInfo
}
const (
vendorID uint16 = 0x04d8
deviceID uint16 = 0xf372
)
// Enumerate returns a slice of attached Luxafors
func Enumerate() []Luxafor {
infos := hid.Enumerate(vendorID, deviceID)
luxs := make([]Luxafor, len(infos))
for _, info := range infos {
lux := Luxafor{
deviceInfo: info,
}
luxs = append(luxs, lux)
}
return luxs
}
func (lux Luxafor) sendCommand(command byte, led LED, r, g, b, speed uint8) (err error) {
info := lux.deviceInfo
device, err := info.Open()
if err != nil {
return errors.Wrap(err, "open lux")
}
defer func() { _ = device.Close() }() // Best effort.
// Sets specified LED to RGB.
if _, err := device.Write([]byte{command, byte(led), r, g, b}); err != nil {
return errors.Wrap(err, "device write")
}
return nil
}
// Solid turns the specified luxafor into a solid RGB color.
func (lux Luxafor) Solid(r, g, b uint8) (err error) {
return lux.Set(All, r, g, b)
}
// Set sets a golux.LED to the specific RGB value.
func (lux Luxafor) Set(led LED, r, g, b uint8) (err error) {
return lux.sendCommand(static, led, r, g, b, 0) // speed isn't used
}
// Sets sets multiple golux.LED to the specific RGB value.
func (lux Luxafor) Sets(leds []LED, r, g, b uint8) (err error) {
for _, led := range leds {
if err := lux.Set(led, r, g, b); err != nil {
return errors.Wrap(err, "set led")
}
}
return nil
}
// Fade sets the led to rgb at speed.
func (lux Luxafor) Fade(led LED, r, g, b, speed uint8) (err error) {
return lux.sendCommand(fade, led, r, g, b, speed)
}
// Police look like da popo
func (lux Luxafor) Police(loops int) (err error) {
for i := 0; i < loops; i++ {
lux.Fade(FrontAll, 255, 0, 0, 255)
lux.Fade(BackAll, 0, 0, 255, 255)
time.Sleep(500 * time.Millisecond)
lux.Fade(FrontAll, 0, 0, 255, 255)
lux.Fade(BackAll, 255, 0, 0, 255)
time.Sleep(500 * time.Millisecond)
}
return nil
}
// Off turns off the luxafor.
func (lux Luxafor) Off() (err error) {
info := lux.deviceInfo
device, err := info.Open()
if err != nil {
return errors.Wrap(err, "open lux")
}
defer func() { _ = device.Close() }() // Best effort.
// Turns off the leds.
if _, err := device.Write([]byte{static, byte(All), 0, 0, 0}); err != nil {
return errors.Wrap(err, "device write")
}
return nil
}