跳到主要内容
版本:3.0 Beta 🧪

USB API

概述

USB API 提供与 USB 设备交互的函数。该 API 包括检查 USB 设备是否连接、通过 USB CDC 发送和接收数据等函数。

该 API 的头文件是 os/MatrixOS.h 的一部分,实现位于 os/system/USB/

命名空间:USB

USB 命名空间包含通用的 USB 函数。


MatrixOS::USB::Connected

bool USB::Connected(void);

检查 USB 设备是否已连接。

返回值:

  • bool:如果 USB 设备已连接则返回 true,否则返回 false

CDC

The USB::CDC namespace provides functions for communication over USB as a virtual serial port (CDC). In the future this will likely migrate into a MatrixOS::Serial namespace with cross port support between USB, UART, BLE, etc.


MatrixOS::USB::CDC::Connected

bool USB::CDC::Connected(void);

Checks if the USB CDC is connected.

Returns:

  • bool: true if the USB CDC is connected; false otherwise.

MatrixOS::USB::CDC::Available

uint32_t USB::CDC::Available(void);

Returns the number of bytes available for reading.

Returns:

  • uint32_t: Number of bytes available.

MatrixOS::USB::CDC::Poll

void USB::CDC::Poll(void);

Processes incoming and outgoing USB CDC data. This function should be called periodically.


MatrixOS::USB::CDC::Print

void USB::CDC::Print(string str);

Sends a string over USB CDC without a newline.

Parameters:

  • str (string): The string to send.

MatrixOS::USB::CDC::Println

void USB::CDC::Println(string str);

Sends a string over USB CDC with a newline appended.

Parameters:

  • str (string): The string to send.

MatrixOS::USB::CDC::Printf

void USB::CDC::Printf(string format, ...);

Sends a formatted string over USB CDC.

Parameters:

  • format (string): The format string.
  • ...: Additional arguments for formatting.

MatrixOS::USB::CDC::VPrintf

void USB::CDC::VPrintf(string format, va_list valst);

Sends a formatted string over USB CDC using a va_list.

Parameters:

  • format (string): The format string.
  • valst (va_list): A list of arguments for formatting.

MatrixOS::USB::CDC::Flush

void USB::CDC::Flush(void);

Flushes the USB CDC output buffer.


MatrixOS::USB::CDC::Read

int8_t USB::CDC::Read(void);

Reads a single byte from the USB CDC input buffer.

Returns:

  • int8_t: The byte read, or a negative value if no data is available.

MatrixOS::USB::CDC::ReadBytes

uint32_t USB::CDC::ReadBytes(void* buffer, uint32_t length);

Reads multiple bytes from the USB CDC input buffer into the provided buffer.

Parameters:

  • buffer (void*): The buffer to store the read data.
  • length (uint32_t): The maximum number of bytes to read.

Returns:

  • uint32_t: The number of bytes actually read.

MatrixOS::USB::CDC::ReadString

string USB::CDC::ReadString(void);

Reads a string from the USB CDC input buffer.

Returns:

  • string: The string read from the buffer.

Comments