Skip to content
EmRoy edited this page Nov 6, 2024 · 1 revision

Table of Contents

OTGSerial Library Documentation

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).

Class: OTGSerialClass

begin

Initializes the OTG serial communication with the specified baud rate.

 <code>
 static void begin(unsigned long baudrate);
 </code>

connected

Returns whether a device is currently connected.

 <code>
 static bool connected();
 </code>

unsupported

Returns whether the connected device is unsupported.

 <code>
 static bool unsupported();
 </code>

println

Sends a string with a newline character over OTG serial.

 <code>
 static void println(char* data);
 </code>

print

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>

printf

Sends formatted data over OTG serial.

 <code>
 static void printf(const char* format, ...);
 </code>

clearBuffer

Clears the OTG serial buffer.

 <code>
 static void clearBuffer();
 </code>

available

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>

read

Reads a single byte from the OTG serial buffer.

 <code>
 static int read();
 </code>

readUntil

Reads data from the OTG serial buffer until the specified delimiter is found.

 <code>
 static char* readUntil(const char delimiter);
 </code>

Struct: Ringbuffer

Constructor

Initializes the ring buffer with the specified size.

 <code>
 Ringbuffer(uint16_t size);
 </code>

Destructor

Destroys the ring buffer and frees allocated memory.

 <code>
 ~Ringbuffer();
 </code>

push

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>

pop

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>

available

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>

clear

Clears the ring buffer.

 <code>
 void clear();
 </code>

Precompiler Definitions

  • 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

Examples

Basic Usage Example

 <code>
  1. include
  2. define TIMEOUT 100
char inputBuffer[256]; unsigned long lastReceiveTime = 0; int bufferIndex = 0;

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>