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 Aug 19 23:03:40 2012 +0000
Revision:
2:7bcea45e60d8
Parent:
1:9db12505727a
Child:
9:ddb97c9850a2
change swap() from a define to a static inline function to quell compiler problems.

Who changed what in which revision?

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