Tweaks and renames

This commit is contained in:
Kyle Lemons
2012-03-27 18:34:56 -07:00
parent 8a56742c74
commit be26b2f34b
3 changed files with 17 additions and 17 deletions

View File

@@ -17,7 +17,7 @@ func main() {
flag.Parse() flag.Parse()
// Only one context should be needed for an application. It should always be closed. // Only one context should be needed for an application. It should always be closed.
ctx = usb.NewContext() ctx := usb.NewContext()
defer ctx.Close() defer ctx.Close()
// Debugging can be turned on; this shows some of the inner workings of the libusb package. // Debugging can be turned on; this shows some of the inner workings of the libusb package.

View File

@@ -11,7 +11,7 @@ import (
"unsafe" "unsafe"
) )
type Endpoint struct { type EndpointInfo struct {
Type DescriptorType Type DescriptorType
Address uint8 Address uint8
Attributes uint8 Attributes uint8
@@ -21,15 +21,15 @@ type Endpoint struct {
SynchAddress uint8 SynchAddress uint8
} }
func (e Endpoint) Number() int { func (e EndpointInfo) Number() int {
return int(e.Address) & ENDPOINT_NUM_MASK return int(e.Address) & ENDPOINT_NUM_MASK
} }
func (e Endpoint) Direction() EndpointDirection { func (e EndpointInfo) Direction() EndpointDirection {
return EndpointDirection(e.Address) & ENDPOINT_DIR_MASK return EndpointDirection(e.Address) & ENDPOINT_DIR_MASK
} }
func (e Endpoint) String() string { func (e EndpointInfo) String() string {
return fmt.Sprintf("Endpoint %d %-3s %s - %s %s", return fmt.Sprintf("Endpoint %d %-3s %s - %s %s",
e.Number(), e.Direction(), e.Number(), e.Direction(),
TransferType(e.Attributes)&TRANSFER_TYPE_MASK, TransferType(e.Attributes)&TRANSFER_TYPE_MASK,
@@ -38,17 +38,17 @@ func (e Endpoint) String() string {
) )
} }
type Interface struct { type InterfaceInfo struct {
Type DescriptorType Type DescriptorType
Number uint8 Number uint8
Alternate uint8 Alternate uint8
IfClass uint8 IfClass uint8
IfSubClass uint8 IfSubClass uint8
IfProtocol uint8 IfProtocol uint8
Endpoints []*Endpoint Endpoints []*EndpointInfo
} }
func (i Interface) String() string { func (i InterfaceInfo) String() string {
return fmt.Sprintf("Interface %02x (config %02x)", i.Number, i.Alternate) return fmt.Sprintf("Interface %02x (config %02x)", i.Number, i.Alternate)
} }
@@ -59,7 +59,7 @@ type Config struct {
Config uint8 Config uint8
Attributes uint8 Attributes uint8
MaxPower uint8 MaxPower uint8
Interfaces [][]*Interface Interfaces [][]*InterfaceInfo
} }
func (c Config) String() string { func (c Config) String() string {
@@ -81,7 +81,7 @@ func newConfig(cfg *C.struct_libusb_config_descriptor) *Config {
Len: int(cfg.bNumInterfaces), Len: int(cfg.bNumInterfaces),
Cap: int(cfg.bNumInterfaces), Cap: int(cfg.bNumInterfaces),
} }
c.Interfaces = make([][]*Interface, 0, len(ifaces)) c.Interfaces = make([][]*InterfaceInfo, 0, len(ifaces))
for _, iface := range ifaces { for _, iface := range ifaces {
var alts []C.struct_libusb_interface_descriptor var alts []C.struct_libusb_interface_descriptor
*(*reflect.SliceHeader)(unsafe.Pointer(&alts)) = reflect.SliceHeader{ *(*reflect.SliceHeader)(unsafe.Pointer(&alts)) = reflect.SliceHeader{
@@ -89,9 +89,9 @@ func newConfig(cfg *C.struct_libusb_config_descriptor) *Config {
Len: int(iface.num_altsetting), Len: int(iface.num_altsetting),
Cap: int(iface.num_altsetting), Cap: int(iface.num_altsetting),
} }
descs := make([]*Interface, 0, len(alts)) descs := make([]*InterfaceInfo, 0, len(alts))
for _, alt := range alts { for _, alt := range alts {
i := &Interface{ i := &InterfaceInfo{
Type: DescriptorType(alt.bDescriptorType), Type: DescriptorType(alt.bDescriptorType),
Number: uint8(alt.bInterfaceNumber), Number: uint8(alt.bInterfaceNumber),
Alternate: uint8(alt.bAlternateSetting), Alternate: uint8(alt.bAlternateSetting),
@@ -105,9 +105,9 @@ func newConfig(cfg *C.struct_libusb_config_descriptor) *Config {
Len: int(alt.bNumEndpoints), Len: int(alt.bNumEndpoints),
Cap: int(alt.bNumEndpoints), Cap: int(alt.bNumEndpoints),
} }
i.Endpoints = make([]*Endpoint, 0, len(ends)) i.Endpoints = make([]*EndpointInfo, 0, len(ends))
for _, end := range ends { for _, end := range ends {
i.Endpoints = append(i.Endpoints, &Endpoint{ i.Endpoints = append(i.Endpoints, &EndpointInfo{
Type: DescriptorType(end.bDescriptorType), Type: DescriptorType(end.bDescriptorType),
Address: uint8(end.bEndpointAddress), Address: uint8(end.bEndpointAddress),
Attributes: uint8(end.bmAttributes), Attributes: uint8(end.bmAttributes),

View File

@@ -39,13 +39,13 @@ func Describe(val interface{}) string {
// //
// The given val must be one of the following: // The given val must be one of the following:
// - *usb.Descriptor "Class (SubClass) Protocol" // - *usb.Descriptor "Class (SubClass) Protocol"
// - *usb.Interface "IfClass (IfSubClass) IfProtocol" // - *usb.InterfaceInfo "IfClass (IfSubClass) IfProtocol"
func Classify(val interface{}) string { func Classify(val interface{}) string {
var class, sub, proto uint8 var class, sub, proto uint8
switch val := val.(type) { switch val := val.(type) {
case *usb.Descriptor: case *usb.Descriptor:
class, sub, proto = val.Class, val.SubClass, val.Protocol class, sub, proto = val.Class, val.SubClass, val.Protocol
case *usb.Interface: case *usb.InterfaceInfo:
class, sub, proto = val.IfClass, val.IfSubClass, val.IfProtocol class, sub, proto = val.IfClass, val.IfSubClass, val.IfProtocol
default: default:
return fmt.Sprintf("Unknown (%T)", val) return fmt.Sprintf("Unknown (%T)", val)