3      Copyright (C) Dean Camera, 2016. 
   5   dean [at] fourwalledcubicle [dot] com 
  10     LUFA Bulk Vendor device demo host test script. This script will send and 
  11     receive a continuous stream of packets to/from to the device, to show 
  12     bidirectional communications. 
  14     Requires the pyUSB library (http://sourceforge.net/projects/pyusb/). 
  18 from time 
import sleep
 
  22 # Bulk Vendor HID device VID and PID 
  29 def get_vendor_device_handle(): 
  30         dev_handle 
= usb
.core
.find(idVendor
=device_vid
, idProduct
=device_pid
) 
  34 def write(device
, packet
): 
  35     device
.write(usb
.util
.ENDPOINT_OUT | device_out_ep
, packet
, 0, 1000) 
  36     print("Sent Packet: {0}".format(packet
)) 
  40     packet 
= device
.read(usb
.util
.ENDPOINT_IN | device_in_ep
, 64, 0, 1000) 
  41     print("Received Packet: {0}".format(''.join([chr(x
) for x 
in packet
]))) 
  46     vendor_device 
= get_vendor_device_handle() 
  48     if vendor_device 
is None: 
  49         print("No valid Vendor device found.") 
  52     vendor_device
.set_configuration() 
  54     print("Connected to device 0x%04X/0x%04X - %s [%s]" % 
  55           (vendor_device
.idVendor
, vendor_device
.idProduct
, 
  56            usb
.util
.get_string(vendor_device
, 255, vendor_device
.iProduct
), 
  57            usb
.util
.get_string(vendor_device
, 255, vendor_device
.iManufacturer
))) 
  62         write(vendor_device
, "TEST PACKET %d" % x
) 
  66 if __name__ 
== '__main__':