Skip to content

Add mutexes to adding buttons and button image writes #51

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 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
9 changes: 7 additions & 2 deletions comms.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"image"
"image/color"
"sync"

"github.com/karalabe/hid"
)
Expand Down Expand Up @@ -66,6 +67,7 @@ type Device struct {
fd *hid.Device
deviceType deviceType
buttonPressListeners []func(int, *Device, error)
mu sync.Mutex // controls access to writing raw data
}

// Open a Streamdeck device, the most common entry point
Expand Down Expand Up @@ -137,12 +139,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 @@ -233,6 +235,9 @@ func (d *Device) WriteRawImageToButton(btnIndex int, rawImg image.Image) error {
}

func (d *Device) rawWriteToButton(btnIndex int, rawImage []byte) error {
d.mu.Lock()
defer d.mu.Unlock()

// Based on set_key_image from https://github.com/abcminiuser/python-elgato-streamdeck/blob/master/src/StreamDeck/Devices/StreamDeckXL.py#L151

if Min(Max(btnIndex, 0), int(d.deviceType.numberOfButtons)) != btnIndex {
Expand Down
8 changes: 7 additions & 1 deletion streamdeck.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package streamdeck

import "image"
import (
"image"
"sync"
)

// ButtonDisplay is the interface to satisfy for displaying on a button
type ButtonDisplay interface {
Expand Down Expand Up @@ -31,6 +34,7 @@ type StreamDeck struct {
dev *Device
buttons map[int]Button
decorators map[int]ButtonDecorator
mu sync.Mutex
}

// New will return a new instance of a `StreamDeck`, and is the main entry point for the higher-level interface. It will return an error if there is no StreamDeck plugged in.
Expand All @@ -56,6 +60,8 @@ func (sd *StreamDeck) GetName() string {
func (sd *StreamDeck) AddButton(btnIndex int, b Button) {
b.RegisterUpdateHandler(sd.ButtonUpdateHandler)
b.SetButtonIndex(btnIndex)
sd.mu.Lock()
defer sd.mu.Unlock()
sd.buttons[btnIndex] = b
sd.updateButton(b)
}
Expand Down