From 028b5adc58507900264c128ca871eccba526747b Mon Sep 17 00:00:00 2001 From: Mathieu Lecarme Date: Mon, 10 Jul 2023 10:24:07 +0200 Subject: [PATCH 1/2] Modules. --- go.mod | 5 +++++ go.sum | 2 ++ 2 files changed, 7 insertions(+) create mode 100644 go.mod create mode 100644 go.sum diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..bf2c517 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/boombuler/led + +go 1.20 + +require github.com/boombuler/hid v0.0.0-20200606170446-6a2dff8586bc diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..2f64242 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/boombuler/hid v0.0.0-20200606170446-6a2dff8586bc h1:mPoyO6/N4SwSKRhuhYccA3dAVSnQFRl+3kuiTf9ptAQ= +github.com/boombuler/hid v0.0.0-20200606170446-6a2dff8586bc/go.mod h1:5tIGNm7VNPEAS8ulRl5g4wU/zCpPgj5vyYI5Bo77Hz8= From 8c373cd942ab4a8a0d41b25c6990ee5ce7a8b81c Mon Sep 17 00:00:00 2001 From: Mathieu Lecarme Date: Mon, 10 Jul 2023 10:24:50 +0200 Subject: [PATCH 2/2] go fmt --- blink1.go | 58 +++++++-------- blinkm.go | 58 +++++++-------- blinkstick.go | 58 +++++++-------- blync.go | 108 ++++++++++++++-------------- busylight.go | 164 +++++++++++++++++++++--------------------- dealextreme.go | 78 ++++++++++---------- dreamcheeky.go | 72 +++++++++---------- led.go | 192 ++++++++++++++++++++++++------------------------- usbdevice.go | 92 ++++++++++++------------ 9 files changed, 440 insertions(+), 440 deletions(-) diff --git a/blink1.go b/blink1.go index 4859087..880cd92 100644 --- a/blink1.go +++ b/blink1.go @@ -1,29 +1,29 @@ -package led - -import ( - "github.com/boombuler/hid" - "image/color" -) - -// Device type: blink(1) -var Blink1 DeviceType - -func init() { - Blink1 = addDriver(usbDriver{ - Name: "blink(1)", - Type: &Blink1, - VendorId: 0x27B8, - ProductId: 0x01ED, - Open: func(d hid.Device) (Device, error) { - return &simpleHidDevice{ - device: d, - setColorFn: blink1SetColor, - }, nil - }, - }) -} - -func blink1SetColor(d hid.Device, c color.Color) error { - r, g, b, _ := c.RGBA() - return d.WriteFeature([]byte{0x01, 0x63, byte(r >> 8), byte(g >> 8), byte(b >> 8), 0x00, 0x00, 0x00, 0x00}) -} +package led + +import ( + "github.com/boombuler/hid" + "image/color" +) + +// Device type: blink(1) +var Blink1 DeviceType + +func init() { + Blink1 = addDriver(usbDriver{ + Name: "blink(1)", + Type: &Blink1, + VendorId: 0x27B8, + ProductId: 0x01ED, + Open: func(d hid.Device) (Device, error) { + return &simpleHidDevice{ + device: d, + setColorFn: blink1SetColor, + }, nil + }, + }) +} + +func blink1SetColor(d hid.Device, c color.Color) error { + r, g, b, _ := c.RGBA() + return d.WriteFeature([]byte{0x01, 0x63, byte(r >> 8), byte(g >> 8), byte(b >> 8), 0x00, 0x00, 0x00, 0x00}) +} diff --git a/blinkm.go b/blinkm.go index 11ffe8c..a8bfb61 100644 --- a/blinkm.go +++ b/blinkm.go @@ -1,29 +1,29 @@ -package led - -import ( - "github.com/boombuler/hid" - "image/color" -) - -// Device type: LinkM / BlinkM -var BlinkM DeviceType - -func init() { - BlinkM = addDriver(usbDriver{ - Name: "LinkM / BlinkM", - Type: &BlinkM, - VendorId: 0x20A0, - ProductId: 0x4110, - Open: func(d hid.Device) (Device, error) { - return &simpleHidDevice{ - device: d, - setColorFn: blinkMSetColor, - }, nil - }, - }) -} - -func blinkMSetColor(d hid.Device, c color.Color) error { - r, g, b, _ := c.RGBA() - return d.WriteFeature([]byte{0x01, 0xDA, 0x01, 0x05, 0x00, 0x09, 0x6E, byte(r >> 8), byte(g >> 8), byte(b >> 8)}) -} +package led + +import ( + "github.com/boombuler/hid" + "image/color" +) + +// Device type: LinkM / BlinkM +var BlinkM DeviceType + +func init() { + BlinkM = addDriver(usbDriver{ + Name: "LinkM / BlinkM", + Type: &BlinkM, + VendorId: 0x20A0, + ProductId: 0x4110, + Open: func(d hid.Device) (Device, error) { + return &simpleHidDevice{ + device: d, + setColorFn: blinkMSetColor, + }, nil + }, + }) +} + +func blinkMSetColor(d hid.Device, c color.Color) error { + r, g, b, _ := c.RGBA() + return d.WriteFeature([]byte{0x01, 0xDA, 0x01, 0x05, 0x00, 0x09, 0x6E, byte(r >> 8), byte(g >> 8), byte(b >> 8)}) +} diff --git a/blinkstick.go b/blinkstick.go index 737a90f..055412a 100644 --- a/blinkstick.go +++ b/blinkstick.go @@ -1,29 +1,29 @@ -package led - -import ( - "github.com/boombuler/hid" - "image/color" -) - -// Device type: BlinkStick -var BlinkStick DeviceType - -func init() { - BlinkStick = addDriver(usbDriver{ - Name: "BlinkStick", - Type: &BlinkStick, - VendorId: 0x20a0, - ProductId: 0x41e5, - Open: func(d hid.Device) (Device, error) { - return &simpleHidDevice{ - device: d, - setColorFn: blinkStickSetColor, - }, nil - }, - }) -} - -func blinkStickSetColor(d hid.Device, c color.Color) error { - r, g, b, _ := c.RGBA() - return d.WriteFeature([]byte{0x01, byte(r >> 8), byte(g >> 8), byte(b >> 8)}) -} +package led + +import ( + "github.com/boombuler/hid" + "image/color" +) + +// Device type: BlinkStick +var BlinkStick DeviceType + +func init() { + BlinkStick = addDriver(usbDriver{ + Name: "BlinkStick", + Type: &BlinkStick, + VendorId: 0x20a0, + ProductId: 0x41e5, + Open: func(d hid.Device) (Device, error) { + return &simpleHidDevice{ + device: d, + setColorFn: blinkStickSetColor, + }, nil + }, + }) +} + +func blinkStickSetColor(d hid.Device, c color.Color) error { + r, g, b, _ := c.RGBA() + return d.WriteFeature([]byte{0x01, byte(r >> 8), byte(g >> 8), byte(b >> 8)}) +} diff --git a/blync.go b/blync.go index b7ee6ce..e1ba2de 100644 --- a/blync.go +++ b/blync.go @@ -1,54 +1,54 @@ -package led - -import ( - "github.com/boombuler/hid" - "image/color" -) - -// Device type: Blync -var Blync DeviceType - -func init() { - Blync = addDriver(blyncDriver{ - usbDriver{ - Name: "Blync", - Type: &Blync, - VendorId: 0x1130, - ProductId: 0x0001, - Open: func(d hid.Device) (Device, error) { - return &simpleHidDevice{ - device: d, - setColorFn: blyncDevSetColor, - }, nil - }, - }, - }) -} - -type blyncDriver struct { - usbDriver -} - -func (drv blyncDriver) convert(hDev *hid.DeviceInfo) DeviceInfo { - // blync adds two devices. but only the one which accepts feature reports will work. - if hDev.FeatureReportLength == 0 { - return drv.usbDriver.convert(hDev) - } - return nil -} - -func blyncDevSetColor(d hid.Device, c color.Color) error { - palette := color.Palette{ - color.RGBA{0x00, 0x00, 0x00, 0x00}, // black - color.RGBA{0xff, 0xff, 0xff, 0xff}, // white - color.RGBA{0x00, 0xff, 0xff, 0xff}, // cyan - color.RGBA{0xff, 0x00, 0xff, 0xff}, // magenta - color.RGBA{0x00, 0x00, 0xff, 0xff}, // blue - color.RGBA{0xff, 0xff, 0x00, 0xff}, // yellow - color.RGBA{0x00, 0xff, 0x00, 0xff}, // lime - color.RGBA{0xff, 0x00, 0x00, 0xff}, // red - } - - value := byte((palette.Index(c) * 16) + 127) - return d.Write([]byte{0x00, 0x55, 0x53, 0x42, 0x43, 0x00, 0x40, 0x02, value}) -} +package led + +import ( + "github.com/boombuler/hid" + "image/color" +) + +// Device type: Blync +var Blync DeviceType + +func init() { + Blync = addDriver(blyncDriver{ + usbDriver{ + Name: "Blync", + Type: &Blync, + VendorId: 0x1130, + ProductId: 0x0001, + Open: func(d hid.Device) (Device, error) { + return &simpleHidDevice{ + device: d, + setColorFn: blyncDevSetColor, + }, nil + }, + }, + }) +} + +type blyncDriver struct { + usbDriver +} + +func (drv blyncDriver) convert(hDev *hid.DeviceInfo) DeviceInfo { + // blync adds two devices. but only the one which accepts feature reports will work. + if hDev.FeatureReportLength == 0 { + return drv.usbDriver.convert(hDev) + } + return nil +} + +func blyncDevSetColor(d hid.Device, c color.Color) error { + palette := color.Palette{ + color.RGBA{0x00, 0x00, 0x00, 0x00}, // black + color.RGBA{0xff, 0xff, 0xff, 0xff}, // white + color.RGBA{0x00, 0xff, 0xff, 0xff}, // cyan + color.RGBA{0xff, 0x00, 0xff, 0xff}, // magenta + color.RGBA{0x00, 0x00, 0xff, 0xff}, // blue + color.RGBA{0xff, 0xff, 0x00, 0xff}, // yellow + color.RGBA{0x00, 0xff, 0x00, 0xff}, // lime + color.RGBA{0xff, 0x00, 0x00, 0xff}, // red + } + + value := byte((palette.Index(c) * 16) + 127) + return d.Write([]byte{0x00, 0x55, 0x53, 0x42, 0x43, 0x00, 0x40, 0x02, value}) +} diff --git a/busylight.go b/busylight.go index ba7ebd3..2734961 100644 --- a/busylight.go +++ b/busylight.go @@ -1,82 +1,82 @@ -package led - -import ( - "github.com/boombuler/hid" - "image/color" - "time" -) - -// Device type: BusyLight UC -var BusyLightUC DeviceType - -// Device type: BusyLight Lync -var BusyLightLync DeviceType - -func init() { - BusyLightUC = addDriver(usbDriver{ - Name: "BusyLight UC", - Type: &BusyLightUC, - VendorId: 0x27BB, - ProductId: 0x3BCB, - Open: func(d hid.Device) (Device, error) { - return newBusyLight(d, func(c color.Color) { - r, g, b, _ := c.RGBA() - d.Write([]byte{0x00, 0x00, 0x00, byte(r >> 8), byte(g >> 8), byte(b >> 8), 0x00, 0x00, 0x00}) - }), nil - }, - }) - BusyLightLync = addDriver(usbDriver{ - Name: "BusyLight Lync", - Type: &BusyLightLync, - VendorId: 0x04D8, - ProductId: 0xF848, - Open: func(d hid.Device) (Device, error) { - return newBusyLight(d, func(c color.Color) { - r, g, b, _ := c.RGBA() - d.Write([]byte{0x00, 0x00, 0x00, byte(r >> 8), byte(g >> 8), byte(b >> 8), 0x00, 0x00, 0x00}) - }), nil - }, - }) -} - -type busylightDev struct { - closeChan chan<- struct{} - colorChan chan<- color.Color -} - -func newBusyLight(d hid.Device, setcolorFn func(c color.Color)) *busylightDev { - closeChan := make(chan struct{}) - colorChan := make(chan color.Color) - ticker := time.NewTicker(20 * time.Second) // If nothing is send after 30 seconds the device turns off. - go func() { - var curColor color.Color = color.Black - closed := false - for !closed { - select { - case <-ticker.C: - setcolorFn(curColor) - case col := <-colorChan: - curColor = col - setcolorFn(curColor) - case <-closeChan: - ticker.Stop() - setcolorFn(color.Black) // turn off device - d.Close() - closed = true - } - } - }() - return &busylightDev{closeChan: closeChan, colorChan: colorChan} -} - -func (d *busylightDev) SetKeepActive(v bool) error { - return ErrKeepActiveNotSupported -} - -func (d *busylightDev) SetColor(c color.Color) error { - d.colorChan <- c - return nil -} -func (d *busylightDev) Close() { - d.closeChan <- struct{}{} -} +package led + +import ( + "github.com/boombuler/hid" + "image/color" + "time" +) + +// Device type: BusyLight UC +var BusyLightUC DeviceType + +// Device type: BusyLight Lync +var BusyLightLync DeviceType + +func init() { + BusyLightUC = addDriver(usbDriver{ + Name: "BusyLight UC", + Type: &BusyLightUC, + VendorId: 0x27BB, + ProductId: 0x3BCB, + Open: func(d hid.Device) (Device, error) { + return newBusyLight(d, func(c color.Color) { + r, g, b, _ := c.RGBA() + d.Write([]byte{0x00, 0x00, 0x00, byte(r >> 8), byte(g >> 8), byte(b >> 8), 0x00, 0x00, 0x00}) + }), nil + }, + }) + BusyLightLync = addDriver(usbDriver{ + Name: "BusyLight Lync", + Type: &BusyLightLync, + VendorId: 0x04D8, + ProductId: 0xF848, + Open: func(d hid.Device) (Device, error) { + return newBusyLight(d, func(c color.Color) { + r, g, b, _ := c.RGBA() + d.Write([]byte{0x00, 0x00, 0x00, byte(r >> 8), byte(g >> 8), byte(b >> 8), 0x00, 0x00, 0x00}) + }), nil + }, + }) +} + +type busylightDev struct { + closeChan chan<- struct{} + colorChan chan<- color.Color +} + +func newBusyLight(d hid.Device, setcolorFn func(c color.Color)) *busylightDev { + closeChan := make(chan struct{}) + colorChan := make(chan color.Color) + ticker := time.NewTicker(20 * time.Second) // If nothing is send after 30 seconds the device turns off. + go func() { + var curColor color.Color = color.Black + closed := false + for !closed { + select { + case <-ticker.C: + setcolorFn(curColor) + case col := <-colorChan: + curColor = col + setcolorFn(curColor) + case <-closeChan: + ticker.Stop() + setcolorFn(color.Black) // turn off device + d.Close() + closed = true + } + } + }() + return &busylightDev{closeChan: closeChan, colorChan: colorChan} +} + +func (d *busylightDev) SetKeepActive(v bool) error { + return ErrKeepActiveNotSupported +} + +func (d *busylightDev) SetColor(c color.Color) error { + d.colorChan <- c + return nil +} +func (d *busylightDev) Close() { + d.closeChan <- struct{}{} +} diff --git a/dealextreme.go b/dealextreme.go index be2adaf..5c0e843 100644 --- a/dealextreme.go +++ b/dealextreme.go @@ -1,39 +1,39 @@ -package led - -import ( - "github.com/boombuler/hid" - "image/color" -) - -// Device type: DealExtreme USBMailNotifier -var DealExtreme DeviceType - -func init() { - DealExtreme = addDriver(usbDriver{ - Name: "DealExtreme USBMailNotifier", - Type: &DealExtreme, - VendorId: 0x1294, - ProductId: 0x1320, - Open: func(d hid.Device) (Device, error) { - return &simpleHidDevice{ - device: d, - setColorFn: dealExtremeSetColor, - }, nil - }, - }) -} - -func dealExtremeSetColor(d hid.Device, c color.Color) error { - palette := color.Palette{ - color.RGBA{0x00, 0x00, 0x00, 0x00}, - color.RGBA{0x00, 0xff, 0x00, 0xff}, - color.RGBA{0xff, 0x00, 0x00, 0xff}, - color.RGBA{0x00, 0x00, 0xff, 0xff}, - color.RGBA{0x00, 0xff, 0xff, 0xff}, - color.RGBA{0x00, 0xff, 0xff, 0xff}, - color.RGBA{0xff, 0xff, 0x00, 0xff}, - color.RGBA{0xff, 0x00, 0xff, 0xff}, - color.RGBA{0xff, 0xff, 0xff, 0xff}, - } - return d.Write([]byte{0x00, byte(palette.Index(c))}) -} +package led + +import ( + "github.com/boombuler/hid" + "image/color" +) + +// Device type: DealExtreme USBMailNotifier +var DealExtreme DeviceType + +func init() { + DealExtreme = addDriver(usbDriver{ + Name: "DealExtreme USBMailNotifier", + Type: &DealExtreme, + VendorId: 0x1294, + ProductId: 0x1320, + Open: func(d hid.Device) (Device, error) { + return &simpleHidDevice{ + device: d, + setColorFn: dealExtremeSetColor, + }, nil + }, + }) +} + +func dealExtremeSetColor(d hid.Device, c color.Color) error { + palette := color.Palette{ + color.RGBA{0x00, 0x00, 0x00, 0x00}, + color.RGBA{0x00, 0xff, 0x00, 0xff}, + color.RGBA{0xff, 0x00, 0x00, 0xff}, + color.RGBA{0x00, 0x00, 0xff, 0xff}, + color.RGBA{0x00, 0xff, 0xff, 0xff}, + color.RGBA{0x00, 0xff, 0xff, 0xff}, + color.RGBA{0xff, 0xff, 0x00, 0xff}, + color.RGBA{0xff, 0x00, 0xff, 0xff}, + color.RGBA{0xff, 0xff, 0xff, 0xff}, + } + return d.Write([]byte{0x00, byte(palette.Index(c))}) +} diff --git a/dreamcheeky.go b/dreamcheeky.go index 0dff508..8e1beb7 100644 --- a/dreamcheeky.go +++ b/dreamcheeky.go @@ -1,36 +1,36 @@ -package led - -import ( - "github.com/boombuler/hid" - "image/color" -) - -// Device type: DreamCheeky USBMailNotifier -var DreamCheeky DeviceType - -func init() { - DreamCheeky = addDriver(usbDriver{ - Name: "DreamCheeky USBMailNotifier", - Type: &DreamCheeky, - VendorId: 0x1D34, - ProductId: 0x0004, - Open: func(d hid.Device) (Device, error) { - if err := d.Write([]byte{0x00, 0x1F, 0x01, 0x29, 0x00, 0xB8, 0x54, 0x2C, 0x03}); err != nil { - return nil, err - } - - if err := d.Write([]byte{0x00, 0x00, 0x01, 0x29, 0x00, 0xB8, 0x54, 0x2C, 0x04}); err != nil { - return nil, err - } - return &simpleHidDevice{ - device: d, - setColorFn: dreamCheekyDevSetColor, - }, nil - }, - }) -} - -func dreamCheekyDevSetColor(d hid.Device, c color.Color) error { - r, g, b, _ := c.RGBA() - return d.Write([]byte{0x00, byte(r >> 10), byte(g >> 10), byte(b >> 10), 0x00, 0x00, 0x54, 0x2C, 0x05}) -} +package led + +import ( + "github.com/boombuler/hid" + "image/color" +) + +// Device type: DreamCheeky USBMailNotifier +var DreamCheeky DeviceType + +func init() { + DreamCheeky = addDriver(usbDriver{ + Name: "DreamCheeky USBMailNotifier", + Type: &DreamCheeky, + VendorId: 0x1D34, + ProductId: 0x0004, + Open: func(d hid.Device) (Device, error) { + if err := d.Write([]byte{0x00, 0x1F, 0x01, 0x29, 0x00, 0xB8, 0x54, 0x2C, 0x03}); err != nil { + return nil, err + } + + if err := d.Write([]byte{0x00, 0x00, 0x01, 0x29, 0x00, 0xB8, 0x54, 0x2C, 0x04}); err != nil { + return nil, err + } + return &simpleHidDevice{ + device: d, + setColorFn: dreamCheekyDevSetColor, + }, nil + }, + }) +} + +func dreamCheekyDevSetColor(d hid.Device, c color.Color) error { + r, g, b, _ := c.RGBA() + return d.Write([]byte{0x00, byte(r >> 10), byte(g >> 10), byte(b >> 10), 0x00, 0x00, 0x54, 0x2C, 0x05}) +} diff --git a/led.go b/led.go index c380c80..e08b22f 100644 --- a/led.go +++ b/led.go @@ -1,96 +1,96 @@ -package led - -import ( - "errors" - "fmt" - "github.com/boombuler/hid" - "image/color" -) - -// Device type identifies the device type. the IDs may change on each program start. -type DeviceType int - -// String returns a representation of the device type -func (dt DeviceType) String() string { - idx := int(dt) - if idx < 0 || idx >= len(drivers) { - return "" - } - return drivers[idx].name() -} - -type driver interface { - name() string - convert(hDev *hid.DeviceInfo) DeviceInfo -} - -var drivers = []driver{} - -func addDriver(drv driver) DeviceType { - dt := DeviceType(len(drivers)) - drivers = append(drivers, drv) - return dt -} - -// Some devices does not support KeepActive. -var ErrKeepActiveNotSupported = errors.New("KeepActive is not supported by this device") - -// Device is an opened LED device. -type Device interface { - // SetColor sets the color of the LED to the closest supported color. - SetColor(c color.Color) error - - // SetKeepActive sets a value that tells the device not turn off the device on calling Close. By default the device is turned off! - SetKeepActive(v bool) error - - // Close the device and release all resources - Close() -} - -// DeviceInfo keeps information about a physical LED device -type DeviceInfo interface { - // GetPath returns a system specific path which can be used to find the device - GetPath() string - // GetType returns the "driver type" of the device - GetType() DeviceType - // Open opens the device for usage - Open() (Device, error) -} - -// Devices returns a channel with all connected LED devices -func Devices() <-chan DeviceInfo { - result := make(chan DeviceInfo) - go func() { - for d := range hid.Devices() { - di := toLedDeviceInfo(d) - if di != nil { - result <- di - } - } - close(result) - }() - return result -} - -// ByPath searches a device by given system specific path. (The path can be obtained from the GetPath func from DeviceInfo) -func ByPath(path string) (DeviceInfo, error) { - hd, err := hid.ByPath(path) - if err != nil { - return nil, err - } - led := toLedDeviceInfo(hd) - if led == nil { - return nil, fmt.Errorf("Unknown LED device (VID: %v, PID: %v)", hd.VendorId, hd.ProductId) - } - return led, nil -} - -func toLedDeviceInfo(dev *hid.DeviceInfo) DeviceInfo { - for _, drv := range drivers { - di := drv.convert(dev) - if di != nil { - return di - } - } - return nil -} +package led + +import ( + "errors" + "fmt" + "github.com/boombuler/hid" + "image/color" +) + +// Device type identifies the device type. the IDs may change on each program start. +type DeviceType int + +// String returns a representation of the device type +func (dt DeviceType) String() string { + idx := int(dt) + if idx < 0 || idx >= len(drivers) { + return "" + } + return drivers[idx].name() +} + +type driver interface { + name() string + convert(hDev *hid.DeviceInfo) DeviceInfo +} + +var drivers = []driver{} + +func addDriver(drv driver) DeviceType { + dt := DeviceType(len(drivers)) + drivers = append(drivers, drv) + return dt +} + +// Some devices does not support KeepActive. +var ErrKeepActiveNotSupported = errors.New("KeepActive is not supported by this device") + +// Device is an opened LED device. +type Device interface { + // SetColor sets the color of the LED to the closest supported color. + SetColor(c color.Color) error + + // SetKeepActive sets a value that tells the device not turn off the device on calling Close. By default the device is turned off! + SetKeepActive(v bool) error + + // Close the device and release all resources + Close() +} + +// DeviceInfo keeps information about a physical LED device +type DeviceInfo interface { + // GetPath returns a system specific path which can be used to find the device + GetPath() string + // GetType returns the "driver type" of the device + GetType() DeviceType + // Open opens the device for usage + Open() (Device, error) +} + +// Devices returns a channel with all connected LED devices +func Devices() <-chan DeviceInfo { + result := make(chan DeviceInfo) + go func() { + for d := range hid.Devices() { + di := toLedDeviceInfo(d) + if di != nil { + result <- di + } + } + close(result) + }() + return result +} + +// ByPath searches a device by given system specific path. (The path can be obtained from the GetPath func from DeviceInfo) +func ByPath(path string) (DeviceInfo, error) { + hd, err := hid.ByPath(path) + if err != nil { + return nil, err + } + led := toLedDeviceInfo(hd) + if led == nil { + return nil, fmt.Errorf("Unknown LED device (VID: %v, PID: %v)", hd.VendorId, hd.ProductId) + } + return led, nil +} + +func toLedDeviceInfo(dev *hid.DeviceInfo) DeviceInfo { + for _, drv := range drivers { + di := drv.convert(dev) + if di != nil { + return di + } + } + return nil +} diff --git a/usbdevice.go b/usbdevice.go index 5d63b5e..810f497 100644 --- a/usbdevice.go +++ b/usbdevice.go @@ -1,46 +1,46 @@ -package led - -import ( - "github.com/boombuler/hid" -) - -type usbDriver struct { - Name string - Type *DeviceType - VendorId uint16 - ProductId uint16 - Open func(hidDevice hid.Device) (Device, error) -} - -func (drv usbDriver) name() string { - return drv.Name -} - -func (sd usbDriver) convert(hDev *hid.DeviceInfo) DeviceInfo { - if hDev.VendorId == sd.VendorId && hDev.ProductId == sd.ProductId { - return &usbDeviceInfo{hDev, &sd} - } - return nil -} - -type usbDeviceInfo struct { - *hid.DeviceInfo - driver *usbDriver -} - -func (usb usbDeviceInfo) GetType() DeviceType { - return *usb.driver.Type -} - -func (usb usbDeviceInfo) GetPath() string { - return usb.Path -} - -func (usb *usbDeviceInfo) Open() (Device, error) { - d, err := usb.DeviceInfo.Open() - if err != nil { - return nil, err - } else { - return usb.driver.Open(d) - } -} +package led + +import ( + "github.com/boombuler/hid" +) + +type usbDriver struct { + Name string + Type *DeviceType + VendorId uint16 + ProductId uint16 + Open func(hidDevice hid.Device) (Device, error) +} + +func (drv usbDriver) name() string { + return drv.Name +} + +func (sd usbDriver) convert(hDev *hid.DeviceInfo) DeviceInfo { + if hDev.VendorId == sd.VendorId && hDev.ProductId == sd.ProductId { + return &usbDeviceInfo{hDev, &sd} + } + return nil +} + +type usbDeviceInfo struct { + *hid.DeviceInfo + driver *usbDriver +} + +func (usb usbDeviceInfo) GetType() DeviceType { + return *usb.driver.Type +} + +func (usb usbDeviceInfo) GetPath() string { + return usb.Path +} + +func (usb *usbDeviceInfo) Open() (Device, error) { + d, err := usb.DeviceInfo.Open() + if err != nil { + return nil, err + } else { + return usb.driver.Open(d) + } +}