3 Copyright (C) Dean Camera, 2014.
5 dean [at] fourwalledcubicle [dot] com
10 LUFA Generic HID device demo host test script. This script will send a
11 continuous stream of generic reports to the device, to show a variable LED
12 pattern on the target board. Send and received report data is printed to
15 Requires the pywinusb library (https://pypi.python.org/pypi/pywinusb/).
19 from time import sleep
20 import pywinusb.hid as hid
22 # Generic HID device VID, PID and report payload length (length is increased
23 # by one to account for the Report ID byte that must be pre-pended)
29 def get_hid_device_handle():
30 hid_device_filter = hid.HidDeviceFilter(vendor_id=device_vid,
31 product_id=device_pid)
33 valid_hid_devices = hid_device_filter.get_devices()
35 if len(valid_hid_devices) is 0:
38 return valid_hid_devices[0]
41 def send_led_pattern(device, led1, led2, led3, led4):
42 # Report data for the demo is the report ID (always zero) followed by the
44 report_data = [0, led1, led2, led3, led4]
46 # Zero-extend the array to the length the report should be
47 report_data.extend([0] * (report_length - len(report_data)))
49 # Send the generated report to the device
50 device.send_output_report(report_data)
52 print("Sent LED Pattern: {0}".format(report_data[1:5]))
55 def received_led_pattern(report_data):
56 print("Received LED Pattern: {0}".format(report_data[1:5]))
60 hid_device = get_hid_device_handle()
62 if hid_device is None:
63 print("No valid HID device found.")
69 print("Connected to device 0x%04X/0x%04X - %s [%s]" %
70 (hid_device.vendor_id, hid_device.product_id,
71 hid_device.product_name, hid_device.vendor_name))
73 # Set up the HID input report handler to receive reports
74 hid_device.set_raw_data_handler(received_led_pattern)
77 while (hid_device.is_plugged()):
78 # Convert the current pattern index to a bit-mask and send
79 send_led_pattern(hid_device,
85 # Compute next LED pattern in sequence
88 # Delay a bit for visual effect
95 if __name__ == '__main__':