|
| 1 | +# SPDX-FileCopyrightText: 2025 Liz Clark for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +""" |
| 5 | +CircuitPython USB VID/PID Reporter |
| 6 | +Feather RP2040 USB Host with OLED FeatherWing |
| 7 | +
|
| 8 | +""" |
| 9 | + |
| 10 | +import usb.core |
| 11 | +import keypad |
| 12 | +import board |
| 13 | +import displayio |
| 14 | +import terminalio |
| 15 | + |
| 16 | +from adafruit_display_text import label |
| 17 | +from i2cdisplaybus import I2CDisplayBus |
| 18 | + |
| 19 | +import adafruit_displayio_sh1107 |
| 20 | + |
| 21 | +displayio.release_displays() |
| 22 | + |
| 23 | +# init display |
| 24 | +i2c = board.I2C() |
| 25 | +display_bus = I2CDisplayBus(i2c, device_address=0x3C) |
| 26 | +display = adafruit_displayio_sh1107.SH1107(display_bus, width=128, height=64) |
| 27 | + |
| 28 | +# text groups |
| 29 | +main = displayio.Group() |
| 30 | +extra_info = displayio.Group() |
| 31 | +groups = [main, extra_info] |
| 32 | +display.root_group = main |
| 33 | + |
| 34 | +# text objects for title, VID and PID |
| 35 | +title_text = label.Label(terminalio.FONT, text="USB VID/PID Reporter", color=0xFFFFFF, x=5, y=15) |
| 36 | +main.append(title_text) |
| 37 | +vid_text = label.Label(terminalio.FONT, text="VID:", color=0xFFFFFF, x=5, y=35) |
| 38 | +main.append(vid_text) |
| 39 | +pid_text = label.Label(terminalio.FONT, text="PID:", color=0xFFFFFF, x=5, y=50) |
| 40 | +main.append(pid_text) |
| 41 | + |
| 42 | +# text objects for manufacturer and product info |
| 43 | +# added to secondary group |
| 44 | +man_header = label.Label(terminalio.FONT, text="Manufacturer:", color=0xFFFFFF, x=5, y=10) |
| 45 | +extra_info.append(man_header) |
| 46 | +man_text = label.Label(terminalio.FONT, text=" ", color=0xFFFFFF, x=5, y=25) |
| 47 | +extra_info.append(man_text) |
| 48 | +prod_header = label.Label(terminalio.FONT, text="Product:", color=0xFFFFFF, x=5, y=40) |
| 49 | +extra_info.append(prod_header) |
| 50 | +prod_text = label.Label(terminalio.FONT, text=" ", color=0xFFFFFF, x=5, y=55) |
| 51 | +extra_info.append(prod_text) |
| 52 | + |
| 53 | +# init A, B and C buttons on FeatherWing as keypad keys |
| 54 | +keys = keypad.Keys((board.D5,board.D6,board.D9,), value_when_pressed=False, pull=True) |
| 55 | + |
| 56 | +# variables |
| 57 | +last_pid = 0x0000 # store last PID |
| 58 | +last_vid = 0x0000 # store last VID |
| 59 | +usb_dev = False # is there a USB device? |
| 60 | +first_run = True # first run through loop |
| 61 | +group_index = 0 # display group index |
| 62 | +while True: |
| 63 | + event = keys.events.get() |
| 64 | + if event: |
| 65 | + # if any button is pressed, toggle graphics group |
| 66 | + if event.pressed: |
| 67 | + group_index = (group_index + 1) % 2 |
| 68 | + display.root_group = groups[group_index] |
| 69 | + # find connected devices |
| 70 | + devices = list(usb.core.find(find_all=True)) |
| 71 | + # if no device, reset states and text objects |
| 72 | + if not devices and usb_dev or first_run: |
| 73 | + print("no device") |
| 74 | + pid_text.text = "PID:" |
| 75 | + vid_text.text = "VID:" |
| 76 | + man_text.text = " " |
| 77 | + prod_text.text = " " |
| 78 | + usb_dev = False |
| 79 | + first_run = False |
| 80 | + else: |
| 81 | + if not usb_dev: |
| 82 | + first_run = False |
| 83 | + for device in devices: |
| 84 | + try: |
| 85 | + # if its the same device, don't scan again |
| 86 | + if (last_pid == hex(device.idProduct) and |
| 87 | + last_vid == hex(device.idVendor) and usb_dev): |
| 88 | + break |
| 89 | + # new device! |
| 90 | + usb_dev = True |
| 91 | + # get PID, VID, manufacturer and product |
| 92 | + # update text elements |
| 93 | + print("pid", hex(device.idProduct)) |
| 94 | + last_pid = hex(device.idProduct) |
| 95 | + pid_text.text = f"PID: {last_pid}" |
| 96 | + print("vid", hex(device.idVendor)) |
| 97 | + last_vid = hex(device.idVendor) |
| 98 | + vid_text.text = f"VID: {last_vid}" |
| 99 | + print("man", device.manufacturer) |
| 100 | + # only update manufacturer and product if info is known |
| 101 | + if device.manufacturer is not None: |
| 102 | + man_text.text = device.manufacturer |
| 103 | + print("product", device.product) |
| 104 | + if device.product is not None: |
| 105 | + prod_text.text = device.product |
| 106 | + print() |
| 107 | + # if there's any error reading info from a USB device, continue |
| 108 | + except usb.core.USBError: |
| 109 | + print("got an error, continuing..") |
| 110 | + continue |
0 commit comments