-
Notifications
You must be signed in to change notification settings - Fork 13
/
simplehiddev.go
45 lines (38 loc) · 986 Bytes
/
simplehiddev.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
package led
import (
"errors"
"github.com/boombuler/hid"
"image/color"
)
type simpleHidDevice struct {
device hid.Device
keepAlive bool
setColorFn func(d hid.Device, c color.Color) error
}
// Error: Device was already closed and is not ready for interaction anymore
var ErrDeviceClosed = errors.New("Device is already closed")
// SetKeepActive sets a value that tells the device not turn off the device on calling Close
func (s *simpleHidDevice) SetKeepActive(v bool) error {
if s.device != nil {
s.keepAlive = v
return nil
}
return ErrDeviceClosed
}
// Close the device and release all resources
func (s *simpleHidDevice) Close() {
if s.device != nil {
if !s.keepAlive {
s.SetColor(color.Black)
}
s.device.Close()
s.device = nil
}
}
// SetColor sets the color of the LED to the closest supported color.
func (s *simpleHidDevice) SetColor(c color.Color) error {
if s.device != nil {
return s.setColorFn(s.device, c)
}
return ErrDeviceClosed
}