From e634e98adf206e7cf8c7429e445b23c6774d9115 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Wed, 25 Apr 2018 09:16:48 +0200 Subject: split hid stuff --- hid.go | 27 +++++++++++++++++++++++++++ redbutton.go | 22 ++++------------------ 2 files changed, 31 insertions(+), 18 deletions(-) create mode 100644 hid.go diff --git a/hid.go b/hid.go new file mode 100644 index 0000000..65e43f9 --- /dev/null +++ b/hid.go @@ -0,0 +1,27 @@ +package redbutton + +import ( + "errors" + + "github.com/karalabe/hid" +) + +const ( + vendorID = 0x1d34 + productID = 0x000d +) + +var ( + ErrUnsupported = errors.New("unsupproted platform") + ErrNotFound = errors.New("device not found") +) + +func Open() (*hid.Device, error) { + if !hid.Supported() { + return nil, ErrUnsupported + } + for _, dev := range hid.Enumerate(vendorID, productID) { + return dev.Open() + } + return nil, ErrNotFound +} diff --git a/redbutton.go b/redbutton.go index a6f73dd..75b755e 100644 --- a/redbutton.go +++ b/redbutton.go @@ -1,17 +1,11 @@ package redbutton import ( - "errors" + "io" "time" - - "github.com/karalabe/hid" ) -const ( - vendor = 0x1d34 - product = 0x000d - PollInterval = 200 * time.Millisecond -) +const PollInterval = 200 * time.Millisecond //go:generate stringer -type=Event @@ -24,7 +18,7 @@ const ( Enabled ) -func Report(dev *hid.Device) (Event, error) { +func Report(dev io.ReadWriter) (Event, error) { // leading zero disables sending of report number buf := []byte{0, 0, 0, 0, 0, 0, 0, 0, 2} if _, err := dev.Write(buf); err != nil { @@ -36,7 +30,7 @@ func Report(dev *hid.Device) (Event, error) { return Event(buf[0] & 3), nil } -func Poll(dev *hid.Device, d time.Duration) <-chan Event { +func Poll(dev io.ReadWriter, d time.Duration) <-chan Event { if d == 0 { d = PollInterval } @@ -59,11 +53,3 @@ func Poll(dev *hid.Device, d time.Duration) <-chan Event { }() return ch } - -func Open() (*hid.Device, error) { - devs := hid.Enumerate(vendor, product) - if len(devs) == 0 { - return nil, errors.New("not found") - } - return devs[0].Open() -} -- cgit v1.2.3