Skip to content

Add button release handler capability #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions comms.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ func RegisterDevicetype(

// Device is a struct which represents an actual Streamdeck device, and holds its reference to the USB HID device
type Device struct {
fd *hid.Device
deviceType deviceType
buttonPressListeners []func(int, *Device, error)
fd *hid.Device
deviceType deviceType
buttonPressListeners []func(int, *Device, error)
buttonReleaseListeners []func(int, *Device, error)
}

// Open a Streamdeck device, the most common entry point
Expand Down Expand Up @@ -137,12 +138,12 @@ func (d *Device) SetBrightness(pct int) error {
}

// GetButtonImageSize returns the size of the images to uploaded to the buttons
func (d* Device) GetButtonImageSize() image.Point {
func (d *Device) GetButtonImageSize() image.Point {
return d.deviceType.imageSize
}

// GetNumButtonsOnDevice returns the number of button this device has
func (d* Device) GetNumButtonsOnDevice() uint {
func (d *Device) GetNumButtonsOnDevice() uint {
return d.deviceType.numberOfButtons
}

Expand Down Expand Up @@ -198,6 +199,9 @@ func (d *Device) buttonPressListener() {
}
buttonMask[i] = true
} else {
if buttonMask[i] {
d.sendButtonReleaseEvent(int(i), nil)
}
buttonMask[i] = false
}
}
Expand All @@ -210,11 +214,22 @@ func (d *Device) sendButtonPressEvent(btnIndex int, err error) {
}
}

func (d *Device) sendButtonReleaseEvent(btnIndex int, err error) {
for _, f := range d.buttonReleaseListeners {
f(btnIndex, d, err)
}
}

// ButtonPress registers a callback to be called whenever a button is pressed
func (d *Device) ButtonPress(f func(int, *Device, error)) {
d.buttonPressListeners = append(d.buttonPressListeners, f)
}

// ButtonRelease registers a callback to be called whenever a button is released
func (d *Device) ButtonRelease(f func(int, *Device, error)) {
d.buttonReleaseListeners = append(d.buttonReleaseListeners, f)
}

// ResetComms will reset the comms protocol to the StreamDeck; useful if things have gotten de-synced, but it will also reboot the StreamDeck
func (d *Device) ResetComms() error {
payload := d.deviceType.resetPacket
Expand Down