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:
4:853097cfa773
Parent:
0:c3dcd4c4983a
Child:
5:315de3647c9f
--- a/Adafruit_SSD1306.cpp	Wed Jan 23 02:43:38 2013 +0000
+++ b/Adafruit_SSD1306.cpp	Tue Apr 29 19:27:01 2014 +0000
@@ -115,12 +115,22 @@
     // turn on VCC (9V?)
     
 #if defined SSD1306_128_32
-    // Init sequence for 128x32 OLED module
+    uint8_t setmultiplex = 0x1F;
+    uint8_t setcompins = 0x02;
+    uint8_t setcontrast = 0x8F;
+#elif defined SSD1306_128_64
+    uint8_t setmultiplex = 0x3F;
+    uint8_t setcompins = 0x12;
+    uint8_t setcontrast = (vccstate == SSD1306_EXTERNALVCC) ? 0x9F : 0xCF;
+#else
+#error "Display dimensions must be defined"
+#endif
+
     ssd1306_command(SSD1306_DISPLAYOFF);                    // 0xAE
     ssd1306_command(SSD1306_SETDISPLAYCLOCKDIV);            // 0xD5
     ssd1306_command(0x80);                                  // the suggested ratio 0x80
     ssd1306_command(SSD1306_SETMULTIPLEX);                  // 0xA8
-    ssd1306_command(0x1F);
+    ssd1306_command(setmultiplex);
     ssd1306_command(SSD1306_SETDISPLAYOFFSET);              // 0xD3
     ssd1306_command(0x0);                                   // no offset
     ssd1306_command(SSD1306_SETSTARTLINE | 0x0);            // line #0
@@ -134,9 +144,9 @@
     ssd1306_command(SSD1306_SEGREMAP | 0x1);
     ssd1306_command(SSD1306_COMSCANDEC);
     ssd1306_command(SSD1306_SETCOMPINS);                    // 0xDA
-    ssd1306_command(0x02);
+    ssd1306_command(setcompins);
     ssd1306_command(SSD1306_SETCONTRAST);                   // 0x81
-    ssd1306_command(0x8F);
+    ssd1306_command(setcontrast);
     ssd1306_command(SSD1306_SETPRECHARGE);                  // 0xd9
     if (vccstate == SSD1306_EXTERNALVCC) 
     { ssd1306_command(0x22); }
@@ -146,44 +156,6 @@
     ssd1306_command(0x40);
     ssd1306_command(SSD1306_DISPLAYALLON_RESUME);           // 0xA4
     ssd1306_command(SSD1306_NORMALDISPLAY);                 // 0xA6
-#endif
-    
-#if defined SSD1306_128_64
-    // Init sequence for 128x64 OLED module
-    ssd1306_command(SSD1306_DISPLAYOFF);                    // 0xAE
-    ssd1306_command(SSD1306_SETDISPLAYCLOCKDIV);            // 0xD5
-    ssd1306_command(0x80);                                  // the suggested ratio 0x80
-    ssd1306_command(SSD1306_SETMULTIPLEX);                  // 0xA8
-    ssd1306_command(0x3F);
-    ssd1306_command(SSD1306_SETDISPLAYOFFSET);              // 0xD3
-    ssd1306_command(0x0);                                   // no offset
-    ssd1306_command(SSD1306_SETSTARTLINE | 0x0);            // line #0
-    ssd1306_command(SSD1306_CHARGEPUMP);                    // 0x8D
-    if (vccstate == SSD1306_EXTERNALVCC) 
-    { ssd1306_command(0x10); }
-    else 
-    { ssd1306_command(0x14); }
-    ssd1306_command(SSD1306_MEMORYMODE);                    // 0x20
-    ssd1306_command(0x00);                                  // 0x0 act like ks0108
-    ssd1306_command(SSD1306_SEGREMAP | 0x1);
-    ssd1306_command(SSD1306_COMSCANDEC);
-    ssd1306_command(SSD1306_SETCOMPINS);                    // 0xDA
-    ssd1306_command(0x12);
-    ssd1306_command(SSD1306_SETCONTRAST);                   // 0x81
-    if (vccstate == SSD1306_EXTERNALVCC) 
-    { ssd1306_command(0x9F); }
-    else 
-    { ssd1306_command(0xCF); }
-    ssd1306_command(SSD1306_SETPRECHARGE);                  // 0xd9
-    if (vccstate == SSD1306_EXTERNALVCC) 
-    { ssd1306_command(0x22); }
-    else 
-    { ssd1306_command(0xF1); }
-    ssd1306_command(SSD1306_SETVCOMDETECT);                 // 0xDB
-    ssd1306_command(0x40);
-    ssd1306_command(SSD1306_DISPLAYALLON_RESUME);           // 0xA4
-    ssd1306_command(SSD1306_NORMALDISPLAY);                 // 0xA6
-#endif
     
     ssd1306_command(SSD1306_DISPLAYON);//--turn on oled panel
 }