-
Notifications
You must be signed in to change notification settings - Fork 5
/
device.go
53 lines (44 loc) · 1.37 KB
/
device.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright (c) 2015-2020 The usbtmc developers. All rights reserved.
// Project site: https://github.com/gotmc/usbtmc
// Use of this source code is governed by a MIT-style license that
// can be found in the LICENSE.txt file for the project.
package google
import (
"fmt"
"github.com/google/gousb"
)
// Device represents a USB device not a USBMTC device.
type Device struct {
dev *gousb.Device
intf *gousb.Interface
cfg *gousb.Config
BulkInEndpoint *gousb.InEndpoint
BulkOutEndpoint *gousb.OutEndpoint
InterruptInEndpoint *gousb.InEndpoint
}
// Close closes the Device.
func (d *Device) Close() error {
d.intf.Close()
err := d.cfg.Close()
if err != nil {
return err
}
return d.dev.Close()
}
// String providers the Stringer interface method for Device.
func (d *Device) String() string {
return fmt.Sprintf("%#v", d)
}
// Write writes to the USB device's bulk out endpoint.
func (d *Device) Write(p []byte) (n int, err error) {
return d.BulkOutEndpoint.Write(p)
}
// WriteString writes the given string to the Device and returns the number
// of bytes written along with an error code.
func (d *Device) WriteString(s string) (n int, err error) {
return d.Write([]byte(s))
}
// Read reads from the USB device's bulk in endpoint.
func (d *Device) Read(p []byte) (n int, err error) {
return d.BulkInEndpoint.Read(p)
}