Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_generic_hid_libusb.py 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env python
  2. """
  3. LUFA Library
  4. Copyright (C) Dean Camera, 2014.
  5. dean [at] fourwalledcubicle [dot] com
  6. www.lufa-lib.org
  7. """
  8. """
  9. LUFA Generic HID device demo host test script. This script will send a
  10. continuous stream of generic reports to the device, to show a variable LED
  11. pattern on the target board. Send and received report data is printed to
  12. the terminal.
  13. Requires the PyUSB library (http://sourceforge.net/apps/trac/pyusb/).
  14. """
  15. import sys
  16. from time import sleep
  17. import usb.core
  18. import usb.util
  19. # Generic HID device VID, PID and report payload length (length is increased
  20. # by one to account for the Report ID byte that must be pre-pended)
  21. device_vid = 0x03EB
  22. device_pid = 0x204F
  23. def get_and_init_hid_device():
  24. device = usb.core.find(idVendor=device_vid, idProduct=device_pid)
  25. if device is None:
  26. sys.exit("Could not find USB device.")
  27. if device.is_kernel_driver_active(0):
  28. try:
  29. device.detach_kernel_driver(0)
  30. except usb.core.USBError as exception:
  31. sys.exit("Could not detatch kernel driver: %s" % str(exception))
  32. try:
  33. device.set_configuration()
  34. except usb.core.USBError as exception:
  35. sys.exit("Could not set configuration: %s" % str(exception))
  36. return device
  37. def send_led_pattern(device, led1, led2, led3, led4):
  38. # Report data for the demo is LED on/off data
  39. report_data = [led1, led2, led3, led4]
  40. # Send the generated report to the device
  41. number_of_bytes_written = device.ctrl_transfer( # Set Report control request
  42. 0b00100001, # bmRequestType (constant for this control request)
  43. 0x09, # bmRequest (constant for this control request)
  44. 0, # wValue (MSB is report type, LSB is report number)
  45. 0, # wIndex (interface number)
  46. report_data # report data to be sent
  47. );
  48. assert number_of_bytes_written == len(report_data)
  49. print("Sent LED Pattern: {0}".format(report_data))
  50. def receive_led_pattern(hid_device):
  51. endpoint = hid_device[0][(0,0)][0]
  52. report_data = hid_device.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize)
  53. return list(report_data)
  54. def main():
  55. hid_device = get_and_init_hid_device()
  56. print("Connected to device 0x%04X/0x%04X - %s [%s]" %
  57. (hid_device.idVendor, hid_device.idProduct,
  58. usb.util.get_string(hid_device, 256, hid_device.iProduct),
  59. usb.util.get_string(hid_device, 256, hid_device.iManufacturer)))
  60. p = 0
  61. while (True):
  62. # Convert the current pattern index to a bit-mask and send
  63. send_led_pattern(hid_device,
  64. (p >> 3) & 1,
  65. (p >> 2) & 1,
  66. (p >> 1) & 1,
  67. (p >> 0) & 1)
  68. # Receive and print the current LED pattern
  69. led_pattern = receive_led_pattern(hid_device)[0:4]
  70. print("Received LED Pattern: {0}".format(led_pattern))
  71. # Compute next LED pattern in sequence
  72. p = (p + 1) % 16
  73. # Delay a bit for visual effect
  74. sleep(.2)
  75. if __name__ == '__main__':
  76. main()