### SSD1306Wire Initialization with connect() Example Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/SSD1306Wire.md Example demonstrating the use of the init() method, which internally calls connect(), to initialize and display text on an SSD1306Wire display. ```cpp SSD1306Wire display(0x3c, 21, 22); if (display.init()) { // Calls connect() internally display.drawString(0, 0, "Connected"); display.display(); } ``` -------------------------------- ### Complete OLED Display UI Example Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/OLEDDisplayUi.md This example demonstrates how to initialize an SSD1306 OLED display and use the OLEDDisplayUi library to manage multiple frames, transitions, and overlays. It includes setup for frames, animations, indicators, and automatic transitions. ```cpp #include #include "SSD1306.h" #include "OLEDDisplayUi.h" SSD1306Wire display(0x3c, 21, 22); OLEDDisplayUi ui(&display); // Frame 1: Clock void drawFrame1(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) { display->setFont(ArialMT_Plain_24); display->setTextAlignment(TEXT_ALIGN_CENTER); display->drawString(64 + x, 20 + y, "12:34"); } // Frame 2: Message void drawFrame2(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) { display->setFont(ArialMT_Plain_16); display->setTextAlignment(TEXT_ALIGN_CENTER); display->drawString(64 + x, 25 + y, "Hello World!"); } // Clock overlay void drawOverlay(OLEDDisplay *display, OLEDDisplayUiState* state) { display->setFont(ArialMT_Plain_10); display->setTextAlignment(TEXT_ALIGN_CENTER); display->drawString(64, 0, "Frame " + String(state->currentFrame + 1)); } void setup() { Serial.begin(115200); delay(200); display.init(); display.flipScreenVertically(); ui.setTargetFPS(30); ui.setTimePerFrame(5000); ui.setTimePerTransition(500); // Configure frames FrameCallback frames[] = { drawFrame1, drawFrame2 }; ui.setFrames(frames, 2); // Configure overlay OverlayCallback overlays[] = { drawOverlay }; ui.setOverlays(overlays, 1); // Configure animation ui.setFrameAnimation(SLIDE_LEFT); // Configure indicator ui.setIndicatorPosition(BOTTOM); ui.enableAutoTransition(); ui.init(); Serial.println("UI initialized"); } void loop() { // Must call update() continuously int8_t remainingTime = ui.update(); // Optional: handle button input for manual frame control // if (buttonPressed) ui.nextFrame(); if (remainingTime > 0) { delay(remainingTime); } } ``` -------------------------------- ### Full ESP32 OLED Display Initialization and Usage Example Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/OLEDDisplay.md A comprehensive example demonstrating the initialization of an SSD1306 OLED display via I2C, configuration of display settings, drawing text and shapes, and updating the display. This snippet includes setup for contrast, font, text alignment, and basic drawing operations. ```cpp #include #include "SSD1306.h" // I2C address 0x3c, SDA=21, SCL=22 SSD1306 display(0x3c, 21, 22); void setup() { // Initialize display and allocate buffers if (!display.init()) { while (1); // Halt on error } // Configure display display.flipScreenVertically(); display.setContrast(200); // Select font and alignment display.setFont(ArialMT_Plain_10); display.setTextAlignment(TEXT_ALIGN_CENTER); // Draw initial content display.clear(); display.drawString(64, 0, "ESP32 OLED Display"); // Draw shapes display.setColor(WHITE); display.drawRect(10, 20, 100, 30); display.fillCircle(64, 50, 8); // Update physical display display.display(); } void loop() { // Update content as needed display.clear(); display.setColor(WHITE); display.drawString(64, 0, millis() / 1000); display.display(); delay(1000); } ``` -------------------------------- ### Complete SSD1306Brzo Example Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/SSD1306Brzo.md This example demonstrates how to initialize and use the SSD1306Brzo library for an OLED display. It includes setting up the display, updating text, and drawing a progress bar with fast updates. ```cpp #include #include "SSD1306Brzo.h" // BRZO I2C: address 0x3C, SDA=GPIO5, SCL=GPIO4 (ESP8266) SSD1306Brzo display(0x3c, 5, 4); void setup() { Serial.begin(115200); delay(200); if (!display.init()) { Serial.println("SSD1306 init failed"); return; } display.flipScreenVertically(); display.setContrast(255); display.setFont(ArialMT_Plain_16); display.setTextAlignment(TEXT_ALIGN_CENTER); // Initial display display.clear(); display.drawString(64, 20, "BRZO I2C"); display.drawString(64, 40, "1 MHz Fast!"); display.display(); delay(2000); } void loop() { // Update display with minimal latency static unsigned long lastUpdate = 0; unsigned long now = millis(); if (now - lastUpdate >= 100) { // Update every 100ms lastUpdate = now; display.clear(); display.setColor(WHITE); display.setTextAlignment(TEXT_ALIGN_CENTER); display.drawString(64, 0, "Frame Time"); display.drawString(64, 20, String(millis() / 1000) + " sec"); // Draw progress bar uint8_t progress = (millis() / 100) % 101; display.drawProgressBar(10, 40, 108, 10, progress); // Fast update via BRZO display.display(); } } ``` -------------------------------- ### Install via PlatformIO Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/README.md Use this command to install the library using PlatformIO. ```bash platformio lib install 562 ``` -------------------------------- ### Minimal UI Example Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/README.md Demonstrates the OLEDDisplayUi framework by setting up two frames with a slide animation and auto-transition. This example requires the Wire library and SSD1306Wire display. ```cpp #include #include "SSD1306Wire.h" #include "OLEDDisplayUi.h" SSD1306Wire display(0x3c, 21, 22); OLEDDisplayUi ui(&display); void drawFrame1(OLEDDisplay *d, OLEDDisplayUiState* s, int16_t x, int16_t y) { d->drawString(64 + x, 32 + y, "Frame 1"); } void drawFrame2(OLEDDisplay *d, OLEDDisplayUiState* s, int16_t x, int16_t y) { d->drawString(64 + x, 32 + y, "Frame 2"); } void setup() { display.init(); ui.setTargetFPS(30); ui.setTimePerFrame(5000); ui.setFrameAnimation(SLIDE_LEFT); FrameCallback frames[] = { drawFrame1, drawFrame2 }; ui.setFrames(frames, 2); ui.enableAutoTransition(); ui.init(); } void loop() { int8_t remainingTime = ui.update(); if (remainingTime > 0) { delay(remainingTime); } } ``` -------------------------------- ### SSD1306Wire Initialization Example Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/SSD1306Wire.md Example of initializing an SSD1306Wire display with a specific I2C address and pins, then displaying text. Ensure Wire.h and SSD1306.h are included. ```cpp #include #include "SSD1306.h" // Includes SSD1306Wire.h // I2C address 0x3C, SDA on GPIO 21, SCL on GPIO 22 SSD1306Wire display(0x3c, 21, 22); void setup() { if (!display.init()) { Serial.println("Display init failed"); return; } display.drawString(0, 0, "Hello I2C"); display.display(); } ``` -------------------------------- ### Complete SSD1306Wire Example Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/SSD1306Wire.md This example demonstrates initializing an SSD1306 display over I2C, setting contrast and font, displaying text, and updating the screen. It shows both full screen updates and optimized updates using double buffering. ```cpp #include #include "SSD1306.h" // I2C: address 0x3C, SDA=GPIO21, SCL=GPIO22 SSD1306Wire display(0x3c, 21, 22); void setup() { Serial.begin(115200); delay(200); if (!display.init()) { Serial.println("SSD1306 init failed"); return; } display.flipScreenVertically(); display.setContrast(255); display.setFont(ArialMT_Plain_16); display.setTextAlignment(TEXT_ALIGN_CENTER); // First screen display.clear(); display.drawString(64, 25, "I2C Display"); display.display(); // Transmit to hardware via I2C delay(2000); // Second screen—only changed region is sent display.clear(); display.drawString(64, 25, "Optimized!"); display.display(); // Faster update due to double buffering } void loop() { // Update display in real-time display.clear(); display.setColor(WHITE); display.drawString(64, 0, "Time:"); display.drawString(64, 20, String(millis() / 1000) + " sec"); // Draw a frame display.drawRect(10, 35, 100, 20); display.display(); // Send changes to display delay(500); } ``` -------------------------------- ### BRZO Transaction Example Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/SSD1306Brzo.md Demonstrates how to send commands and data to the SSD1306 display using the brzo_i2c library for high-speed communication. Ensure the brzo_i2c library is installed and configured. ```cpp // Command transmission via BRZO uint8_t command[2] = {0x80, 0x81}; // Command mode + contrast command brzo_i2c_start_transaction(0x3C, BRZO_I2C_SPEED); brzo_i2c_write(command, 2, true); brzo_i2c_end_transaction(); // Data transmission via BRZO uint8_t data[17]; data[0] = 0x40; // Data mode for (int i = 1; i <= 16; i++) { data[i] = buffer[i-1]; } brzo_i2c_start_transaction(0x3C, BRZO_I2C_SPEED); brzo_i2c_write(data, 17, true); brzo_i2c_end_transaction(); ``` -------------------------------- ### SH1106Wire Initialization Example Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/SH1106Wire.md Demonstrates how to initialize the SH1106 display using the connect() method, which is called internally by init(). ```cpp SH1106Wire display(0x3c, 21, 22); if (display.init()) { // Calls connect() internally Serial.println("SH1106 connected via I2C"); } ``` -------------------------------- ### Complete ESP32 OLED Display and UI Configuration Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/configuration.md This example demonstrates the full setup for an SSD1306 OLED display using I2C, including display initialization, contrast, font, alignment, and UI configuration such as frame transitions and indicators. ```cpp #include #include "SSD1306Wire.h" #include "OLEDDisplayUi.h" // Display configuration SSD1306Wire display(0x3c, 21, 22); // I2C address 0x3C, SDA=21, SCL=22 // UI configuration OLEDDisplayUi ui(&display); void setup() { // Initialize display if (!display.init()) { Serial.println("Init failed"); return; } // Display configuration display.flipScreenVertically(); display.setContrast(200); display.setFont(ArialMT_Plain_10); display.setTextAlignment(TEXT_ALIGN_CENTER); display.setColor(WHITE); // UI configuration ui.setTargetFPS(30); ui.setTimePerFrame(5000); // 5 seconds per frame ui.setTimePerTransition(500); // 500ms animation ui.setFrameAnimation(SLIDE_LEFT); ui.setIndicatorPosition(BOTTOM); ui.setIndicatorDirection(LEFT_RIGHT); // Frame and overlay setup FrameCallback frames[] = { drawFrame1, drawFrame2 }; ui.setFrames(frames, 2); OverlayCallback overlays[] = { drawOverlay }; ui.setOverlays(overlays, 1); // Enable auto-transition ui.enableAutoTransition(); ui.setAutoTransitionForwards(); ui.init(); } void loop() { int8_t remainingTime = ui.update(); if (remainingTime > 0) { delay(remainingTime); } } ``` -------------------------------- ### Complete SSD1306 SPI OLED Display Example Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/SSD1306Spi.md This example shows how to initialize the SSD1306 SPI display, set contrast and font, and continuously update the display with text and graphics in the loop function. It's suitable for applications requiring high frame rates. ```cpp #include #include "SSD1306Spi.h" // SPI pins: RST=D0, DC=D2, CS=D8 // CLK=D5 (SPI, fixed), MOSI=D7 (SPI, fixed) SSD1306Spi display(D0, D2, D8); void setup() { Serial.begin(115200); delay(200); if (!display.init()) { Serial.println("SSD1306 init failed"); return; } display.flipScreenVertically(); display.setContrast(255); display.setFont(ArialMT_Plain_16); display.setTextAlignment(TEXT_ALIGN_CENTER); // Initial display display.clear(); display.drawString(64, 20, "SPI Ready"); display.drawHorizontalLine(0, 55, 128); display.display(); delay(2000); } void loop() { static int count = 0; // Update display at high frame rate (SPI enables this) display.clear(); display.setColor(WHITE); // Title display.setTextAlignment(TEXT_ALIGN_CENTER); display.drawString(64, 0, "SPI OLED Demo"); // Content display.setTextAlignment(TEXT_ALIGN_LEFT); display.drawString(10, 20, "Frame: " + String(count++)); display.drawString(10, 35, "Speed: ~1ms per update"); // Graphics display.drawRect(10, 50, 100, 12); display.fillRect(10, 50, count % 100, 12); // Update hardware (very fast with SPI) display.display(); // Can achieve ~60 FPS with SPI delay(16); // ~60 Hz frame rate } ``` -------------------------------- ### Minimal I2C Example Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/README.md Initializes an SSD1306 display over I2C and draws "Hello World" to the center. Ensure the display address and pins are correctly set. ```cpp #include #include "SSD1306Wire.h" SSD1306Wire display(0x3c, 21, 22); // Address, SDA, SCL void setup() { display.init(); display.setFont(ArialMT_Plain_10); display.setTextAlignment(TEXT_ALIGN_CENTER); display.clear(); display.drawString(64, 25, "Hello World"); display.display(); } void loop() { // Update display as needed } ``` -------------------------------- ### Usage Example for LoadingStage Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/types.md Demonstrates how to define and use LoadingStage structures to manage a multi-stage loading process with callback functions. ```cpp void initWifi() { WiFi.begin("SSID", "password"); } void initSensor() { sensor.init(); } LoadingStage stages[] = { { "WiFi", initWifi }, { "Sensor", initSensor } }; ui.runLoadingProcess(stages, 2); ``` -------------------------------- ### SSD1306Brzo connect() Example Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/SSD1306Brzo.md Initializes the BRZO I2C bus with configured SDA and SCL pins. This method is called internally by init(). ```cpp SSD1306Brzo display(0x3c, 5, 4); if (display.init()) { // Calls connect() internally Serial.println("BRZO connected"); } ``` -------------------------------- ### Example Usage of OverlayCallback Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/types.md Shows an example of an OverlayCallback function used to display a clock on the screen. Note that overlays do not use x, y offsets. ```cpp void clockOverlay(OLEDDisplay *display, OLEDDisplayUiState* state) { display->setFont(ArialMT_Plain_10); display->setTextAlignment(TEXT_ALIGN_CENTER); display->drawString(64, 0, "12:34"); // No x, y offsets } OverlayCallback overlays[] = { clockOverlay }; ui.setOverlays(overlays, 1); ``` -------------------------------- ### Minimal SPI Example Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/README.md Initializes an SSD1306 display over SPI and draws "SPI Display". Ensure the correct pins for RST, DC, and CS are connected. ```cpp #include #include "SSD1306Spi.h" SSD1306Spi display(D0, D2, D8); // RST, DC, CS void setup() { display.init(); display.drawString(0, 0, "SPI Display"); display.display(); } void loop() {} ``` -------------------------------- ### Example Usage of LoadingDrawFunction Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/types.md Provides an example of a LoadingDrawFunction that draws the current stage's process text, a progress bar, and the percentage completion. ```cpp void drawLoadingScreen(OLEDDisplay *display, LoadingStage* stage, uint8_t progress) { display->setFont(ArialMT_Plain_10); display->setTextAlignment(TEXT_ALIGN_CENTER); display->drawString(64, 20, stage->process); display->drawProgressBar(10, 40, 108, 10, progress); display->drawString(64, 55, String(progress) + "% கடு); } ui.setLoadingDrawFunction(drawLoadingScreen); ``` -------------------------------- ### OLEDDisplayUiState Usage Example Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/types.md Demonstrates how to access and utilize the OLEDDisplayUiState within a frame drawing function. Shows how to get the current frame index, check animation state, and store custom user data. ```cpp void drawFrame(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) { // Access current frame index display->drawString(0, 0, "Frame " + String(state->currentFrame)); // Check if animating if (state->frameState == IN_TRANSITION) { // Use x, y offsets for animation } // Store application state if (state->userData == NULL) { state->userData = (void*)42; // Custom value } } ``` -------------------------------- ### Internal Example of sendCommand Usage Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/SH1106Wire.md This example shows how the `sendCommand` method is used internally within the library, typically during the initialization sequence to set display parameters like contrast. ```cpp // Called internally by the initialization sequence // sendCommand(0x81); // Contrast command // sendCommand(128); // Contrast value ``` -------------------------------- ### Internal Send Command Example Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/SSD1306Wire.md This example demonstrates the internal use of the sendCommand() method, which sends a single command byte to the display controller. It is typically called by initialization sequences and not directly by end-users. ```cpp // This is called internally by the initialization sequence // sendCommand(0x81); // Contrast command // sendCommand(128); // Contrast value ``` -------------------------------- ### SSD1306Brzo Constructor Example Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/SSD1306Brzo.md Initializes a display object for high-speed BRZO I2C communication. Specify the I2C address, SDA, and SCL pins. ```cpp #include #include "SSD1306Brzo.h" // I2C address 0x3C, SDA on GPIO 5, SCL on GPIO 4 SSD1306Brzo display(0x3c, 5, 4); void setup() { if (!display.init()) { Serial.println("Display init failed"); return; } display.drawString(0, 0, "BRZO I2C"); display.display(); } ``` -------------------------------- ### SSD1306Spi Constructor Example Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/SSD1306Spi.md Initializes a display object for SPI communication. Ensure correct GPIO pin assignments for RST, DC, and CS. The display initialization must be checked for success. ```cpp #include #include "SSD1306Spi.h" // RST=D0(GPIO16), DC=D2(GPIO4), CS=D8(GPIO15) SSD1306Spi display(D0, D2, D8); void setup() { if (!display.init()) { Serial.println("Display init failed"); return; } display.drawString(0, 0, "SPI Display"); display.display(); } ``` -------------------------------- ### SH1106Wire and SSD1306Wire API Compatibility Example Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/SH1106Wire.md Demonstrates that the same code can be used for both SH1106 and SSD1306 displays by simply changing the include file and constructor. This highlights the library's goal of providing a unified API. ```cpp // The same code works for both: // #include "SSD1306Wire.h" // Use SSD1306 #include "SH1106Wire.h" // Use SH1106 instead SH1106Wire display(0x3c, 21, 22); display.init(); display.setFont(ArialMT_Plain_16); display.drawString(0, 0, "Works the same"); display.display(); ``` -------------------------------- ### Install brzo_i2c Library using PlatformIO Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/SSD1306Brzo.md Add the 'brzo_i2c' library to your project dependencies in PlatformIO by including it in your 'lib_deps' configuration. ```ini lib_deps = pasko-zh/brzo_i2c@^1.3.9 ``` -------------------------------- ### Complete SH1106Wire I2C Display Example Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/SH1106Wire.md A full Arduino sketch demonstrating how to initialize and use the SH1106Wire library for an I2C OLED display. It covers setting up the display, drawing text, and updating content dynamically in the loop. ```cpp #include #include "SH1106Wire.h" // I2C: address 0x3C, SDA=GPIO21, SCL=GPIO22 (ESP32) SH1106Wire display(0x3c, 21, 22); void setup() { Serial.begin(115200); delay(200); if (!display.init()) { Serial.println("SH1106 init failed"); return; } display.flipScreenVertically(); display.setContrast(200); display.setFont(ArialMT_Plain_16); display.setTextAlignment(TEXT_ALIGN_CENTER); // First screen display.clear(); display.drawString(64, 10, "SH1106"); display.drawString(64, 35, "I2C Display"); display.display(); delay(2000); // Second screen display.clear(); display.drawString(64, 25, "Ready!"); display.display(); } void loop() { // Update display with real-time data display.clear(); display.setColor(WHITE); // Title display.setTextAlignment(TEXT_ALIGN_CENTER); display.drawString(64, 0, "SH1106 Demo"); // Content display.setTextAlignment(TEXT_ALIGN_LEFT); display.drawString(10, 20, "Time: " + String(millis() / 1000) + "s"); display.drawString(10, 35, "Uptime: " + String(millis() / 1000) + "sec"); // Graphics display.drawRect(10, 50, 100, 12); uint8_t progress = (millis() / 100) % 101; display.fillRect(10, 50, progress, 12); display.display(); delay(500); } ``` -------------------------------- ### SPI Command Sequence Example Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/SSD1306Spi.md Illustrates the sequence of SPI signals (CS, DC) and data transfers for sending a command to set the column address on the SSD1306 display. ```text CS=HIGH (idle) DC=LOW (command mode) CS=LOW (select) SPI.transfer(0x21); // Column address command SPI.transfer(0x00); // Start column SPI.transfer(0x7F); // End column CS=HIGH (deselect) ``` -------------------------------- ### Example Usage of FrameCallback Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/types.md Illustrates how to implement and assign a FrameCallback function to draw content on the display, including setting font, text alignment, and drawing strings. ```cpp void myFrame(OLEDDisplay *display, OLEDDisplayUiState* state, int16_t x, int16_t y) { display->setFont(ArialMT_Plain_16); display->setTextAlignment(TEXT_ALIGN_CENTER); display->drawString(64 + x, 32 + y, "Hello"); } FrameCallback frames[] = { myFrame }; ui.setFrames(frames, 1); ``` -------------------------------- ### SH1106Wire Display Update Example Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/SH1106Wire.md Demonstrates clearing the display, drawing text, and updating the SH1106 hardware via I2C. Subsequent updates with double buffering enabled send only changed regions, reducing I2C traffic. ```cpp display.clear(); display.setColor(WHITE); display.drawString(0, 0, "Update now"); display.display(); // Sends to hardware via I2C // Next update—only changed area sent display.drawString(0, 10, "Optimized"); display.display(); // Minimal I2C traffic ``` -------------------------------- ### OLEDDisplay Text Alignment Usage Example Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/types.md Shows how to set text alignment for positioning text relative to a given coordinate. ```cpp // Left-aligned (default) display.setTextAlignment(TEXT_ALIGN_LEFT); display.drawString(0, 0, "Left"); // Right-aligned (x is the right edge) display.setTextAlignment(TEXT_ALIGN_RIGHT); display.drawString(127, 0, "Right"); // Centered horizontally at x=64 display.setTextAlignment(TEXT_ALIGN_CENTER); display.drawString(64, 0, "Center"); // Centered at point (64, 32) display.setTextAlignment(TEXT_ALIGN_CENTER_BOTH); display.drawString(64, 32, "Centered Box"); ``` -------------------------------- ### OLEDDisplay Color Usage Example Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/types.md Demonstrates setting pixel color modes for drawing operations like text and shapes. ```cpp display.setColor(WHITE); display.drawString(0, 0, "White text"); display.setColor(BLACK); display.fillRect(0, 20, 50, 20); // Clear rectangle display.setColor(INVERSE); display.drawLine(0, 0, 127, 63); // Toggle pixels along diagonal ``` -------------------------------- ### Initialize SSD1306 with Wire.h Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/README.md Initializes an SSD1306 display using the standard Wire.h library for I2C communication. Ensure ADDRESS, SDA, and SDC are defined appropriately for your hardware setup. ```C++ #include #include "SSD1306.h" SSD1306 display(ADDRESS, SDA, SDC); ``` -------------------------------- ### Initialize SH1106 with Wire.h Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/README.md Initializes an SH1106 display using the standard Wire.h library for I2C communication. Ensure ADDRESS, SDA, and SDC are defined appropriately for your hardware setup. ```C++ #include #include "SH1106.h" SH1106 display(ADDRESS, SDA, SDC); ``` -------------------------------- ### Initialize SH1106 with SPI Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/README.md Initializes an SH1106 display using the SPI interface. Ensure RES, DC, and CS pins are correctly defined for your hardware setup. ```C++ #include #include "SH1106Spi.h" SH1106Spi display(RES, DC, CS); ``` -------------------------------- ### Initialize SSD1306 with brzo_i2c Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/README.md Initializes an SSD1306 display using the faster brzo_i2c library for I2C communication. Ensure ADDRESS, SDA, and SDC are defined appropriately for your hardware setup. ```C++ #include #include "SSD1306Brzo.h" SSD1306Brzo display(ADDRESS, SDA, SDC); ``` -------------------------------- ### Initialize SH1106 with brzo_i2c Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/README.md Initializes an SH1106 display using the faster brzo_i2c library for I2C communication. Ensure ADDRESS, SDA, and SDC are defined appropriately for your hardware setup. ```C++ #include #include "SH1106Brzo.h" SH1106Brzo display(ADDRESS, SDA, SDC); ``` -------------------------------- ### Get String Width for Centering Text Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/OLEDDisplay.md Use this method to get the pixel width of a String. This is useful for calculating positioning, such as centering text on the display. ```cpp String msg = "Test"; uint16_t w = display.getStringWidth(msg); int x = 64 - w / 2; // Center on screen display.drawString(x, 0, msg); ``` -------------------------------- ### connect() Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/SSD1306Wire.md Initializes the I2C bus and configures Wire for display communication. ```APIDOC ## connect() ### Description Initializes the I2C bus and configures Wire for display communication. ### Method bool ### Details - Calls `Wire.begin()` with the SDA and SCL pins configured in the constructor. - Sets I2C clock speed to ~700 kHz (or ~400 kHz if ESP in 80 MHz mode). ### Return Value - Always returns `true`; connection errors are not explicitly reported. ### Request Example ```cpp SSD1306Wire display(0x3c, 21, 22); if (display.init()) { // Calls connect() internally display.drawString(0, 0, "Connected"); display.display(); } ``` ``` -------------------------------- ### init() Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/OLEDDisplay.md Initializes the display hardware and allocates internal buffers. Must be called before any drawing operations. Returns true if initialization succeeds, false otherwise. ```APIDOC ## init() ### Description Initializes the display hardware and allocates internal buffers. Must be called before any drawing operations. ### Return Type `bool` - Returns `true` if initialization succeeds, `false` if display connection fails or memory allocation fails. ### Example ```cpp #include "SSD1306.h" SSD1306 display(0x3c, 21, 22); // address, SDA pin, SCL pin if (!display.init()) { Serial.println("Failed to initialize display"); return; } ``` ``` -------------------------------- ### connect() Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/SSD1306Spi.md Initializes the SPI bus, configures GPIO pins, and performs the hardware reset sequence for the SSD1306 display. ```APIDOC ## connect() ### Description Initializes SPI bus, configures GPIO pins, and performs hardware reset sequence. ### Method `bool connect()` ### Return Type `bool` - Returns `true` if successful. ### Initialization Sequence: 1. Set RST, DC, CS pins to OUTPUT mode 2. Initialize SPI with `SPI.begin()` 3. Set SPI clock divider to `SPI_CLOCK_DIV2` (~8 MHz on 160 MHz ESP) 4. Perform reset: RST HIGH → 1ms delay → RST LOW → 10ms delay → RST HIGH 5. Wait for display to stabilize ### Example: ```cpp SSD1306Spi display(D0, D2, D8); if (display.init()) { // Calls connect() internally Serial.println("SPI connected and reset"); } ``` ``` -------------------------------- ### Draw Vertical Line on OLED Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/OLEDDisplay.md Draws a vertical line of a specified length starting from the given X and Y coordinates. ```cpp display.setColor(WHITE); display.drawVerticalLine(64, 0, 64); // Line down middle display.display(); ``` -------------------------------- ### Constructor Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/OLEDDisplayUi.md Initializes the UI framework with a display instance. This constructor sets up the necessary components for the OLED display UI. ```APIDOC ## Constructor ### OLEDDisplayUi(OLEDDisplay *display) Initializes the UI framework with a display instance. #### Parameters - **display** (OLEDDisplay*) - Required - Pointer to initialized display instance ``` -------------------------------- ### Draw Horizontal Line on OLED Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/OLEDDisplay.md Draws a horizontal line of a specified length starting from the given X and Y coordinates. ```cpp display.setColor(WHITE); display.drawHorizontalLine(0, 32, 128); // Line across middle display.display(); ``` -------------------------------- ### SSD1306Wire connect() Method Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/SSD1306Wire.md Initializes the I2C bus and configures Wire for display communication. This method is called internally by init(). Connection errors are not explicitly reported. ```cpp bool connect() ``` -------------------------------- ### OLEDDisplayUi Initialization and Configuration Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/README.md Functions to initialize the UI library, set target FPS, and manage automatic transitions between frames. Configure frame display times and transition durations. ```C++ /** * Initialise the display */ void init(); /** * Configure the internal used target FPS */ void setTargetFPS(uint8_t fps); /** * Enable automatic transition to next frame after the some time can be configured with * `setTimePerFrame` and `setTimePerTransition`. */ void enableAutoTransition(); /** * Disable automatic transition to next frame. */ void disableAutoTransition(); /** * Set the direction if the automatic transitioning */ void setAutoTransitionForwards(); void setAutoTransitionBackwards(); /** * Set the approx. time a frame is displayed */ void setTimePerFrame(uint16_t time); /** * Set the approx. time a transition will take */ void setTimePerTransition(uint16_t time); ``` -------------------------------- ### connect() Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/SSD1306Brzo.md Initializes the BRZO I2C bus with configured SDA and SCL pins, enabling high-speed communication. ```APIDOC ## connect() ### Description Initializes the BRZO I2C bus with configured SDA and SCL pins. ### Method `bool connect()` ### Return Type `bool` - Returns `true` if successful. ### Details - Calls `brzo_i2c_setup()` with SDA, SCL pins and speed 0 (default 1000 kHz). - BRZO library is written in optimized assembler for high-speed I2C. - Faster than standard Arduino Wire library. - Always returns `true`; connection errors are not explicitly reported. ### Example ```cpp SSD1306Brzo display(0x3c, 5, 4); if (display.init()) { // Calls connect() internally Serial.println("BRZO connected"); } ``` ``` -------------------------------- ### Initialize SSD1306Brzo (BRZO I2C) Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/configuration.md Configure the SSD1306 display using the BRZO I2C interface. This constructor is identical to SSD1306Wire, requiring the I2C address and GPIO pins for SDA and SCL. ```cpp SSD1306Brzo display(0x3c, 5, 4); ``` -------------------------------- ### Get String Width Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/OLEDDisplay.md Calculates and returns the pixel width of a given C-string using the currently active font. Specify the number of characters to measure. ```cpp display.setFont(ArialMT_Plain_10); uint16_t width = display.getStringWidth("Hello", 5); ``` -------------------------------- ### connect() Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/SH1106Wire.md Initializes the I2C bus and configures Wire for display communication. This method is called internally by `init()`. ```APIDOC ## connect() ### Description Initializes the I2C bus and configures Wire for display communication. ### Method `bool connect()` ### Return Type `bool` - Returns `true` if successful. Connection errors are not explicitly reported. ### Details - Calls `Wire.begin()` with the SDA and SCL pins configured in the constructor. - Sets I2C clock speed to ~700 kHz (or ~400 kHz if ESP in 80 MHz mode). - SH1106 initialization is identical to SSD1306 at the Wire level. ### Example ```cpp SH1106Wire display(0x3c, 21, 22); if (display.init()) { // Calls connect() internally Serial.println("SH1106 connected via I2C"); } ``` ``` -------------------------------- ### Text Drawing Functions Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/README.md Provides functions for drawing strings, wrapping text, and getting text dimensions. Use `setTextAlignment` and `setFont` to configure text rendering. ```C++ void drawString(int16_t x, int16_t y, String text); // Draws a String with a maximum width at the given location. // If the given String is wider than the specified width // The text will be wrapped to the next line at a space or dash void drawStringMaxWidth(int16_t x, int16_t y, int16_t maxLineWidth, String text); // Returns the width of the const char* with the current // font settings uint16_t getStringWidth(const char* text, uint16_t length); // Convencience method for the const char version uint16_t getStringWidth(String text); // Specifies relative to which anchor point // the text is rendered. Available constants: // TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER, TEXT_ALIGN_RIGHT, TEXT_ALIGN_CENTER_BOTH void setTextAlignment(OLEDDISPLAY_TEXT_ALIGNMENT textAlignment); // Sets the current font. Available default fonts // ArialMT_Plain_10, ArialMT_Plain_16, ArialMT_Plain_24 // Or create one with the font tool at http://oleddisplay.squix.ch void setFont(const char* fontData); ``` -------------------------------- ### OLEDDisplayUi Framework Initialization and Configuration Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/INDEX.md Initializes the UI framework and configures its core components. Set frames, overlays, and loading functions. The UI framework simplifies creating interactive interfaces. ```cpp // Constructor and initialization OLEDDisplayUi ui(&display); void ui.init(); // Frame configuration void ui.setFrames(FrameCallback* callbacks, uint8_t count); void ui.setOverlays(OverlayCallback* callbacks, uint8_t count); void ui.setLoadingDrawFunction(LoadingDrawFunction func); void ui.runLoadingProcess(LoadingStage* stages, uint8_t count); ``` -------------------------------- ### Initialize OLEDDisplayUi Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/configuration.md Instantiate the OLEDDisplayUi class by passing a pointer to an initialized OLEDDisplay instance. ```cpp SSD1306Wire display(0x3c, 21, 22); OLEDDisplayUi ui(&display); ``` -------------------------------- ### Run Loading Process with Stages Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/OLEDDisplayUi.md Execute a loading process with multiple defined stages. Each stage has associated text and a callback function. The UI will display the current stage's process and a progress bar. ```cpp void stageInit() { Serial.println("Initializing..."); delay(500); } void stageWifi() { Serial.println("Connecting WiFi..."); delay(1000); } LoadingStage stages[] = { { "Init", stageInit }, { "WiFi", stageWifi } }; ui.runLoadingProcess(stages, 2); ``` -------------------------------- ### Get OLEDDisplayUi State Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/OLEDDisplayUi.md Retrieves a pointer to the current UI state structure. This can be used to access and modify state variables like the current frame or custom user data. ```cpp OLEDDisplayUiState* state = ui.getUiState(); Serial.println("Current frame: " + String(state->currentFrame)); // Store custom data state->userData = (void*)myCustomValue; ``` -------------------------------- ### Initialization Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/OLEDDisplayUi.md Initializes the UI framework. This method must be called after the display is initialized and prepares the UI for frame updates and transitions. ```APIDOC ## Initialization ### init() Initializes the UI framework. Sets up internal state and prepares for frame updates. **Notes:** - Must be called after `display.init()`. - Resets frame transitions and state. - Prepares first frame for display. ``` -------------------------------- ### Initialize and Use LogBuffer Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/UPGRADE-3.0.md Initialize the `LogBuffer` by specifying the number of lines and average characters per line. Then, use `drawLogBuffer` to display the logged text on the screen. Remember to call `display()` to update the screen. ```c++ setLogBuffer(lines, chars); ``` ```c++ drawLogBuffer(x, y); ``` -------------------------------- ### Initialize and Display Text Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/INDEX.md Initializes the SSD1306 display over I2C and draws a simple string. Ensure the correct I2C address and pins are used. ```cpp SSD1306Wire display(0x3c, 21, 22); display.init(); display.setFont(ArialMT_Plain_10); display.drawString(0, 0, "Hello"); display.display(); ``` -------------------------------- ### SSD1306Wire Constructor and Initialization Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/INDEX.md Initializes the SSD1306 display using the I2C interface. Use `init()` to begin and `end()` to stop the display. `resetDisplay()` can be called to reset the display hardware. ```cpp // Constructor SSD1306Wire display(0x3c, 21, 22); // Initialization bool display.init(); void display.end(); void display.resetDisplay(); ``` -------------------------------- ### SSD1306Brzo Constructor Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/SSD1306Brzo.md Initializes a display object for high-speed BRZO I2C communication with an SSD1306 display. ```APIDOC ## SSD1306Brzo Constructor ### Description Initializes a display object for high-speed BRZO I2C communication. ### Method Constructor ### Parameters #### Path Parameters - **address** (uint8_t) - Required - I2C slave address (typically 0x3C or 0x3D) - **sda** (uint8_t) - Required - GPIO pin for I2C SDA (data line) - **scl** (uint8_t) - Required - GPIO pin for I2C SCL (clock line) ### Request Example ```cpp #include #include "SSD1306Brzo.h" // I2C address 0x3C, SDA on GPIO 5, SCL on GPIO 4 SSD1306Brzo display(0x3c, 5, 4); void setup() { if (!display.init()) { Serial.println("Display init failed"); return; } display.drawString(0, 0, "BRZO I2C"); display.display(); } ``` ``` -------------------------------- ### Update Display with Double Buffering Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/SSD1306Wire.md Call display() after drawing operations to render changes on the physical display. With double buffering enabled (default), only updated regions are transmitted, reducing I2C traffic. This example shows clearing the screen, drawing text, and then updating the display. ```cpp display.clear(); display.setColor(WHITE); display.drawString(0, 0, "Update now"); display.display(); // Sends to hardware // Next update—only changed area sent display.drawString(0, 10, "Optimized"); display.display(); // Minimal I2C traffic ``` -------------------------------- ### I2C Data Transmission Batching Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/SSD1306Wire.md This snippet demonstrates how data is sent in chunks of 16 bytes to minimize I2C overhead. It shows the typical sequence of starting a transmission, writing the data mode prefix, sending 16 bytes of display data, and ending the transmission. ```cpp Wire.beginTransmission(address) Wire.write(0x40) // Data mode Wire.write(data[0-15]) // 16 bytes of display data Wire.endTransmission() ``` -------------------------------- ### Display Initialization Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/INDEX.md Methods for initializing and managing the display connection and state. ```APIDOC ## Display Initialization ### Description Methods for initializing and managing the display connection and state. ### Methods - `SSD1306Wire display(0x3c, 21, 22);` - Constructor to initialize the display object with I2C address and pins. - `bool display.init();` - Initializes the display connection and prepares it for drawing. - `void display.end();` - Cleans up and closes the display connection. - `void display.resetDisplay();` - Resets the display to its default state. ``` -------------------------------- ### OLEDDisplayUi Constructor Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/configuration.md Initializes the OLEDDisplayUi with a pointer to an initialized display instance. ```APIDOC ## OLEDDisplayUi Constructor ### Description Initializes the OLEDDisplayUi with a pointer to an initialized display instance. ### Constructor ```cpp OLEDDisplayUi(OLEDDisplay* display) ``` ### Parameters #### Path Parameters - **display** (OLEDDisplay*) - Required - Pointer to initialized display instance ### Request Example ```cpp SSD1306Wire display(0x3c, 21, 22); OLEDDisplayUi ui(&display); ``` ``` -------------------------------- ### Initialize OLEDDisplayUi Framework Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/OLEDDisplayUi.md Initializes the UI framework, preparing it for frame updates. This function should be called after the display has been initialized and resets internal state. ```cpp display.init(); ui.init(); ``` -------------------------------- ### Create Multi-Frame UI Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/INDEX.md Sets up a multi-frame user interface using the OLEDDisplayUi class. This pattern is useful for rotating through different screens or information. ```cpp OLEDDisplayUi ui(&display); FrameCallback frames[] = { frame1, frame2 }; ui.setFrames(frames, 2); ui.enableAutoTransition(); ui.setTimePerFrame(5000); while (true) { ui.update(); } ``` -------------------------------- ### Selecting Display Classes for OLED Modules Source: https://github.com/lilygo/esp32-oled0.96-ssd1306/blob/master/_autodocs/api-reference/SH1106Wire.md Choose the appropriate include file and constructor based on your display controller (SSD1306 or SH1106) and interface type (I2C or SPI). Ensure correct pin assignments for SPI and I2C addresses/pins for I2C. ```cpp #include "SSD1306Wire.h" SSD1306Wire display(0x3c, 21, 22); ``` ```cpp #include "SH1106Wire.h" SH1106Wire display(0x3c, 21, 22); ``` ```cpp #include "SSD1306Spi.h" SSD1306Spi display(D0, D2, D8); ``` ```cpp #include "SH1106Spi.h" SH1106Spi display(D0, D2, D8); ``` ```cpp #include "SSD1306Brzo.h" SSD1306Brzo display(0x3c, 5, 4); ``` ```cpp #include "SH1106Brzo.h" SH1106Brzo display(0x3c, 5, 4); ```