-
Notifications
You must be signed in to change notification settings - Fork 0
Home
The OTGSerial library is designed to provide serial communication over USB OTG for ESP32 devices with OTG ports. This library allows you to send and receive data through the USB OTG port, enabling communication with a variety of USB devices.
Key Information:
- Tested on ESP32S3 with onboard OTG port.
- Written for Arduino without the need for IDF component installation.
- User should not use `OTGSerialClass` directly. Use the `OTGSerial` object instead.
- Only CH340 devices are supported.
- Only one OTG connection is possible (static object).
Initializes the OTG serial communication with the specified baud rate.
<code> static void begin(unsigned long baudrate); </code>
Returns whether a device is currently connected.
<code> static bool connected(); </code>
Returns whether the connected device is unsupported.
<code> static bool unsupported(); </code>
Sends a string with a newline character over OTG serial.
<code> static void println(char* data); </code>
Sends data over OTG serial. Can send a single character, a string, or an integer.
<code> static void print(char data); static void print(const char* data); static void print(int data); </code>
Sends formatted data over OTG serial.
<code> static void printf(const char* format, ...); </code>
Clears the OTG serial buffer.
<code> static void clearBuffer(); </code>
Returns the number of bytes available to read. Can also check for a specific delimiter.
<code> static uint16_t available(); static uint16_t available(const char delimiter); </code>
Reads a single byte from the OTG serial buffer.
<code> static int read(); </code>
Reads data from the OTG serial buffer until the specified delimiter is found.
<code> static char* readUntil(const char delimiter); </code>
Initializes the ring buffer with the specified size.
<code> Ringbuffer(uint16_t size); </code>
Destroys the ring buffer and frees allocated memory.
<code> ~Ringbuffer(); </code>
Adds a single character or a block of data to the ring buffer.
<code> void push(char value); void push(const char* data, uint16_t len); </code>
Removes and returns a single character from the ring buffer. Can also remove and return data up to a delimiter or a specific length.
<code> char pop(); char* pop(const char delimiter); void pop(char* dest, uint16_t len); </code>
Returns the number of bytes available in the ring buffer. Can also check for a specific delimiter.
<code> uint16_t available() const; uint16_t available(const char delimiter); </code>
Clears the ring buffer.
<code> void clear(); </code>
-
OTG_BUFFER_SIZE
- Description: Defines the size of the OTG inbound buffer.
- Default: 1024 bytes
-
OTG_OUT_BUFFER_SIZE
- Description: Defines the size of the OTG outbound buffer.
- Default: 256 bytes
-
OTG_SERIAL_SETTINGS
- Description: Defines the serial settings for OTG communication.
- Default: CS8 | NONE | STOP1
-
OTG_DAEMON_TASK_CORE
- Description: Defines the core for the OTG daemon task.
- Default: Core 0
-
OTG_DAEMON_TASK_PRIORITY
- Description: Defines the priority for the OTG daemon task.
- Default: Priority level 2
-
OTG_CLASS_TASK_CORE
- Description: Defines the core for the OTG class task.
- Default: Core 1
-
OTG_CLASS_TASK_PRIORITY
- Description: Defines the priority for the OTG class task.
- Default: Priority level 3
-
OTG_CLIENT_NUM_EVENT_MSG
- Description: Defines the number of event messages for the OTG client.
- Default: 5
-
OTG_HW_CONTROL
- Description: Optional hardware control definition.
- Default: Not defined
<code>
- include
- define TIMEOUT 100
void setup() {
Serial.begin(115200); OTGSerial.begin(9600);
}
void loop() {
while (Serial.available() > 0) { char c = Serial.read(); if (bufferIndex < sizeof(inputBuffer) - 1) { inputBuffer[bufferIndex++] = c; } lastReceiveTime = millis(); }
if (millis() - lastReceiveTime > TIMEOUT && bufferIndex > 0) { inputBuffer[bufferIndex] = '\0'; OTGSerial.print(inputBuffer); bufferIndex = 0; }
uint16_t available = OTGSerial.available('\n'); if (available > 0) { char* line = OTGSerial.readUntil('\n'); Serial.printf("(R) %d %s", available, line); free(line); } Serial.println(esp_get_free_heap_size());
delay(10);
}
</code>