From 43ed3539fae8f3b1d84d0528e5d0ac6ff42cadba Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 15 Jul 2026 16:11:40 +1000 Subject: [PATCH 1/2] usb-device-cdc: Optimise CDCInterface constructor. Don't set instance attributes that will just be reset by `self.init()`. Saves 30 bytes of bytecode. Signed-off-by: Damien George --- micropython/usb/usb-device-cdc/manifest.py | 2 +- micropython/usb/usb-device-cdc/usb/device/cdc.py | 8 ++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/micropython/usb/usb-device-cdc/manifest.py b/micropython/usb/usb-device-cdc/manifest.py index 3807dbee5..94e1f7847 100644 --- a/micropython/usb/usb-device-cdc/manifest.py +++ b/micropython/usb/usb-device-cdc/manifest.py @@ -1,3 +1,3 @@ -metadata(version="0.1.3") +metadata(version="0.1.4") require("usb-device") package("usb") diff --git a/micropython/usb/usb-device-cdc/usb/device/cdc.py b/micropython/usb/usb-device-cdc/usb/device/cdc.py index a51941bc0..ca59b811c 100644 --- a/micropython/usb/usb-device-cdc/usb/device/cdc.py +++ b/micropython/usb/usb-device-cdc/usb/device/cdc.py @@ -107,18 +107,14 @@ def __init__(self, **kwargs): self.line_coding_cb = None self._line_state = 0 # DTR & RTS - # Set a default line coding of 115200/8N1 - self._line_coding = bytearray(b"\x00\xc2\x01\x00\x00\x00\x08") - - self._wb = () # Optional write Buffer (IN endpoint), set by CDC.init() - self._rb = () # Optional read Buffer (OUT endpoint), set by CDC.init() - self._timeout = 1000 # set from CDC.init() as well + self._line_coding = bytearray(7) # Will be populated by .init() # one control interface endpoint, two data interface endpoints self.ep_c_in = self.ep_d_in = self.ep_d_out = None self._c_itf = None # Number of control interface, data interface is one more + # The _timeout, _wb and _rb attributes will be set by this call to .init(). self.init(**kwargs) def init( From 149809877350e799a83a5cedb634847301752f76 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 15 Jul 2026 16:13:53 +1000 Subject: [PATCH 2/2] usb/examples: Put timeout=0 in CDCInterface constructor. Makes the example a little bit simpler. Signed-off-by: Damien George --- micropython/usb/examples/device/cdc_repl_example.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/micropython/usb/examples/device/cdc_repl_example.py b/micropython/usb/examples/device/cdc_repl_example.py index 06dc9a76c..d27a89766 100644 --- a/micropython/usb/examples/device/cdc_repl_example.py +++ b/micropython/usb/examples/device/cdc_repl_example.py @@ -25,8 +25,8 @@ import usb.device from usb.device.cdc import CDCInterface -cdc = CDCInterface() -cdc.init(timeout=0) # zero timeout makes this non-blocking, suitable for os.dupterm() +# Zero timeout makes this non-blocking, suitable for os.dupterm(). +cdc = CDCInterface(timeout=0) # pass builtin_driver=True so that we get the built-in USB-CDC alongside, # if it's available.