Fork of Adafruit OLED driver to debug integration with RedBear Nano SPI driver

Dependents:   SPI_GFX_nano

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 #include "spi_master.h"
00029 
00030 #include <vector>
00031 #include <algorithm>
00032 
00033 // A DigitalOut sub-class that provides a constructed default state
00034 class DigitalOut2 : public DigitalOut
00035 {
00036 public:
00037     DigitalOut2(PinName pin, bool active = false) : DigitalOut(pin) { write(active); };
00038     DigitalOut2& operator= (int value) { write(value); return *this; };
00039     DigitalOut2& operator= (DigitalOut2& rhs) { write(rhs.read()); return *this; };
00040     operator int() { return read(); };
00041 };
00042 
00043 #define SSD1306_EXTERNALVCC 0x1
00044 #define SSD1306_SWITCHCAPVCC 0x2
00045 
00046 /** The pure base class for the SSD1306 display driver.
00047  *
00048  * You should derive from this for a new transport interface type,
00049  * such as the SPI and I2C drivers.
00050  */
00051 class Adafruit_SSD1306 : public Adafruit_GFX
00052 {
00053 public:
00054     Adafruit_SSD1306(PinName RST, uint8_t rawHeight = 32, uint8_t rawWidth = 128)
00055         : Adafruit_GFX(rawWidth,rawHeight)
00056         , rst(RST,false)
00057     {
00058         buffer.resize(rawHeight * rawWidth / 8);
00059     };
00060 
00061     void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC);
00062     void configure(uint8_t switchvcc = SSD1306_SWITCHCAPVCC);   
00063     
00064     // These must be implemented in the derived transport driver
00065     virtual void command(uint8_t c) = 0;
00066     virtual void data(uint8_t c) = 0;
00067     virtual void drawPixel(int16_t x, int16_t y, uint16_t color);
00068 
00069     /// Clear the display buffer    
00070     void clearDisplay(void);
00071     virtual void invertDisplay(bool i);
00072 
00073     /// Cause the display to be updated with the buffer content.
00074     void display();
00075     /// Fill the buffer with the AdaFruit splash screen.
00076     virtual void splash();
00077     
00078 protected:
00079     virtual void sendDisplayBuffer() = 0;
00080     DigitalOut2 rst;
00081 
00082     // the memory buffer for the LCD
00083     std::vector<uint8_t> buffer;
00084 };
00085 
00086 
00087 /** This is the SPI SSD1306 display driver transport class
00088  *
00089  */
00090 class Adafruit_SSD1306_Spi : public Adafruit_SSD1306
00091 {
00092 public:
00093     /** Create a SSD1306 SPI transport display driver instance with the specified DC, RST, and CS pins, as well as the display dimentions
00094      *
00095      * Required parameters
00096      * @param spi - a reference to an initialized SPI object
00097      * @param DC (Data/Command) pin name
00098      * @param RST (Reset) pin name
00099      * @param CS (Chip Select) pin name
00100      *
00101      * Optional parameters
00102      * @param rawHeight - the vertical number of pixels for the display, defaults to 32
00103      * @param rawWidth - the horizonal number of pixels for the display, defaults to 128
00104      */
00105     Adafruit_SSD1306_Spi(SPI &spi, PinName DC, PinName RST, PinName CS, uint8_t rawHieght = 32, uint8_t rawWidth = 128)
00106         : Adafruit_SSD1306(RST, rawHieght, rawWidth)
00107         , cs(CS,true) // Initialise set
00108         , dc(DC,false)// Initialise clear
00109         , mspi(spi)
00110         {
00111             begin();
00112             splash();
00113             display();
00114         };
00115 
00116     virtual void command(uint8_t c)
00117     {
00118         cs = 1;
00119         dc = 0;
00120         cs = 0;
00121         mspi.write(c);
00122         cs = 1;
00123     };
00124 
00125     virtual void data(uint8_t c)
00126     {
00127         cs = 1;
00128         dc = 1;
00129         cs = 0;
00130         mspi.write(c);
00131         cs = 1;
00132     };
00133 
00134 protected:
00135     virtual void sendDisplayBuffer()
00136     {
00137         cs = 1;
00138         dc = 1;
00139         cs = 0;
00140 
00141         for(uint16_t i=0, q=buffer.size(); i<q; i++)
00142             mspi.write(buffer[i]);
00143 
00144         if(height() == 32)
00145         {
00146             for(uint16_t i=0, q=buffer.size(); i<q; i++)
00147                 mspi.write(0);
00148         }
00149 
00150         cs = 1;
00151     };
00152 
00153     DigitalOut2 cs, dc;
00154     SPI &mspi;
00155 };
00156 
00157 /** This is the I2C SSD1306 display driver transport class
00158  *
00159  */
00160 class Adafruit_SSD1306_I2c : public Adafruit_SSD1306
00161 {
00162 public:
00163     #define SSD_I2C_ADDRESS     0x78
00164     /** Create a SSD1306 I2C transport display driver instance with the specified RST pin name, the I2C address, as well as the display dimensions
00165      *
00166      * Required parameters
00167      * @param i2c - A reference to an initialized I2C object
00168      * @param RST - The Reset pin name
00169      *
00170      * Optional parameters
00171      * @param i2cAddress - The i2c address of the display
00172      * @param rawHeight - The vertical number of pixels for the display, defaults to 32
00173      * @param rawWidth - The horizonal number of pixels for the display, defaults to 128
00174      */
00175     Adafruit_SSD1306_I2c(I2C &i2c, PinName RST, uint8_t i2cAddress = SSD_I2C_ADDRESS, uint8_t rawHeight = 32, uint8_t rawWidth = 128)
00176         : Adafruit_SSD1306(RST, rawHeight, rawWidth)
00177         , mi2c(i2c)
00178         , mi2cAddress(i2cAddress)
00179         {
00180             begin();
00181             splash();
00182             display();
00183         };
00184 
00185     virtual void command(uint8_t c)
00186     {
00187         char buff[2];
00188         buff[0] = 0; // Command Mode
00189         buff[1] = c;
00190         mi2c.write(mi2cAddress, buff, sizeof(buff));
00191     }
00192 
00193     virtual void data(uint8_t c)
00194     {
00195         char buff[2];
00196         buff[0] = 0x40; // Data Mode
00197         buff[1] = c;
00198         mi2c.write(mi2cAddress, buff, sizeof(buff));
00199     };
00200 
00201 protected:
00202     virtual void sendDisplayBuffer()
00203     {
00204         char buff[17];
00205         buff[0] = 0x40; // Data Mode
00206 
00207         // send display buffer in 16 byte chunks
00208         for(uint16_t i=0, q=buffer.size(); i<q; i+=16 ) 
00209         {   uint8_t x ;
00210 
00211             // TODO - this will segfault if buffer.size() % 16 != 0
00212             for(x=1; x<sizeof(buff); x++) 
00213                 buff[x] = buffer[i+x-1];
00214             mi2c.write(mi2cAddress, buff, sizeof(buff));
00215         }
00216     };
00217 
00218     I2C &mi2c;
00219     uint8_t mi2cAddress;
00220 };
00221 /** This is the SPI SSD1306 display driver transport class
00222  *
00223  */
00224 class Adafruit_SSD1306_nrf : public Adafruit_SSD1306
00225 {
00226 public:
00227     /** Create a SSD1306 SPI transport display driver instance with the specified DC, RST, and CS pins, as well as the display dimentions
00228      *
00229      * Required parameters
00230      * @param spi - nRF SPI object
00231      * @param DC (Data/Command) pin name
00232      * @param RST (Reset) pin name
00233      * @param CS (Chip Select) pin name
00234      *
00235      * Optional parameters
00236      * @param rawHeight - the vertical number of pixels for the display, defaults to 32
00237      * @param rawWidth - the horizonal number of pixels for the display, defaults to 128
00238      */
00239     Adafruit_SSD1306_nrf(PinName DC, PinName RST, PinName CS, uint8_t rawHieght = 32, uint8_t rawWidth = 128)
00240         : Adafruit_SSD1306(RST, rawHieght, rawWidth)
00241         , cs(CS,true)
00242         , dc(DC,false)
00243         , mspi(NRF_SPI1)
00244         {
00245               mspi.begin(P0_8, P0_9, P0_11);
00246               begin();
00247         //    splash();
00248         //    display();
00249         };
00250 
00251     virtual void command(uint8_t c)
00252     {
00253         cs = 1;
00254         dc = 0;
00255         cs = 0;
00256         mspi.transfer(c);
00257         cs = 1;
00258     };
00259 
00260     virtual void data(uint8_t c)
00261     {
00262         cs = 1;
00263         dc = 1;
00264         cs = 0;
00265         mspi.transfer(c);
00266         cs = 1;
00267     };
00268 
00269 protected:
00270     virtual void sendDisplayBuffer()
00271     {
00272         cs = 1;
00273         dc = 1;
00274         cs = 0;
00275 
00276         for(uint16_t i=0, q=buffer.size(); i<q; i++)
00277             mspi.transfer(buffer[i]);
00278 
00279         if(height() == 32)
00280         {
00281             for(uint16_t i=0, q=buffer.size(); i<q; i++)
00282                 mspi.transfer(0);
00283         }
00284 
00285         cs = 1;
00286     };
00287 
00288     DigitalOut2 cs, dc;
00289     SPIClass mspi;
00290 };
00291 #endif