Tiny4kOLED

repository·master·Indexed 43 days ago

https://github.com/datacute/tiny4koled

A library for the ATTiny85 to drive SSD1306-powered OLED displays over I2C. It features double buffering support for 128x32 and 'zoomed' 128x64 displays, custom font support, and optimized code size for resource-constrained environments. It is compatible with Wire.h, TinyWireM.h, and TinyI2CMaster.h I2C implementations and provides Arduino Print class compatibility via SSD1306PrintDevice.

Tokens
3.2K
Snippets
11
Records
18
Agent score
54%

What's inside Tiny4kOLED

  1. Overview of Tiny4kOLED (SSD1306xLED)

    master
    Tiny4kOLED (formerly DigiOLED) is a C library designed to control SSD1306-based dot matrix OLED/PLED displays (typically 128x64). It is optimized for microcontrollers like the ATtiny85 and is intended to be 'Arduino friendly' by supporting the Wire and Print libraries. While designed with the Tinusaur board in mind, it is compatible with other ATtiny85-based boards.
  2. Overview of SSD1306xLED library

    master
    SSD1306xLED is a C library designed to control dot matrix OLED/PLED displays using the SSD1306 driver. While optimized for the Tinusaur board, it is compatible with any microcontroller based on ATtiny85 or similar architectures. The library is written in plain C and requires only standard SDK libraries to function. It has been tested and supports 8 MHz CPU clock modes.
  3. How double buffering and 'zoom' work in Tiny4kOLED

    master

    Tiny4kOLED supports double buffering, which allows a slow microcontroller like the ATTiny85 to update a frame buffer in the background before switching it to the display.

    For 128x64 pixel screens, the library includes a 'zoom' feature. In zoom mode, each row is drawn twice, which effectively uses only half of the SSD1306's RAM. This allows the double buffering technique to be used even on 128x64 displays.

    For 128x32 pixel displays, half of the SSD1306's RAM is naturally available to be used as a frame buffer for double buffering.

  4. SSD1306xLED library file structure

    master

    The library is organized into the following modules:

    • ssd1306xled: The core library containing:
      • ssd1306xled.c / ssd1306xled.h: Main implementation and headers.
      • font6x8.h: Small 6x8 font.
      • font8x16.h: Large 8x16 font.
      • ssd1306xled8x16.c / ssd1306xled8x16.h: Large font 8x16 implementation.
    • ssd1306xled_test: A testing suite including main.c and sample bitmap images (img0_128x64c1.h, img1_128x64c1.h).
    • ssd1306xled_cn / ssd1306xled_test_cn: Modules for Chinese character handling (Note: currently reported as non-functional).
  5. Optimize initialization for white OLED displays

    master

    Blue OLED screens require a long initialization period on power-on. If you are using a white OLED, you can reclaim approximately 20 bytes of program storage space by defining TINY4KOLED_QUICK_BEGIN before including the library header.

    // Uncomment this for white OLEDs to save 20 bytes of program space
    #define TINY4KOLED_QUICK_BEGIN
    #include <Tiny4kOLED.h>
  6. Configure I2C implementation for Tiny4kOLED

    master

    Before including Tiny4kOLED.h, you must select and include your preferred I2C implementation. The library defaults to Wire.h if no other library is included.

    Supported implementations include:

    // To use the Wire library:
    #include <Wire.h>
    
    // To use the Adafruit's TinyWireM library:
    #include <TinyWireM.h>
    
    // To use the TinyI2C library:
    #include <TinyI2CMaster.h>
    
    #include <Tiny4kOLED.h>
  7. Reset SSD1306 to default settings

    master

    Because the SSD1306 controller retains settings even when powered off, you may need to explicitly reset it to default values. Use the tiny4koled_init_defaults sequence. Note that the charge pump is off by default in this sequence, so you will likely need to enable it manually for most boards.

    // Reset to defaults
    oled.begin(128, 64, sizeof(tiny4koled_init_defaults), tiny4koled_init_defaults);
    
    // Most boards require the charge pump to be enabled
    oled.enableChargePump();
    
    // Set preferred orientation (default is often not the most common)
    oled.setRotation(1);
  8. Use SSD1306PrintDevice for Arduino Print compatibility

    master

    The SSD1306PrintDevice class inherits from both SSD1306Device and the Arduino Print class. This allows you to use standard Arduino functions like print() and println() directly on the OLED object.

    // Assuming oled is an instance of SSD1306PrintDevice
    // or a class inheriting from it
    Print *p = &oled;
    p->print("Hello World!");
    p->println("New Line");
  9. Complete Example Usage

    master

    This example demonstrates a standard loop: initializing the display, setting a font, and updating a millisecond counter using the frame-switching method to ensure smooth updates.

    #include <Tiny4kOLED.h>
    
    void setup() {
      oled.begin();
      oled.setFont(FONT8X16);
      oled.clear();
      oled.on();
      oled.switchRenderFrame();
    }
    
    void loop() {
      updateDisplay();
      delay(50);
    }
    
    void updateDisplay() {
      oled.clear();
      oled.setCursor(0, 1);
      oled.print(F("ms: "));
      oled.print(millis());
      oled.switchFrame();
    }
  10. Initialize and configure the OLED display

    master

    Use the following methods in setup() to prepare the display:

    • oled.begin(): Sends the initialization sequence. Note that this leaves the display turned off by default. It also sets the rotation to 1.
    • oled.setRotation(0): Sets the rotation to 0 (the default is 1).
    • oled.setInternalIref(bool): Configures the internal current reference. Use true for high current to achieve a brighter display and more effective contrast (useful for newer devices without an external current reference).
    • oled.setFont(font): Sets the active font. Built-in options include FONT8X16 and FONT6X8.
    • oled.clear(): Clears the display memory.
    • oled.on(): Turns the display on.
    • oled.switchRenderFrame(): Switches the writing target to the half of RAM that is not currently being displayed.
    void setup() {
      oled.begin();
      // oled.setRotation(0);
      // oled.setInternalIref(true);
      oled.setFont(FONT8X16);
      oled.clear();
      oled.on();
      oled.switchRenderFrame();
    }
  11. Write text and manage display frames

    master

    To prevent flickering and manage the limited RAM, the library uses a double-buffering approach via RAM frames.

    Positioning Text

    Use oled.setCursor(x, y) to position the cursor.

    • x: Position in pixels.
    • y: Position in rows of 8 pixels, starting from 0. Valid values are 0, 1, 2, or 3 (aligned with the four 8-bit high RAM pages).

    Frame Management

    • oled.switchRenderFrame(): Switches the active writing buffer to the half of RAM not currently being displayed.
    • oled.switchFrame(): A convenience method that performs both switchRenderFrame() and switchDisplayFrame(). This is used to swap the buffer being written to with the buffer currently being displayed.
    void updateDisplay() {
      oled.clear();
      // oled.setCursor(X_PIXELS, Y_8PIXEL_ROWS);
      oled.setCursor(0, 1);
      oled.print(F("text"));
      
      // Swap buffers to show the new content
      oled.switchFrame();
    }
  12. Configure display pages for clear and fill operations

    master

    The library's clear and fill commands, as well as text line wrapping, operate based on the screen's height measured in pages. One page is equivalent to 8 rows of pixels.

    The library defaults to 4 pages. If your display has a different geometry, you must call the setPages method or use the appropriate begin settings for your specific resolution.