Skip to content

Commit 650d283

Browse files
authored
Merge pull request #42 from RudolfVonKrugstein/feature/more-error-handling
Add error return values to some functions to allow better error handling
2 parents ad6db5a + 8c42f93 commit 650d283

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

comms.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (d *Device) Close() {
119119

120120
// SetBrightness sets the button brightness
121121
// pct is an integer between 0-100
122-
func (d *Device) SetBrightness(pct int) {
122+
func (d *Device) SetBrightness(pct int) error {
123123
if pct < 0 {
124124
pct = 0
125125
}
@@ -129,15 +129,23 @@ func (d *Device) SetBrightness(pct int) {
129129

130130
preamble := d.deviceType.brightnessPacket
131131
payload := append(preamble, byte(pct))
132-
d.fd.SendFeatureReport(payload)
132+
_, err := d.fd.SendFeatureReport(payload)
133+
if err != nil {
134+
return err
135+
}
136+
return nil
133137
}
134138

135139
// ClearButtons writes a black square to all buttons
136-
func (d *Device) ClearButtons() {
140+
func (d *Device) ClearButtons() error {
137141
numButtons := int(d.deviceType.numberOfButtons)
138142
for i := 0; i < numButtons; i++ {
139-
d.WriteColorToButton(i, color.Black)
143+
err := d.WriteColorToButton(i, color.Black)
144+
if err != nil {
145+
return err
146+
}
140147
}
148+
return nil
141149
}
142150

143151
// WriteColorToButton writes a specified color to the given button
@@ -156,7 +164,10 @@ func (d *Device) WriteImageToButton(btnIndex int, filename string) error {
156164
if err != nil {
157165
return err
158166
}
159-
d.WriteRawImageToButton(btnIndex, img)
167+
err = d.WriteRawImageToButton(btnIndex, img)
168+
if err != nil {
169+
return err
170+
}
160171
return nil
161172
}
162173

@@ -195,9 +206,10 @@ func (d *Device) ButtonPress(f func(int, *Device, error)) {
195206
}
196207

197208
// ResetComms will reset the comms protocol to the StreamDeck; useful if things have gotten de-synced, but it will also reboot the StreamDeck
198-
func (d *Device) ResetComms() {
209+
func (d *Device) ResetComms() error {
199210
payload := d.deviceType.resetPacket
200-
d.fd.SendFeatureReport(payload)
211+
_, err := d.fd.SendFeatureReport(payload)
212+
return err
201213
}
202214

203215
// 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)
@@ -240,7 +252,10 @@ func (d *Device) rawWriteToButton(btnIndex int, rawImage []byte) error {
240252
padding := make([]byte, imageReportLength-len(payload))
241253

242254
thingToSend := append(payload, padding...)
243-
d.fd.Write(thingToSend)
255+
_, err := d.fd.Write(thingToSend)
256+
if err != nil {
257+
return err
258+
}
244259

245260
bytesRemaining = bytesRemaining - thisLength
246261
pageNumber = pageNumber + 1
@@ -256,7 +271,7 @@ func Min(x, y int) int {
256271
return y
257272
}
258273

259-
func Max (x, y int) int {
274+
func Max(x, y int) int {
260275
if x > y {
261276
return x
262277
}

0 commit comments

Comments
 (0)