Skip to content

USB Monitor

Heiko Sparenberg edited this page May 31, 2021 · 2 revisions

Motivation

Every USB device can be identified by a manufacturer ID and product ID. You can try to directly connect to a device using this information.

But during the development of different USB applications, I wanted a more convenient way to detect USB devices I am actually interested in. I also wanted a hot-plug feature so devices could be disconnected and/or reconnected.

Rather than copying (or rewriting) the code in every small project, I wrote usb_monitor.py.

It periodically (every second) checks which USB devices are currently connected. If a new USB device is found, usb_monitor.py calls a callback function which needs to be registered first. Also, if usb_monitor.py detects, that a device got disconnected, it calls another callback function.

Usage

In helix_usb.py's main function a usb_monitor is initialised. Here, the Line6's manufacturer ID (0e41) as well as the HX Stomp's product ID (4246) are used to initialise the internal "filter" (or: the devices we are looking for).

#!python

# only report Line6 Helix devices
usb_monitor = UsbMonitor(['0e41:4246'])

Next we register the two callback functions explained above:

#!python

usb_monitor.register_device_found_cb(helix_usb.usb_device_found_cb)
usb_monitor.register_device_lost_cb(helix_usb.usb_device_lost_cb)

Finally usb_monitor can be started:

#!python

usb_monitor.start()

usb_monitor.py will start his own thread and therefore not block the main execution of helix_usb.py. Whenever the USB configuration changes, the defined callback-functions will be executed.

Clone this wiki locally