SSD1306Ascii Library

repository·master·Indexed 20 days ago

https://github.com/greiman/ssd1306ascii

A lightweight, unbuffered character-only library for SSD1306-based OLED displays, optimized for microcontrollers with limited RAM such as Arduino AVR boards. It supports hardware SPI, software SPI, and I2C (via Wire or AvrI2c) interfaces and requires only a few bytes of dynamic memory.

Tokens
1.7K
Snippets
7
Records
10
Agent score
69%

What's inside SSD1306Ascii

  1. Overview of SSD1306Ascii

    master

    SSD1306Ascii is an unbuffered, character-only library designed for small OLED displays using SSD1306 controllers (such as the Adafruit 1.3" and 0.96" Monochrome displays).

    Key characteristics:

    • Unbuffered: It does not use a full frame buffer, making it extremely memory-efficient.
    • Low RAM usage: It requires only a few bytes of RAM. For example, a 'Hello world!' SPI sketch on an Arduino Uno uses only 54 bytes (2%) of dynamic memory.
    • Compatibility: Runs on Arduino AVR boards, Arduino Due, and other Arduino-style boards that support the SPI or Wire libraries.
  2. Initialize an SSD1306 SPI display

    master

    Use the SSD1306AsciiSpi::begin() method to initialize the display controller on the hardware SPI bus. You can provide either a standard configuration or include a reset pin for hardware resets.

    Method Signatures

    begin(const DevType* dev, uint8_t cs, uint8_t dc)

    Initializes the display using:

    • dev: A pointer to a DevType initialization structure (defining display dimensions, etc.).
    • cs: The Chip Select pin.
    • dc: The Data/Command pin.

    begin(const DevType* dev, uint8_t cs, uint8_t dc, uint8_t rst)

    Initializes the display and performs a hardware reset using:

    • dev: A pointer to a DevType initialization structure.
    • cs: The Chip Select pin.
    • dc: The Data/Command pin.
    • rst: The Reset pin.
    // Example: Initializing with CS, DC, and Reset pins
    SSD1306AsciiSpi display;
    SSD1306AsciiDev dev;
    
    dev.width = 128;
    dev.height = 64;
    
    // begin(device_config, chip_select_pin, data_command_pin, reset_pin)
    display.begin(&dev, 10, 9, 8);
  3. SSD1306AsciiSoftSpi::begin() signatures

    master

    The begin() method initializes the pins and the display controller. It has two overloads:

    1. void begin(const DevType* dev, uint8_t cs, uint8_t dc, uint8_t clk, uint8_t data)

      • dev: A pointer to a DevType initialization structure.
      • cs: Chip select pin.
      • dc: Data/command pin.
      • clk: SPI clock pin.
      • data: SPI MOSI pin.
    2. void begin(const DevType* dev, uint8_t cs, uint8_t dc, uint8_t clk, uint8_t data, uint8_t rst)

      • dev: A pointer to a DevType initialization structure.
      • cs: Chip select pin.
      • dc: Data/command pin.
      • clk: SPI clock pin.
      • data: SPI MOSI pin.
      • rst: Reset pin. This overload performs a hardware reset (pulling the pin LOW for 10ms, then HIGH for 10ms) before calling the standard initialization.
  4. Use specific I2C buses with SSD1306AsciiWire

    master

    If your project uses MULTIPLE_I2C_PORTS, you can instantiate SSD1306AsciiWire on a specific I2C bus by passing the desired Wire instance to the constructor. If MULTIPLE_I2C_PORTS is not defined, the class defaults to using the standard Wire object.

    // Using a specific I2C bus (e.g., Wire1)
    SSD1306AsciiWire oled(Wire1);
  5. Initialize SSD1306AsciiWire for I2C displays

    master

    The SSD1306AsciiWire class is used to interface with SSD1306 OLED displays via the I2C protocol using the Arduino Wire library.

    To initialize the display, use the begin() method. You must provide a DevType structure (defined in the base SSD1306Ascii class) and the I2C address of the display.

    If your hardware includes a reset pin, you can use the overloaded begin() method that accepts a reset pin number to perform a hardware reset before initialization.

    // Basic initialization
    SSD1306AsciiWire oled;
    // dev is a pointer to a DevType configuration structure
    oled.begin(&dev, 0x3C);
    
    // Initialization with a hardware reset pin
    // rst is the GPIO pin number connected to the OLED reset pin
    oled.begin(&dev, 0x3C, RST_PIN);
  6. Initialize a software SPI display with SSD1306AsciiSoftSpi

    master

    Use the SSD1306AsciiSoftSpi class to drive SSD1306 displays using bit-banged (software) SPI. This is useful when you need to use pins other than the hardware SPI pins. You can initialize the display using two versions of the begin() method: one that only requires the SPI pins, and one that also includes a reset pin.

    // Using begin() without a reset pin
    // dev: pointer to device initialization structure
    // cs: chip select pin
    // dc: data/command pin
    // clk: clock pin
    // data: MOSI pin
    display.begin(dev, cs_pin, dc_pin, clk_pin, data_pin);
    
    // Using begin() with a reset pin
    // rst: reset pin
    display.begin(dev, cs_pin, dc_pin, clk_pin, data_pin, rst_pin);
  7. Initialize SSD1306AsciiAvrI2c display

    master

    Use the begin() method to initialize an SSD1306 I2C display on an AVR-based board. This class uses the AvrI2c library, which is optimized to be smaller and faster than the standard Arduino Wire library.

    You can initialize the display in two ways:

    1. Standard initialization: Provide a device initialization structure (DevType) and the I2C address.
    2. With Reset Pin: Provide the device initialization structure, the I2C address, and the specific GPIO pin used for the display's reset (rst).
    // Standard initialization
    display.begin(&dev, 0x3C);
    
    // Initialization with a reset pin
    display.begin(&dev, 0x3C, RST_PIN);