Skip to content

Commit eeb2d91

Browse files
committed
Add button release handler capability
magicmonkey#53
1 parent 85ced3e commit eeb2d91

File tree

2 files changed

+93
-3
lines changed

2 files changed

+93
-3
lines changed

comms.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ func RegisterDevicetype(
6363

6464
// Device is a struct which represents an actual Streamdeck device, and holds its reference to the USB HID device
6565
type Device struct {
66-
fd *hid.Device
67-
deviceType deviceType
68-
buttonPressListeners []func(int, *Device, error)
66+
fd *hid.Device
67+
deviceType deviceType
68+
buttonPressListeners []func(int, *Device, error)
69+
buttonReleaseListeners []func(int, *Device, error)
6970
}
7071

7172
// Open a Streamdeck device, the most common entry point
@@ -243,6 +244,9 @@ func (d *Device) buttonPressListener() {
243244
}
244245
buttonMask[i] = true
245246
} else {
247+
if buttonMask[i] {
248+
d.sendButtonReleaseEvent(int(i), nil)
249+
}
246250
buttonMask[i] = false
247251
}
248252
}
@@ -255,11 +259,22 @@ func (d *Device) sendButtonPressEvent(btnIndex int, err error) {
255259
}
256260
}
257261

262+
func (d *Device) sendButtonReleaseEvent(btnIndex int, err error) {
263+
for _, f := range d.buttonReleaseListeners {
264+
f(btnIndex, d, err)
265+
}
266+
}
267+
258268
// ButtonPress registers a callback to be called whenever a button is pressed
259269
func (d *Device) ButtonPress(f func(int, *Device, error)) {
260270
d.buttonPressListeners = append(d.buttonPressListeners, f)
261271
}
262272

273+
// ButtonRelease registers a callback to be called whenever a button is released
274+
func (d *Device) ButtonRelease(f func(int, *Device, error)) {
275+
d.buttonReleaseListeners = append(d.buttonReleaseListeners, f)
276+
}
277+
263278
// ResetComms will reset the comms protocol to the StreamDeck; useful if things have gotten de-synced, but it will also reboot the StreamDeck
264279
func (d *Device) ResetComms() error {
265280
payload := d.deviceType.resetPacket
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
From e7cbc98bdc4c218a0671d01ce4adae47a5ed22ba Mon Sep 17 00:00:00 2001
2+
From: Craig Peterson <[email protected]>
3+
Date: Mon, 12 Feb 2024 11:19:16 -0500
4+
Subject: [PATCH] add button release handler capability
5+
6+
---
7+
comms.go | 25 ++++++++++++++++++++-----
8+
1 file changed, 20 insertions(+), 5 deletions(-)
9+
10+
diff --git a/comms.go b/comms.go
11+
index 146fd5f..1cc1ce4 100644
12+
--- a/comms.go
13+
+++ b/comms.go
14+
@@ -63,9 +63,10 @@ func RegisterDevicetype(
15+
16+
// Device is a struct which represents an actual Streamdeck device, and holds its reference to the USB HID device
17+
type Device struct {
18+
- fd *hid.Device
19+
- deviceType deviceType
20+
- buttonPressListeners []func(int, *Device, error)
21+
+ fd *hid.Device
22+
+ deviceType deviceType
23+
+ buttonPressListeners []func(int, *Device, error)
24+
+ buttonReleaseListeners []func(int, *Device, error)
25+
}
26+
27+
// Open a Streamdeck device, the most common entry point
28+
@@ -137,12 +138,12 @@ func (d *Device) SetBrightness(pct int) error {
29+
}
30+
31+
// GetButtonImageSize returns the size of the images to uploaded to the buttons
32+
-func (d* Device) GetButtonImageSize() image.Point {
33+
+func (d *Device) GetButtonImageSize() image.Point {
34+
return d.deviceType.imageSize
35+
}
36+
37+
// GetNumButtonsOnDevice returns the number of button this device has
38+
-func (d* Device) GetNumButtonsOnDevice() uint {
39+
+func (d *Device) GetNumButtonsOnDevice() uint {
40+
return d.deviceType.numberOfButtons
41+
}
42+
43+
@@ -198,6 +199,9 @@ func (d *Device) buttonPressListener() {
44+
}
45+
buttonMask[i] = true
46+
} else {
47+
+ if buttonMask[i] {
48+
+ d.sendButtonReleaseEvent(int(i), nil)
49+
+ }
50+
buttonMask[i] = false
51+
}
52+
}
53+
@@ -210,11 +214,22 @@ func (d *Device) sendButtonPressEvent(btnIndex int, err error) {
54+
}
55+
}
56+
57+
+func (d *Device) sendButtonReleaseEvent(btnIndex int, err error) {
58+
+ for _, f := range d.buttonReleaseListeners {
59+
+ f(btnIndex, d, err)
60+
+ }
61+
+}
62+
+
63+
// ButtonPress registers a callback to be called whenever a button is pressed
64+
func (d *Device) ButtonPress(f func(int, *Device, error)) {
65+
d.buttonPressListeners = append(d.buttonPressListeners, f)
66+
}
67+
68+
+// ButtonRelease registers a callback to be called whenever a button is released
69+
+func (d *Device) ButtonRelease(f func(int, *Device, error)) {
70+
+ d.buttonReleaseListeners = append(d.buttonReleaseListeners, f)
71+
+}
72+
+
73+
// ResetComms will reset the comms protocol to the StreamDeck; useful if things have gotten de-synced, but it will also reboot the StreamDeck
74+
func (d *Device) ResetComms() error {
75+
payload := d.deviceType.resetPacket

0 commit comments

Comments
 (0)