A derived version of the BSD licensed Adafrut GFX library for the SSD1306 controller for an OLED 128x32 or 128x64 display using SPI or I2C.

Dependents:   servo_sensor ArchPro_TFT BLE_Display SSD1306_demo ... more

Import libraryAdafruit_GFX

A derived version of the BSD licensed Adafrut GFX library for the SSD1306 controller for an OLED 128x32 or 128x64 display using SPI or I2C.

This is an SPI or I2C driver, font, and graphics drawing library as initially provided by Adafruit which has been modified for use in the mbed envionment.

128x32 OLED Display

Example

/*
 *  Copyright (c) 2012 Neal Horman - http://www.wanlink.com
 *  
 *  License: MIT open source (http://opensource.org/licenses/MIT)
 *      Summary;
 *      Use / modify / distribute / publish it how you want and 
 *      if you use it, or don't, you can't hold me liable for how
 *      it does or doesn't work.
 *      If it doesn't work how you want, don't use it, or change
 *      it so that it does work.
 */
 
#include "mbed.h"
#include "Adafruit_SSD1306.h"

DigitalOut myled(LED1);

// an SPI sub-class that provides a constructed default
class SPIPreInit : public SPI
{
public:
    SPIPreInit(PinName mosi, PinName miso, PinName clk) : SPI(mosi,miso,clk)
    {
        format(8,3);
        frequency(2000000);
    };
};

// an I2C sub-class that provides a constructed default
class I2CPreInit : public I2C
{
public:
    I2CPreInit(PinName sda, PinName scl) : I2C(sda, scl)
    {
        frequency(400000);
        start();
    };
};

SPIPreInit gSpi(p5,NC,p7);
Adafruit_SSD1306_Spi gOled1(gSpi,p26,p25,p24);

I2CPreInit gI2C(p9,p10);
Adafruit_SSD1306_I2c gOled2(gI2C,p27);

int main()
{   uint16_t x=0;

    gOled1.printf("%ux%u OLED Display\r\n", gOled1.width(), gOled1.height());
    gOled2.printf("%ux%u OLED Display\r\n", gOled2.width(), gOled2.height());
    
    while(1)
    {
        myled = !myled;
        gOled1.printf("%u\r",x);
        gOled1.display();
        gOled2.printf("%u\r",x);
        gOled2.display();
        x++;
        wait(1.0);
    }
}
Revision:
6:1be3e3b46eb7
Parent:
5:315de3647c9f
Child:
9:ddb97c9850a2
--- a/Adafruit_SSD1306.cpp	Thu Oct 16 23:18:25 2014 -0500
+++ b/Adafruit_SSD1306.cpp	Sat Oct 18 11:47:17 2014 -0500
@@ -94,6 +94,7 @@
 };
 #endif
 
+#ifdef SSD_USES_SPI
 Adafruit_SSD1306::Adafruit_SSD1306(SPI &spi, PinName DC, PinName RST, PinName CS)
     : Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT)
     , rst(RST,false), cs(CS,true), dc(DC,false), mspi(spi)
@@ -104,6 +105,22 @@
     begin();
     display();
 };
+#elif defined SSD_USES_I2C
+Adafruit_SSD1306::Adafruit_SSD1306(I2C &i2c, PinName RST)
+    : Adafruit_GFX(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT)
+    , rst(RST,false),mi2c(i2c)
+{
+    
+    mi2c.frequency(400000);
+    mi2c.start();
+#ifndef WITHOUT_SPLASH
+    memcpy(buffer,splashScreen,sizeof(buffer));
+#endif
+    begin();
+    display();
+};
+#endif
+
 
 void Adafruit_SSD1306::begin(uint8_t vccstate)
 {
@@ -198,20 +215,39 @@
 
 void Adafruit_SSD1306::ssd1306_command(uint8_t c)
 {
+#ifdef SSD_USES_SPI
     cs = 1;
     dc = 0;
     cs = 0;
     mspi.write(c);
     cs = 1;
+    
+#elif defined SSD_USES_I2C
+    char buff[2] ;
+    buff[0] = SSD_Command_Mode ; 
+    buff[1] = c;
+    mi2c.write(SSD_I2C_ADDRESS,buff,sizeof(buff));
+#endif
+
 }
 
 void Adafruit_SSD1306::ssd1306_data(uint8_t c)
 {
+#ifdef SSD_USES_SPI
     cs = 1;
     dc = 1;
     cs = 0;
     mspi.write(c);
     cs = 1;
+#elif defined SSD_USES_I2C
+    char buff[2] ;
+    // Setup D/C to switch to data mode
+    buff[0] = SSD_Data_Mode; 
+    buff[1] = c;
+    // Write on i2c
+    mi2c.write(SSD_I2C_ADDRESS,buff,sizeof(buff));
+#endif
+
 }
 
 void Adafruit_SSD1306::display(void)
@@ -219,7 +255,7 @@
     ssd1306_command(SSD1306_SETLOWCOLUMN | 0x0);  // low col = 0
     ssd1306_command(SSD1306_SETHIGHCOLUMN | 0x0);  // hi col = 0
     ssd1306_command(SSD1306_SETSTARTLINE | 0x0); // line #0
-
+#ifdef SSD_USES_SPI
     cs = 1;
     dc = 1;
     cs = 0;
@@ -235,6 +271,22 @@
     }
     
     cs = 1;
+    
+#elif defined  SSD_USES_I2C
+    char buff[17] ;
+    uint8_t x ;
+    // Setup D/C to switch to data mode
+    buff[0] = SSD_Data_Mode; 
+
+    // loop trough all OLED buffer and 
+    // send a bunch of 16 data byte in one xmission
+    for (uint16_t i=0; i<(SSD1306_LCDWIDTH*SSD1306_LCDHEIGHT/8); i+=16 ) 
+    {
+        for (x=1; x<=16; x++) 
+            buff[x] = buffer[i+x];
+        mi2c.write(SSD_I2C_ADDRESS, buff, 17);
+    }
+#endif
 }
 
 // clear everything