forked from muesli/streamdeck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamdeck.go
477 lines (418 loc) · 12.7 KB
/
streamdeck.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package streamdeck
import (
"bytes"
"fmt"
"image"
"image/color"
"image/jpeg"
"github.com/karalabe/hid"
"golang.org/x/image/draw"
)
const (
VID_ELGATO = 0x0fd9
PID_STREAMDECK = 0x0060
PID_STREAMDECK_V2 = 0x006d
PID_STREAMDECK_MK2 = 0x0080
PID_STREAMDECK_MINI = 0x0063
PID_STREAMDECK_XL = 0x006c
)
var (
c_REV1_FIRMWARE = []byte{0x04}
c_REV1_RESET = []byte{0x0b, 0x63}
c_REV1_BRIGHTNESS = []byte{0x05, 0x55, 0xaa, 0xd1, 0x01}
c_REV2_FIRMWARE = []byte{0x05}
c_REV2_RESET = []byte{0x03, 0x02}
c_REV2_BRIGHTNESS = []byte{0x03, 0x08}
)
// Device represents a single Stream Deck device.
type Device struct {
ID string
Serial string
Columns uint8
Rows uint8
Keys uint8
Pixels uint
DPI uint
Padding uint
featureReportSize int
firmwareOffset int
keyStateOffset int
translateKeyIndex func(index, columns uint8) uint8
imagePageSize int
imagePageHeaderSize int
toImageFormat func(image.Image) ([]byte, error)
imagePageHeader func(pageIndex int, keyIndex uint8, payloadLength int, lastPage bool) []byte
getFirmwareCommand []byte
resetCommand []byte
setBrightnessCommand []byte
keyState []byte
device *hid.Device
info hid.DeviceInfo
}
// Key holds the current status of a key on the device.
type Key struct {
Index uint8
Pressed bool
}
// Devices returns all attached Stream Decks.
func Devices() ([]Device, error) {
dd := []Device{}
devs := hid.Enumerate(VID_ELGATO, 0)
for _, d := range devs {
var dev Device
switch {
case d.VendorID == VID_ELGATO && d.ProductID == PID_STREAMDECK:
dev = Device{
ID: d.Path,
Serial: d.Serial,
Columns: 5,
Rows: 3,
Keys: 15,
Pixels: 72,
DPI: 124,
Padding: 16,
featureReportSize: 17,
firmwareOffset: 5,
keyStateOffset: 1,
translateKeyIndex: translateRightToLeft,
imagePageSize: 7819,
imagePageHeaderSize: 16,
imagePageHeader: rev1ImagePageHeader,
toImageFormat: toBMP,
getFirmwareCommand: c_REV1_FIRMWARE,
resetCommand: c_REV1_RESET,
setBrightnessCommand: c_REV1_BRIGHTNESS,
}
case d.VendorID == VID_ELGATO && d.ProductID == PID_STREAMDECK_MINI:
dev = Device{
ID: d.Path,
Serial: d.Serial,
Columns: 3,
Rows: 2,
Keys: 6,
Pixels: 80,
DPI: 138,
Padding: 16,
featureReportSize: 17,
firmwareOffset: 5,
keyStateOffset: 1,
translateKeyIndex: translateRightToLeft,
imagePageSize: 1024,
imagePageHeaderSize: 16,
imagePageHeader: rev1ImagePageHeader,
toImageFormat: toBMP,
getFirmwareCommand: c_REV1_FIRMWARE,
resetCommand: c_REV1_RESET,
setBrightnessCommand: c_REV1_BRIGHTNESS,
}
case d.VendorID == VID_ELGATO && (d.ProductID == PID_STREAMDECK_V2 || d.ProductID == PID_STREAMDECK_MK2):
dev = Device{
ID: d.Path,
Serial: d.Serial,
Columns: 5,
Rows: 3,
Keys: 15,
Pixels: 72,
DPI: 124,
Padding: 16,
featureReportSize: 32,
firmwareOffset: 6,
keyStateOffset: 4,
translateKeyIndex: identity,
imagePageSize: 1024,
imagePageHeaderSize: 8,
imagePageHeader: rev2ImagePageHeader,
toImageFormat: toJPEG,
getFirmwareCommand: c_REV2_FIRMWARE,
resetCommand: c_REV2_RESET,
setBrightnessCommand: c_REV2_BRIGHTNESS,
}
case d.VendorID == VID_ELGATO && d.ProductID == PID_STREAMDECK_XL:
dev = Device{
ID: d.Path,
Serial: d.Serial,
Columns: 8,
Rows: 4,
Keys: 32,
Pixels: 96,
DPI: 166,
Padding: 16,
featureReportSize: 32,
firmwareOffset: 6,
keyStateOffset: 4,
translateKeyIndex: identity,
imagePageSize: 1024,
imagePageHeaderSize: 8,
imagePageHeader: rev2ImagePageHeader,
toImageFormat: toJPEG,
getFirmwareCommand: c_REV2_FIRMWARE,
resetCommand: c_REV2_RESET,
setBrightnessCommand: c_REV2_BRIGHTNESS,
}
}
if dev.ID != "" {
dev.keyState = make([]byte, dev.Columns*dev.Rows)
dev.info = d
dd = append(dd, dev)
}
}
return dd, nil
}
// Open the device for input/output. This must be called before trying to
// communicate with the device.
func (d *Device) Open() error {
var err error
d.device, err = d.info.Open()
return err
}
// Close the connection with the device.
func (d Device) Close() error {
return d.device.Close()
}
// FirmwareVersion returns the firmware version of the device.
func (d Device) FirmwareVersion() (string, error) {
result, err := d.getFeatureReport(d.getFirmwareCommand)
if err != nil {
return "", err
}
return string(result[d.firmwareOffset:]), nil
}
// Resets the Stream Deck, clears all button images and shows the standby image.
func (d Device) Reset() error {
return d.sendFeatureReport(d.resetCommand)
}
// Clears the Stream Deck, setting a black image on all buttons.
func (d Device) Clear() error {
img := image.NewRGBA(image.Rect(0, 0, int(d.Pixels), int(d.Pixels)))
draw.Draw(img, img.Bounds(), image.NewUniform(color.RGBA{0, 0, 0, 255}), image.Point{}, draw.Src)
for i := uint8(0); i <= d.Columns*d.Rows; i++ {
err := d.SetImage(i, img)
if err != nil {
fmt.Println(err)
return err
}
}
return nil
}
// ReadKeys returns a channel, which it will use to emit key presses/releases.
func (d Device) ReadKeys() (chan Key, error) {
kch := make(chan Key)
keyBuffer := make([]byte, d.keyStateOffset+len(d.keyState))
go func() {
for {
copy(d.keyState, keyBuffer[d.keyStateOffset:])
_, err := d.device.Read(keyBuffer)
if err != nil {
close(kch)
return
}
for i := d.keyStateOffset; i < len(keyBuffer); i++ {
keyIndex := uint8(i - d.keyStateOffset)
if keyBuffer[i] != d.keyState[keyIndex] {
kch <- Key{
Index: d.translateKeyIndex(keyIndex, d.Columns),
Pressed: keyBuffer[i] == 1,
}
}
}
}
}()
return kch, nil
}
// SetBrightness sets the background lighting brightness from 0 to 100 percent.
func (d Device) SetBrightness(percent uint8) error {
if percent > 100 {
percent = 100
}
report := make([]byte, len(d.setBrightnessCommand)+1)
copy(report, d.setBrightnessCommand)
report[len(report)-1] = percent
return d.sendFeatureReport(report)
}
// SetImage sets the image of a button on the Stream Deck. The provided image
// needs to be in the correct resolution for the device. The index starts with
// 0 being the top-left button.
func (d Device) SetImage(index uint8, img image.Image) error {
if img.Bounds().Dy() != int(d.Pixels) ||
img.Bounds().Dx() != int(d.Pixels) {
return fmt.Errorf("supplied image has wrong dimensions, expected %[1]dx%[1]d pixels", d.Pixels)
}
imageBytes, err := d.toImageFormat(img)
if err != nil {
return fmt.Errorf("cannot convert image data: %v", err)
}
imageData := imageData{
image: imageBytes,
pageSize: d.imagePageSize - d.imagePageHeaderSize,
}
data := make([]byte, d.imagePageSize)
var page int
var lastPage bool
for !lastPage {
var payload []byte
payload, lastPage = imageData.Page(page)
header := d.imagePageHeader(page, d.translateKeyIndex(index, d.Columns), len(payload), lastPage)
copy(data, header)
copy(data[len(header):], payload)
_, err := d.device.Write(data)
if err != nil {
return fmt.Errorf("cannot write image page %d of %d (%d image bytes) %d bytes: %v", page, imageData.PageCount(), imageData.Length(), len(data), err)
}
page++
}
return nil
}
// getFeatureReport from the device without worries about the correct payload size.
func (d Device) getFeatureReport(payload []byte) ([]byte, error) {
b := make([]byte, d.featureReportSize)
copy(b, payload)
_, err := d.device.GetFeatureReport(b)
if err != nil {
return nil, err
}
return b, nil
}
// sendFeatureReport to the device without worries about the correct payload size.
func (d Device) sendFeatureReport(payload []byte) error {
b := make([]byte, d.featureReportSize)
copy(b, payload)
_, err := d.device.SendFeatureReport(b)
return err
}
// translateRightToLeft translates the given key index from right-to-left to left-to-right, based on the given number of columns.
func translateRightToLeft(index, columns uint8) uint8 {
keyCol := index % columns
return (index - keyCol) + (columns - 1) - keyCol
}
// identity returns the given key index as it is.
func identity(index, _ uint8) uint8 {
return index
}
// toRGBA converts an image.Image to an image.RGBA.
func toRGBA(img image.Image) *image.RGBA {
switch img := img.(type) {
case *image.RGBA:
return img
}
out := image.NewRGBA(img.Bounds())
draw.Copy(out, image.Pt(0, 0), img, img.Bounds(), draw.Src, nil)
return out
}
// toBMP returns the raw bytes of the given image in BMP format, flipped horizontally.
func toBMP(img image.Image) ([]byte, error) {
rgba := toRGBA(img)
// this is a BMP file header followed by a BPM bitmap info header
// find more information here: https://en.wikipedia.org/wiki/BMP_file_format
header := []byte{
0x42, 0x4d, 0xf6, 0x3c, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x28, 0x00,
0x00, 0x00, 0x48, 0x00, 0x00, 0x00, 0x48, 0x00,
0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00,
0x00, 0x00, 0xc0, 0x3c, 0x00, 0x00, 0xc4, 0x0e,
0x00, 0x00, 0xc4, 0x0e, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
}
buffer := make([]byte, len(header)+rgba.Bounds().Dx()*rgba.Bounds().Dy()*3)
copy(buffer, header)
i := len(header)
for y := 0; y < rgba.Bounds().Dy(); y++ {
// flip image horizontally
for x := rgba.Bounds().Dx() - 1; x >= 0; x-- {
c := rgba.RGBAAt(x, y)
buffer[i] = c.B
buffer[i+1] = c.G
buffer[i+2] = c.R
i += 3
}
}
return buffer, nil
}
// toJPEG returns the raw bytes of the given image in JPEG format, flipped horizontally and vertically.
func toJPEG(img image.Image) ([]byte, error) {
// flip image horizontally and vertically
flipped := image.NewRGBA(img.Bounds())
draw.Copy(flipped, image.Point{}, img, img.Bounds(), draw.Src, nil)
for y := 0; y < flipped.Bounds().Dy()/2; y++ {
yy := flipped.Bounds().Max.Y - y - 1
for x := 0; x < flipped.Bounds().Dx(); x++ {
xx := flipped.Bounds().Max.X - x - 1
c := flipped.RGBAAt(x, y)
flipped.SetRGBA(x, y, flipped.RGBAAt(xx, yy))
flipped.SetRGBA(xx, yy, c)
}
}
buffer := bytes.NewBuffer([]byte{})
opts := jpeg.Options{
Quality: 100,
}
err := jpeg.Encode(buffer, flipped, &opts)
if err != nil {
return nil, err
}
return buffer.Bytes(), err
}
// rev1ImagePageHeader returns the image page header sequence used by the Stream Deck v1 and Stream Deck Mini.
func rev1ImagePageHeader(pageIndex int, keyIndex uint8, payloadLength int, lastPage bool) []byte {
var lastPageByte byte
if lastPage {
lastPageByte = 1
}
return []byte{
0x02, 0x01,
byte(pageIndex + 1), 0x00,
lastPageByte,
keyIndex + 1,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
}
}
// rev2ImagePageHeader returns the image page header sequence used by Stream Deck XL and Stream Deck v2.
func rev2ImagePageHeader(pageIndex int, keyIndex uint8, payloadLength int, lastPage bool) []byte {
var lastPageByte byte
if lastPage {
lastPageByte = 1
}
return []byte{
0x02, 0x07, keyIndex, lastPageByte,
byte(payloadLength), byte(payloadLength >> 8),
byte(pageIndex), byte(pageIndex >> 8),
}
}
// imageData allows to access raw image data in a byte array through pages of a given size.
type imageData struct {
image []byte
pageSize int
}
// Page returns the page with the given index and an indication if this is the last page.
func (d imageData) Page(pageIndex int) ([]byte, bool) {
offset := pageIndex * d.pageSize
if offset >= len(d.image) {
return []byte{}, true
}
length := d.pageLength(pageIndex)
if offset+length > len(d.image) {
length = len(d.image) - offset
}
return d.image[offset : offset+length], pageIndex == d.PageCount()-1
}
func (d imageData) pageLength(pageIndex int) int {
remaining := len(d.image) - (pageIndex * d.pageSize)
if remaining > d.pageSize {
return d.pageSize
}
if remaining > 0 {
return remaining
}
return 0
}
// PageCount returns the total number of pages.
func (d imageData) PageCount() int {
count := len(d.image) / d.pageSize
if len(d.image)%d.pageSize != 0 {
return count + 1
}
return count
}
// Length of the raw image data in bytes.
func (d imageData) Length() int {
return len(d.image)
}