USB API
Overviewβ
MatrixOS.USB exposes USB and CDC helpers.
Use available() before read() in app loops so the app can return when no CDC data is waiting.
MatrixOS.USB.connectedβ
connected() -> bool
Checks whether USB is connected.
Returns:
bool:Truewhen USB is connected.
MatrixOS.USB.CDC.connectedβ
connected() -> bool
Checks whether USB CDC is connected.
Returns:
bool:Truewhen CDC is connected.
MatrixOS.USB.CDC.availableβ
available() -> int
Returns the number of bytes waiting in the CDC receive buffer.
Returns:
int: Available byte count.
MatrixOS.USB.CDC.pollβ
poll() -> None
Polls the CDC interface.
MatrixOS.USB.CDC.printβ
print(text: str) -> None
Writes text to CDC without adding a newline.
Parameters:
text: Text to write.
MatrixOS.USB.CDC.printlnβ
println(text: str) -> None
Writes text to CDC with a newline.
Parameters:
text: Text to write.
MatrixOS.USB.CDC.flushβ
flush() -> None
Flushes pending CDC output.
MatrixOS.USB.CDC.readβ
read() -> int | None
Reads one byte from CDC.
Returns:
int | None: Byte value0..255, orNonewhen no data is available.
MatrixOS.USB.CDC.read_bytesβ
read_bytes(length: int) -> bytes
Reads up to length bytes from CDC.
Parameters:
length: Maximum number of bytes to read.
Returns:
bytes: Bytes read. Returnsb""when no data is available orlength <= 0.
MatrixOS.USB.CDC.read_stringβ
read_string() -> str
Reads the current CDC receive buffer as a string.
Returns:
str: String content, or an empty string when no data is available.
CDC Echo Exampleβ
import MatrixOS
if MatrixOS.USB.CDC.connected():
# Send a short banner once the CDC connection is active.
MatrixOS.USB.CDC.println("ready")
# Avoid blocking the app loop when no serial data is waiting.
if MatrixOS.USB.CDC.available():
value = MatrixOS.USB.CDC.read()
MatrixOS.USB.CDC.println("byte " + str(value))
Comments