-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
52 lines (43 loc) · 1.33 KB
/
controller.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
package blink1
import (
"fmt"
"sync"
hid "github.com/b1ug/gid"
)
// Controller provides a high-level API for operating blink(1) devices, abstracting away the low-level details.
type Controller struct {
mu sync.Mutex
dev *Device
gamma bool
quitCh chan struct{}
}
// OpenController opens a blink(1) controller for device which is connected to the system.
func OpenController(info *hid.DeviceInfo) (*Controller, error) {
dev, err := OpenDevice(info)
if err != nil {
return nil, err
}
return &Controller{dev: dev, gamma: true}, nil
}
// NewController creates a blink(1) controller for existing device instance.
func NewController(dev *Device) *Controller {
return &Controller{dev: dev, gamma: true}
}
func (c *Controller) String() string {
return fmt.Sprintf("🎮(ctrl=%q gen=%d sn=%s)", c.dev.pn, c.dev.gen, c.dev.sn)
}
// GetDevice returns the underlying blink(1) device.
func (c *Controller) GetDevice() *Device {
return c.dev
}
// Close closes the device and release the kept resources.
func (c *Controller) Close() {
c.dev.Close()
}
// SetGammaCorrection sets the gamma correction on/off for the controller. Default is on.
// If it is true, the gamma correction will be applied for state and pattern while playing or writing.
func (c *Controller) SetGammaCorrection(on bool) {
c.mu.Lock()
defer c.mu.Unlock()
c.gamma = on
}