Skip to content

Commit 8c1775f

Browse files
committed
Moved the image header generation into the definition file
1 parent e3e352d commit 8c1775f

File tree

3 files changed

+80
-39
lines changed

3 files changed

+80
-39
lines changed

comms.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ const vendorID = 0x0fd9
1212

1313
// deviceType represents one of the various types of StreamDeck (mini/orig/orig2/xl)
1414
type deviceType struct {
15-
name string
16-
imageSize image.Point
17-
usbProductID uint16
18-
resetPacket []byte
19-
numberOfButtons uint
20-
brightnessPacket []byte
21-
buttonReadOffset uint
15+
name string
16+
imageSize image.Point
17+
usbProductID uint16
18+
resetPacket []byte
19+
numberOfButtons uint
20+
brightnessPacket []byte
21+
buttonReadOffset uint
22+
imageFormat string
23+
imagePayloadPerPage uint
24+
imageHeaderFunc func(bytesRemaining uint, btnIndex uint, pageNumber uint) []byte
2225
}
2326

2427
var deviceTypes []deviceType
@@ -32,15 +35,21 @@ func RegisterDevicetype(
3235
numberOfButtons uint,
3336
brightnessPacket []byte,
3437
buttonReadOffset uint,
38+
imageFormat string,
39+
imagePayloadPerPage uint,
40+
imageHeaderFunc func(bytesRemaining uint, btnIndex uint, pageNumber uint) []byte,
3541
) {
3642
d := deviceType{
37-
name: name,
38-
imageSize: imageSize,
39-
usbProductID: usbProductID,
40-
resetPacket: resetPacket,
41-
numberOfButtons: numberOfButtons,
42-
brightnessPacket: brightnessPacket,
43-
buttonReadOffset: buttonReadOffset,
43+
name: name,
44+
imageSize: imageSize,
45+
usbProductID: usbProductID,
46+
resetPacket: resetPacket,
47+
numberOfButtons: numberOfButtons,
48+
brightnessPacket: brightnessPacket,
49+
buttonReadOffset: buttonReadOffset,
50+
imageFormat: imageFormat,
51+
imagePayloadPerPage: imagePayloadPerPage,
52+
imageHeaderFunc: imageHeaderFunc,
4453
}
4554
deviceTypes = append(deviceTypes, d)
4655
}
@@ -180,7 +189,7 @@ func (d *Device) ResetComms() {
180189

181190
// 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)
182191
func (d *Device) WriteRawImageToButton(btnIndex int, rawImg image.Image) error {
183-
img := resizeAndRotate(rawImg, 96, 96)
192+
img := resizeAndRotate(rawImg, d.deviceType.imageSize.X, d.deviceType.imageSize.Y)
184193
return d.rawWriteToButton(btnIndex, getImageAsJpeg(img))
185194
}
186195

@@ -189,33 +198,24 @@ func (d *Device) rawWriteToButton(btnIndex int, rawImage []byte) error {
189198
pageNumber := 0
190199
bytesRemaining := len(rawImage)
191200

192-
imageReportLength := 1024
193-
imageReportHeaderLength := 8
194-
imageReportPayloadLength := imageReportLength - imageReportHeaderLength
195-
196201
// Surely no image can be more than 20 packets...?
197202
payloads := make([][]byte, 20)
198203

199204
for bytesRemaining > 0 {
205+
206+
header := d.deviceType.imageHeaderFunc(uint(bytesRemaining), uint(btnIndex), uint(pageNumber))
207+
imageReportLength := int(d.deviceType.imagePayloadPerPage)
208+
imageReportHeaderLength := len(header)
209+
imageReportPayloadLength := imageReportLength - imageReportHeaderLength
210+
200211
thisLength := 0
201212
if imageReportPayloadLength < bytesRemaining {
202213
thisLength = imageReportPayloadLength
203214
} else {
204215
thisLength = bytesRemaining
205216
}
206-
bytesSent := pageNumber * imageReportPayloadLength
207-
header := []byte{'\x02', '\x07', byte(btnIndex)}
208-
if thisLength == bytesRemaining {
209-
header = append(header, '\x01')
210-
} else {
211-
header = append(header, '\x00')
212-
}
213217

214-
header = append(header, byte(thisLength&0xff))
215-
header = append(header, byte(thisLength>>8))
216-
217-
header = append(header, byte(pageNumber&0xff))
218-
header = append(header, byte(pageNumber>>8))
218+
bytesSent := pageNumber * imageReportPayloadLength
219219

220220
payload := append(header, rawImage[bytesSent:(bytesSent+thisLength)]...)
221221
padding := make([]byte, imageReportLength-len(payload))

devices/xl.go

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,52 @@ import (
66
streamdeck "github.com/magicmonkey/go-streamdeck"
77
)
88

9+
var (
10+
name string
11+
buttonWidth uint
12+
buttonHeight uint
13+
imageReportPayloadLength uint
14+
)
15+
16+
// GetImageHeaderXl returns the USB comms header for a button image for the XL
17+
func GetImageHeaderXl(bytesRemaining uint, btnIndex uint, pageNumber uint) []byte {
18+
thisLength := uint(0)
19+
if imageReportPayloadLength < bytesRemaining {
20+
thisLength = imageReportPayloadLength
21+
} else {
22+
thisLength = bytesRemaining
23+
}
24+
header := []byte{'\x02', '\x07', byte(btnIndex)}
25+
if thisLength == bytesRemaining {
26+
header = append(header, '\x01')
27+
} else {
28+
header = append(header, '\x00')
29+
}
30+
31+
header = append(header, byte(thisLength&0xff))
32+
header = append(header, byte(thisLength>>8))
33+
34+
header = append(header, byte(pageNumber&0xff))
35+
header = append(header, byte(pageNumber>>8))
36+
37+
return header
38+
}
39+
940
func init() {
41+
name = "Streamdeck XL"
42+
buttonWidth = 96
43+
buttonHeight = 96
44+
imageReportPayloadLength = 1024
1045
streamdeck.RegisterDevicetype(
11-
"Streamdeck XL", // Name
12-
image.Point{X: 96, Y: 96}, // Width/height of a button
13-
0x6c, // USB productID
14-
[]byte{'\x03', '\x02'}, // Reset packet
15-
32, // Number of buttons
16-
[]byte{'\x03', '\x08'}, // Set brightness packet preamble
17-
4, // Button read offset
46+
name, // Name
47+
image.Point{X: int(buttonWidth), Y: int(buttonHeight)}, // Width/height of a button
48+
0x6c, // USB productID
49+
[]byte{'\x03', '\x02'}, // Reset packet
50+
32, // Number of buttons
51+
[]byte{'\x03', '\x08'}, // Set brightness packet preamble
52+
4, // Button read offset
53+
"JPEG", // Image format
54+
imageReportPayloadLength, // Amount of image payload allowed per USB packet
55+
GetImageHeaderXl, // Function to get the comms image header
1856
)
1957
}

examples/client/client.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ func main() {
3131
myNextButton.SetActionHandler(&actionhandlers.TextLabelChangeAction{NewLabel: "8"})
3232
sd.AddButton(7, myNextButton)
3333

34-
anotherButton, _ := buttons.NewImageFileButton("/home/kevin/streamdeck/go-streamdeck/examples/play.jpg")
34+
anotherButton, err := buttons.NewImageFileButton("/home/kevin/streamdeck/go-streamdeck/examples/test/play.jpg")
35+
if err != nil {
36+
panic(err)
37+
}
3538
sd.AddButton(9, anotherButton)
3639

3740
cButton := buttons.NewColourButton(color.RGBA{255, 255, 0, 255})

0 commit comments

Comments
 (0)