Selaa lähdekoodia

taking optional read size to be compatible with broken hardware

Serge A. Zaitsev 9 vuotta sitten
vanhempi
commit
bef0a2cefc
3 muutettua tiedostoa jossa 7 lisäystä ja 4 poistoa
  1. 1 1
      example/main.go
  2. 1 1
      hid.go
  3. 5 2
      usb_linux.go

+ 1 - 1
example/main.go

@@ -28,7 +28,7 @@ func shell(device hid.Device) {
 
 	go func() {
 		for {
-			if buf, err := device.Read(1 * time.Second); err == nil {
+			if buf, err := device.Read(-1, 1*time.Second); err == nil {
 				log.Println("Input report:  ", hex.EncodeToString(buf))
 			}
 		}

+ 1 - 1
hid.go

@@ -26,6 +26,6 @@ type Device interface {
 	HIDReport() ([]byte, error)
 	SetReport(int, []byte) error
 	GetReport(int) ([]byte, error)
-	Read(ms time.Duration) ([]byte, error)
+	Read(size int, ms time.Duration) ([]byte, error)
 	Write(data []byte, ms time.Duration) (int, error)
 }

+ 5 - 2
usb_linux.go

@@ -125,8 +125,11 @@ func (hid *usbDevice) intr(ep int, data []byte, t int) (int, error) {
 	}
 }
 
-func (hid *usbDevice) Read(timeout time.Duration) ([]byte, error) {
-	data := make([]byte, hid.inputPacketSize, hid.inputPacketSize)
+func (hid *usbDevice) Read(size int, timeout time.Duration) ([]byte, error) {
+	if size < 0 {
+		size = int(hid.inputPacketSize)
+	}
+	data := make([]byte, size, size)
 	ms := timeout / (1 * time.Millisecond)
 	n, err := hid.intr(hid.epIn, data, int(ms))
 	if err == nil {