OLEDs

Introduction

Library - Bitbank

In the example code, you will need to set the size of your display and the pins used for the I2C connections for SDA and SCL. There is also a control for rotating the screen.

Main Library Repository
Library Wiki

  • Supports any number of simultaneous displays of any type (mix and match)

  • Optionally detect the display address and type (I2C only)

  • Supports 72x40, 96x16, 64x32, 128x32, 128x64, 128x128 (SH1107) and 132x64 (SH1106) display sizes

  • Drive displays from I2C, SPI or any 2 GPIO pins (virtual I2C)

  • 5 sizes of fixed fonts (6x8, 8x8, 12x16, 16x16, 16x32)

  • Deferred rendering allows preparing a back buffer, then displaying it (usually faster)

  • Text scrolling features (vertical and horizontal)

  • Text cursor position with optional line wrap

  • a function to load a Windows BMP file

  • Pixel drawing on SH1106/7 without needing backing RAM

  • Optimized Bresenham line drawing

  • Optimized Bresenham outline and filled ellipse drawing

  • Optimized outline and filled rectangle drawing

  • Optional backing RAM for drawing pixels for systems with enough RAM

  • 16x16 Tile/Sprite drawing at any angle.

  • Run full frame animations at high frame rates with a simple API

  • Light enough to run on an ATtiny85

Example

Demonstrates basic functionality and compared buffered / non-buffered write speeds

//
// OneBitDisplay library simple demo
//
// Demonstrates how to initialize and use a few functions of the library
// If your MCU has enough RAM, enable the backbuffer to see a demonstration
// of the speed difference between drawing directly on the display versus
// deferred rendering, followed by a "dump" of the memory to the display
//
#include <OneBitDisplay.h>

// if your system doesn't have enough RAM for a back buffer, comment out
// this line (e.g. ATtiny85)
#define USE_BACKBUFFER

#ifdef USE_BACKBUFFER
static uint8_t ucBackBuffer[2048];
#else
static uint8_t *ucBackBuffer = NULL;
#endif

// Use -1 for the Wire library default pins
// or specify the pin numbers to use with the Wire library or bit banging on any GPIO pins
// These are the pin numbers for the M5Stack Atom Grove port I2C (reversed SDA/SCL for straight through wiring)
#define SDA_PIN 20
#define SCL_PIN 21
// Set this to -1 to disable or the GPIO pin number connected to the reset
// line of your display if it requires an external reset
#define RESET_PIN -1
// let OneBitDisplay figure out the display address
#define OLED_ADDR -1
// don't rotate the display
#define FLIP180 1
// don't invert the display
#define INVERT 0
// Bit-Bang the I2C bus
#define USE_HW_I2C 1

// Change these if you're using a different OLED display
#define MY_OLED OLED_128x128
#define OLED_WIDTH 128
#define OLED_HEIGHT 128
//#define MY_OLED OLED_64x32
//#define OLED_WIDTH 64
//#define OLED_HEIGHT 32

OBDISP obd;

void setup() {
int rc;
// The I2C SDA/SCL pins set to -1 means to use the default Wire library
// If pins were specified, they would be bit-banged in software
// This isn't inferior to hw I2C and in fact allows you to go faster on certain CPUs
// The reset pin is optional and I've only seen it needed on larger OLEDs (2.4")
//    that can be configured as either SPI or I2C
//
// obdI2CInit(OBDISP *, type, oled_addr, rotate180, invert, bWire, SDA_PIN, SCL_PIN, RESET_PIN, speed)

rc = obdI2CInit(&obd, MY_OLED, OLED_ADDR, FLIP180, INVERT, USE_HW_I2C, SDA_PIN, SCL_PIN, RESET_PIN, 800000L); // use standard I2C bus at 400Khz
  if (rc != OLED_NOT_FOUND)
  {
    char *msgs[] = {(char *)"SSD1306 @ 0x3C", (char *)"SSD1306 @ 0x3D",(char *)"SH1106 @ 0x3C",(char *)"SH1106 @ 0x3D"};
    obdFill(&obd, 0, 1);
    obdWriteString(&obd, 0,0,0,msgs[rc], FONT_8x8, 0, 1);
    obdSetBackBuffer(&obd, ucBackBuffer);
    delay(2000);
  }
} /* setup() */

void loop() {
  // put your main code here, to run repeatedly:
int i, x, y;
char szTemp[32];
unsigned long ms;

  obdFill(&obd, 0x0, 1);
  obdWriteString(&obd, 0,28,0,(char *)"OLED Demo", FONT_8x8, 0, 1);
  obdWriteString(&obd, 0,0,1,(char *)"Written by Larry Bank", FONT_6x8, 1, 1);
  obdWriteString(&obd, 0,0,3,(char *)"**Demo**", FONT_16x32, 0, 1);
  delay(2000);
  
 // Pixel and line functions won't work without a back buffer
#ifdef USE_BACKBUFFER
  obdFill(&obd, 0, 1);
  obdWriteString(&obd, 0,0,0,(char *)"Backbuffer Test", FONT_8x8,0,1);
  obdWriteString(&obd, 0,0,1,(char *)"3000 Random dots", FONT_8x8,0,1);
  delay(2000);
  obdFill(&obd, 0,1);
  ms = millis();
  for (i=0; i<3000; i++)
  {
    x = random(OLED_WIDTH);
    y = random(OLED_HEIGHT);
    obdSetPixel(&obd, x, y, 1, 1);
  }
  ms = millis() - ms;
  sprintf(szTemp, "%dms", (int)ms);
  obdWriteString(&obd, 0,0,0,szTemp, FONT_8x8, 0, 1);
  obdWriteString(&obd, 0,0,1,(char *)"Without backbuffer", FONT_6x8,0,1);
  delay(2000);
  obdFill(&obd, 0,1);
  ms = millis();
  for (i=0; i<3000; i++)
  {
    x = random(OLED_WIDTH);
    y = random(OLED_HEIGHT);
    obdSetPixel(&obd, x, y, 1, 0);
  }
  obdDumpBuffer(&obd, NULL);
  ms = millis() - ms;
  sprintf(szTemp, "%dms", (int)ms);
  obdWriteString(&obd, 0,0,0,szTemp, FONT_8x8, 0, 1);
  obdWriteString(&obd, 0,0,1,(char *)"With backbuffer", FONT_6x8,0,1);
  delay(2000);
  obdFill(&obd, 0, 1);
  obdWriteString(&obd, 0,0,0,(char *)"Backbuffer Test", FONT_8x8,0,1);
  obdWriteString(&obd, 0,0,1,(char *)"96 lines", FONT_8x8,0,1);
  delay(2000);
  ms = millis();
  for (x=0; x<OLED_WIDTH-1; x+=2)
  {
    obdDrawLine(&obd, x, 0, OLED_WIDTH-x, OLED_HEIGHT-1, 1, 1);
  }
  for (y=0; y<OLED_HEIGHT-1; y+=2)
  {
    obdDrawLine(&obd, OLED_WIDTH-1,y, 0,OLED_HEIGHT-1-y, 1, 1);
  }
  ms = millis() - ms;
  sprintf(szTemp, "%dms", (int)ms);
  obdWriteString(&obd, 0,0,0,szTemp, FONT_8x8, 0, 1);
  obdWriteString(&obd, 0,0,1,(char *)"Without backbuffer", FONT_6x8,0,1);
  delay(2000);
  obdFill(&obd, 0,1);
  ms = millis();
  for (x=0; x<OLED_WIDTH-1; x+=2)
  {
    obdDrawLine(&obd, x, 0, OLED_WIDTH-1-x, OLED_HEIGHT-1, 1, 0);
  }
  for (y=0; y<OLED_HEIGHT-1; y+=2)
  {
    obdDrawLine(&obd, OLED_WIDTH-1,y, 0,OLED_HEIGHT-1-y, 1, 0);
  }
  obdDumpBuffer(&obd, ucBackBuffer);
  ms = millis() - ms;
  sprintf(szTemp, "%dms", (int)ms);
  obdWriteString(&obd, 0,0,0,szTemp, FONT_8x8, 0, 1);
  obdWriteString(&obd, 0,0,1,(char *)"With backbuffer", FONT_6x8,0,1);
  delay(2000);
#endif
} /* loop() */

Last updated

Was this helpful?