58 lines
1.7 KiB
Python
58 lines
1.7 KiB
Python
import logging
|
|
|
|
import usb.core
|
|
import usb.util
|
|
from usb.core import Endpoint
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
def init_printer() -> tuple[Endpoint, Endpoint]:
|
|
"""
|
|
! DEBUG ONLY !
|
|
Initializes printer and returns the input and output endpoints, in that order.
|
|
"""
|
|
dev = usb.core.find(idVendor=0x28e9, idProduct=0x0289)
|
|
|
|
if dev is None:
|
|
raise ValueError("Printer could not be found")
|
|
else:
|
|
logger.debug("Printer found!")
|
|
|
|
# Check for config
|
|
cfg = dev.get_active_configuration()
|
|
logger.debug(f"Found configuration: {cfg.bConfigurationValue}")
|
|
|
|
intf = cfg[(0, 0)] # Select first interface and first setting
|
|
|
|
logger.debug(f"Interface #: {intf.bInterfaceNumber}, Endpoints: {intf.bNumEndpoints}")
|
|
|
|
# Find Bulk OUT- und IN-Endpoints
|
|
ep_out = usb.util.find_descriptor(
|
|
intf,
|
|
custom_match=lambda endpoint: usb.util.endpoint_direction(endpoint.bEndpointAddress) == usb.util.ENDPOINT_OUT
|
|
)
|
|
|
|
ep_in = usb.util.find_descriptor(
|
|
intf,
|
|
custom_match=lambda endpoint: usb.util.endpoint_direction(endpoint.bEndpointAddress) == usb.util.ENDPOINT_IN
|
|
)
|
|
|
|
# Validate endpoints
|
|
if ep_out:
|
|
logger.debug(f"OUT-Endpoint found: {ep_out.bEndpointAddress}")
|
|
else:
|
|
raise ValueError("OUT-Endpoint not found")
|
|
|
|
if ep_in:
|
|
logger.debug(f"IN-Endpoint found: {ep_in.bEndpointAddress}")
|
|
else:
|
|
raise ValueError("IN-Endpoint not found")
|
|
|
|
if dev.is_kernel_driver_active(intf.bInterfaceNumber):
|
|
try:
|
|
dev.detach_kernel_driver(intf.bInterfaceNumber)
|
|
except usb.core.USBError as e:
|
|
raise ValueError(f"Could not detach kernel driver from interface({intf.bInterfaceNumber}): {e}")
|
|
|
|
return ep_in, ep_out
|