esp8266-oled-ssd1306

repository·master·Indexed 24 days ago

https://github.com/thingpulse/esp8266-oled-ssd1306

A driver for SSD1306 and SH1106 OLED displays (128x64, 128x32, 64x48, and 64x32) compatible with Arduino, ESP8266, ESP32, and mbed-os platforms. It supports I2C (via Wire.h or brzo_i2c) and SPI interfaces. The library includes a high-level UI system (OLEDDisplayUi) for managing frames and overlays, as well as tools for drawing shapes, pixels, XBM bitmaps, and rendering text with custom fonts.

Tokens
8.6K
Snippets
17
Records
54
Agent score
78%

What's inside esp8266-oled-ssd1306

  1. Select the correct hardware implementation class

    master

    The library abstracted hardware connections into specific subclasses of OLEDDisplay. Depending on your connection type, you should instantiate one of the following:

    • SSD1306Wire: For I2C using the standard Arduino Wire library.
    • SSD1306Brzo: For I2C using the faster brzo_i2c library.
    • SSD1306Spi: For SPI connections.

    Note: SSD1306 is maintained as an alias for SSD1306Wire for backwards compatibility if you are not using UI components.

  2. How the OLEDDisplayUi library works

    master

    The OLEDDisplayUi library provides a high-level UI system based on two main abstractions:

    1. Frames: Used to display information to the user. By default, the library cycles through a set of Frames for a defined duration using enableAutoTransition().
    2. Overlays: Used for information that should remain visible at a constant position (e.g., a clock) regardless of which Frame is currently active.

    Key Lifecycle Methods:

    • init(): Initializes the UI.
    • update(): Must be called in the main loop. It returns the remaining time (in ms) available for drawing to stay within the frame budget.
    • setFrames(FrameCallback* frameFunctions, uint8_t frameCount): Registers the array of functions used to draw each frame.
    • setOverlays(OverlayCallback* overlayFunctions, uint8_t overlayCount): Registers the array of functions used to draw overlays.

    Manual Control: You can bypass automatic transitions using nextFrame(), previousFrame(), switchToFrame(uint8_t frame) (instant switch), or transitionToFrame(uint8_t frame) (animated switch).

  3. Display XBM bitmaps

    master

    To display custom images, convert them to 1-bit XBM format.

    Conversion Steps:

    1. Gimp: Export as 1-bit XBM.
    2. Online (e.g., image2cpp):
      • Upload image (matching screen dimensions, e.g., 128x64).
      • Draw Mode: Horizontal - 1 bit per pixel.
      • Swap bits in byte: Check the swap checkbox.

    Usage: Store the resulting array in a header file and use drawXbm() to render it.

    const unsigned char epd_example [] PROGMEM = {
    	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ... 
        ...
    };
    
    // Rendering the bitmap
    display.clear();
    display.drawXbm(0, 0, 128, 64, epd_example); // assuming your bitmap is 128x64
    display.display();
  4. Install the SSD1306 OLED driver

    master

    You can install this library using several methods depending on your development environment:

    Arduino IDE

    Search for ESP8266 and ESP32 Oled Driver for SSD1306 display in the Arduino Library Manager.

    PlatformIO

    Execute the following command in your terminal:

    platformio lib install 2978

    mbed-os

    Copy the following files directly into your mbed-os project:

    • OLEDDisplay.cpp
    • OLEDDisplay.h
    • OLEDDisplayFonts.h
    • OLEDDisplayUi.cpp
    • OLEDDisplayUi.h
    • SSD1306I2C.h
  5. Manage UI frames and transitions with OLEDDisplayUi

    master

    The OLEDDisplayUi class provides a high-level abstraction for managing multiple screens (frames) on an OLED display. It supports automatic or manual frame switching, animated transitions (sliding), and overlay drawing.

    To use it, you must provide an array of FrameCallback functions via setFrames(). Each callback is responsible for drawing the content of a specific frame.

    Key Concepts

    • Frames: Individual screens that you define via FrameCallback functions.
    • Transitions: Animations used when switching between frames (e.g., SLIDE_UP, SLIDE_LEFT).
    • Overlays: Drawing functions that run independently of the current frame, useful for persistent UI elements like status bars.
    • Indicators: A visual bar (often used to show progress through a menu) that can be configured with custom symbols and positions.
  6. Use the log buffer for scrolling text

    master

    The OLEDDisplay class includes a log buffer mechanism that allows you to use print() and println() to create a scrolling log on the screen.

    • cls(): Clears the display immediately and empties the logBuffer. This is different from clear(), which only clears the graphics buffer. After calling cls(), the next print() will start at the top of the display.
    • drawLogBuffer(uint16_t x, uint16_t y): Manually draws the contents of the log buffer at the specified position. This is automatically called when using print(), println(), or printf().
  7. Initialize SSD1306Brzo for the BRZO platform

    master

    To use the SSD1306 driver with the BRZO I2C abstraction, instantiate the SSD1306Brzo class. You must provide the I2C address, the SDA pin, the SCL pin, and optionally the display geometry.

    Constructor Parameters:

    • address: The I2C address of the SSD1306 display.
    • sda: The GPIO pin number for the SDA line.
    • scl: The GPIO pin number for the SCL line.
    • g: The display geometry (defaults to GEOMETRY_128_64).

    After instantiation, you must call connect() to initialize the BRZO I2C hardware.

  8. Use 64x48 geometry with Wire.h

    master

    To use a 64x48 pixel display with the standard Wire.h library, initialize the SSD1306Wire class and pass GEOMETRY_64_48 as the fourth argument to the constructor. This configuration has been tested with a WEMOS D1 mini Lite and a WEMOS OLED shield.

    #include <Wire.h>
    #include <SSD1306Wire.h>
    SSD1306Wire display(0x3c, D2, D1, GEOMETRY_64_48 ); // WEMOS OLED shield
  9. Use 64x48 geometry with brzo_i2c

    master

    To use a 64x48 pixel display with the brzo_i2c library, initialize the SSD1306Brzo class and pass GEOMETRY_64_48 as the fourth argument to the constructor. This configuration has been tested with a WEMOS D1 mini Lite and a WEMOS OLED shield.

    #include <SSD1306Brzo.h>
    SSD1306Brzo display(0x3c, D2, D1, GEOMETRY_64_48 ); // WEMOS OLED Shield
  10. Use LogBuffer for text logging

    master

    The OLEDDisplay class implements the Arduino Print class, allowing you to use it like Serial. You can set up a LogBuffer to manage scrolling text lines.

    1. Initialize the buffer with setLogBuffer(lines, chars), where lines is the number of lines and chars is the average characters per line. This returns false if memory is insufficient.
    2. Write to the buffer using standard print() or println() methods.
    3. Draw the buffer at a specific location using drawLogBuffer(x, y).
    4. Call display() to refresh the screen.
  11. Update frame and overlay callback signatures

    master

    Due to the renaming of the base class to OLEDDisplay and the UI library to OLEDDisplayUi, you must update the type definitions in your drawing callbacks.

    Frame drawing callbacks must change from:

    bool frame1(SSD1306 *display,  SSD1306UiState* state, int x, int y);

    to:

    void frame1(OLEDDisplay *display,  OLEDDisplayUiState* state, int16_t x, int16_t y);

    Overlay drawing callbacks must change from:

    bool overlay1(SSD1306 *display,  SSD1306UiState* state);

    to:

    void overlay1(OLEDDisplay *display,  OLEDDisplayUiState* state);