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:
11:86909e6db3c8
Parent:
9:ddb97c9850a2
Child:
13:8f03f908f22a
--- a/Adafruit_GFX.h	Sun Oct 19 20:55:27 2014 +0000
+++ b/Adafruit_GFX.h	Tue Oct 21 02:04:08 2014 +0000
@@ -41,6 +41,15 @@
 #define BLACK 0
 #define WHITE 1
 
+/**
+ * This is a Text and Graphics element drawing class.
+ * These functions draw to the display buffer.
+ *
+ * Display drivers should be derived from here.
+ * The Display drivers push the display buffer to the
+ * hardware based on application control.
+ *
+ */
 class Adafruit_GFX : public Stream
 {
  public:
@@ -58,6 +67,7 @@
         , wrap(true)
         {};
 
+    /// Paint one BLACK or WHITE pixel in the display buffer
     // this must be defined by the subclass
     virtual void drawPixel(int16_t x, int16_t y, uint16_t color) = 0;
     // this is optional
@@ -70,43 +80,96 @@
 
 #ifdef GFX_WANT_ABSTRACTS
     // these are 'generic' drawing functions, so we can share them!
+    
+    /** Draw a Horizontal Line
+     * @note GFX_WANT_ABSTRACTS must be defined in Adafruit_GFX_config.h
+     */
     virtual void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
+    /** Draw a rectangle
+     * @note GFX_WANT_ABSTRACTS must be defined in Adafruit_GFX_config.h
+     */
     virtual void drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);
+    /** Fill the entire display
+     * @note GFX_WANT_ABSTRACTS must be defined in Adafruit_GFX_config.h
+     */
     virtual void fillScreen(uint16_t color);
 
+    /** Draw a circle
+     * @note GFX_WANT_ABSTRACTS must be defined in Adafruit_GFX_config.h
+     */
     void drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color);
     void drawCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, uint16_t color);
+    
+    /** Draw and fill a circle
+     * @note GFX_WANT_ABSTRACTS must be defined in Adafruit_GFX_config.h
+     */
     void fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color);
     void fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, int16_t delta, uint16_t color);
 
+    /** Draw a triangle
+     * @note GFX_WANT_ABSTRACTS must be defined in Adafruit_GFX_config.h
+     */
     void drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color);
+    /** Draw and fill a triangle
+     * @note GFX_WANT_ABSTRACTS must be defined in Adafruit_GFX_config.h
+     */
     void fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color);
+    
+    /** Draw a rounded rectangle
+     * @note GFX_WANT_ABSTRACTS must be defined in Adafruit_GFX_config.h
+     */
     void drawRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius, uint16_t color);
+    /** Draw and fill a rounded rectangle
+     * @note GFX_WANT_ABSTRACTS must be defined in Adafruit_GFX_config.h
+     */
     void fillRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius, uint16_t color);
 #endif
 
 #if defined(GFX_WANT_ABSTRACTS) || defined(GFX_SIZEABLE_TEXT)
+    /** Draw a line
+     * @note GFX_WANT_ABSTRACTS or GFX_SIZEABLE_TEXT must be defined in Adafruit_GFX_config.h
+     */
     virtual void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color);
+    /** Draw a vertical line
+     * @note GFX_WANT_ABSTRACTS or GFX_SIZEABLE_TEXT must be defined in Adafruit_GFX_config.h
+     */
     virtual void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
+    /** Draw and fill a rectangle
+     * @note GFX_WANT_ABSTRACTS or GFX_SIZEABLE_TEXT must be defined in Adafruit_GFX_config.h
+     */
     virtual void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);
 #endif
 
+    /// Draw a bitmap
     void drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color);
+    /// Draw a text character at a specified pixel location
     void drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg, uint8_t size);
+    /// Draw a text character at the text cursor location
     size_t writeChar(uint8_t);
 
+    /// Get the width of the display in pixels
     int16_t width(void) { return _width; };
+    /// Get the height of the display in pixels
     int16_t height(void) { return _height; };
 
+    /// Set the text cursor location, based on the size of the text
     void setTextCursor(int16_t x, int16_t y) { cursor_x = x; cursor_y = y; };
 #if defined(GFX_SIZEABLE_TEXT)
+    /** Set the size of the text to be drawn
+     * @note Make sure to enable either GFX_SIZEABLE_TEXT or GFX_WANT_ABSTRACTS
+     */
     void setTextSize(uint8_t s) { textsize = (s > 0) ? s : 1; };
 #endif
+    /// Set the text foreground and background colors to be the same
     void setTextColor(uint16_t c) { textcolor = c; textbgcolor = c; }
+    /// Set the text foreground and background colors independantly
     void setTextColor(uint16_t c, uint16_t b) { textcolor = c; textbgcolor = b; };
+    /// Set text wraping mode true or false
     void setTextWrap(bool w) { wrap = w; };
 
+    /// Set the display rotation, 1, 2, 3, or 4
     void setRotation(uint8_t r);
+    /// Get the current rotation
     uint8_t getRotation(void) { rotation %= 4; return rotation; };
 
 protected: