Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Adafruit_SSD1306.cpp
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 below must be included in any redistribution 00017 *********************************************************************/ 00018 00019 /* 00020 * Modified by Neal Horman 7/14/2012 for use in mbed 00021 */ 00022 00023 #include "mbed.h" 00024 #include "Adafruit_SSD1306.h" 00025 00026 #define SSD1306_SETCONTRAST 0x81 00027 #define SSD1306_DISPLAYALLON_RESUME 0xA4 00028 #define SSD1306_DISPLAYALLON 0xA5 00029 #define SSD1306_NORMALDISPLAY 0xA6 00030 #define SSD1306_INVERTDISPLAY 0xA7 00031 #define SSD1306_DISPLAYOFF 0xAE 00032 #define SSD1306_DISPLAYON 0xAF 00033 #define SSD1306_SETDISPLAYOFFSET 0xD3 00034 #define SSD1306_SETCOMPINS 0xDA 00035 #define SSD1306_SETVCOMDETECT 0xDB 00036 #define SSD1306_SETDISPLAYCLOCKDIV 0xD5 00037 #define SSD1306_SETPRECHARGE 0xD9 00038 #define SSD1306_SETMULTIPLEX 0xA8 00039 #define SSD1306_SETLOWCOLUMN 0x00 00040 #define SSD1306_SETHIGHCOLUMN 0x10 00041 #define SSD1306_SETSTARTLINE 0x40 00042 #define SSD1306_MEMORYMODE 0x20 00043 #define SSD1306_COMSCANINC 0xC0 00044 #define SSD1306_COMSCANDEC 0xC8 00045 #define SSD1306_SEGREMAP 0xA0 00046 #define SSD1306_CHARGEPUMP 0x8D 00047 00048 void Adafruit_SSD1306::begin(uint8_t vccstate) 00049 { 00050 rst = 1; 00051 // VDD (3.3V) goes high at start, lets just chill for a ms 00052 wait_ms(1); 00053 // bring reset low 00054 rst = 0; 00055 // wait 10ms 00056 wait_ms(10); 00057 // bring out of reset 00058 rst = 1; 00059 // turn on VCC (9V?) 00060 00061 command(SSD1306_DISPLAYOFF); 00062 command(SSD1306_SETDISPLAYCLOCKDIV); 00063 command(0x80); // the suggested ratio 0x80 00064 00065 command(SSD1306_SETMULTIPLEX); 00066 command(_rawHeight-1); 00067 00068 command(SSD1306_SETDISPLAYOFFSET); 00069 command(0x0); // no offset 00070 00071 command(SSD1306_SETSTARTLINE | 0x0); // line #0 00072 00073 command(SSD1306_CHARGEPUMP); 00074 command((vccstate == SSD1306_EXTERNALVCC) ? 0x10 : 0x14); 00075 00076 command(SSD1306_MEMORYMODE); 00077 command(0x00); // 0x0 act like ks0108 00078 00079 command(SSD1306_SEGREMAP | 0x1); 00080 00081 command(SSD1306_COMSCANDEC); 00082 00083 command(SSD1306_SETCOMPINS); 00084 command(_rawHeight == 32 ? 0x02 : 0x12); // TODO - calculate based on _rawHieght ? 00085 00086 command(SSD1306_SETCONTRAST); 00087 command(_rawHeight == 32 ? 0x8F : ((vccstate == SSD1306_EXTERNALVCC) ? 0x9F : 0xCF) ); 00088 00089 command(SSD1306_SETPRECHARGE); 00090 command((vccstate == SSD1306_EXTERNALVCC) ? 0x22 : 0xF1); 00091 00092 command(SSD1306_SETVCOMDETECT); 00093 command(0x40); 00094 00095 command(SSD1306_DISPLAYALLON_RESUME); 00096 00097 command(SSD1306_NORMALDISPLAY); 00098 00099 command(SSD1306_DISPLAYON); 00100 } 00101 00102 // Set a single pixel 00103 void Adafruit_SSD1306::drawPixel(int16_t x, int16_t y, uint16_t color) 00104 { 00105 if ((x < 0) || (x >= width()) || (y < 0) || (y >= height())) 00106 return; 00107 00108 // check rotation, move pixel around if necessary 00109 switch (getRotation()) { 00110 case 1: 00111 swap(x, y); 00112 x = _rawWidth - x - 1; 00113 break; 00114 case 2: 00115 x = _rawWidth - x - 1; 00116 y = _rawHeight - y - 1; 00117 break; 00118 case 3: 00119 swap(x, y); 00120 y = _rawHeight - y - 1; 00121 break; 00122 } 00123 00124 // x is which column 00125 if (color == WHITE) 00126 buffer[x+ (y/8)*_rawWidth] |= _BV((y%8)); 00127 else // else black 00128 buffer[x+ (y/8)*_rawWidth] &= ~_BV((y%8)); 00129 } 00130 00131 void Adafruit_SSD1306::invertDisplay(bool i) 00132 { 00133 command(i ? SSD1306_INVERTDISPLAY : SSD1306_NORMALDISPLAY); 00134 } 00135 00136 // Send the display buffer out to the display 00137 void Adafruit_SSD1306::display(void) 00138 { 00139 command(SSD1306_SETLOWCOLUMN | 0x0); // low col = 0 00140 command(SSD1306_SETHIGHCOLUMN | 0x0); // hi col = 0 00141 command(SSD1306_SETSTARTLINE | 0x0); // line #0 00142 sendDisplayBuffer(); 00143 } 00144 00145 // Clear the display buffer. Requires a display() call at some point afterwards 00146 void Adafruit_SSD1306::clearDisplay(void) 00147 { 00148 std::fill(buffer.begin(),buffer.end(),0); 00149 } 00150 00151 void Adafruit_SSD1306::splash(void) 00152 { 00153 #ifndef NO_SPLASH_ADAFRUIT 00154 uint8_t adaFruitLogo[32 * 128 / 8] = { 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, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 00157 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x03, 0x01, 00158 0x01, 0x01, 0x03, 0x03, 0xDF, 0xFF, 0xFF, 0xFF, 0x03, 0x03, 0x03, 0x03, 0x03, 0xFF, 0xFF, 0x3F, 00159 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 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, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 00163 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 00164 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 00165 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x07, 0x03, 0x00, 0x00, 00166 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x80, 0xFF, 0xF1, 0xFE, 00167 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 00168 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 00169 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 00170 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 00171 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 00172 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 00173 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x3F, 0x0F, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 00174 0xE0, 0xF0, 0xF8, 0xFC, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 00175 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 00176 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 00177 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 00178 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 00179 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 00180 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 00181 0xFF, 0xFF, 0xEF, 0xE3, 0xE1, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xF0, 0xF8, 0xFE, 0xFF, 0xFF, 0xFF, 00182 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 00183 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 00184 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 00185 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 00186 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 00187 }; 00188 00189 00190 00191 std::copy( 00192 &adaFruitLogo[0] 00193 , &adaFruitLogo[0] + (_rawHeight == 32 ? sizeof(adaFruitLogo) : sizeof(adaFruitLogo)) 00194 , buffer.begin() 00195 ); 00196 #endif 00197 }
Generated on Mon Jul 25 2022 03:54:50 by
