|
| 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