Sam Shi / Adafruit_GFX_customizedfont

Dependents:   unytefont

Fork of Adafruit_GFX by Neal Horman

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Adafruit_SSD1306.cpp Source File

Adafruit_SSD1306.cpp

00001 /*********************************************************************
00002 This is a library for our Monochrome OLEDs based on SSD1306 drivers
00003   Pick one up today in the adafruit shop!
00004   ------> http://www.adafruit.com/category/63_98
00005 These displays use SPI to communicate, 4 or 5 pins are required to  
00006 interface
00007 Adafruit invests time and resources providing this open source code, 
00008 please support Adafruit and open-source hardware by purchasing 
00009 products from Adafruit!
00010 Written by Limor Fried/Ladyada  for Adafruit Industries.  
00011 BSD license, check license.txt for more information
00012 All text above, and the splash screen below must be included in any redistribution
00013 *********************************************************************/
00014 
00015 /*
00016  *  Modified by Neal Horman 7/14/2012 for use in mbed
00017  */
00018 
00019 #include "mbed.h"
00020 #include "Adafruit_SSD1306.h"
00021 
00022 #define SSD1306_SETCONTRAST 0x81
00023 #define SSD1306_DISPLAYALLON_RESUME 0xA4
00024 #define SSD1306_DISPLAYALLON 0xA5
00025 #define SSD1306_NORMALDISPLAY 0xA6
00026 #define SSD1306_INVERTDISPLAY 0xA7
00027 #define SSD1306_DISPLAYOFF 0xAE
00028 #define SSD1306_DISPLAYON 0xAF
00029 #define SSD1306_SETDISPLAYOFFSET 0xD3
00030 #define SSD1306_SETCOMPINS 0xDA
00031 #define SSD1306_SETVCOMDETECT 0xDB
00032 #define SSD1306_SETDISPLAYCLOCKDIV 0xD5
00033 #define SSD1306_SETPRECHARGE 0xD9
00034 #define SSD1306_SETMULTIPLEX 0xA8
00035 #define SSD1306_SETLOWCOLUMN 0x00
00036 #define SSD1306_SETHIGHCOLUMN 0x10
00037 #define SSD1306_SETSTARTLINE 0x40
00038 #define SSD1306_MEMORYMODE 0x20
00039 #define SSD1306_COMSCANINC 0xC0
00040 #define SSD1306_COMSCANDEC 0xC8
00041 #define SSD1306_SEGREMAP 0xA0
00042 #define SSD1306_CHARGEPUMP 0x8D
00043 
00044 void Adafruit_SSD1306::begin(uint8_t vccstate)
00045 {
00046     rst = 1;
00047     // VDD (3.3V) goes high at start, lets just chill for a ms
00048     wait_ms(1);
00049     // bring reset low
00050     rst = 0;
00051     // wait 10ms
00052     wait_ms(10);
00053     // bring out of reset
00054     rst = 1;
00055     // turn on VCC (9V?)
00056 
00057     command(SSD1306_DISPLAYOFF);
00058     command(SSD1306_SETDISPLAYCLOCKDIV);
00059     command(0x80);                                  // the suggested ratio 0x80
00060 
00061     command(SSD1306_SETMULTIPLEX);
00062     command(_rawHeight-1);
00063 
00064     command(SSD1306_SETDISPLAYOFFSET);
00065     command(0x0);                                   // no offset
00066 
00067     command(SSD1306_SETSTARTLINE | 0x0);            // line #0
00068 
00069     command(SSD1306_CHARGEPUMP);
00070     command((vccstate == SSD1306_EXTERNALVCC) ? 0x10 : 0x14);
00071 
00072     command(SSD1306_MEMORYMODE);
00073     command(0x00);                                  // 0x0 act like ks0108
00074 
00075     command(SSD1306_SEGREMAP | 0x1);
00076 
00077     command(SSD1306_COMSCANDEC);
00078 
00079     command(SSD1306_SETCOMPINS);
00080     command(_rawHeight == 32 ? 0x02 : 0x12);        // TODO - calculate based on _rawHieght ?
00081 
00082     command(SSD1306_SETCONTRAST);
00083     command(_rawHeight == 32 ? 0x8F : ((vccstate == SSD1306_EXTERNALVCC) ? 0x9F : 0xCF) );
00084 
00085     command(SSD1306_SETPRECHARGE);
00086     command((vccstate == SSD1306_EXTERNALVCC) ? 0x22 : 0xF1);
00087 
00088     command(SSD1306_SETVCOMDETECT);
00089     command(0x40);
00090 
00091     command(SSD1306_DISPLAYALLON_RESUME);
00092 
00093     command(SSD1306_NORMALDISPLAY);
00094     
00095     command(SSD1306_DISPLAYON);
00096 }
00097 
00098 // Set a single pixel
00099 void Adafruit_SSD1306::drawPixel(int16_t x, int16_t y, uint16_t color)
00100 {
00101     if ((x < 0) || (x >= width()) || (y < 0) || (y >= height()))
00102         return;
00103     
00104     // check rotation, move pixel around if necessary
00105     switch (getRotation())
00106     {
00107         case 1:
00108             swap(x, y);
00109             x = _rawWidth - x - 1;
00110             break;
00111         case 2:
00112             x = _rawWidth - x - 1;
00113             y = _rawHeight - y - 1;
00114             break;
00115         case 3:
00116             swap(x, y);
00117             y = _rawHeight - y - 1;
00118             break;
00119     }  
00120     
00121     // x is which column
00122     if (color == WHITE) 
00123         buffer[x+ (y/8)*_rawWidth] |= _BV((y%8));  
00124     else // else black
00125         buffer[x+ (y/8)*_rawWidth] &= ~_BV((y%8)); 
00126 }
00127 
00128 void Adafruit_SSD1306::invertDisplay(bool i)
00129 {
00130     command(i ? SSD1306_INVERTDISPLAY : SSD1306_NORMALDISPLAY);
00131 }
00132 
00133 // Send the display buffer out to the display
00134 void Adafruit_SSD1306::display(void)
00135 {
00136     command(SSD1306_SETLOWCOLUMN | 0x0);  // low col = 0
00137     command(SSD1306_SETHIGHCOLUMN | 0x0);  // hi col = 0
00138     command(SSD1306_SETSTARTLINE | 0x0); // line #0
00139     sendDisplayBuffer();
00140 }
00141 
00142 // Clear the display buffer. Requires a display() call at some point afterwards
00143 void Adafruit_SSD1306::clearDisplay(void)
00144 {
00145     std::fill(buffer.begin(),buffer.end(),0);
00146 }
00147 
00148 void Adafruit_SSD1306::splash(void)
00149 {
00150 #ifndef NO_SPLASH_ADAFRUIT
00151     uint8_t adaFruitLogo[64 * 128 / 8] =
00152     { 
00153         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00154         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00155         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00156         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
00157         0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00158         0x00, 0x80, 0x80, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00159         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00160         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00161         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00162         0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xF8, 0xE0, 0x00, 0x00, 0x00, 0x00,
00163         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80,
00164         0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0xFF,
00165         0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00,
00166         0x80, 0xFF, 0xFF, 0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x80, 0x80,
00167         0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x8C, 0x8E, 0x84, 0x00, 0x00, 0x80, 0xF8,
00168         0xF8, 0xF8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00169         0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xE0, 0xE0, 0xC0, 0x80,
00170         0x00, 0xE0, 0xFC, 0xFE, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00,
00171         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFF, 0xC7, 0x01, 0x01,
00172         0x01, 0x01, 0x83, 0xFF, 0xFF, 0x00, 0x00, 0x7C, 0xFE, 0xC7, 0x01, 0x01, 0x01, 0x01, 0x83, 0xFF,
00173         0xFF, 0xFF, 0x00, 0x38, 0xFE, 0xC7, 0x83, 0x01, 0x01, 0x01, 0x83, 0xC7, 0xFF, 0xFF, 0x00, 0x00,
00174         0x01, 0xFF, 0xFF, 0x01, 0x01, 0x00, 0xFF, 0xFF, 0x07, 0x01, 0x01, 0x01, 0x00, 0x00, 0x7F, 0xFF,
00175         0x80, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x01, 0xFF,
00176         0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00177         0x03, 0x0F, 0x3F, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE7, 0xC7, 0xC7, 0x8F,
00178         0x8F, 0x9F, 0xBF, 0xFF, 0xFF, 0xC3, 0xC0, 0xF0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xFC, 0xFC,
00179         0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0xF8, 0xF8, 0xF0, 0xF0, 0xE0, 0xC0, 0x00, 0x01, 0x03, 0x03, 0x03,
00180         0x03, 0x03, 0x01, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01,
00181         0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, 0x03, 0x03, 0x00, 0x00,
00182         0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
00183         0x03, 0x03, 0x03, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x01, 0x00, 0x00, 0x00, 0x03,
00184         0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00185         // 128x32^^^  128x64vvv
00186         0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0x1F, 0x0F,
00187         0x87, 0xC7, 0xF7, 0xFF, 0xFF, 0x1F, 0x1F, 0x3D, 0xFC, 0xF8, 0xF8, 0xF8, 0xF8, 0x7C, 0x7D, 0xFF,
00188         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x3F, 0x0F, 0x07, 0x00, 0x30, 0x30, 0x00, 0x00,
00189         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00190         0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00191         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xC0, 0x00,
00192         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00,
00193         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00194         0x00, 0xC0, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x7F, 0x3F, 0x1F,
00195         0x0F, 0x07, 0x1F, 0x7F, 0xFF, 0xFF, 0xF8, 0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xF8, 0xE0,
00196         0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00,
00197         0x00, 0xFC, 0xFE, 0xFC, 0x0C, 0x06, 0x06, 0x0E, 0xFC, 0xF8, 0x00, 0x00, 0xF0, 0xF8, 0x1C, 0x0E,
00198         0x06, 0x06, 0x06, 0x0C, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0x00, 0x00, 0xFC,
00199         0xFE, 0xFC, 0x00, 0x18, 0x3C, 0x7E, 0x66, 0xE6, 0xCE, 0x84, 0x00, 0x00, 0x06, 0xFF, 0xFF, 0x06,
00200         0x06, 0xFC, 0xFE, 0xFC, 0x0C, 0x06, 0x06, 0x06, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0xC0, 0xF8,
00201         0xFC, 0x4E, 0x46, 0x46, 0x46, 0x4E, 0x7C, 0x78, 0x40, 0x18, 0x3C, 0x76, 0xE6, 0xCE, 0xCC, 0x80,
00202         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00203         0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x0F, 0x1F, 0x1F, 0x3F, 0x3F, 0x3F, 0x3F, 0x1F, 0x0F, 0x03,
00204         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00,
00205         0x00, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x03, 0x07, 0x0E, 0x0C,
00206         0x18, 0x18, 0x0C, 0x06, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x01, 0x0F, 0x0E, 0x0C, 0x18, 0x0C, 0x0F,
00207         0x07, 0x01, 0x00, 0x04, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, 0x07, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00,
00208         0x00, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x07,
00209         0x07, 0x0C, 0x0C, 0x18, 0x1C, 0x0C, 0x06, 0x06, 0x00, 0x04, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, 0x07,
00210         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00211         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00212         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00213         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00214         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00215         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00216         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00217         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
00218     };
00219     
00220     std::copy(
00221         &adaFruitLogo[0]
00222         , &adaFruitLogo[0] + (_rawHeight == 32 ? sizeof(adaFruitLogo)/2 : sizeof(adaFruitLogo))
00223         , buffer.begin()
00224         );
00225 #endif
00226 }