* AM2321的取温度间隔得大于2s,否则,i2c会不工作了 * SimpleTimer有个bug,会导致两次快速的读温度,现在读温度函数里加了保护 * Blynk有个bug,会导致无法把数据传到服务器 * 现在可以正常工作了

Dependencies:   mbed

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 
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 #define ADAFRUIT_SSD1306_CPP
00025 #include "Adafruit_SSD1306.h"
00026 
00027 #define SSD1306_SETCONTRAST 0x81
00028 #define SSD1306_DISPLAYALLON_RESUME 0xA4
00029 #define SSD1306_DISPLAYALLON 0xA5
00030 #define SSD1306_NORMALDISPLAY 0xA6
00031 #define SSD1306_INVERTDISPLAY 0xA7
00032 #define SSD1306_DISPLAYOFF 0xAE
00033 #define SSD1306_DISPLAYON 0xAF
00034 #define SSD1306_SETDISPLAYOFFSET 0xD3
00035 #define SSD1306_SETCOMPINS 0xDA
00036 #define SSD1306_SETVCOMDETECT 0xDB
00037 #define SSD1306_SETDISPLAYCLOCKDIV 0xD5
00038 #define SSD1306_SETPRECHARGE 0xD9
00039 #define SSD1306_SETMULTIPLEX 0xA8
00040 #define SSD1306_SETLOWCOLUMN 0x00
00041 #define SSD1306_SETHIGHCOLUMN 0x10
00042 #define SSD1306_SETSTARTLINE 0x40
00043 #define SSD1306_MEMORYMODE 0x20
00044 #define SSD1306_COMSCANINC 0xC0
00045 #define SSD1306_COMSCANDEC 0xC8
00046 #define SSD1306_SEGREMAP 0xA0
00047 #define SSD1306_CHARGEPUMP 0x8D
00048 
00049 
00050 //0xAE, 0xD5, 0x80, 0xA8, _rawHeight-1, 0xD3, 0x00, 0x40, 0x8D, 0x10, 0x20, 0x00, 0xA1, 0xC8, 0xDA, 0x12, 0x81, 0x9F, 0xD9, 0x22, 0xDB, 0x40, 0xA4, 0xA6, 0xAF
00051 #if 1
00052 void Adafruit_SSD1306::begin(uint8_t vccstate)
00053 {
00054     //printf("begin: vccstate = %d\r\n", vccstate);
00055     rst = 1;
00056     // VDD (3.3V) goes high at start, lets just chill for a ms
00057     wait_ms(1);
00058     // bring reset low
00059     rst = 0;
00060     // wait 10ms
00061     wait_ms(10);
00062     // bring out of reset
00063     rst = 1;
00064     // turn on VCC (9V?)
00065 
00066     command(SSD1306_DISPLAYOFF);
00067     command(SSD1306_SETDISPLAYCLOCKDIV);
00068     command(0x80);                                  // the suggested ratio 0x80
00069 
00070     command(SSD1306_SETMULTIPLEX);
00071     command(_rawHeight-1);
00072 
00073     command(SSD1306_SETDISPLAYOFFSET);
00074     command(0x0);                                   // no offset
00075 
00076     command(SSD1306_SETSTARTLINE | 0x0);            // line #0
00077 
00078     command(SSD1306_CHARGEPUMP);
00079     command((vccstate == SSD1306_EXTERNALVCC) ? 0x10 : 0x14);
00080 
00081     command(SSD1306_MEMORYMODE);
00082     command(0x00);                                  // 0x0 act like ks0108
00083 
00084     command(SSD1306_SEGREMAP | 0x1);
00085 
00086     command(SSD1306_COMSCANDEC);
00087 
00088     command(SSD1306_SETCOMPINS);
00089     command(_rawHeight == 32 ? 0x02 : 0x12);        // TODO - calculate based on _rawHieght ?
00090 
00091     command(SSD1306_SETCONTRAST);
00092     command(_rawHeight == 32 ? 0x8F : ((vccstate == SSD1306_EXTERNALVCC) ? 0x9F : 0xCF) );
00093 
00094     command(SSD1306_SETPRECHARGE);
00095     command((vccstate == SSD1306_EXTERNALVCC) ? 0x22 : 0xF1);
00096 
00097     command(SSD1306_SETVCOMDETECT);
00098     command(0x40);
00099 
00100     command(SSD1306_DISPLAYALLON_RESUME);
00101 
00102     command(SSD1306_NORMALDISPLAY);
00103     
00104     command(SSD1306_DISPLAYON);
00105 }
00106 #else
00107 void Adafruit_SSD1306::begin(uint8_t vccstate)
00108 {
00109     command(0xAE);
00110     command(0xD5);
00111     command(0x80);                                  // the suggested ratio 0x80
00112 
00113     command(0xA8);
00114     command(_rawHeight-1);
00115 
00116     command(0xD3);
00117     command(0x0);                                   // no offset
00118 
00119     command(0x40);            // line #0
00120 
00121     command(0x8D);
00122     command(0x14);
00123 
00124     command(0x20);
00125     command(0x00);                                  // 0x0 act like ks0108
00126 
00127     command(0xA1);
00128 
00129     command(0xC8);
00130 
00131     command(0xDA);
00132     command(0x12);        // TODO - calculate based on _rawHieght ?
00133 
00134     command(0x8a);
00135     command(0xCF);
00136 
00137     command(0xD9);
00138     command(0xF1);
00139 
00140     command(0xDB);
00141     command(0x40);
00142 
00143     command(0x2E);//???
00144     command(SSD1306_DISPLAYALLON_RESUME);
00145 
00146     command(SSD1306_NORMALDISPLAY);
00147     
00148     command(SSD1306_DISPLAYON);
00149 }
00150 #endif
00151 
00152 // Set a single pixel
00153 void Adafruit_SSD1306::drawPixel(int16_t x, int16_t y, uint16_t color)
00154 {
00155     if ((x < 0) || (x >= width()) || (y < 0) || (y >= height()))
00156         return;
00157     
00158     // check rotation, move pixel around if necessary
00159     switch (getRotation())
00160     {
00161         case 1:
00162             swap(x, y);
00163             x = _rawWidth - x - 1;
00164             break;
00165         case 2:
00166             x = _rawWidth - x - 1;
00167             y = _rawHeight - y - 1;
00168             break;
00169         case 3:
00170             swap(x, y);
00171             y = _rawHeight - y - 1;
00172             break;
00173     }  
00174     
00175     // x is which column
00176     if (color == WHITE) 
00177         buffer[x+ (y/8)*_rawWidth] |= _BV((y%8));  
00178     else // else black
00179         buffer[x+ (y/8)*_rawWidth] &= ~_BV((y%8)); 
00180 }
00181 
00182 void Adafruit_SSD1306::invertDisplay(bool i)
00183 {
00184     command(i ? SSD1306_INVERTDISPLAY : SSD1306_NORMALDISPLAY);
00185 }
00186 
00187 // Send the display buffer out to the display
00188 void Adafruit_SSD1306::display(void)
00189 {
00190     //printf("display\r\n");
00191     command(SSD1306_SETLOWCOLUMN | 0x0);  // low col = 0
00192     command(SSD1306_SETHIGHCOLUMN | 0x0);  // hi col = 0
00193     command(SSD1306_SETSTARTLINE | 0x0); // line #0
00194     sendDisplayBuffer();
00195 }
00196 
00197 // Clear the display buffer. Requires a display() call at some point afterwards
00198 void Adafruit_SSD1306::clearDisplay(void)
00199 {
00200     #if 0
00201     std::fill(buffer.begin(),buffer.end(),0);
00202     #else
00203     memset(buffer, 0, sizeof(buffer));
00204     #endif
00205 }
00206 
00207 void Adafruit_SSD1306::splash(void)
00208 {
00209 #ifndef NO_SPLASH_ADAFRUIT
00210     uint8_t adaFruitLogo[64 * 128 / 8] =
00211     { 
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, 0x80,
00216         0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00217         0x00, 0x80, 0x80, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00218         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00219         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00220         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00221         0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xF8, 0xE0, 0x00, 0x00, 0x00, 0x00,
00222         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80,
00223         0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0xFF,
00224         0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00,
00225         0x80, 0xFF, 0xFF, 0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x80, 0x80,
00226         0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x8C, 0x8E, 0x84, 0x00, 0x00, 0x80, 0xF8,
00227         0xF8, 0xF8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00228         0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xE0, 0xE0, 0xC0, 0x80,
00229         0x00, 0xE0, 0xFC, 0xFE, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00,
00230         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFF, 0xC7, 0x01, 0x01,
00231         0x01, 0x01, 0x83, 0xFF, 0xFF, 0x00, 0x00, 0x7C, 0xFE, 0xC7, 0x01, 0x01, 0x01, 0x01, 0x83, 0xFF,
00232         0xFF, 0xFF, 0x00, 0x38, 0xFE, 0xC7, 0x83, 0x01, 0x01, 0x01, 0x83, 0xC7, 0xFF, 0xFF, 0x00, 0x00,
00233         0x01, 0xFF, 0xFF, 0x01, 0x01, 0x00, 0xFF, 0xFF, 0x07, 0x01, 0x01, 0x01, 0x00, 0x00, 0x7F, 0xFF,
00234         0x80, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x01, 0xFF,
00235         0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00236         0x03, 0x0F, 0x3F, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE7, 0xC7, 0xC7, 0x8F,
00237         0x8F, 0x9F, 0xBF, 0xFF, 0xFF, 0xC3, 0xC0, 0xF0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xFC, 0xFC,
00238         0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0xF8, 0xF8, 0xF0, 0xF0, 0xE0, 0xC0, 0x00, 0x01, 0x03, 0x03, 0x03,
00239         0x03, 0x03, 0x01, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01,
00240         0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, 0x03, 0x03, 0x00, 0x00,
00241         0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
00242         0x03, 0x03, 0x03, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x01, 0x00, 0x00, 0x00, 0x03,
00243         0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00244         // 128x32^^^  128x64vvv
00245         0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0x1F, 0x0F,
00246         0x87, 0xC7, 0xF7, 0xFF, 0xFF, 0x1F, 0x1F, 0x3D, 0xFC, 0xF8, 0xF8, 0xF8, 0xF8, 0x7C, 0x7D, 0xFF,
00247         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x3F, 0x0F, 0x07, 0x00, 0x30, 0x30, 0x00, 0x00,
00248         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00249         0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00250         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xC0, 0x00,
00251         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00,
00252         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00253         0x00, 0xC0, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x7F, 0x3F, 0x1F,
00254         0x0F, 0x07, 0x1F, 0x7F, 0xFF, 0xFF, 0xF8, 0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xF8, 0xE0,
00255         0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00,
00256         0x00, 0xFC, 0xFE, 0xFC, 0x0C, 0x06, 0x06, 0x0E, 0xFC, 0xF8, 0x00, 0x00, 0xF0, 0xF8, 0x1C, 0x0E,
00257         0x06, 0x06, 0x06, 0x0C, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0x00, 0x00, 0xFC,
00258         0xFE, 0xFC, 0x00, 0x18, 0x3C, 0x7E, 0x66, 0xE6, 0xCE, 0x84, 0x00, 0x00, 0x06, 0xFF, 0xFF, 0x06,
00259         0x06, 0xFC, 0xFE, 0xFC, 0x0C, 0x06, 0x06, 0x06, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0xC0, 0xF8,
00260         0xFC, 0x4E, 0x46, 0x46, 0x46, 0x4E, 0x7C, 0x78, 0x40, 0x18, 0x3C, 0x76, 0xE6, 0xCE, 0xCC, 0x80,
00261         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00262         0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x0F, 0x1F, 0x1F, 0x3F, 0x3F, 0x3F, 0x3F, 0x1F, 0x0F, 0x03,
00263         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00,
00264         0x00, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x03, 0x07, 0x0E, 0x0C,
00265         0x18, 0x18, 0x0C, 0x06, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x01, 0x0F, 0x0E, 0x0C, 0x18, 0x0C, 0x0F,
00266         0x07, 0x01, 0x00, 0x04, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, 0x07, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00,
00267         0x00, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x07,
00268         0x07, 0x0C, 0x0C, 0x18, 0x1C, 0x0C, 0x06, 0x06, 0x00, 0x04, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, 0x07,
00269         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00270         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00271         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00272         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00273         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00274         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00275         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00276         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
00277     };
00278     #if 0
00279     std::copy(
00280         &adaFruitLogo[0]
00281         , &adaFruitLogo[0] + (_rawHeight == 32 ? sizeof(adaFruitLogo)/2 : sizeof(adaFruitLogo))
00282         , buffer.begin()
00283         );
00284     #else
00285     memcpy(buffer, adaFruitLogo, sizeof(adaFruitLogo));
00286     #endif
00287 #endif
00288 }