Overview of Tiny4kOLED (SSD1306xLED)
masterWire and Print libraries. While designed with the Tinusaur board in mind, it is compatible with other ATtiny85-based boards.repository·master·Indexed 43 days ago
https://github.com/datacute/tiny4koledA 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.
Wire and Print libraries. While designed with the Tinusaur board in mind, it is compatible with other ATtiny85-based boards.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.
The library is organized into the following modules:
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.main.c and sample bitmap images (img0_128x64c1.h, img1_128x64c1.h).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>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:
Wire.h (Default)TinyWireM.h (Adafruit's TinyWireM)TinyI2CMaster.h (from technoblogy/tiny-i2c)// 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>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);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");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();
}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();
}To prevent flickering and manage the limited RAM, the library uses a double-buffering approach via RAM frames.
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).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();
}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.