-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdeck_xl.go
More file actions
188 lines (143 loc) · 4.57 KB
/
Copy pathdeck_xl.go
File metadata and controls
188 lines (143 loc) · 4.57 KB
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
package streamdeck
//revive:disable:add-constant // many numbers with single use or only protocol value
import (
"bytes"
"encoding/binary"
"fmt"
"image"
"image/color"
"image/jpeg"
"strings"
"sync"
"github.com/disintegration/imaging"
"github.com/sstallion/go-hid"
)
const (
deckXLMaxPacketSize = 1024
deckXLHeaderSize = 8
)
type deckConfigXL struct {
dev *hid.Device
writeLock sync.Mutex
keyState []EventType
}
func newDeckConfigXL() deckConfig {
d := &deckConfigXL{}
d.keyState = make([]EventType, d.NumKeys())
return d
}
func (d *deckConfigXL) ClearAllKeys() error {
for i := 0; i < d.NumKeys(); i++ {
if err := d.ClearKey(i); err != nil {
return fmt.Errorf("clearing key: %w", err)
}
}
return nil
}
func (d *deckConfigXL) ClearKey(keyIdx int) error {
return d.FillColor(keyIdx, color.RGBA{0x0, 0x0, 0x0, 0xff})
}
func (d *deckConfigXL) FillColor(keyIdx int, col color.RGBA) error {
img := image.NewRGBA(image.Rect(0, 0, d.IconSize(), d.IconSize()))
for x := 0; x < d.IconSize(); x++ {
for y := 0; y < d.IconSize(); y++ {
img.Set(x, y, col)
}
}
return d.FillImage(keyIdx, img)
}
func (d *deckConfigXL) FillImage(keyIdx int, img image.Image) error {
if keyIdx >= d.NumKeys() || keyIdx < 0 {
return fmt.Errorf("key index %d out of bounds", keyIdx)
}
d.writeLock.Lock()
defer d.writeLock.Unlock()
buf := new(bytes.Buffer)
// We need to rotate the image or it will be presented upside down
rimg := imaging.Rotate180(img)
if err := jpeg.Encode(buf, rimg, &jpeg.Options{Quality: 95}); err != nil {
return fmt.Errorf("encoding jpeg: %w", err)
}
var partIndex int16
for buf.Len() > 0 {
chunk := make([]byte, deckXLMaxPacketSize-deckXLHeaderSize)
n, err := buf.Read(chunk)
if err != nil {
return fmt.Errorf("reading image chunk: %w", err)
}
var last uint8
if n < deckXLMaxPacketSize-deckXLHeaderSize || buf.Len() == 0 {
last = 1
}
tbuf := new(bytes.Buffer)
tbuf.Write([]byte{0x02, 0x07, byte(keyIdx), last}) //#nosec:G115 // keyIdx is guarded to safe values
_ = binary.Write(tbuf, binary.LittleEndian, int16(n)) //#nosec:G115 // guarded to safe values
_ = binary.Write(tbuf, binary.LittleEndian, partIndex)
tbuf.Write(chunk)
if _, err = d.dev.Write(tbuf.Bytes()); err != nil {
return fmt.Errorf("sending image chunk: %w", err)
}
partIndex++
}
return nil
}
func (d *deckConfigXL) FillPanel(img image.RGBA) error {
if img.Bounds().Size().X < d.KeyColumns()*d.IconSize() || img.Bounds().Size().Y < d.KeyRows()*d.IconSize() {
return fmt.Errorf("image is too small")
}
for k := 0; k < d.NumKeys(); k++ {
var (
ky = k / d.KeyColumns()
kx = k % d.KeyColumns()
)
if err := d.FillImage(k, img.SubImage(image.Rect(kx*d.IconSize(), ky*d.IconSize(), (kx+1)*d.IconSize(), (ky+1)*d.IconSize()))); err != nil {
return fmt.Errorf("setting key image: %w", err)
}
}
return nil
}
func (d *deckConfigXL) GetFimwareVersion() (string, error) {
fw := make([]byte, 32)
fw[0] = 5
_, err := d.dev.GetFeatureReport(fw)
if err != nil {
return "", fmt.Errorf("getting feature report: %w", err)
}
return strings.TrimRight(string(fw[6:]), "\x00"), nil
}
func (d *deckConfigXL) IconBytes() int { return d.IconSize() * d.IconSize() * 3 }
func (*deckConfigXL) IconSize() int { return 96 }
func (*deckConfigXL) KeyColumns() int { return 8 }
func (*deckConfigXL) KeyDataOffset() int { return 4 }
func (*deckConfigXL) KeyDirection() keyDirection { return keyDirectionLTR }
func (*deckConfigXL) KeyRows() int { return 4 }
func (*deckConfigXL) Model() uint16 { return StreamDeckXL }
func (*deckConfigXL) NumKeys() int { return 32 }
func (d *deckConfigXL) ResetToLogo() error {
if _, err := d.dev.SendFeatureReport([]byte{
0x03,
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
}); err != nil {
return fmt.Errorf("sending feature report: %w", err)
}
return nil
}
func (d *deckConfigXL) SetBrightness(pct int) error {
if pct < 0 || pct > 100 {
return fmt.Errorf("percentage %d out of bounds 0-100", pct)
}
if _, err := d.dev.SendFeatureReport([]byte{
0x03, 0x08, byte(pct), 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
}); err != nil {
return fmt.Errorf("sending feature report: %w", err)
}
return nil
}
func (d *deckConfigXL) SetDevice(dev *hid.Device) { d.dev = dev }
func (*deckConfigXL) TransformKeyIndex(keyIdx int) int { return keyIdx }