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