Chris Taylor / Mbed 2 deprecated RETRO-SpaceRaiders Featured

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LCD_ST7735.cpp Source File

LCD_ST7735.cpp

00001 #include "mbed.h"
00002 #include "LCD_ST7735.h"
00003 
00004 const uint16_t LCD_ST7735::DefaultPalette[] = {
00005     0x0000, // 0  - Black
00006     0x0019, // 1  - Blue
00007     0xc800, // 2  - Red
00008     0xc819, // 3  - Magenta
00009     0x0660, // 4  - Green
00010     0x0679, // 5  - Cyan
00011     0xce60, // 6  - Yellow
00012     0xce79, // 7  - White        
00013     0x001f, // 8  - Bright Blue
00014     0xf800, // 9  - Bright Red
00015     0xf81f, // 10 - Bright Magenta
00016     0x07e0, // 11 - Bright Green
00017     0x07ff, // 12 - Bright Cyan
00018     0xffe0, // 13 - Bright Yellow
00019     0xffff, // 14 - Bright White
00020 };
00021 
00022 LCD_ST7735::LCD_ST7735(
00023     PinName backlightPin,
00024     PinName resetPin,
00025     PinName dsPin,
00026     PinName mosiPin,
00027     PinName misoPin,
00028     PinName clkPin,
00029     PinName csPin,
00030     PanelColorFilter colorFilter
00031     ) :
00032         _colorFilter(colorFilter),
00033         _backlight(backlightPin, 0),
00034         _reset(resetPin, 1),
00035         _ds(dsPin, 0),
00036         _cs(csPin, 1),
00037         _spi(mosiPin, misoPin, clkPin)        
00038 {        
00039     _spi.format(8, 3);
00040     _spi.frequency(18000000);
00041     
00042     initDisplay();
00043     clearScreen();
00044     setForegroundColor(0xffff);
00045     setBackgroundColor(0x0000);    
00046     
00047     _palette = (uint16_t*)DefaultPalette;    
00048 }
00049 
00050 void LCD_ST7735::setOrientation(Orientation orientation, bool flip)
00051 {
00052     const static uint8_t my = 0x80; 
00053     const static uint8_t mx = 0x40;
00054     const static uint8_t mv = 0x20;
00055     
00056     uint8_t madctlData = _colorFilter;
00057     switch(orientation)
00058     {
00059         case Rotate0:
00060             _width = 128;
00061             _height = 160;
00062             madctlData |= flip ? mx : 0;
00063             break;
00064             
00065         case Rotate90:
00066             _width = 160;
00067             _height = 128;
00068             madctlData |= flip ? my | mv | mx : mv | mx;
00069             break;
00070             
00071         case Rotate180:
00072             _width = 128;
00073             _height = 160;
00074             madctlData |= flip ? my : mx | my;
00075             break;
00076             
00077         case Rotate270:
00078             _width = 160;
00079             _height = 128;
00080             madctlData |= flip ? mv : mv | my;
00081             break;
00082     }
00083     write(CMD_MADCTL, (uint8_t[]){madctlData}, 1);
00084 }
00085 
00086 int LCD_ST7735::getWidth()
00087 {
00088     return _width;
00089 }
00090         
00091 int LCD_ST7735::getHeight()
00092 {
00093     return _height;
00094 }
00095 
00096 void LCD_ST7735::setBacklight(bool state)
00097 {
00098     _backlight = state ? 1 : 0;
00099 }
00100 
00101 void LCD_ST7735::clearScreen(uint16_t color)
00102 {
00103     clipRect(0, 0, _width - 1, _height - 1);
00104     beginBatchCommand(CMD_RAMWR);
00105     uint8_t colorHigh = color >> 8;
00106     uint8_t colorLow = color;
00107     for(int i = 0; i < 128 * 160 * 2; ++i)
00108     {
00109         writeBatchData(colorHigh, colorLow);
00110     }
00111     endBatchCommand();
00112 }
00113 
00114 void LCD_ST7735::setPixel(int x, int y, uint16_t color)
00115 {
00116     write(CMD_CASET, (uint8_t[]){0, x, 0, x}, 4);    
00117     write(CMD_RASET, (uint8_t[]){0, y, 0, y}, 4);    
00118     write(CMD_RAMWR, color);
00119 }
00120 
00121 void LCD_ST7735::drawLine(int x1, int y1, int x2, int y2, uint16_t color)
00122 {
00123     int dx = abs(x2 - x1);
00124     int dy = abs(y2 - y1);
00125     
00126     if (dx == 0) 
00127     {
00128         if (y1 > y2) swap(y1, y2);
00129         drawVertLine(x1, y1, y2, color);
00130         return;
00131     }
00132     else if(dy == 0)
00133     {
00134         if (x1 > x2) swap(x1, x2);
00135         drawHorizLine(x1, y1, x2, color);
00136         return;
00137     }
00138    
00139     int sx = (x1 < x2) ? 1 : -1;
00140     int sy = (y1 < y2) ? 1 : -1;
00141     int err = dx - dy;
00142     while(x1 != x2 || y1 != y2)
00143     {
00144         setPixel(x1, y1, color);
00145         int e2 = err << 1;
00146         if (e2 > -dy)
00147         {
00148             err -= dy;
00149             x1 += sx;            
00150         }
00151         if (e2 < dx)
00152         {
00153             err += dx;
00154             y1 += sy;
00155         }
00156     }
00157     setPixel(x2, y2, color);
00158 }
00159 
00160 void LCD_ST7735::swap(int &a, int &b)
00161 {
00162     int t = a;
00163     a = b;
00164     b = t;
00165 }
00166 
00167 void LCD_ST7735::drawRect(int x1, int y1, int x2, int y2, uint16_t color)
00168 {
00169     if (x1 > x2) swap(x1, x2);
00170     if (y1 > y2) swap(y1, y2);
00171     
00172     drawHorizLine(x1, y1, x2, color);
00173     drawHorizLine(x1, y2, x2, color);
00174     drawVertLine(x1, y1, y2, color);
00175     drawVertLine(x2, y1, y2, color);
00176 }
00177 
00178 void LCD_ST7735::drawCircle(int x, int y, int r, uint16_t color)
00179 {
00180     int ix = r;
00181     int iy = 0;
00182     int err = 1 - r;
00183     
00184     while(ix >= iy)
00185     {
00186         setPixel(x + ix, y + iy, color);
00187         setPixel(x + iy, y + ix, color);
00188         setPixel(x - ix, y + iy, color);
00189         setPixel(x - iy, y + ix, color);
00190         setPixel(x - ix, y - iy, color);
00191         setPixel(x - iy, y - ix, color);
00192         setPixel(x + ix, y - iy, color);
00193         setPixel(x + iy, y - ix, color);
00194         iy++;
00195         if (err < 0)
00196         {
00197             err += 2 * iy + 1;
00198         }
00199         else
00200         {
00201             ix--;
00202             err += 2 * (iy - ix + 1);
00203         }
00204     }
00205 }
00206 
00207 void LCD_ST7735::drawEllipse(int x, int y, int rx, int ry, uint16_t color)
00208 {
00209     int a2 = rx * rx;
00210     int b2 = ry * ry;
00211     int fa2 = 4 * a2;
00212     int fb2 = 4 * b2;
00213     
00214     int ix, iy, sigma;    
00215     for (ix = 0, iy = ry, sigma = 2 * b2 + a2 * (1 - 2 * ry); b2 * ix <= a2 * iy; ix++)
00216     {
00217         setPixel(x + ix, y + iy, color);
00218         setPixel(x - ix, y + iy, color);
00219         setPixel(x + ix, y - iy, color);
00220         setPixel(x - ix, y - iy, color);
00221         if (sigma >= 0)
00222         {
00223             sigma+= fa2 * (1 - iy);
00224             iy--;
00225         }
00226         sigma += b2 * ((4 * ix) + 6);
00227     }
00228     
00229     for (ix = rx, iy = 0, sigma = 2 * a2 + b2 * (1 - 2 * rx); a2 * iy <= b2 * ix; iy++)
00230     {
00231         setPixel(x + ix, y + iy, color);
00232         setPixel(x - ix, y + iy, color);
00233         setPixel(x + ix, y - iy, color);
00234         setPixel(x - ix, y - iy, color);
00235         if (sigma >= 0)
00236         {
00237             sigma+= fb2 * (1 - ix);
00238             ix--;
00239         }
00240         sigma += a2 * ((4 * iy) + 6);
00241     }
00242 }
00243 void LCD_ST7735::fillRect(int x1, int y1, int x2, int y2, uint16_t fillColor)
00244 {
00245     clipRect(x1, y1, x2, y2);
00246     int c = ((x2-x1) * (y2-y1)) << 1;
00247     uint8_t colorHigh = fillColor >> 8;
00248     uint8_t colorLow = fillColor;
00249     beginBatchCommand(CMD_RAMWR);
00250     while(c--)
00251     {
00252         writeBatchData(colorHigh, colorLow);
00253     }
00254     endBatchCommand();
00255 }
00256 
00257 void LCD_ST7735::fillRect(int x1, int y1, int x2, int y2, uint16_t borderColor, uint16_t fillColor)
00258 {
00259     if (x1 > x2) swap(x1, x2);
00260     if (y1 > y2) swap(y1, y2);
00261     
00262     drawRect(x1, y1, x2, y2, borderColor);
00263     clipRect(x1 + 1, y1 + 1, x2 - 1, y2 - 1);
00264     int c = ((x2-x1-2) * (y2-y1-2)) << 1;
00265     uint8_t colorHigh = fillColor >> 8;
00266     uint8_t colorLow = fillColor;
00267     beginBatchCommand(CMD_RAMWR);
00268     while(c--)
00269     {
00270         writeBatchData(colorHigh, colorLow);
00271     }
00272     endBatchCommand();
00273 }
00274 
00275 void LCD_ST7735::fillCircle(int x, int y, int r, uint16_t borderColor, uint16_t fillColor)
00276 {
00277     int ix = r;
00278     int iy = 0;
00279     int err = 1 - r;
00280     
00281     while(ix >= iy)
00282     {
00283         setPixel(x - ix, y + iy, borderColor);
00284         setPixel(x + ix, y + iy, borderColor);        
00285         drawHorizLine(x - ix + 1, y + iy, x + ix - 1, fillColor);
00286         
00287         setPixel(x - iy, y + ix, borderColor);
00288         setPixel(x + iy, y + ix, borderColor);                
00289         drawHorizLine(x - iy + 1, y + ix, x + iy - 1, fillColor);
00290                 
00291         setPixel(x - ix, y - iy, borderColor);
00292         setPixel(x + ix, y - iy, borderColor);
00293         drawHorizLine(x - ix + 1, y - iy, x + ix - 1, fillColor);
00294         
00295         setPixel(x - iy, y - ix, borderColor);        
00296         setPixel(x + iy, y - ix, borderColor);
00297         drawHorizLine(x - iy + 1, y - ix, x + iy - 1, fillColor);
00298         iy++;
00299         if (err < 0)
00300         {
00301             err += 2 * iy + 1;
00302         }
00303         else
00304         {
00305             ix--;
00306             err += 2 * (iy - ix + 1);
00307         }
00308     }
00309 }
00310 
00311 void LCD_ST7735::fillEllipse(int x, int y, int rx, int ry, uint16_t borderColor, uint16_t fillColor)
00312 {
00313     int a2 = rx * rx;
00314     int b2 = ry * ry;
00315     int fa2 = 4 * a2;
00316     int fb2 = 4 * b2;
00317     
00318     int ix, iy, sigma;    
00319     for (ix = 0, iy = ry, sigma = 2 * b2 + a2 * (1 - 2 * ry); b2 * ix <= a2 * iy; ix++)
00320     {
00321         setPixel(x + ix, y + iy, borderColor);
00322         setPixel(x - ix, y + iy, borderColor);
00323         drawHorizLine(x - ix + 1, y + iy, x + ix - 1, fillColor);
00324         
00325         setPixel(x + ix, y - iy, borderColor);
00326         setPixel(x - ix, y - iy, borderColor);
00327         drawHorizLine(x - ix + 1, y - iy, x + ix - 1, fillColor);
00328         
00329         if (sigma >= 0)
00330         {
00331             sigma+= fa2 * (1 - iy);
00332             iy--;
00333         }
00334         sigma += b2 * ((4 * ix) + 6);
00335     }
00336     
00337     for (ix = rx, iy = 0, sigma = 2 * a2 + b2 * (1 - 2 * rx); a2 * iy <= b2 * ix; iy++)
00338     {
00339         setPixel(x + ix, y + iy, borderColor);
00340         setPixel(x - ix, y + iy, borderColor);
00341         drawHorizLine(x - ix + 1, y + iy, x + ix - 1, fillColor);
00342         
00343         setPixel(x + ix, y - iy, borderColor);
00344         setPixel(x - ix, y - iy, borderColor);
00345         drawHorizLine(x - ix + 1, y - iy, x + ix - 1, fillColor);
00346         if (sigma >= 0)
00347         {
00348             sigma+= fb2 * (1 - ix);
00349             ix--;
00350         }
00351         sigma += a2 * ((4 * iy) + 6);
00352     }
00353 }
00354 
00355 void LCD_ST7735::drawBitmap(int x, int y, const uint16_t *pbmp)
00356 {
00357     int w = *pbmp++;
00358     int h = *pbmp++;
00359     
00360     drawBitmap(x, y, pbmp, 0, 0, w, h);
00361 }
00362 
00363 void LCD_ST7735::drawBitmap(int x, int y, const uint16_t *pbmp, int srcX, int srcY, int srcWidth, int srcHeight)
00364 {
00365     // Clip if out of screen    
00366     if ((x >= _width) || (x + srcWidth < 0) || 
00367         (y >= _height) || (y + srcHeight < 0))
00368     {
00369         return;
00370     }
00371     
00372     // Clip X
00373     if (x < 0) { srcX += -x; srcWidth += x; x = 0; }
00374     if (x + srcWidth >= _width) { srcWidth += _width - (x + srcWidth); }
00375     
00376     // Clip Y
00377     if (y  < 0) {srcY += -y; srcHeight += y; y = 0; }  
00378     if (y + srcHeight >= _height) { srcHeight += _height - (y + srcHeight); }
00379     
00380     int w = *pbmp++;
00381     int h = *pbmp++;
00382     
00383     clip(x, y, srcWidth, srcHeight);
00384     beginBatchCommand(CMD_RAMWR);    
00385     const uint16_t *p = pbmp + srcX + (srcY * w);
00386     for(int iy = 0; iy < srcHeight; ++iy)
00387     {
00388         for(int ix = 0; ix < srcWidth; ++ix)
00389         {
00390             writeBatchData(*(p + ix));
00391         }
00392         p += w;
00393     } 
00394     endBatchCommand();
00395 }
00396 
00397 void LCD_ST7735::drawBitmap(int x, int y, Bitmap4bpp &bmp, int srcX, int srcY, int srcWidth, int srcHeight)
00398 {
00399     // Clip if out of screen    
00400     if ((x >= _width) || (x + srcWidth < 0) || 
00401         (y >= _height) || (y + srcHeight < 0))
00402     {
00403         return;
00404     }
00405     
00406     // Clip X
00407     if (x < 0) { srcX += -x; srcWidth += x; x = 0; }
00408     if (x + srcWidth >= _width) { srcWidth += _width - (x + srcWidth); }
00409     
00410     // Clip Y
00411     if (y  < 0) {srcY += -y; srcHeight += y; y = 0; }  
00412     if (y + srcHeight >= _height) { srcHeight += _height - (y + srcHeight); }
00413     
00414     int stride = bmp.getStride();
00415     
00416     bool oddStart = srcX & 0x01;
00417     bool oddWidth = srcWidth & 0x01;    
00418     bool oddEnd = oddStart ^ oddWidth;
00419     
00420     int startX = oddStart ? 1 : 0;
00421     int endX = (oddEnd ? srcWidth : srcWidth + 1) >> 1;    
00422     
00423     const uint8_t *p = bmp.getBitmapData() + (srcX >> 1) + (srcY * stride);    
00424 
00425     clip(x, y, srcWidth, srcHeight);
00426     beginBatchCommand(CMD_RAMWR);        
00427     for(int iy = 0; iy < srcHeight; ++iy, p += stride)
00428     {
00429         if (oddStart) writeBatchData(_palette[*p & 0x0f]);
00430         for(int ix = startX; ix < endX; ++ix)
00431         {
00432             uint8_t c = *(p + ix);
00433             writeBatchData(_palette[(c >> 4) & 0x0f]);
00434             writeBatchData(_palette[c & 0x0f]);
00435         }        
00436         if (oddEnd) writeBatchData(_palette[(*(p + endX) >> 4) & 0x0f]);
00437     } 
00438     endBatchCommand();
00439 }
00440 
00441 void LCD_ST7735::drawBitmap(int x, int y, Bitmap1bpp &bmp, int srcX, int srcY, int srcWidth, int srcHeight, uint16_t foregroundColor, uint16_t backgroundColor)
00442 {
00443     // Clip if out of screen    
00444     if ((x >= _width) || (x + srcWidth < 0) || 
00445         (y >= _height) || (y + srcHeight < 0))
00446     {
00447         return;
00448     }
00449     
00450     // Clip X
00451     if (x < 0) { srcX += -x; srcWidth += x; x = 0; }
00452     if (x + srcWidth >= _width) { srcWidth += _width - (x + srcWidth); }
00453     
00454     // Clip Y
00455     if (y  < 0) {srcY += -y; srcHeight += y; y = 0; }  
00456     if (y + srcHeight >= _height) { srcHeight += _height - (y + srcHeight); }
00457     
00458     uint8_t fch = foregroundColor >> 8;
00459     uint8_t fcl = foregroundColor;
00460     uint8_t bch = backgroundColor >> 8;
00461     uint8_t bcl = backgroundColor;
00462     
00463     clip(x, y, srcWidth, srcHeight);
00464     
00465     int offset = (bmp.getStride() * srcY) + (srcX / 8);
00466     int startbits = srcX % 8;
00467     
00468     beginBatchCommand(CMD_RAMWR);
00469     for(int r = 0; r < srcHeight; ++r)
00470     {
00471         const uint8_t *p = bmp.getBitmapData() + offset;
00472         
00473         uint8_t b = *p;
00474         for (int c = 0, shift = startbits; c < srcWidth; ++c, ++shift)
00475         {
00476             if (shift == 8) 
00477             {
00478                 shift = 0;
00479                 b = *++p;
00480             }
00481             
00482             if ((b << shift) & 0x80)                            
00483             {
00484                 writeBatchData(fch, fcl);                
00485             }
00486             else
00487             {
00488                 writeBatchData(bch, bcl);                
00489             }
00490         }   
00491         offset += bmp.getStride();             
00492     }
00493     endBatchCommand();
00494 }
00495 
00496 void LCD_ST7735::drawBitmap(int x, int y, const uint8_t *pbmp, int srcX, int srcY, int srcWidth, int srcHeight, uint16_t foregroundColor, uint16_t backgroundColor)
00497 {
00498     // Clip if out of screen    
00499     if ((x >= _width) || (x + srcWidth < 0) || 
00500         (y >= _height) || (y + srcHeight < 0))
00501     {
00502         return;
00503     }
00504     
00505     // Clip X
00506     if (x < 0) { srcX += -x; srcWidth += x; x = 0; }
00507     if (x + srcWidth >= _width) { srcWidth += _width - (x + srcWidth); }
00508     
00509     // Clip Y
00510     if (y  < 0) {srcY += -y; srcHeight += y; y = 0; }  
00511     if (y + srcHeight >= _height) { srcHeight += _height - (y + srcHeight); }
00512     
00513     uint8_t fch = foregroundColor >> 8;
00514     uint8_t fcl = foregroundColor;
00515     uint8_t bch = backgroundColor >> 8;
00516     uint8_t bcl = backgroundColor;
00517     
00518     uint16_t w = (*(pbmp + 1) << 8) | (*(pbmp + 0));    
00519     pbmp += 4;
00520     
00521     int stride = w / 8;
00522     clip(x, y, srcWidth, srcHeight);
00523     
00524     int offset = (stride * srcY) + (srcX / 8);
00525     int startbits = srcX % 8;
00526     
00527     beginBatchCommand(CMD_RAMWR);
00528     for(int r = 0; r < srcHeight; ++r)
00529     {
00530         const uint8_t *p = pbmp + offset;
00531         
00532         uint8_t b = *p;
00533         for (int c = 0, shift = startbits; c < srcWidth; ++c, ++shift)
00534         {
00535             if (shift == 8) 
00536             {
00537                 shift = 0;
00538                 b = *++p;
00539             }
00540             
00541             if ((b << shift) & 0x80)                            
00542             {
00543                 writeBatchData(fch, fcl);                
00544             }
00545             else
00546             {
00547                 writeBatchData(bch, bcl);                
00548             }
00549         }   
00550         offset += stride;             
00551     }
00552     endBatchCommand();
00553 }
00554 
00555 void LCD_ST7735::setForegroundColor(uint16_t color)
00556 {
00557     _foregroundColorHigh = color >> 8;
00558     _foregroundColorLow = color;
00559 }
00560 
00561 void LCD_ST7735::setBackgroundColor(uint16_t color)
00562 {
00563     _backgroundColorHigh = color >> 8;
00564     _backgroundColorLow = color;
00565 }
00566         
00567 void LCD_ST7735::drawString(const uint8_t *pFont, int x, int y, const char *pString)
00568 {
00569     char *p = (char*)pString;
00570     while(*p != 0)
00571     {
00572         drawChar(pFont, x, y, *p++);
00573         x += 8;
00574     }
00575 }
00576 
00577 void LCD_ST7735::selectDevice()
00578 {
00579     _spi.prepareFastSPI();
00580 }
00581 
00582 void LCD_ST7735::drawVertLine(int x1, int y1, int y2, uint16_t color)
00583 {
00584     clipRect(x1, y1, x1, y2);
00585     beginBatchCommand(CMD_RAMWR);
00586     int c = (y2 - y1) << 1;
00587     uint8_t colorHigh = color >> 8;
00588     uint8_t colorLow = color;
00589     for (int i = 0; i < c; ++i)
00590     {
00591         writeBatchData(colorHigh, colorLow);        
00592     }
00593     endBatchCommand();
00594 }
00595 
00596 void LCD_ST7735::drawHorizLine(int x1, int y1, int x2, uint16_t color)
00597 {    
00598     clipRect(x1, y1, x2, y1);
00599     beginBatchCommand(CMD_RAMWR);
00600     int c = (x2 - x1) << 1;
00601     uint8_t colorHigh = color >> 8;
00602     uint8_t colorLow = color;
00603     for (int i = 0; i < c; ++i)
00604     {
00605         writeBatchData(colorHigh, colorLow);
00606     }
00607     endBatchCommand();
00608 }
00609 
00610 void LCD_ST7735::drawChar(const uint8_t *pFont, int x, int y, char c)
00611 {
00612     const uint8_t *pChar = pFont + (c * 8);
00613     
00614     clip(x, y, 8, 8);
00615     beginBatchCommand(CMD_RAMWR);
00616     for(int r = 0; r < 8; ++r)
00617     {
00618         uint8_t b = pChar[r];
00619         for(int c = 0; c < 8; ++c)
00620         {
00621             if (b & 0x80)
00622             {
00623                 writeBatchData(_foregroundColorHigh);
00624                 writeBatchData(_foregroundColorLow);
00625             }
00626             else
00627             {
00628                 writeBatchData(_backgroundColorHigh);
00629                 writeBatchData(_backgroundColorLow);
00630             }
00631                 
00632             b <<= 1;
00633         }
00634     }
00635     endBatchCommand();
00636 }
00637 
00638 void LCD_ST7735::initDisplay()
00639 {
00640     selectDevice();
00641     reset();
00642     
00643     writeCommand(CMD_SLPOUT);
00644     
00645     write(CMD_FRMCTR1, (uint8_t[]){0x01, 0x2c, 0x2d}, 3);
00646     write(CMD_FRMCTR2, (uint8_t[]){0x01, 0x2c, 0x2d}, 3);
00647     write(CMD_FRMCTR3, (uint8_t[]){0x01, 0x2c, 0x2d, 0x01, 0x2c, 0x2d}, 6);
00648     
00649     write(CMD_INVCTR, (uint8_t[]){0x07}, 1);
00650     
00651     write(CMD_PWCTR1, (uint8_t[]){0xa2, 0x02, 0x84}, 3);
00652     write(CMD_PWCTR2, (uint8_t[]){0xc5}, 1);
00653     write(CMD_PWCTR3, (uint8_t[]){0x0a, 0x00}, 2);
00654     write(CMD_PWCTR4, (uint8_t[]){0x8a, 0x2a}, 2);
00655     write(CMD_PWCTR5, (uint8_t[]){0x8a, 0xee}, 2);
00656     
00657     write(CMD_VMCTR1, (uint8_t[]){0x0e}, 1);
00658     
00659     write(CMD_MADCTL, (uint8_t[]){0xc0 | _colorFilter}, 1);
00660     
00661     // Gama sequence
00662     write(CMD_GAMCTRP1, (uint8_t[])
00663         {
00664             0x0f, 0x1a,
00665             0x0f, 0x18,
00666             0x2f, 0x28,
00667             0x20, 0x22,
00668             0x1f, 0x1b,
00669             0x23, 0x37,
00670             0x00, 0x07,
00671             0x02, 0x10
00672         }, 16);
00673         
00674     write(CMD_GAMCTRN1, (uint8_t[])
00675         {
00676             0x0f, 0x1b,
00677             0x0f, 0x17,
00678             0x33, 0x2c,
00679             0x29, 0x2e,
00680             0x30, 0x30,
00681             0x39, 0x3f,
00682             0x00, 0x07,
00683             0x03, 0x10
00684         }, 16);
00685         
00686     write(CMD_CASET, (uint8_t[]){0x00, 0x00, 0x00, 0x7f}, 4);
00687     write(CMD_RASET, (uint8_t[]){0x00, 0x00, 0x00, 0x9f}, 4);
00688     
00689     write(CMD_EXTCTRL, (uint8_t[]){0x01}, 1);            
00690     
00691     // Disable RAM power save
00692     write(0xf6, (uint8_t[]){0x00}, 1);                    
00693     
00694     // 65k color mode
00695     write(CMD_COLMOD, (uint8_t[]){0x05}, 1);            
00696     
00697     // Enable display
00698     writeCommand(CMD_DISPON);            
00699     
00700     setBacklight(true);
00701 }
00702 
00703 void LCD_ST7735::reset()
00704 {
00705     _reset = 0;
00706     wait_us(100);
00707     _reset = 1;
00708     wait_us(100);
00709 }
00710 
00711 void LCD_ST7735::clip(int x, int y, int w, int h)
00712 {
00713     clipRect(x, y, (x + w) - 1, (y + h) - 1);
00714 }
00715 
00716 void LCD_ST7735::clipRect(int x1, int y1, int x2, int y2)
00717 {
00718     uint8_t x1l = (uint8_t)x1;
00719     //uint8_t x1h = (uint8_t)(x1 >> 8);
00720     uint8_t x2l = (uint8_t)x2;
00721     //uint8_t x2h = (uint8_t)(x2 >> 8);
00722     write(CMD_CASET, (uint8_t[]){0, x1l, 0, x2l}, 4);    
00723     
00724     uint8_t y1l = (uint8_t)y1;
00725     //uint8_t y1h = (uint8_t)(y1 >> 8);
00726     uint8_t y2l = (uint8_t)y2;
00727     //uint8_t y2h = (uint8_t)(y2 >> 8);
00728     write(CMD_RASET, (uint8_t[]){0, y1l, 0, y2l}, 4);    
00729 }
00730         
00731 void LCD_ST7735::writeCommand(uint8_t cmd)
00732 {
00733     _cs = 0;
00734     _ds = 0;    
00735     _spi.fastWrite(cmd);
00736     _spi.waitWhileBusy();
00737     _spi.clearRx();
00738     _cs = 1;
00739 }
00740 
00741 void LCD_ST7735::write(uint8_t cmd, uint8_t data[], int dataLen)
00742 {
00743     _cs = 0;
00744     _ds = 0;    
00745     _spi.fastWrite(cmd);
00746     _spi.waitWhileBusy();
00747     if (data != NULL & dataLen > 0)
00748     {
00749         _ds = 1;        
00750         for(int i = 0; i < dataLen; ++i)
00751         {            
00752             _spi.fastWrite(data[i]); 
00753         }        
00754         _spi.waitWhileBusy();
00755         _ds = 0; 
00756     }    
00757     _spi.clearRx();
00758     _cs = 1;
00759 }
00760 
00761 void LCD_ST7735::write(uint8_t cmd, uint16_t data)
00762 {
00763     _cs = 0; 
00764     _ds = 0;    
00765     _spi.fastWrite(cmd);       
00766     _spi.waitWhileBusy();
00767     _ds = 1;            
00768     _spi.fastWrite(data >> 8);
00769     _spi.fastWrite(data);
00770     _spi.waitWhileBusy();
00771     _spi.clearRx();
00772     _ds = 0;     
00773     _cs = 1;
00774 }
00775 
00776 void LCD_ST7735::beginBatchCommand(uint8_t cmd)
00777 {
00778     _cs = 0;
00779     _ds = 0;        
00780     _spi.fastWrite(cmd);   
00781     _spi.waitWhileBusy();
00782     _ds = 1;
00783 }
00784 
00785 void LCD_ST7735::writeBatchData(uint8_t data)
00786 {
00787     _spi.fastWrite(data);
00788 }
00789 
00790 void LCD_ST7735::writeBatchData(uint8_t dataHigh, uint8_t dataLow)
00791 {
00792     _spi.fastWrite(dataHigh);
00793     _spi.fastWrite(dataLow);
00794 }
00795 
00796 
00797 void LCD_ST7735::writeBatchData(uint16_t data)
00798 {
00799     _spi.fastWrite(data >> 8);
00800     _spi.fastWrite(data); 
00801 }
00802 
00803 void LCD_ST7735::endBatchCommand()
00804 {
00805     _spi.waitWhileBusy();
00806     _spi.clearRx();
00807     _ds = 0; 
00808     _cs = 1; 
00809 }
00810 
00811