Adafruit OLED Libary

Dependents:   SSD1306_LCD-HelloWorld WizFi250-Geolocation_NTP Tweeting_Machine_HelloWorld_WIZwiki-W7500 display_irita ... more

Fork of Adafruit_GFX by Neal Horman

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Adafruit_SSD1306.h Source File

Adafruit_SSD1306.h

00001 /*********************************************************************
00002 This is a library for our Monochrome OLEDs based on SSD1306 drivers
00003 
00004   Pick one up today in the adafruit shop!
00005   ------> http://www.adafruit.com/category/63_98
00006 
00007 These displays use SPI to communicate, 4 or 5 pins are required to  
00008 interface
00009 
00010 Adafruit invests time and resources providing this open source code, 
00011 please support Adafruit and open-source hardware by purchasing 
00012 products from Adafruit!
00013 
00014 Written by Limor Fried/Ladyada  for Adafruit Industries.  
00015 BSD license, check license.txt for more information
00016 All text above, and the splash screen must be included in any redistribution
00017 *********************************************************************/
00018 
00019 /*
00020  *  Modified by Neal Horman 7/14/2012 for use in mbed
00021  */
00022 
00023 #ifndef _ADAFRUIT_SSD1306_H_
00024 #define _ADAFRUIT_SSD1306_H_
00025 
00026 #include "mbed.h"
00027 #include "Adafruit_GFX.h"
00028 
00029 #include <vector>
00030 #include <algorithm>
00031 
00032 // A DigitalOut sub-class that provides a constructed default state
00033 class DigitalOut2 : public DigitalOut
00034 {
00035 public:
00036     DigitalOut2(PinName pin, bool active = false) : DigitalOut(pin) { write(active); };
00037     DigitalOut2& operator= (int value) { write(value); return *this; };
00038     DigitalOut2& operator= (DigitalOut2& rhs) { write(rhs.read()); return *this; };
00039     operator int() { return read(); };
00040 };
00041 
00042 #define SSD1306_EXTERNALVCC 0x1
00043 #define SSD1306_SWITCHCAPVCC 0x2
00044 
00045 /** The pure base class for the SSD1306 display driver.
00046  *
00047  * You should derive from this for a new transport interface type,
00048  * such as the SPI and I2C drivers.
00049  */
00050 class Adafruit_SSD1306 : public Adafruit_GFX
00051 {
00052 public:
00053     Adafruit_SSD1306(PinName RST, uint8_t rawHeight = 32, uint8_t rawWidth = 128)
00054         : Adafruit_GFX(rawWidth,rawHeight)
00055         , rst(RST,false)
00056     {
00057         buffer.resize(rawHeight * rawWidth / 8);
00058     };
00059 
00060     void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC);
00061     
00062     // These must be implemented in the derived transport driver
00063     virtual void command(uint8_t c) = 0;
00064     virtual void data(uint8_t c) = 0;
00065     virtual void drawPixel(int16_t x, int16_t y, uint16_t color);
00066 
00067     /// Clear the display buffer    
00068     void clearDisplay(void);
00069     virtual void invertDisplay(bool i);
00070 
00071     /// Cause the display to be updated with the buffer content.
00072     void display();
00073     /// Fill the buffer with the AdaFruit splash screen.
00074     virtual void splash();
00075     
00076 protected:
00077     virtual void sendDisplayBuffer() = 0;
00078     DigitalOut2 rst;
00079 
00080     // the memory buffer for the LCD
00081     std::vector<uint8_t> buffer;
00082 };
00083 
00084 
00085 /** This is the SPI SSD1306 display driver transport class
00086  *
00087  */
00088 class Adafruit_SSD1306_Spi : public Adafruit_SSD1306
00089 {
00090 public:
00091     /** Create a SSD1306 SPI transport display driver instance with the specified DC, RST, and CS pins, as well as the display dimentions
00092      *
00093      * Required parameters
00094      * @param spi - a reference to an initialized SPI object
00095      * @param DC (Data/Command) pin name
00096      * @param RST (Reset) pin name
00097      * @param CS (Chip Select) pin name
00098      *
00099      * Optional parameters
00100      * @param rawHeight - the vertical number of pixels for the display, defaults to 32
00101      * @param rawWidth - the horizonal number of pixels for the display, defaults to 128
00102      */
00103     Adafruit_SSD1306_Spi(SPI &spi, PinName DC, PinName RST, PinName CS, uint8_t rawHieght = 32, uint8_t rawWidth = 128)
00104         : Adafruit_SSD1306(RST, rawHieght, rawWidth)
00105         , cs(CS,true)
00106         , dc(DC,false)
00107         , mspi(spi)
00108         {
00109             begin();
00110             splash();
00111             display();
00112         };
00113 
00114     virtual void command(uint8_t c)
00115     {
00116         cs = 1;
00117         dc = 0;
00118         cs = 0;
00119         mspi.write(c);
00120         cs = 1;
00121     };
00122 
00123     virtual void data(uint8_t c)
00124     {
00125         cs = 1;
00126         dc = 1;
00127         cs = 0;
00128         mspi.write(c);
00129         cs = 1;
00130     };
00131 
00132 protected:
00133     virtual void sendDisplayBuffer()
00134     {
00135         cs = 1;
00136         dc = 1;
00137         cs = 0;
00138 
00139         for(uint16_t i=0, q=buffer.size(); i<q; i++)
00140             mspi.write(buffer[i]);
00141 
00142         if(height() == 32)
00143         {
00144             for(uint16_t i=0, q=buffer.size(); i<q; i++)
00145                 mspi.write(0);
00146         }
00147 
00148         cs = 1;
00149     };
00150 
00151     DigitalOut2 cs, dc;
00152     SPI &mspi;
00153 };
00154 
00155 /** This is the I2C SSD1306 display driver transport class
00156  *
00157  */
00158 class Adafruit_SSD1306_I2c : public Adafruit_SSD1306
00159 {
00160 public:
00161     #define SSD_I2C_ADDRESS     0x78
00162     /** Create a SSD1306 I2C transport display driver instance with the specified RST pin name, the I2C address, as well as the display dimensions
00163      *
00164      * Required parameters
00165      * @param i2c - A reference to an initialized I2C object
00166      * @param RST - The Reset pin name
00167      *
00168      * Optional parameters
00169      * @param i2cAddress - The i2c address of the display
00170      * @param rawHeight - The vertical number of pixels for the display, defaults to 32
00171      * @param rawWidth - The horizonal number of pixels for the display, defaults to 128
00172      */
00173     Adafruit_SSD1306_I2c(I2C &i2c, PinName RST, uint8_t i2cAddress = SSD_I2C_ADDRESS, uint8_t rawHeight = 32, uint8_t rawWidth = 128)
00174         : Adafruit_SSD1306(RST, rawHeight, rawWidth)
00175         , mi2c(i2c)
00176         , mi2cAddress(i2cAddress)
00177         {
00178             begin();
00179             splash();
00180             display();
00181         };
00182 
00183     virtual void command(uint8_t c)
00184     {
00185         char buff[2];
00186         buff[0] = 0; // Command Mode
00187         buff[1] = c;
00188         mi2c.write(mi2cAddress, buff, sizeof(buff));
00189     }
00190 
00191     virtual void data(uint8_t c)
00192     {
00193         char buff[2];
00194         buff[0] = 0x40; // Data Mode
00195         buff[1] = c;
00196         mi2c.write(mi2cAddress, buff, sizeof(buff));
00197     };
00198 
00199 protected:
00200     virtual void sendDisplayBuffer()
00201     {
00202         char buff[17];
00203         buff[0] = 0x40; // Data Mode
00204 
00205         // send display buffer in 16 byte chunks
00206         for(uint16_t i=0, q=buffer.size(); i<q; i+=16 ) 
00207         {   uint8_t x ;
00208 
00209             // TODO - this will segfault if buffer.size() % 16 != 0
00210             for(x=1; x<sizeof(buff); x++) 
00211                 buff[x] = buffer[i+x-1];
00212             mi2c.write(mi2cAddress, buff, sizeof(buff));
00213         }
00214     };
00215 
00216     I2C &mi2c;
00217     uint8_t mi2cAddress;
00218 };
00219 
00220 #endif