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);
    }
}
Committer:
nkhorman
Date:
Sun Oct 19 04:45:11 2014 +0000
Revision:
8:416b8fe451b3
Parent:
7:779204e24db4
Child:
9:ddb97c9850a2
fix compile warning and error

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nkhorman 0:c3dcd4c4983a 1 /*********************************************************************
nkhorman 0:c3dcd4c4983a 2 This is a library for our Monochrome OLEDs based on SSD1306 drivers
nkhorman 0:c3dcd4c4983a 3
nkhorman 0:c3dcd4c4983a 4 Pick one up today in the adafruit shop!
nkhorman 0:c3dcd4c4983a 5 ------> http://www.adafruit.com/category/63_98
nkhorman 0:c3dcd4c4983a 6
nkhorman 0:c3dcd4c4983a 7 These displays use SPI to communicate, 4 or 5 pins are required to
nkhorman 0:c3dcd4c4983a 8 interface
nkhorman 0:c3dcd4c4983a 9
nkhorman 0:c3dcd4c4983a 10 Adafruit invests time and resources providing this open source code,
nkhorman 0:c3dcd4c4983a 11 please support Adafruit and open-source hardware by purchasing
nkhorman 0:c3dcd4c4983a 12 products from Adafruit!
nkhorman 0:c3dcd4c4983a 13
nkhorman 0:c3dcd4c4983a 14 Written by Limor Fried/Ladyada for Adafruit Industries.
nkhorman 0:c3dcd4c4983a 15 BSD license, check license.txt for more information
nkhorman 0:c3dcd4c4983a 16 All text above, and the splash screen must be included in any redistribution
nkhorman 0:c3dcd4c4983a 17 *********************************************************************/
nkhorman 0:c3dcd4c4983a 18
nkhorman 0:c3dcd4c4983a 19 /*
nkhorman 0:c3dcd4c4983a 20 * Modified by Neal Horman 7/14/2012 for use in LPC1768
nkhorman 0:c3dcd4c4983a 21 */
nkhorman 0:c3dcd4c4983a 22
nkhorman 0:c3dcd4c4983a 23 #ifndef _ADAFRUIT_SSD1306_H_
nkhorman 0:c3dcd4c4983a 24 #define _ADAFRUIT_SSD1306_H_
nkhorman 0:c3dcd4c4983a 25
nkhorman 0:c3dcd4c4983a 26 #include "mbed.h"
nkhorman 0:c3dcd4c4983a 27 #include "Adafruit_GFX.h"
nkhorman 0:c3dcd4c4983a 28
nkhorman 0:c3dcd4c4983a 29 /*=========================================================================
nkhorman 0:c3dcd4c4983a 30 SSD1306 Displays
nkhorman 0:c3dcd4c4983a 31 -----------------------------------------------------------------------
nkhorman 0:c3dcd4c4983a 32 The driver is used in multiple displays (128x64, 128x32, etc.).
nkhorman 0:c3dcd4c4983a 33 Select the appropriate display below to create an appropriately
nkhorman 0:c3dcd4c4983a 34 sized framebuffer, etc.
nkhorman 0:c3dcd4c4983a 35
nkhorman 0:c3dcd4c4983a 36 SSD1306_128_64 128x64 pixel display
nkhorman 0:c3dcd4c4983a 37
nkhorman 0:c3dcd4c4983a 38 SSD1306_128_32 128x32 pixel display
nkhorman 0:c3dcd4c4983a 39
nkhorman 0:c3dcd4c4983a 40 You also need to set the LCDWIDTH and LCDHEIGHT defines to an
nkhorman 0:c3dcd4c4983a 41 appropriate size
nkhorman 0:c3dcd4c4983a 42
nkhorman 0:c3dcd4c4983a 43 -----------------------------------------------------------------------*/
nkhorman 0:c3dcd4c4983a 44 //#define SSD1306_128_64
nkhorman 0:c3dcd4c4983a 45 #define SSD1306_128_32
Neal Horman 6:1be3e3b46eb7 46
Neal Horman 6:1be3e3b46eb7 47 #define SSD_USES_SPI
Neal Horman 6:1be3e3b46eb7 48 //#define SSD_USES_I2C
nkhorman 0:c3dcd4c4983a 49 /*=========================================================================*/
nkhorman 0:c3dcd4c4983a 50
nkhorman 0:c3dcd4c4983a 51 #if defined SSD1306_128_64 && defined SSD1306_128_32
nkhorman 0:c3dcd4c4983a 52 #error "Only one SSD1306 display can be specified at once in SSD1306.h"
nkhorman 0:c3dcd4c4983a 53 #endif
nkhorman 0:c3dcd4c4983a 54 #if !defined SSD1306_128_64 && !defined SSD1306_128_32
nkhorman 0:c3dcd4c4983a 55 #error "At least one SSD1306 display must be specified in SSD1306.h"
nkhorman 0:c3dcd4c4983a 56 #endif
nkhorman 0:c3dcd4c4983a 57
nkhorman 0:c3dcd4c4983a 58 #if defined SSD1306_128_64
nkhorman 0:c3dcd4c4983a 59 #define SSD1306_LCDWIDTH 128
nkhorman 0:c3dcd4c4983a 60 #define SSD1306_LCDHEIGHT 64
nkhorman 0:c3dcd4c4983a 61 #endif
nkhorman 0:c3dcd4c4983a 62 #if defined SSD1306_128_32
nkhorman 0:c3dcd4c4983a 63 #define SSD1306_LCDWIDTH 128
nkhorman 0:c3dcd4c4983a 64 #define SSD1306_LCDHEIGHT 32
nkhorman 0:c3dcd4c4983a 65 #endif
nkhorman 0:c3dcd4c4983a 66
Neal Horman 6:1be3e3b46eb7 67 #if defined SSD_USES_SPI && defined SSD_USES_I2C
Neal Horman 6:1be3e3b46eb7 68 #error "Only one communication mode can be specified at once in SSD1306.h"
Neal Horman 6:1be3e3b46eb7 69 #endif
Neal Horman 6:1be3e3b46eb7 70 #if !defined SSD_USES_SPI && !defined SSD_USES_I2C
Neal Horman 6:1be3e3b46eb7 71 #error "At least one communication mode must be specified in SSD1306.h"
Neal Horman 6:1be3e3b46eb7 72 #endif
Neal Horman 6:1be3e3b46eb7 73
Neal Horman 6:1be3e3b46eb7 74 //#define WITHOUT_SPLASH
Neal Horman 7:779204e24db4 75 #define SSD_I2C_ADDRESS 0x78
Neal Horman 6:1be3e3b46eb7 76
Neal Horman 6:1be3e3b46eb7 77 #define SSD_Command_Mode 0x00
Neal Horman 6:1be3e3b46eb7 78 #define SSD_Data_Mode 0x40
Neal Horman 6:1be3e3b46eb7 79
nkhorman 0:c3dcd4c4983a 80 #define SSD1306_SETCONTRAST 0x81
nkhorman 0:c3dcd4c4983a 81 #define SSD1306_DISPLAYALLON_RESUME 0xA4
nkhorman 0:c3dcd4c4983a 82 #define SSD1306_DISPLAYALLON 0xA5
nkhorman 0:c3dcd4c4983a 83 #define SSD1306_NORMALDISPLAY 0xA6
nkhorman 0:c3dcd4c4983a 84 #define SSD1306_INVERTDISPLAY 0xA7
nkhorman 0:c3dcd4c4983a 85 #define SSD1306_DISPLAYOFF 0xAE
nkhorman 0:c3dcd4c4983a 86 #define SSD1306_DISPLAYON 0xAF
nkhorman 0:c3dcd4c4983a 87 #define SSD1306_SETDISPLAYOFFSET 0xD3
nkhorman 0:c3dcd4c4983a 88 #define SSD1306_SETCOMPINS 0xDA
nkhorman 0:c3dcd4c4983a 89 #define SSD1306_SETVCOMDETECT 0xDB
nkhorman 0:c3dcd4c4983a 90 #define SSD1306_SETDISPLAYCLOCKDIV 0xD5
nkhorman 0:c3dcd4c4983a 91 #define SSD1306_SETPRECHARGE 0xD9
nkhorman 0:c3dcd4c4983a 92 #define SSD1306_SETMULTIPLEX 0xA8
nkhorman 0:c3dcd4c4983a 93 #define SSD1306_SETLOWCOLUMN 0x00
nkhorman 0:c3dcd4c4983a 94 #define SSD1306_SETHIGHCOLUMN 0x10
nkhorman 0:c3dcd4c4983a 95 #define SSD1306_SETSTARTLINE 0x40
nkhorman 0:c3dcd4c4983a 96 #define SSD1306_MEMORYMODE 0x20
nkhorman 0:c3dcd4c4983a 97 #define SSD1306_COMSCANINC 0xC0
nkhorman 0:c3dcd4c4983a 98 #define SSD1306_COMSCANDEC 0xC8
nkhorman 0:c3dcd4c4983a 99 #define SSD1306_SEGREMAP 0xA0
nkhorman 0:c3dcd4c4983a 100 #define SSD1306_CHARGEPUMP 0x8D
nkhorman 0:c3dcd4c4983a 101 #define SSD1306_EXTERNALVCC 0x1
nkhorman 0:c3dcd4c4983a 102 #define SSD1306_SWITCHCAPVCC 0x2
nkhorman 0:c3dcd4c4983a 103
nkhorman 0:c3dcd4c4983a 104 // an DigitalOut sub-class that provides a constructed default state
nkhorman 0:c3dcd4c4983a 105 class DigitalOut2 : public DigitalOut
nkhorman 0:c3dcd4c4983a 106 {
nkhorman 0:c3dcd4c4983a 107 public:
nkhorman 3:969ca9fe5e2b 108 DigitalOut2(PinName pin, bool active = false) : DigitalOut(pin) { write(active); };
nkhorman 0:c3dcd4c4983a 109 DigitalOut2& operator= (int value) { write(value); return *this; };
nkhorman 0:c3dcd4c4983a 110 DigitalOut2& operator= (DigitalOut2& rhs) { write(rhs.read()); return *this; };
nkhorman 0:c3dcd4c4983a 111 operator int() { return read(); };
nkhorman 0:c3dcd4c4983a 112 };
nkhorman 0:c3dcd4c4983a 113
nkhorman 0:c3dcd4c4983a 114 class Adafruit_SSD1306 : public Adafruit_GFX
nkhorman 0:c3dcd4c4983a 115 {
nkhorman 0:c3dcd4c4983a 116 public:
Neal Horman 6:1be3e3b46eb7 117 #ifdef SSD_USES_SPI
nkhorman 0:c3dcd4c4983a 118 Adafruit_SSD1306(SPI &spi, PinName DC, PinName RST, PinName CS);
Neal Horman 6:1be3e3b46eb7 119 #elif defined SSD_USES_I2C
Neal Horman 6:1be3e3b46eb7 120 Adafruit_SSD1306(I2C &i2c, PinName RST);
Neal Horman 6:1be3e3b46eb7 121 #endif
nkhorman 0:c3dcd4c4983a 122 void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC);
nkhorman 0:c3dcd4c4983a 123 void ssd1306_command(uint8_t c);
nkhorman 0:c3dcd4c4983a 124 void ssd1306_data(uint8_t c);
nkhorman 0:c3dcd4c4983a 125
nkhorman 0:c3dcd4c4983a 126 void clearDisplay(void);
nkhorman 0:c3dcd4c4983a 127 virtual void invertDisplay(bool i);
nkhorman 0:c3dcd4c4983a 128 void display();
nkhorman 0:c3dcd4c4983a 129
nkhorman 0:c3dcd4c4983a 130 virtual void drawPixel(int16_t x, int16_t y, uint16_t color);
nkhorman 0:c3dcd4c4983a 131
nkhorman 0:c3dcd4c4983a 132 private:
Neal Horman 6:1be3e3b46eb7 133
Neal Horman 6:1be3e3b46eb7 134 DigitalOut2 rst;
Neal Horman 6:1be3e3b46eb7 135 #ifdef SSD_USES_SPI
nkhorman 8:416b8fe451b3 136 DigitalOut2 cs,dc;
nkhorman 0:c3dcd4c4983a 137 SPI &mspi;
Neal Horman 6:1be3e3b46eb7 138 #elif defined SSD_USES_I2C
Neal Horman 6:1be3e3b46eb7 139 I2C &mi2c;
Neal Horman 6:1be3e3b46eb7 140 #endif
nkhorman 0:c3dcd4c4983a 141 // the memory buffer for the LCD
nkhorman 0:c3dcd4c4983a 142 uint8_t buffer[SSD1306_LCDHEIGHT * SSD1306_LCDWIDTH / 8];
nkhorman 0:c3dcd4c4983a 143 };
nkhorman 0:c3dcd4c4983a 144
nkhorman 0:c3dcd4c4983a 145 #endif