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_winusb.py 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. """
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. """
  7. """
  8. LUFA Generic HID device demo host test script. This script will send a
  9. continuous stream of generic reports to the device, to show a variable LED
  10. pattern on the target board. Send and received report data is printed to
  11. the terminal.
  12. Requires the pywinusb library (https://pypi.python.org/pypi/pywinusb/).
  13. """
  14. import sys
  15. from time import sleep
  16. import pywinusb.hid as hid
  17. # Generic HID device VID, PID and report payload length (length is increased
  18. # by one to account for the Report ID byte that must be pre-pended)
  19. device_vid = 0x03EB
  20. device_pid = 0x204F
  21. report_length = 1 + 8
  22. def get_hid_device_handle():
  23. hid_device_filter = hid.HidDeviceFilter(vendor_id=device_vid,
  24. product_id=device_pid)
  25. valid_hid_devices = hid_device_filter.get_devices()
  26. if len(valid_hid_devices) is 0:
  27. return None
  28. else:
  29. return valid_hid_devices[0]
  30. def send_led_pattern(device, led1, led2, led3, led4):
  31. # Report data for the demo is the report ID (always zero) followed by the
  32. # LED on/off data
  33. report_data = [0, led1, led2, led3, led4]
  34. # Zero-extend the array to the length the report should be
  35. report_data.extend([0] * (report_length - len(report_data)))
  36. # Send the generated report to the device
  37. device.send_output_report(report_data)
  38. print("Sent LED Pattern: {0}".format(report_data[1:5]))
  39. def received_led_pattern(report_data):
  40. print("Received LED Pattern: {0}".format(report_data[1:5]))
  41. def main():
  42. hid_device = get_hid_device_handle()
  43. if hid_device is None:
  44. print("No valid HID device found.")
  45. sys.exit(1)
  46. try:
  47. hid_device.open()
  48. print("Connected to device 0x%04X/0x%04X - %s [%s]" %
  49. (hid_device.vendor_id, hid_device.product_id,
  50. hid_device.product_name, hid_device.vendor_name))
  51. # Set up the HID input report handler to receive reports
  52. hid_device.set_raw_data_handler(received_led_pattern)
  53. p = 0
  54. while (hid_device.is_plugged()):
  55. # Convert the current pattern index to a bit-mask and send
  56. send_led_pattern(hid_device,
  57. (p >> 3) & 1,
  58. (p >> 2) & 1,
  59. (p >> 1) & 1,
  60. (p >> 0) & 1)
  61. # Compute next LED pattern in sequence
  62. p = (p + 1) % 16
  63. # Delay a bit for visual effect
  64. sleep(.2)
  65. finally:
  66. hid_device.close()
  67. if __name__ == '__main__':
  68. main()