Dmitry Dzhafarkhanov / Adafruit_GFX

Fork of Adafruit_GFX by Neal Horman

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Adafruit_SSD1351.cpp Source File

Adafruit_SSD1351.cpp

00001 /*********************************************************************
00002 This is a library for our Monochrome OLEDs based on SSD1351 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_SSD1351.h"
00025 
00026 #ifndef _BV
00027 #define _BV(bit) (1<<(bit))
00028 #endif
00029 
00030 
00031 #define SSD1351_COLORORDER_BGR
00032 
00033 
00034 
00035 
00036 // Timing Delays
00037 #define SSD1351_DELAYS_HWFILL       (3)
00038 #define SSD1351_DELAYS_HWLINE       (1)
00039 
00040 //old
00041 #define SSD1351_SETHIGHCOLUMN 0x10
00042 
00043 // SSD1351 Commands
00044 #define SSD1351_CMD_SETCOLUMN       0x15
00045 #define SSD1351_CMD_SETROW          0x75
00046 #define SSD1351_CMD_WRITERAM        0x5C
00047 #define SSD1351_CMD_READRAM         0x5D
00048 #define SSD1351_CMD_SETREMAP        0xA0
00049 #define SSD1351_CMD_STARTLINE       0xA1  //Set display start line 
00050 #define SSD1351_CMD_DISPLAYOFFSET   0xA2
00051 #define SSD1351_CMD_DISPLAYALLOFF   0xA4
00052 #define SSD1351_CMD_DISPLAYALLON    0xA5
00053 #define SSD1351_CMD_NORMALDISPLAY   0xA6
00054 #define SSD1351_CMD_INVERTDISPLAY   0xA7
00055 #define SSD1351_CMD_FUNCTIONSELECT  0xAB
00056 #define SSD1351_CMD_DISPLAYOFF      0xAE
00057 #define SSD1351_CMD_DISPLAYON       0xAF
00058 #define SSD1351_CMD_PRECHARGE       0xB1
00059 #define SSD1351_CMD_DISPLAYENHANCE  0xB2
00060 #define SSD1351_CMD_CLOCKDIV        0xB3
00061 #define SSD1351_CMD_SETVSL          0xB4
00062 #define SSD1351_CMD_SETGPIO         0xB5
00063 #define SSD1351_CMD_PRECHARGE2      0xB6
00064 #define SSD1351_CMD_SETGRAY         0xB8
00065 #define SSD1351_CMD_USELUT          0xB9
00066 #define SSD1351_CMD_PRECHARGELEVEL  0xBB
00067 #define SSD1351_CMD_VCOMH           0xBE
00068 #define SSD1351_CMD_CONTRASTABC     0xC1
00069 #define SSD1351_CMD_CONTRASTMASTER  0xC7
00070 #define SSD1351_CMD_MUXRATIO            0xCA
00071 #define SSD1351_CMD_COMMANDLOCK         0xFD
00072 #define SSD1351_CMD_HORIZSCROLL         0x96
00073 #define SSD1351_CMD_STOPSCROLL          0x9E
00074 #define SSD1351_CMD_STARTSCROLL         0x9F
00075 
00076 
00077 
00078 
00079 void Adafruit_SSD1351::begin(void)//oled init
00080 {
00081     rst = 1;
00082     // VDD (3.3V) goes high at start, lets just chill for a ms
00083     wait_ms(10);
00084     // bring reset low
00085     rst = 0;
00086     // wait 10ms
00087     wait_ms(10);
00088     // bring out of reset
00089     rst = 1;
00090     // turn on VCC (13V?)
00091     wait_ms(10);
00092     
00093     
00094     // Initialization Sequence
00095     writeCommand(SSD1351_CMD_COMMANDLOCK);  // set command lock
00096     writeData(0x12);  
00097     writeCommand(SSD1351_CMD_COMMANDLOCK);  // set command lock
00098     writeData(0xB1);
00099     
00100     writeCommand(SSD1351_CMD_DISPLAYOFF);       // 0xAE
00101     
00102     writeCommand(SSD1351_CMD_CLOCKDIV);         // 0xB3
00103     writeCommand(0xF1);                         // 7:4 = Oscillator Frequency, 3:0 = CLK Div Ratio (A[3:0]+1 = 1..16)   
00104     
00105     writeCommand(SSD1351_CMD_MUXRATIO);
00106     writeData(127);
00107     
00108     writeCommand(SSD1351_CMD_SETREMAP);
00109     writeData(0x74);
00110   
00111     writeCommand(SSD1351_CMD_SETCOLUMN);
00112     writeData(0x00);
00113     writeData(0x7F);
00114     writeCommand(SSD1351_CMD_SETROW);
00115     writeData(0x00);
00116     writeData(0x7F);
00117 
00118     writeCommand(SSD1351_CMD_STARTLINE);        // 0xA1
00119     
00120     if (height() == 96) {
00121       writeData(96);
00122     } else {
00123       writeData(0);
00124     }
00125 
00126     writeCommand(SSD1351_CMD_DISPLAYOFFSET);    // 0xA2
00127     writeData(0x00);
00128 
00129     writeCommand(SSD1351_CMD_SETGPIO);
00130     writeData(0x00);
00131     
00132     writeCommand(SSD1351_CMD_FUNCTIONSELECT);
00133     writeData(0x01); // internal (diode drop)
00134     //writeData(0x01); // external bias
00135 
00136 //    writeCommand(SSSD1351_CMD_SETPHASELENGTH);
00137 //    writeData(0x32);
00138 
00139     writeCommand(SSD1351_CMD_PRECHARGE);        // 0xB1
00140     writeCommand(0x32);
00141  
00142     writeCommand(SSD1351_CMD_VCOMH);            // 0xBE
00143     writeCommand(0x05);
00144 
00145     writeCommand(SSD1351_CMD_NORMALDISPLAY);    // 0xA6
00146 
00147     writeCommand(SSD1351_CMD_CONTRASTABC);
00148     writeData(0xC8); //0x8A   //0xC8)
00149     writeData(0x80);//0x51      //0x80
00150     writeData(0xC8);//  0x8A        //0xC8
00151 
00152     writeCommand(SSD1351_CMD_CONTRASTMASTER);
00153     writeData(0x0F);
00154 
00155     writeCommand(SSD1351_CMD_SETVSL );
00156     writeData(0xA0);
00157     writeData(0xB5);
00158     writeData(0x55);
00159     
00160     writeCommand(SSD1351_CMD_PRECHARGE2);
00161     writeData(0x01);
00162     
00163     writeCommand(SSD1351_CMD_DISPLAYON);        //--turn on oled panel   
00164 }
00165 
00166 void  Adafruit_SSD1351::invert(bool v) {
00167    if (v) {
00168      writeCommand(SSD1351_CMD_INVERTDISPLAY);
00169    } else {
00170         writeCommand(SSD1351_CMD_NORMALDISPLAY);
00171    }
00172  }
00173 
00174 void Adafruit_SSD1351::goTo(int x, int y) {
00175   if ((x >= width()) || (y >= height())) return;
00176   
00177   // set x and y coordinate
00178   writeCommand(SSD1351_CMD_SETCOLUMN);
00179   writeData(x);
00180   writeData(width()-1);
00181 
00182   writeCommand(SSD1351_CMD_SETROW);
00183   writeData(y);
00184   writeData(height()-1);
00185   writeCommand(SSD1351_CMD_WRITERAM);  
00186 }
00187 
00188 uint16_t Adafruit_SSD1351::Color565(uint8_t r, uint8_t g, uint8_t b) {
00189   uint16_t c;
00190   c = r >> 3;
00191   c <<= 6;
00192   c |= g >> 2;
00193   c <<= 5;
00194   c |= b >> 3;
00195 
00196   return c;
00197 }
00198 
00199 void Adafruit_SSD1351::fillScreen(uint16_t fillcolor) {
00200   fillRect(0, 0, width(), height(), fillcolor);
00201 }
00202 
00203 // Draw a filled rectangle with no rotation.
00204 void Adafruit_SSD1351::rawFillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t fillcolor) {
00205   // Bounds check
00206   if ((x >= width()) || (y >= height()))
00207     return;
00208 
00209   // Y bounds check
00210   if (y+h > height())
00211   {
00212     h = height() - y - 1;
00213   }
00214 
00215   // X bounds check
00216   if (x+w > width())
00217   {
00218     w = width() - x - 1;
00219   }
00220   
00221   /*
00222   Serial.print(x); Serial.print(", ");
00223   Serial.print(y); Serial.print(", ");
00224   Serial.print(w); Serial.print(", ");
00225   Serial.print(h); Serial.println(", ");
00226 */
00227 
00228   // set location
00229   writeCommand(SSD1351_CMD_SETCOLUMN);
00230   writeData(x);
00231   writeData(x+w-1);
00232   writeCommand(SSD1351_CMD_SETROW);
00233   writeData(y);
00234   writeData(y+h-1);
00235   // fill!
00236   writeCommand(SSD1351_CMD_WRITERAM);  
00237 
00238   for (uint16_t i=0; i < w*h; i++) {
00239     writeData(fillcolor >> 8);
00240     writeData(fillcolor);
00241   }
00242 }
00243 
00244 
00245 void Adafruit_SSD1351::fillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t fillcolor) {
00246   // Transform x and y based on current rotation.
00247   switch (getRotation()) {
00248   case 0:  // No rotation
00249     rawFillRect(x, y, w, h, fillcolor);
00250     break;
00251   case 1:  // Rotated 90 degrees clockwise.
00252     swap(x, y);
00253     x = _rawWidth - x - h;
00254     rawFillRect(x, y, h, w, fillcolor);
00255     break;
00256   case 2:  // Rotated 180 degrees clockwise.
00257     x = _rawWidth - x - w;
00258     y = _rawHeight - y - h;
00259     rawFillRect(x, y, w, h, fillcolor);
00260     break;
00261   case 3:  // Rotated 270 degrees clockwise.
00262     swap(x, y);
00263     y = _rawHeight - y - w;
00264     rawFillRect(x, y, h, w, fillcolor);
00265     break;
00266   }
00267 }
00268 
00269 // Draw a horizontal line ignoring any screen rotation.
00270 void Adafruit_SSD1351::rawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color) {
00271   // Bounds check
00272   if ((x >= width()) || (y >= height()))
00273     return;
00274 
00275   // X bounds check
00276   if (x+w > width())
00277   {
00278     w = width() - x - 1;
00279   }
00280 
00281   if (w < 0) return;
00282 
00283   // set location
00284   writeCommand(SSD1351_CMD_SETCOLUMN);
00285   writeData(x);
00286   writeData(x+w-1);
00287   writeCommand(SSD1351_CMD_SETROW);
00288   writeData(y);
00289   writeData(y);
00290   // fill!
00291   writeCommand(SSD1351_CMD_WRITERAM);  
00292 
00293   for (uint16_t i=0; i < w; i++) {
00294     writeData(color >> 8);
00295     writeData(color);
00296   }
00297 }
00298 
00299 // Draw a vertical line ignoring any screen rotation.
00300 void Adafruit_SSD1351::rawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color) {
00301   // Bounds check
00302   if ((x >= width()) || (y >= height()))
00303   return;
00304 
00305   // X bounds check
00306   if (y+h > height())
00307   {
00308     h = height() - y - 1;
00309   }
00310 
00311   if (h < 0) return;
00312 
00313   // set location
00314   writeCommand(SSD1351_CMD_SETCOLUMN);
00315   writeData(x);
00316   writeData(x);
00317   writeCommand(SSD1351_CMD_SETROW);
00318   writeData(y);
00319   writeData(y+h-1);
00320   // fill!
00321   writeCommand(SSD1351_CMD_WRITERAM);  
00322 
00323   for (uint16_t i=0; i < h; i++) {
00324     writeData(color >> 8);
00325     writeData(color);
00326   }
00327 }
00328 
00329 void Adafruit_SSD1351::drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color) {
00330   // Transform x and y based on current rotation.
00331   switch (getRotation()) {
00332   case 0:  // No rotation
00333     rawFastVLine(x, y, h, color);
00334     break;
00335   case 1:  // Rotated 90 degrees clockwise.
00336     swap(x, y);
00337     x = _rawWidth - x - h;
00338     rawFastHLine(x, y, h, color);
00339     break;
00340   case 2:  // Rotated 180 degrees clockwise.
00341     x = _rawWidth - x - 1;
00342     y = _rawHeight - y - h;
00343     rawFastVLine(x, y, h, color);
00344     break;
00345   case 3:  // Rotated 270 degrees clockwise.
00346     swap(x, y);
00347     y = _rawHeight - y - 1;
00348     rawFastHLine(x, y, h, color);
00349     break;
00350   }
00351 }
00352 
00353 void Adafruit_SSD1351::drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color) {
00354   // Transform x and y based on current rotation.
00355   switch (getRotation()) {
00356   case 0:  // No rotation.
00357     rawFastHLine(x, y, w, color);
00358     break;
00359   case 1:  // Rotated 90 degrees clockwise.
00360     swap(x, y);
00361     x = _rawWidth - x - 1;
00362     rawFastVLine(x, y, w, color);
00363     break;
00364   case 2:  // Rotated 180 degrees clockwise.
00365     x = _rawWidth - x - w;
00366     y = _rawHeight - y - 1;
00367     rawFastHLine(x, y, w, color);
00368     break;
00369   case 3:  // Rotated 270 degrees clockwise.
00370     swap(x, y);
00371     y = _rawHeight - y - w;
00372     rawFastVLine(x, y, w, color);
00373     break;
00374   }
00375 }
00376 
00377 
00378 void Adafruit_SSD1351::setContrastControl(uint8_t contrast) {
00379    
00380    writeCommand(SSD1351_CMD_CONTRASTMASTER);
00381     //writeData(0x0F);
00382     writeData(contrast);
00383    
00384 } 
00385 
00386 
00387 
00388 
00389 
00390 
00391 // Set a single pixel
00392 void Adafruit_SSD1351::drawPixel(int16_t x, int16_t y, uint16_t color)
00393 {
00394     if ((x < 0) || (x >= width()) || (y < 0) || (y >= height()))  // Bounds check.
00395         return;
00396     
00397     // check rotation, move pixel around if necessary
00398     switch (getRotation())
00399     {
00400         case 1:                             // Rotated 90 degrees clockwise.
00401             swap(x, y);
00402             x = _rawWidth - x - 1;
00403             break;
00404         case 2:                             // Rotated 180 degrees clockwise.
00405             x = _rawWidth - x - 1;
00406             y = _rawHeight - y - 1;
00407             break;
00408         case 3:                             // Rotated 270 degrees clockwise.
00409             swap(x, y);
00410             y = _rawHeight - y - 1;
00411             break;
00412     }  
00413     
00414 {
00415   
00416   }
00417 
00418   goTo(x, y);
00419   writeData(color >> 8) ;
00420   writeData(color);
00421 
00422 }
00423 
00424    
00425 
00426 
00427 
00428 
00429 
00430 
00431