Deleted Adafruit Llgo

Dependents:   GPS_OLED_HelloWorld WIZwiki-REST-io_v101 WIZwiki-REST-io_v102 WIZwiki-REST-io_v103

Fork of Adafruit_GFX by DongEun Koak

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     
00077 protected:
00078     virtual void sendDisplayBuffer() = 0;
00079     DigitalOut2 rst;
00080 
00081     // the memory buffer for the LCD
00082     std::vector<uint8_t> buffer;
00083 };
00084 
00085 
00086 /** This is the SPI SSD1306 display driver transport class
00087  *
00088  */
00089 class Adafruit_SSD1306_Spi : public Adafruit_SSD1306
00090 {
00091 public:
00092     /** Create a SSD1306 SPI transport display driver instance with the specified DC, RST, and CS pins, as well as the display dimentions
00093      *
00094      * Required parameters
00095      * @param spi - a reference to an initialized SPI object
00096      * @param DC (Data/Command) pin name
00097      * @param RST (Reset) pin name
00098      * @param CS (Chip Select) pin name
00099      *
00100      * Optional parameters
00101      * @param rawHeight - the vertical number of pixels for the display, defaults to 32
00102      * @param rawWidth - the horizonal number of pixels for the display, defaults to 128
00103      */
00104     Adafruit_SSD1306_Spi(SPI &spi, PinName DC, PinName RST, PinName CS, uint8_t rawHieght = 32, uint8_t rawWidth = 128)
00105         : Adafruit_SSD1306(RST, rawHieght, rawWidth)
00106         , cs(CS,true)
00107         , dc(DC,false)
00108         , mspi(spi)
00109         {
00110             begin();
00111             splash();
00112             display();
00113         };
00114 
00115     virtual void command(uint8_t c)
00116     {
00117         cs = 1;
00118         dc = 0;
00119         cs = 0;
00120         mspi.write(c);
00121         cs = 1;
00122     };
00123 
00124     virtual void data(uint8_t c)
00125     {
00126         cs = 1;
00127         dc = 1;
00128         cs = 0;
00129         mspi.write(c);
00130         cs = 1;
00131     };
00132 
00133 protected:
00134     virtual void sendDisplayBuffer()
00135     {
00136         cs = 1;
00137         dc = 1;
00138         cs = 0;
00139 
00140         for(uint16_t i=0, q=buffer.size(); i<q; i++)
00141             mspi.write(buffer[i]);
00142 
00143         if(height() == 32)
00144         {
00145             for(uint16_t i=0, q=buffer.size(); i<q; i++)
00146                 mspi.write(0);
00147         }
00148 
00149         cs = 1;
00150     };
00151 
00152     DigitalOut2 cs, dc;
00153     SPI &mspi;
00154 };
00155 
00156 /** This is the I2C SSD1306 display driver transport class
00157  *
00158  */
00159 class Adafruit_SSD1306_I2c : public Adafruit_SSD1306
00160 {
00161 public:
00162     #define SSD_I2C_ADDRESS     0x78
00163     /** Create a SSD1306 I2C transport display driver instance with the specified RST pin name, the I2C address, as well as the display dimensions
00164      *
00165      * Required parameters
00166      * @param i2c - A reference to an initialized I2C object
00167      * @param RST - The Reset pin name
00168      *
00169      * Optional parameters
00170      * @param i2cAddress - The i2c address of the display
00171      * @param rawHeight - The vertical number of pixels for the display, defaults to 32
00172      * @param rawWidth - The horizonal number of pixels for the display, defaults to 128
00173      */
00174     Adafruit_SSD1306_I2c(I2C &i2c, PinName RST, uint8_t i2cAddress = SSD_I2C_ADDRESS, uint8_t rawHeight = 32, uint8_t rawWidth = 128)
00175         : Adafruit_SSD1306(RST, rawHeight, rawWidth)
00176         , mi2c(i2c)
00177         , mi2cAddress(i2cAddress)
00178         {
00179             begin();
00180           //  splash();
00181             display();
00182         };
00183 
00184     virtual void command(uint8_t c)
00185     {
00186         char buff[2];
00187         buff[0] = 0; // Command Mode
00188         buff[1] = c;
00189         mi2c.write(mi2cAddress, buff, sizeof(buff));
00190     }
00191 
00192     virtual void data(uint8_t c)
00193     {
00194         char buff[2];
00195         buff[0] = 0x40; // Data Mode
00196         buff[1] = c;
00197         mi2c.write(mi2cAddress, buff, sizeof(buff));
00198     };
00199 
00200 protected:
00201     virtual void sendDisplayBuffer()
00202     {
00203         char buff[17];
00204         buff[0] = 0x40; // Data Mode
00205 
00206         // send display buffer in 16 byte chunks
00207         for(uint16_t i=0, q=buffer.size(); i<q; i+=16 ) 
00208         {   uint8_t x ;
00209 
00210             // TODO - this will segfault if buffer.size() % 16 != 0
00211             for(x=1; x<sizeof(buff); x++) 
00212                 buff[x] = buffer[i+x-1];
00213             mi2c.write(mi2cAddress, buff, sizeof(buff));
00214         }
00215     };
00216 
00217     I2C &mi2c;
00218     uint8_t mi2cAddress;
00219 };
00220 
00221 #endif