]> git.gir.st - tmk_keyboard.git/blob - protocol/lufa/LUFA-git/Demos/Device/LowLevel/GenericHID/HostTestApp/test_generic_hid.py
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
[tmk_keyboard.git] / protocol / lufa / LUFA-git / Demos / Device / LowLevel / GenericHID / HostTestApp / test_generic_hid.py
1 """
2 LUFA Library
3 Copyright (C) Dean Camera, 2014.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 """
8
9 """
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
13 the terminal.
14
15 Requires the pywinusb library (https://pypi.python.org/pypi/pywinusb/).
16 """
17
18 import sys
19 from time import sleep
20 import pywinusb.hid as hid
21
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)
24 device_vid = 0x03EB
25 device_pid = 0x204F
26 report_length = 1 + 8
27
28
29 def get_hid_device_handle():
30 hid_device_filter = hid.HidDeviceFilter(vendor_id=device_vid,
31 product_id=device_pid)
32
33 valid_hid_devices = hid_device_filter.get_devices()
34
35 if len(valid_hid_devices) is 0:
36 return None
37 else:
38 return valid_hid_devices[0]
39
40
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
43 # LED on/off data
44 report_data = [0, led1, led2, led3, led4]
45
46 # Zero-extend the array to the length the report should be
47 report_data.extend([0] * (report_length - len(report_data)))
48
49 # Send the generated report to the device
50 device.send_output_report(report_data)
51
52 print("Sent LED Pattern: {0}".format(report_data[1:5]))
53
54
55 def received_led_pattern(report_data):
56 print("Received LED Pattern: {0}".format(report_data[1:5]))
57
58
59 def main():
60 hid_device = get_hid_device_handle()
61
62 if hid_device is None:
63 print("No valid HID device found.")
64 sys.exit(1)
65
66 try:
67 hid_device.open()
68
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))
72
73 # Set up the HID input report handler to receive reports
74 hid_device.set_raw_data_handler(received_led_pattern)
75
76 p = 0
77 while (hid_device.is_plugged()):
78 # Convert the current pattern index to a bit-mask and send
79 send_led_pattern(hid_device,
80 (p >> 3) & 1,
81 (p >> 2) & 1,
82 (p >> 1) & 1,
83 (p >> 0) & 1)
84
85 # Compute next LED pattern in sequence
86 p = (p + 1) % 16
87
88 # Delay a bit for visual effect
89 sleep(.2)
90
91 finally:
92 hid_device.close()
93
94
95 if __name__ == '__main__':
96 main()
Imprint / Impressum