-
Notifications
You must be signed in to change notification settings - Fork 22
/
comms.go
300 lines (263 loc) · 7.96 KB
/
comms.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package streamdeck
import (
"errors"
"fmt"
"image"
"image/color"
"github.com/karalabe/hid"
)
const vendorID = 0x0fd9
// deviceType represents one of the various types of StreamDeck (mini/orig/orig2/xl)
type deviceType struct {
name string
imageSize image.Point
usbProductID uint16
resetPacket []byte
numberOfButtons uint
buttonRows uint
buttonCols uint
brightnessPacket []byte
buttonReadOffset uint
imageFormat string
imagePayloadPerPage uint
imageHeaderFunc func(bytesRemaining uint, btnIndex uint, pageNumber uint) []byte
}
var deviceTypes []deviceType
// RegisterDevicetype allows the declaration of a new type of device, intended for use by subpackage "devices"
func RegisterDevicetype(
name string,
imageSize image.Point,
usbProductID uint16,
resetPacket []byte,
numberOfButtons uint,
buttonRows uint,
buttonCols uint,
brightnessPacket []byte,
buttonReadOffset uint,
imageFormat string,
imagePayloadPerPage uint,
imageHeaderFunc func(bytesRemaining uint, btnIndex uint, pageNumber uint) []byte,
) {
d := deviceType{
name: name,
imageSize: imageSize,
usbProductID: usbProductID,
resetPacket: resetPacket,
numberOfButtons: numberOfButtons,
buttonRows: buttonRows,
buttonCols: buttonCols,
brightnessPacket: brightnessPacket,
buttonReadOffset: buttonReadOffset,
imageFormat: imageFormat,
imagePayloadPerPage: imagePayloadPerPage,
imageHeaderFunc: imageHeaderFunc,
}
deviceTypes = append(deviceTypes, d)
}
// Device is a struct which represents an actual Streamdeck device, and holds its reference to the USB HID device
type Device struct {
fd *hid.Device
deviceType deviceType
buttonPressListeners []func(int, *Device, error)
}
// Open a Streamdeck device, the most common entry point
func Open() (*Device, error) {
return rawOpen(true)
}
// OpenWithoutReset will open a Streamdeck device, without resetting it
func OpenWithoutReset() (*Device, error) {
return rawOpen(false)
}
// Opens a new StreamdeckXL device, and returns a handle
func rawOpen(reset bool) (*Device, error) {
devices := hid.Enumerate(vendorID, 0)
if len(devices) == 0 {
return nil, errors.New("No elgato devices found")
}
retval := &Device{}
for _, device := range devices {
// Iterate over the known device types, matching to product ID
for _, devType := range deviceTypes {
if device.ProductID == devType.usbProductID {
retval.deviceType = devType
dev, err := device.Open()
if err != nil {
return nil, err
}
retval.fd = dev
if reset {
retval.ResetComms()
}
go retval.buttonPressListener()
return retval, nil
}
}
}
return nil, errors.New("Found an Elgato device, but not one for which there is a definition; have you imported the devices package?")
}
// GetName returns the name of the type of Streamdeck
func (d *Device) GetName() string {
return d.deviceType.name
}
// Close the device
func (d *Device) Close() {
d.fd.Close()
}
// SetBrightness sets the button brightness
// pct is an integer between 0-100
func (d *Device) SetBrightness(pct int) error {
if pct < 0 {
pct = 0
}
if pct > 100 {
pct = 100
}
preamble := d.deviceType.brightnessPacket
payload := append(preamble, byte(pct))
_, err := d.fd.SendFeatureReport(payload)
if err != nil {
return err
}
return nil
}
// GetButtonImageSize returns the size of the images to uploaded to the buttons
func (d* Device) GetButtonImageSize() image.Point {
return d.deviceType.imageSize
}
// GetNumButtonsOnDevice returns the number of button this device has
func (d* Device) GetNumButtonsOnDevice() uint {
return d.deviceType.numberOfButtons
}
// ClearButtons writes a black square to all buttons
func (d *Device) ClearButtons() error {
numButtons := int(d.deviceType.numberOfButtons)
for i := 0; i < numButtons; i++ {
err := d.WriteColorToButton(i, color.Black)
if err != nil {
return err
}
}
return nil
}
// WriteColorToButton writes a specified color to the given button
func (d *Device) WriteColorToButton(btnIndex int, colour color.Color) error {
img := getSolidColourImage(colour, d.deviceType.imageSize.X)
imgForButton, err := getImageForButton(img, d.deviceType.imageFormat)
if err != nil {
return err
}
return d.rawWriteToButton(btnIndex, imgForButton)
}
// WriteImageToButton writes a specified image file to the given button
func (d *Device) WriteImageToButton(btnIndex int, filename string) error {
img, err := getImageFile(filename)
if err != nil {
return err
}
err = d.WriteRawImageToButton(btnIndex, img)
if err != nil {
return err
}
return nil
}
func (d *Device) buttonPressListener() {
var buttonMask []bool
buttonMask = make([]bool, d.deviceType.numberOfButtons)
for {
data := make([]byte, d.deviceType.numberOfButtons+d.deviceType.buttonReadOffset)
_, err := d.fd.Read(data)
if err != nil {
d.sendButtonPressEvent(-1, err)
break
}
for i := uint(0); i < d.deviceType.numberOfButtons; i++ {
if data[d.deviceType.buttonReadOffset+i] == 1 {
if !buttonMask[i] {
d.sendButtonPressEvent(int(i), nil)
}
buttonMask[i] = true
} else {
buttonMask[i] = false
}
}
}
}
func (d *Device) sendButtonPressEvent(btnIndex int, err error) {
for _, f := range d.buttonPressListeners {
f(btnIndex, d, err)
}
}
// ButtonPress registers a callback to be called whenever a button is pressed
func (d *Device) ButtonPress(f func(int, *Device, error)) {
d.buttonPressListeners = append(d.buttonPressListeners, f)
}
// ResetComms will reset the comms protocol to the StreamDeck; useful if things have gotten de-synced, but it will also reboot the StreamDeck
func (d *Device) ResetComms() error {
payload := d.deviceType.resetPacket
_, err := d.fd.SendFeatureReport(payload)
return err
}
// WriteRawImageToButton takes an `image.Image` and writes it to the given button, after resizing and rotating the image to fit the button (for some reason the StreamDeck screens are all upside down)
func (d *Device) WriteRawImageToButton(btnIndex int, rawImg image.Image) error {
img := resizeAndRotate(rawImg, d.deviceType.imageSize.X, d.deviceType.imageSize.Y, d.deviceType.name)
imgForButton, err := getImageForButton(img, d.deviceType.imageFormat)
if err != nil {
return err
}
return d.rawWriteToButton(btnIndex, imgForButton)
}
func (d *Device) rawWriteToButton(btnIndex int, rawImage []byte) error {
// Based on set_key_image from https://github.com/abcminiuser/python-elgato-streamdeck/blob/master/src/StreamDeck/Devices/StreamDeckXL.py#L151
if Min(Max(btnIndex, 0), int(d.deviceType.numberOfButtons)) != btnIndex {
return errors.New(fmt.Sprintf("Invalid key index: %d", btnIndex))
}
pageNumber := 0
bytesRemaining := len(rawImage)
halfImage := len(rawImage) / 2
bytesSent := 0
for bytesRemaining > 0 {
header := d.deviceType.imageHeaderFunc(uint(bytesRemaining), uint(btnIndex), uint(pageNumber))
imageReportLength := int(d.deviceType.imagePayloadPerPage)
imageReportHeaderLength := len(header)
imageReportPayloadLength := imageReportLength - imageReportHeaderLength
/*
if halfImage > imageReportPayloadLength {
log.Fatalf("image too large: %d", halfImage*2)
}
*/
thisLength := 0
if imageReportPayloadLength < bytesRemaining {
if d.deviceType.name == "Stream Deck Original" {
thisLength = halfImage
} else {
thisLength = imageReportPayloadLength
}
} else {
thisLength = bytesRemaining
}
payload := append(header, rawImage[bytesSent:(bytesSent+thisLength)]...)
padding := make([]byte, imageReportLength-len(payload))
thingToSend := append(payload, padding...)
_, err := d.fd.Write(thingToSend)
if err != nil {
return err
}
bytesRemaining = bytesRemaining - thisLength
pageNumber = pageNumber + 1
bytesSent = bytesSent + thisLength
}
return nil
}
// Golang Min/Max
func Min(x, y int) int {
if x < y {
return x
}
return y
}
func Max(x, y int) int {
if x > y {
return x
}
return y
}