コード例 #1
0
ファイル: usb.go プロジェクト: thequux/gousb
// Open a device by vendor/product id. If more than one device
// matches, return the first.
func (ctx *Context) Open(vendor, product int) (*DeviceHandle, *UsbError) {
	handle := &DeviceHandle{ctx, nil, 0, make(map[byte]*Interface, 0)}
	dev := C.libusb_open_device_with_vid_pid(ctx.ctx, C.uint16_t(vendor), C.uint16_t(product))

	if dev == nil {
		return nil, UsbErrorMisc
	}
	handle.handle = dev
	return handle, nil
}
コード例 #2
0
ファイル: eeprom.go プロジェクト: sstallion/go
// First returns the first supported device attached to the host. Unlike Walk,
// the returned Device is opened automatically. This function exists primarily
// for testing.
func First() (*Device, error) {
	handle := C.libusb_open_device_with_vid_pid(context, idVendor, idProduct)
	if handle == nil {
		return nil, errors.New("no devices found")
	}
	if err := C.libusb_claim_interface(handle, interfaceNum); err != C.LIBUSB_SUCCESS {
		C.libusb_close(handle)
		return nil, &libusbError{err}
	}
	return &Device{
		dev:    C.libusb_get_device(handle),
		handle: handle,
	}, nil
}
コード例 #3
0
ファイル: golibusb.go プロジェクト: acsellers/Go510
func Start(color chan byte, output chan guinput.KeyEvent) *C.struct_libusb_device_handle {

	C.libusb_init(nil)
	fmt.Println(KEYMAP[0x21])

	key_device_handle := C.libusb_open_device_with_vid_pid(nil, 0x046d, 0xc22d)

	//detach any necessary kernel drivers from MY keyboard
	if C.libusb_kernel_driver_active(key_device_handle, 0) == 1 {
		log.Print("kernel driver active on main interface")
		e := C.libusb_detach_kernel_driver(key_device_handle, 0)
		if e != 0 {
			log.Fatal("Can't detach kernel driver")
		}
	}
	if C.libusb_kernel_driver_active(key_device_handle, 1) == 1 {
		fmt.Println("kernel driver active")
		e := C.libusb_detach_kernel_driver(key_device_handle, 1)
		if e != 0 {
			log.Fatal("Can't detach kernel driver")
		}
	}

	//Claim the interfaces we'll be listening on
	r := C.libusb_claim_interface(key_device_handle, 0)
	if r != 0 {
		log.Fatal("Can't claim main interface")
	}
	r = C.libusb_claim_interface(key_device_handle, 1)
	if r != 0 {
		log.Fatal("Can't claim special interface")
	}

	log.Print("Starting libusb goroutines\n")

	go ColorChange(key_device_handle, color)
	//go LCDChange(key_device_handle, lcd)
	go NormalKeyMonitor(key_device_handle, output)
	go SpecialKeyMonitor(key_device_handle, output)

	log.Print("Libusb Goroutines started\n")
	return key_device_handle
}
コード例 #4
0
ファイル: usb.go プロジェクト: giddyinc/gousb
// OpenDeviceWithVidPid opens Device from specific VendorId and ProductId.
// If there are any errors, it'll returns at second value.
func (c *Context) OpenDeviceWithVidPid(vid, pid int) (*Device, error) {
	handle := C.libusb_open_device_with_vid_pid(c.ctx, (C.uint16_t)(vid), (C.uint16_t)(pid))
	if handle == nil {
		return nil, ERROR_NOT_FOUND
	}

	dev := C.libusb_get_device(handle)
	if dev == nil {
		return nil, ERROR_NO_DEVICE
	}

	desc, err := newDescriptor(dev)

	// return an error from nil-handle and nil-device
	if err != nil {
		return nil, err
	}

	device := newDevice(handle, desc)
	return device, nil
}
コード例 #5
0
ファイル: new.go プロジェクト: acsellers/Go510
func StartLibUsb() error {
	Connected = true

	key_device_handle := C.libusb_open_device_with_vid_pid(nil, 0x046d, 0xc22d)
	if key_device_handle != nil {

		if C.libusb_kernel_driver_active(key_device_handle, 1) == 1 {
			e := C.libusb_detach_kernel_driver(key_device_handle, 1)
			if e != 0 {
				log.Fatal("Can't detach kernel driver")
			}
		}

		r := C.libusb_claim_interface(key_device_handle, 1)
		if r != 0 {
			log.Fatal("Can't claim special interface")
		}

		keyboardHandle = key_device_handle

		return nil
	}
	return errors.New("could not open driver")
}