MCUXpresso_MIMXRT1052xxxxB/boards/evkbimxrt1050/azure_rtos_examples/usbx_device_hid_generic/test_usbx_device_hid_generic.py
Yilin Sun 75f32185d2
Updated to v2.14.0
Signed-off-by: Yilin Sun <imi415@imi.moe>
2023-11-30 20:55:00 +08:00

55 lines
1.3 KiB
Python

#!/usr/bin/python
# This script is used to test the usbx_device_hid_generic example.
# This script will send a string to a device running the usbx_device_hid_generic example,
# and the device will send the string back.
import time
import usb.core
import usb.util
import sys
while True:
dev = usb.core.find(idVendor=0x1fc9, idProduct=0x00a2)
if dev:
break
time.sleep(1)
print("Found device")
cfg = dev.get_active_configuration()
intf = cfg[(0,0)]
if dev.is_kernel_driver_active(0):
try:
dev.detach_kernel_driver(0)
except usb.core.USBError as e:
sys.exit("Could not detatch kernel driver from interface({0}): {1}".format(i, str(e)))
endpoint_out = usb.util.find_descriptor(intf,
# match the first OUT endpoint
custom_match = \
lambda e: \
usb.util.endpoint_direction(e.bEndpointAddress) == \
usb.util.ENDPOINT_OUT)
endpoint_in = usb.util.find_descriptor(intf,
# match the first OUT endpoint
custom_match = \
lambda e: \
usb.util.endpoint_direction(e.bEndpointAddress) == \
usb.util.ENDPOINT_IN)
message = b'Message from HOST'
while True:
print("SEND:")
print(message.decode())
endpoint_out.write(message)
buffer = endpoint_in.read(100)
print("REV:")
print(buffer.tobytes().decode())
time.sleep(1)