Skip to content

Commit f1e8967

Browse files
committed
Add NewWithID method to connect to a specific device
1 parent 4780578 commit f1e8967

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

comms.go

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,51 @@ func OpenWithoutReset() (*Device, error) {
7878
return rawOpen(false)
7979
}
8080

81+
// Open a Streamdeck device with a specific ProductID
82+
func OpenWithID(productID uint16) (*Device, error) {
83+
return rawOpenWithID(true, productID)
84+
}
85+
86+
// Opens a new Streamdeck device, and returns a handle
87+
func rawOpenWithID(reset bool, productID uint16) (*Device, error) {
88+
devices := hid.Enumerate(vendorID, 0)
89+
if len(devices) == 0 {
90+
return nil, errors.New("No elgato devices found")
91+
}
92+
93+
retval := &Device{}
94+
deviceConnected := false
95+
for _, device := range devices {
96+
if device.ProductID != productID {
97+
continue
98+
}
99+
100+
for _, devType := range deviceTypes {
101+
if device.ProductID == devType.usbProductID {
102+
retval.deviceType = devType
103+
dev, err := device.Open()
104+
if err != nil {
105+
return nil, err
106+
}
107+
retval.fd = dev
108+
if reset {
109+
retval.ResetComms()
110+
}
111+
go retval.buttonPressListener()
112+
return retval, nil
113+
}
114+
}
115+
116+
deviceConnected = true
117+
break
118+
}
119+
if !deviceConnected {
120+
return nil, errors.New("No device connected with given product ID.")
121+
}
122+
123+
return nil, errors.New("No device registered for requested product ID.")
124+
}
125+
81126
// Opens a new StreamdeckXL device, and returns a handle
82127
func rawOpen(reset bool) (*Device, error) {
83128
devices := hid.Enumerate(vendorID, 0)
@@ -137,12 +182,12 @@ func (d *Device) SetBrightness(pct int) error {
137182
}
138183

139184
// GetButtonImageSize returns the size of the images to uploaded to the buttons
140-
func (d* Device) GetButtonImageSize() image.Point {
185+
func (d *Device) GetButtonImageSize() image.Point {
141186
return d.deviceType.imageSize
142187
}
143188

144189
// GetNumButtonsOnDevice returns the number of button this device has
145-
func (d* Device) GetNumButtonsOnDevice() uint {
190+
func (d *Device) GetNumButtonsOnDevice() uint {
146191
return d.deviceType.numberOfButtons
147192
}
148193

streamdeck.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ func New() (*StreamDeck, error) {
4747
return sd, nil
4848
}
4949

50+
// NewWithID will return a new instance of a `StreamDeck`. It will return an error if there is no StreamDeck plugged in with the given ID.
51+
func NewWithID(productID uint16) (*StreamDeck, error) {
52+
sd := &StreamDeck{}
53+
d, err := OpenWithID(productID)
54+
if err != nil {
55+
return nil, err
56+
}
57+
sd.dev = d
58+
sd.buttons = make(map[int]Button)
59+
sd.decorators = make(map[int]ButtonDecorator)
60+
sd.dev.ButtonPress(sd.pressHandler)
61+
return sd, nil
62+
}
63+
5064
// GetName returns the name of the type of Streamdeck
5165
func (sd *StreamDeck) GetName() string {
5266
return sd.dev.deviceType.name

0 commit comments

Comments
 (0)