circuitpython/tests/circuitpython-manual/usb/basic_keyboard.py
Scott Shawcroft 83593a1558
Start of USB host API
This allows you to list and explore connected USB devices. It
only stubs out the methods to communicate to endpoints. That will
come in a follow up once TinyUSB has it. (It's in progress.)

Related to #5986
2022-03-07 18:07:25 -08:00

35 lines
840 B
Python

import array
import usb.core
import sys
# This is a WASD Code Keyboard with a generic controller in it.
USB_VID = 0x04D9
USB_PID = 0x0169
# This is ordered by bit position.
MODIFIERS = []
device = usb.core.find(idVendor=USB_VID, idProduct=USB_PID)
print(device.manufacturer, device.product)
# Test to see if the kernel is using the device and detach it.
if device.is_kernel_driver_active(0):
device.detach_kernel_driver(0)
# Boot keyboards have 8 byte reports
buf = array.array("B", [0] * 8)
report_count = 0
while True:
try:
count = device.read(0x81, buf)
except usb.core.USBTimeoutError:
continue
if report_count % 15 == 0:
print("modifiers keys")
print(buf[0], end=" ")
for i in range(2, 8):
if buf[i] > 0:
print(buf[i], end=" ")
print()
report_count += 1