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 20:55:27 2014 +0000
Revision:
9:ddb97c9850a2
Parent:
2:7bcea45e60d8
Child:
11:86909e6db3c8
c++'ify the SPI vs I2C driver portions, instead of compile time defines.; The SSD1306 driver can now be instantiated multiple times, supporting simultaneous displays of differing dimensions.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nkhorman 9:ddb97c9850a2 1 /***********************************
nkhorman 9:ddb97c9850a2 2 This is a our graphics core library, for all our displays.
nkhorman 9:ddb97c9850a2 3 We'll be adapting all the
nkhorman 9:ddb97c9850a2 4 existing libaries to use this core to make updating, support
nkhorman 9:ddb97c9850a2 5 and upgrading easier!
nkhorman 9:ddb97c9850a2 6
nkhorman 9:ddb97c9850a2 7 Adafruit invests time and resources providing this open source code,
nkhorman 9:ddb97c9850a2 8 please support Adafruit and open-source hardware by purchasing
nkhorman 9:ddb97c9850a2 9 products from Adafruit!
nkhorman 9:ddb97c9850a2 10
nkhorman 9:ddb97c9850a2 11 Written by Limor Fried/Ladyada for Adafruit Industries.
nkhorman 9:ddb97c9850a2 12 BSD license, check license.txt for more information
nkhorman 9:ddb97c9850a2 13 All text above must be included in any redistribution
nkhorman 9:ddb97c9850a2 14 ****************************************/
nkhorman 9:ddb97c9850a2 15
nkhorman 9:ddb97c9850a2 16 /*
nkhorman 9:ddb97c9850a2 17 * Modified by Neal Horman 7/14/2012 for use in mbed
nkhorman 9:ddb97c9850a2 18 */
nkhorman 9:ddb97c9850a2 19
nkhorman 9:ddb97c9850a2 20 #ifndef _ADAFRUIT_GFX_H_
nkhorman 9:ddb97c9850a2 21 #define _ADAFRUIT_GFX_H_
nkhorman 9:ddb97c9850a2 22
nkhorman 9:ddb97c9850a2 23 // Uncomment this on to enable all functionality
nkhorman 9:ddb97c9850a2 24 //#define GFX_WANT_ABSTRACTS
nkhorman 9:ddb97c9850a2 25
nkhorman 9:ddb97c9850a2 26 // Uncomment this to enable only runtime font scaling, without all the rest of the Abstracts
nkhorman 9:ddb97c9850a2 27 //#define GFX_SIZEABLE_TEXT
nkhorman 9:ddb97c9850a2 28
nkhorman 9:ddb97c9850a2 29 static inline void swap(int16_t &a, int16_t &b)
nkhorman 9:ddb97c9850a2 30 {
nkhorman 9:ddb97c9850a2 31 int16_t t = a;
nkhorman 9:ddb97c9850a2 32
nkhorman 9:ddb97c9850a2 33 a = b;
nkhorman 9:ddb97c9850a2 34 b = t;
nkhorman 9:ddb97c9850a2 35 }
nkhorman 9:ddb97c9850a2 36
nkhorman 9:ddb97c9850a2 37 #ifndef _BV
nkhorman 9:ddb97c9850a2 38 #define _BV(bit) (1<<(bit))
nkhorman 9:ddb97c9850a2 39 #endif
nkhorman 9:ddb97c9850a2 40
nkhorman 9:ddb97c9850a2 41 #define BLACK 0
nkhorman 9:ddb97c9850a2 42 #define WHITE 1
nkhorman 9:ddb97c9850a2 43
nkhorman 9:ddb97c9850a2 44 class Adafruit_GFX : public Stream
nkhorman 9:ddb97c9850a2 45 {
nkhorman 9:ddb97c9850a2 46 public:
nkhorman 9:ddb97c9850a2 47 Adafruit_GFX(int16_t w, int16_t h)
nkhorman 9:ddb97c9850a2 48 : _rawWidth(w)
nkhorman 9:ddb97c9850a2 49 , _rawHeight(h)
nkhorman 9:ddb97c9850a2 50 , _width(w)
nkhorman 9:ddb97c9850a2 51 , _height(h)
nkhorman 9:ddb97c9850a2 52 , cursor_x(0)
nkhorman 9:ddb97c9850a2 53 , cursor_y(0)
nkhorman 9:ddb97c9850a2 54 , textcolor(WHITE)
nkhorman 9:ddb97c9850a2 55 , textbgcolor(BLACK)
nkhorman 9:ddb97c9850a2 56 , textsize(1)
nkhorman 9:ddb97c9850a2 57 , rotation(0)
nkhorman 9:ddb97c9850a2 58 , wrap(true)
nkhorman 9:ddb97c9850a2 59 {};
nkhorman 9:ddb97c9850a2 60
nkhorman 9:ddb97c9850a2 61 // this must be defined by the subclass
nkhorman 9:ddb97c9850a2 62 virtual void drawPixel(int16_t x, int16_t y, uint16_t color) = 0;
nkhorman 9:ddb97c9850a2 63 // this is optional
nkhorman 9:ddb97c9850a2 64 virtual void invertDisplay(bool i) {};
nkhorman 9:ddb97c9850a2 65
nkhorman 9:ddb97c9850a2 66 // Stream implementation - provides printf() interface
nkhorman 9:ddb97c9850a2 67 // You would otherwise be forced to use writeChar()
nkhorman 9:ddb97c9850a2 68 virtual int _putc(int value) { return writeChar(value); };
nkhorman 9:ddb97c9850a2 69 virtual int _getc() { return -1; };
nkhorman 9:ddb97c9850a2 70
nkhorman 9:ddb97c9850a2 71 #ifdef GFX_WANT_ABSTRACTS
nkhorman 9:ddb97c9850a2 72 // these are 'generic' drawing functions, so we can share them!
nkhorman 9:ddb97c9850a2 73 virtual void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
nkhorman 9:ddb97c9850a2 74 virtual void drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);
nkhorman 9:ddb97c9850a2 75 virtual void fillScreen(uint16_t color);
nkhorman 9:ddb97c9850a2 76
nkhorman 9:ddb97c9850a2 77 void drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color);
nkhorman 9:ddb97c9850a2 78 void drawCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, uint16_t color);
nkhorman 9:ddb97c9850a2 79 void fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color);
nkhorman 9:ddb97c9850a2 80 void fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, int16_t delta, uint16_t color);
nkhorman 9:ddb97c9850a2 81
nkhorman 9:ddb97c9850a2 82 void drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color);
nkhorman 9:ddb97c9850a2 83 void fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color);
nkhorman 9:ddb97c9850a2 84 void drawRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius, uint16_t color);
nkhorman 9:ddb97c9850a2 85 void fillRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius, uint16_t color);
nkhorman 9:ddb97c9850a2 86 #endif
nkhorman 9:ddb97c9850a2 87
nkhorman 9:ddb97c9850a2 88 #if defined(GFX_WANT_ABSTRACTS) || defined(GFX_SIZEABLE_TEXT)
nkhorman 9:ddb97c9850a2 89 virtual void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color);
nkhorman 9:ddb97c9850a2 90 virtual void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
nkhorman 9:ddb97c9850a2 91 virtual void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);
nkhorman 9:ddb97c9850a2 92 #endif
nkhorman 9:ddb97c9850a2 93
nkhorman 9:ddb97c9850a2 94 void drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color);
nkhorman 9:ddb97c9850a2 95 void drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg, uint8_t size);
nkhorman 9:ddb97c9850a2 96 size_t writeChar(uint8_t);
nkhorman 9:ddb97c9850a2 97
nkhorman 9:ddb97c9850a2 98 int16_t width(void) { return _width; };
nkhorman 9:ddb97c9850a2 99 int16_t height(void) { return _height; };
nkhorman 9:ddb97c9850a2 100
nkhorman 9:ddb97c9850a2 101 void setTextCursor(int16_t x, int16_t y) { cursor_x = x; cursor_y = y; };
nkhorman 9:ddb97c9850a2 102 #if defined(GFX_SIZEABLE_TEXT)
nkhorman 9:ddb97c9850a2 103 void setTextSize(uint8_t s) { textsize = (s > 0) ? s : 1; };
nkhorman 9:ddb97c9850a2 104 #endif
nkhorman 9:ddb97c9850a2 105 void setTextColor(uint16_t c) { textcolor = c; textbgcolor = c; }
nkhorman 9:ddb97c9850a2 106 void setTextColor(uint16_t c, uint16_t b) { textcolor = c; textbgcolor = b; };
nkhorman 9:ddb97c9850a2 107 void setTextWrap(bool w) { wrap = w; };
nkhorman 9:ddb97c9850a2 108
nkhorman 9:ddb97c9850a2 109 void setRotation(uint8_t r);
nkhorman 9:ddb97c9850a2 110 uint8_t getRotation(void) { rotation %= 4; return rotation; };
nkhorman 9:ddb97c9850a2 111
nkhorman 9:ddb97c9850a2 112 protected:
nkhorman 9:ddb97c9850a2 113 int16_t _rawWidth, _rawHeight; // this is the 'raw' display w/h - never changes
nkhorman 9:ddb97c9850a2 114 int16_t _width, _height; // dependent on rotation
nkhorman 9:ddb97c9850a2 115 int16_t cursor_x, cursor_y;
nkhorman 9:ddb97c9850a2 116 uint16_t textcolor, textbgcolor;
nkhorman 9:ddb97c9850a2 117 uint8_t textsize;
nkhorman 9:ddb97c9850a2 118 uint8_t rotation;
nkhorman 9:ddb97c9850a2 119 bool wrap; // If set, 'wrap' text at right edge of display
nkhorman 9:ddb97c9850a2 120 };
nkhorman 9:ddb97c9850a2 121
nkhorman 9:ddb97c9850a2 122 #endif