Compiles against mbed OS 5

Fork of Adafruit_GFX_1351 by Tisham Dhar

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Adafruit_SSD1351.cpp Source File

Adafruit_SSD1351.cpp

00001 #include "Adafruit_SSD1351.h"
00002 
00003 #include <stdarg.h>
00004 
00005 #define pgm_read_byte(addr) (*(const unsigned char *)(addr))
00006 
00007 
00008 Adafruit_SSD1351::Adafruit_SSD1351(PinName cs, PinName rs, PinName dc, PinName clk, PinName data)
00009     : Adafruit_GFX(SSD1351WIDTH, SSD1351HEIGHT),
00010       _spi(data, NC, clk), 
00011       _cs(cs), 
00012       _reset(rs), 
00013       _dc(dc)
00014 {
00015 }
00016 
00017 void Adafruit_SSD1351::writeCommand(uint8_t code)
00018 {
00019     _cs = 1;
00020     _dc = 0;
00021     _cs = 0;
00022     _spi.write(code);
00023     _cs = 1;
00024 }
00025 
00026 void Adafruit_SSD1351::writeData(uint8_t value)
00027 {
00028     _cs = 1;
00029     _dc = 1;
00030     _cs = 0;
00031     _spi.write(value);
00032     _cs = 1;
00033 }
00034 
00035 void Adafruit_SSD1351::off()
00036 {
00037     writeCommand(0xAE);
00038 }
00039 
00040 void Adafruit_SSD1351::on()
00041 {
00042     writeCommand(0xAF);
00043 }
00044 
00045 void Adafruit_SSD1351::goTo(int x, int y) {
00046   if ((x >= SSD1351WIDTH) || (y >= SSD1351HEIGHT)) return;
00047   
00048   // set x and y coordinate
00049   writeCommand(SSD1351_CMD_SETCOLUMN);
00050   writeData(x+16);
00051   writeData(SSD1351WIDTH-1);
00052 
00053   writeCommand(SSD1351_CMD_SETROW);
00054   writeData(SSD1351HEIGHT-y+95);
00055   writeData(SSD1351HEIGHT-1);
00056 
00057   writeCommand(SSD1351_CMD_WRITERAM);  
00058 }
00059 
00060 uint16_t Adafruit_SSD1351::Color565(uint8_t r, uint8_t g, uint8_t b) {
00061   uint16_t c;
00062   c = r >> 3;
00063   c <<= 6;
00064   c |= g >> 2;
00065   c <<= 5;
00066   c |= b >> 3;
00067 
00068   return c;
00069 }
00070 
00071 void Adafruit_SSD1351::fillScreen(uint16_t fillcolor) {
00072   fillRect(0, 0, SSD1351WIDTH, SSD1351HEIGHT, fillcolor);
00073 }
00074 
00075 // Draw a filled rectangle with no rotation.
00076 void Adafruit_SSD1351::rawFillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t fillcolor) {
00077   // Bounds check
00078   if ((x >= SSD1351WIDTH) || (y >= SSD1351HEIGHT))
00079     return;
00080 
00081   // Y bounds check
00082   if (y+h > SSD1351HEIGHT)
00083   {
00084     h = SSD1351HEIGHT - y - 1;
00085   }
00086 
00087   // X bounds check
00088   if (x+w > SSD1351WIDTH)
00089   {
00090     w = SSD1351WIDTH - x - 1;
00091   }
00092   
00093   /*
00094   Serial.print(x); Serial.print(", ");
00095   Serial.print(y); Serial.print(", ");
00096   Serial.print(w); Serial.print(", ");
00097   Serial.print(h); Serial.println(", ");
00098 */
00099 
00100   // set location
00101   writeCommand(SSD1351_CMD_SETCOLUMN);
00102   writeData(x);
00103   writeData(x+w-1);
00104   writeCommand(SSD1351_CMD_SETROW);
00105   writeData(y);
00106   writeData(y+h-1);
00107   // fill!
00108   writeCommand(SSD1351_CMD_WRITERAM);  
00109 
00110   for (uint16_t i=0; i < w*h; i++) {
00111     writeData(fillcolor >> 8);
00112     writeData(fillcolor);
00113   }
00114 }
00115 
00116 /**************************************************************************/
00117 /*!
00118     @brief  Draws a filled rectangle using HW acceleration
00119 */
00120 /**************************************************************************/
00121 void Adafruit_SSD1351::fillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t fillcolor) {
00122   // Transform x and y based on current rotation.
00123   switch (getRotation()) {
00124   case 0:  // No rotation
00125     rawFillRect(x, y, w, h, fillcolor);
00126     break;
00127   case 1:  // Rotated 90 degrees clockwise.
00128     SWAP(x, y);
00129     x = _rawWidth - x - h;
00130     rawFillRect(x, y, h, w, fillcolor);
00131     break;
00132   case 2:  // Rotated 180 degrees clockwise.
00133     x = _rawWidth - x - w;
00134     y = _rawHeight - y - h;
00135     rawFillRect(x, y, w, h, fillcolor);
00136     break;
00137   case 3:  // Rotated 270 degrees clockwise.
00138     SWAP(x, y);
00139     y = _rawHeight - y - w;
00140     rawFillRect(x, y, h, w, fillcolor);
00141     break;
00142   }
00143 }
00144 
00145 // Draw a horizontal line ignoring any screen rotation.
00146 void Adafruit_SSD1351::rawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color) {
00147   // Bounds check
00148   if ((x >= SSD1351WIDTH) || (y >= SSD1351HEIGHT))
00149     return;
00150 
00151   // X bounds check
00152   if (x+w > SSD1351WIDTH)
00153   {
00154     w = SSD1351WIDTH - x - 1;
00155   }
00156 
00157   if (w < 0) return;
00158 
00159   // set location
00160   writeCommand(SSD1351_CMD_SETCOLUMN);
00161   writeData(x);
00162   writeData(x+w-1);
00163   writeCommand(SSD1351_CMD_SETROW);
00164   writeData(y);
00165   writeData(y);
00166   // fill!
00167   writeCommand(SSD1351_CMD_WRITERAM);  
00168 
00169   for (uint16_t i=0; i < w; i++) {
00170     writeData(color >> 8);
00171     writeData(color);
00172   }
00173 }
00174 
00175 // Draw a vertical line ignoring any screen rotation.
00176 void Adafruit_SSD1351::rawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color) {
00177   // Bounds check
00178   if ((x >= SSD1351WIDTH) || (y >= SSD1351HEIGHT))
00179   return;
00180 
00181   // X bounds check
00182   if (y+h > SSD1351HEIGHT)
00183   {
00184     h = SSD1351HEIGHT - y - 1;
00185   }
00186 
00187   if (h < 0) return;
00188 
00189   // set location
00190   writeCommand(SSD1351_CMD_SETCOLUMN);
00191   writeData(x);
00192   writeData(x);
00193   writeCommand(SSD1351_CMD_SETROW);
00194   writeData(y);
00195   writeData(y+h-1);
00196   // fill!
00197   writeCommand(SSD1351_CMD_WRITERAM);  
00198 
00199   for (uint16_t i=0; i < h; i++) {
00200     writeData(color >> 8);
00201     writeData(color);
00202   }
00203 }
00204 
00205 void Adafruit_SSD1351::drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color) {
00206   // Transform x and y based on current rotation.
00207   switch (getRotation()) {
00208   case 0:  // No rotation
00209     rawFastVLine(x, y, h, color);
00210     break;
00211   case 1:  // Rotated 90 degrees clockwise.
00212     SWAP(x, y);
00213     x = _rawWidth - x - h;
00214     rawFastHLine(x, y, h, color);
00215     break;
00216   case 2:  // Rotated 180 degrees clockwise.
00217     x = _rawWidth - x - 1;
00218     y = _rawHeight - y - h;
00219     rawFastVLine(x, y, h, color);
00220     break;
00221   case 3:  // Rotated 270 degrees clockwise.
00222     SWAP(x, y);
00223     y = _rawHeight - y - 1;
00224     rawFastHLine(x, y, h, color);
00225     break;
00226   }
00227 }
00228 
00229 void Adafruit_SSD1351::drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color) {
00230   // Transform x and y based on current rotation.
00231   switch (getRotation()) {
00232   case 0:  // No rotation.
00233     rawFastHLine(x, y, w, color);
00234     break;
00235   case 1:  // Rotated 90 degrees clockwise.
00236     SWAP(x, y);
00237     x = _rawWidth - x - 1;
00238     rawFastVLine(x, y, w, color);
00239     break;
00240   case 2:  // Rotated 180 degrees clockwise.
00241     x = _rawWidth - x - w;
00242     y = _rawHeight - y - 1;
00243     rawFastHLine(x, y, w, color);
00244     break;
00245   case 3:  // Rotated 270 degrees clockwise.
00246     SWAP(x, y);
00247     y = _rawHeight - y - w;
00248     rawFastVLine(x, y, w, color);
00249     break;
00250   }
00251 }
00252 
00253 void Adafruit_SSD1351::drawPixel(int16_t x, int16_t y, uint16_t color)
00254 {
00255   // Transform x and y based on current rotation.
00256   switch (getRotation()) {
00257   // Case 0: No rotation
00258   case 1:  // Rotated 90 degrees clockwise.
00259     SWAP(x, y);
00260     x = _rawWidth - x - 1;
00261     break;
00262   case 2:  // Rotated 180 degrees clockwise.
00263     x = _rawWidth - x - 1;
00264     y = _rawHeight - y - 1;
00265     break;
00266   case 3:  // Rotated 270 degrees clockwise.
00267     SWAP(x, y);
00268     y = _rawHeight - y - 1;
00269     break;
00270   }
00271 
00272   // Bounds check.
00273   if ((x >= SSD1351WIDTH) || (y >= SSD1351HEIGHT)) return;
00274   if ((x < 0) || (y < 0)) return;
00275 
00276   goTo(x, y);
00277   
00278   writeData(color >> 8);    
00279   writeData(color);
00280 }
00281 
00282 void Adafruit_SSD1351::begin(void) {
00283 #if 0
00284     // set pin directions
00285     pinMode(_rs, OUTPUT);
00286     
00287     if (_sclk) {
00288         pinMode(_sclk, OUTPUT);
00289         
00290         pinMode(_sid, OUTPUT);
00291     } else {
00292         // using the hardware SPI
00293         SPI.begin();
00294         SPI.setDataMode(SPI_MODE3);
00295     }
00296     
00297     // Toggle RST low to reset; CS low so it'll listen to us
00298     pinMode(_cs, OUTPUT);
00299     digitalWrite(_cs, LOW);
00300     
00301     if (_rst) {
00302         pinMode(_rst, OUTPUT);
00303         digitalWrite(_rst, HIGH);
00304         delay(500);
00305         digitalWrite(_rst, LOW);
00306         delay(500);
00307         digitalWrite(_rst, HIGH);
00308         delay(500);
00309     }
00310 #endif
00311 
00312     _spi.format(8,3);
00313     //_spi.format(9,3);// no D/C# pin
00314     //_spi.frequency(2000000);
00315 
00316     _cs = 0;
00317     _reset = 1;
00318     wait(0.1f);
00319     _reset = 0;
00320     wait(0.1f);
00321     _reset = 1;
00322     wait(0.1f);
00323 
00324     // Initialization Sequence
00325     writeCommand(SSD1351_CMD_COMMANDLOCK);  // set command lock
00326     writeData(0x12);  
00327     writeCommand(SSD1351_CMD_COMMANDLOCK);  // set command lock
00328     writeData(0xB1);
00329 
00330     writeCommand(SSD1351_CMD_DISPLAYOFF);       // 0xAE
00331 
00332     writeCommand(SSD1351_CMD_CLOCKDIV);         // 0xB3
00333     writeCommand(0xF1);                         // 7:4 = Oscillator Frequency, 3:0 = CLK Div Ratio (A[3:0]+1 = 1..16)
00334     
00335     writeCommand(SSD1351_CMD_MUXRATIO);
00336     writeData(127);
00337     
00338     writeCommand(SSD1351_CMD_SETREMAP);
00339     writeData(0x74);
00340   
00341     writeCommand(SSD1351_CMD_SETCOLUMN);
00342     writeData(0x00);
00343     writeData(0x7F);
00344     writeCommand(SSD1351_CMD_SETROW);
00345     writeData(0x00);
00346     writeData(0x7F);
00347 
00348     writeCommand(SSD1351_CMD_STARTLINE);        // 0xA1
00349     if (SSD1351HEIGHT == 96) {
00350       writeData(96);
00351     } else {
00352       writeData(0);
00353     }
00354 
00355 
00356     writeCommand(SSD1351_CMD_DISPLAYOFFSET);    // 0xA2
00357     writeData(0x0);
00358 
00359     writeCommand(SSD1351_CMD_SETGPIO);
00360     writeData(0x00);
00361     
00362     writeCommand(SSD1351_CMD_FUNCTIONSELECT);
00363     writeData(0x01); // internal (diode drop)
00364     //writeData(0x01); // external bias
00365 
00366 //    writeCommand(SSSD1351_CMD_SETPHASELENGTH);
00367 //    writeData(0x32);
00368 
00369     writeCommand(SSD1351_CMD_PRECHARGE);        // 0xB1
00370     writeCommand(0x32);
00371  
00372     writeCommand(SSD1351_CMD_VCOMH);            // 0xBE
00373     writeCommand(0x05);
00374 
00375     writeCommand(SSD1351_CMD_NORMALDISPLAY);    // 0xA6
00376 
00377     writeCommand(SSD1351_CMD_CONTRASTABC);
00378     writeData(0xC8);
00379     writeData(0x80);
00380     writeData(0xC8);
00381 
00382     writeCommand(SSD1351_CMD_CONTRASTMASTER);
00383     writeData(0x0F);
00384 
00385     writeCommand(SSD1351_CMD_SETVSL );
00386     writeData(0xA0);
00387     writeData(0xB5);
00388     writeData(0x55);
00389     
00390     writeCommand(SSD1351_CMD_PRECHARGE2);
00391     writeData(0x01);
00392     
00393     writeCommand(SSD1351_CMD_DISPLAYON);        //--turn on oled panel    
00394 }
00395 
00396 void  Adafruit_SSD1351::invert(bool v) {
00397    if (v) {
00398      writeCommand(SSD1351_CMD_INVERTDISPLAY);
00399    } else {
00400         writeCommand(SSD1351_CMD_NORMALDISPLAY);
00401    }
00402  }