diff options
author | tmk <hasu@tmk-kbd.com> | 2015-05-13 11:13:10 +0900 |
---|---|---|
committer | tmk <hasu@tmk-kbd.com> | 2015-05-13 11:13:10 +0900 |
commit | f6d56675f9f981c5464f0ca7a1fbb0162154e8c5 (patch) | |
tree | 57c9d4b3808a26116ae0ee7956fc00b84841aa2b /protocol/lufa/LUFA-git/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_libusb.py | |
parent | 4d116a04e94cf0d19317d5b44e4fa9f34a3e5594 (diff) |
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
dc0e46e Rename LUFA to LUFA-git
3bfa7fa Remove LUFA-120730
215b764 Merge commit 'afa0f22a9299686fd88f58ce09c5b521ac917e8f' as 'protocol/lufa/LUFA'
afa0f22 Squashed 'protocol/lufa/LUFA/' content from commit def7fca
c0c42fa Remove submodule of LUFA
30f897d Merge commit '87ced33feb74e79c3281dda36eb6d6d153399b41' as 'protocol/usb_hid/USB_Host_Shield_2.0'
87ced33 Squashed 'protocol/usb_hid/USB_Host_Shield_2.0/' content from commit aab4a69
14f6d49 Remove submodule of USB_Host_Shield_2.0
git-subtree-dir: tmk_core
git-subtree-split: dc0e46eaa4367d4e218f8816e3c117895820f07c
Diffstat (limited to 'protocol/lufa/LUFA-git/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_libusb.py')
m--------- | protocol/lufa/LUFA-git | 0 | ||||
-rwxr-xr-x | protocol/lufa/LUFA-git/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_libusb.py | 98 |
2 files changed, 98 insertions, 0 deletions
diff --git a/protocol/lufa/LUFA-git b/protocol/lufa/LUFA-git deleted file mode 160000 -Subproject b6c18b2a7c544653efbe12a1d4e8ba65e7d83c3 diff --git a/protocol/lufa/LUFA-git/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_libusb.py b/protocol/lufa/LUFA-git/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_libusb.py new file mode 100755 index 0000000000..b61970d7a4 --- /dev/null +++ b/protocol/lufa/LUFA-git/Demos/Device/ClassDriver/GenericHID/HostTestApp/test_generic_hid_libusb.py @@ -0,0 +1,98 @@ +#!/usr/bin/env python + +""" + LUFA Library + Copyright (C) Dean Camera, 2014. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +""" + +""" + LUFA Generic HID device demo host test script. This script will send a + continuous stream of generic reports to the device, to show a variable LED + pattern on the target board. Send and received report data is printed to + the terminal. + + Requires the PyUSB library (http://sourceforge.net/apps/trac/pyusb/). +""" + +import sys +from time import sleep +import usb.core +import usb.util + +# Generic HID device VID, PID and report payload length (length is increased +# by one to account for the Report ID byte that must be pre-pended) +device_vid = 0x03EB +device_pid = 0x204F + +def get_and_init_hid_device(): + device = usb.core.find(idVendor=device_vid, idProduct=device_pid) + + if device is None: + sys.exit("Could not find USB device.") + + if device.is_kernel_driver_active(0): + try: + device.detach_kernel_driver(0) + except usb.core.USBError as exception: + sys.exit("Could not detatch kernel driver: %s" % str(exception)) + + try: + device.set_configuration() + except usb.core.USBError as exception: + sys.exit("Could not set configuration: %s" % str(exception)) + + return device + +def send_led_pattern(device, led1, led2, led3, led4): + # Report data for the demo is LED on/off data + report_data = [led1, led2, led3, led4] + + # Send the generated report to the device + number_of_bytes_written = device.ctrl_transfer( # Set Report control request + 0b00100001, # bmRequestType (constant for this control request) + 0x09, # bmRequest (constant for this control request) + 0, # wValue (MSB is report type, LSB is report number) + 0, # wIndex (interface number) + report_data # report data to be sent + ); + assert number_of_bytes_written == len(report_data) + + print("Sent LED Pattern: {0}".format(report_data)) + +def receive_led_pattern(hid_device): + endpoint = hid_device[0][(0,0)][0] + report_data = hid_device.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize) + return list(report_data) + +def main(): + hid_device = get_and_init_hid_device() + + print("Connected to device 0x%04X/0x%04X - %s [%s]" % + (hid_device.idVendor, hid_device.idProduct, + usb.util.get_string(hid_device, 256, hid_device.iProduct), + usb.util.get_string(hid_device, 256, hid_device.iManufacturer))) + + p = 0 + while (True): + # Convert the current pattern index to a bit-mask and send + send_led_pattern(hid_device, + (p >> 3) & 1, + (p >> 2) & 1, + (p >> 1) & 1, + (p >> 0) & 1) + + # Receive and print the current LED pattern + led_pattern = receive_led_pattern(hid_device)[0:4] + print("Received LED Pattern: {0}".format(led_pattern)) + + # Compute next LED pattern in sequence + p = (p + 1) % 16 + + # Delay a bit for visual effect + sleep(.2) + +if __name__ == '__main__': + main() |