# # Klipper USB/IP integration module. # Author: Roman Rikhter # # Uses ~/usbip.sh script where you may specify the script to connect # the MCU. # # 1) Put this script into ~/klipper/klippy/extras/usbip.py on host # 2) Define [usbip] section in your printer.cfg # import os import time class UsbIpSupport: def __init__(self, config): self.printer = config.get_printer() self.gcode = self.printer.lookup_object("gcode") self.printer.register_event_handler( "klippy:disconnect", self._handle_disconnect) self.serial = config.getsection("mcu").get("serial") self.reconnecting = False if not self._exists(): self._handle_connect() def _exists(self): return os.access(self.serial, os.R_OK | os.W_OK) def _connect(self): if self._exists(): return self.reconnecting = True self._log("USB/IP: Connecting...") os.system(os.environ.get("HOME") + "/usbip.sh") self._log("USB/IP: Connected") self.reconnecting = False def _handle_disconnect(self): if self.reconnecting: return self._log("USB/IP: Reconnection due to a disconnect event") time.sleep(0.25) self._connect() def _handle_connect(self): if self.reconnecting: return self._log("USB/IP: Initial connection") self._connect() def _log(self, msg): self.gcode.respond_info(msg) def load_config(config): return UsbIpSupport(config)