SPI based library for the ST7735 LCD controller.

Dependents:   RayCastingEngine RETRO_LCD_PerformanceTest RETRO_loop_test RETRO_RickGame ... more

Revision:
7:f39c980a589c
Parent:
6:67d3d4a953e0
Child:
8:12f16befa7e1
--- a/LCD_ST7735.cpp	Sun Oct 05 22:26:57 2014 +0000
+++ b/LCD_ST7735.cpp	Sun Oct 05 23:57:36 2014 +0000
@@ -77,6 +77,7 @@
 
 void LCD_ST7735::clearScreen(uint16_t color)
 {
+    _spi.prepareFastSPI();
     clipRect(0, 0, _width - 1, _height - 1);
     beginBatchCommand(CMD_RAMWR);
     uint8_t colorHigh = color >> 8;
@@ -90,13 +91,13 @@
 
 void LCD_ST7735::setPixel(int x, int y, uint16_t color)
 {
-    write(CMD_CASET, (uint8_t[]){0, x, 0, x}, 4);    
-    write(CMD_RASET, (uint8_t[]){0, y, 0, y}, 4);    
-    write(CMD_RAMWR, color);
+    _spi.prepareFastSPI();
+    setPixelFast(x, y, color);
 }
 
 void LCD_ST7735::drawLine(int x1, int y1, int x2, int y2, uint16_t color)
 {
+    _spi.prepareFastSPI();
     int dx = abs(x2 - x1);
     int dy = abs(y2 - y1);
     
@@ -116,7 +117,7 @@
     int err = dx - dy;
     while(x1 != x2 || y1 != y2)
     {
-        setPixel(x1, y1, color);
+        setPixelFast(x1, y1, color);
         int e2 = err << 1;
         if (e2 > -dy)
         {
@@ -129,7 +130,7 @@
             y1 += sy;
         }
     }
-    setPixel(x2, y2, color);
+    setPixelFast(x2, y2, color);
 }
 
 void LCD_ST7735::swap(int &a, int &b)
@@ -141,6 +142,7 @@
 
 void LCD_ST7735::drawRect(int x1, int y1, int x2, int y2, uint16_t color)
 {
+    _spi.prepareFastSPI();
     if (x1 > x2) swap(x1, x2);
     if (y1 > y2) swap(y1, y2);
     
@@ -152,20 +154,21 @@
 
 void LCD_ST7735::drawCircle(int x, int y, int r, uint16_t color)
 {
+    _spi.prepareFastSPI();
     int ix = r;
     int iy = 0;
     int err = 1 - r;
     
     while(ix >= iy)
     {
-        setPixel(x + ix, y + iy, color);
-        setPixel(x + iy, y + ix, color);
-        setPixel(x - ix, y + iy, color);
-        setPixel(x - iy, y + ix, color);
-        setPixel(x - ix, y - iy, color);
-        setPixel(x - iy, y - ix, color);
-        setPixel(x + ix, y - iy, color);
-        setPixel(x + iy, y - ix, color);
+        setPixelFast(x + ix, y + iy, color);
+        setPixelFast(x + iy, y + ix, color);
+        setPixelFast(x - ix, y + iy, color);
+        setPixelFast(x - iy, y + ix, color);
+        setPixelFast(x - ix, y - iy, color);
+        setPixelFast(x - iy, y - ix, color);
+        setPixelFast(x + ix, y - iy, color);
+        setPixelFast(x + iy, y - ix, color);
         iy++;
         if (err < 0)
         {
@@ -181,6 +184,7 @@
 
 void LCD_ST7735::drawEllipse(int x, int y, int rx, int ry, uint16_t color)
 {
+    _spi.prepareFastSPI();
     int a2 = rx * rx;
     int b2 = ry * ry;
     int fa2 = 4 * a2;
@@ -189,10 +193,10 @@
     int ix, iy, sigma;    
     for (ix = 0, iy = ry, sigma = 2 * b2 + a2 * (1 - 2 * ry); b2 * ix <= a2 * iy; ix++)
     {
-        setPixel(x + ix, y + iy, color);
-        setPixel(x - ix, y + iy, color);
-        setPixel(x + ix, y - iy, color);
-        setPixel(x - ix, y - iy, color);
+        setPixelFast(x + ix, y + iy, color);
+        setPixelFast(x - ix, y + iy, color);
+        setPixelFast(x + ix, y - iy, color);
+        setPixelFast(x - ix, y - iy, color);
         if (sigma >= 0)
         {
             sigma+= fa2 * (1 - iy);
@@ -203,10 +207,10 @@
     
     for (ix = rx, iy = 0, sigma = 2 * a2 + b2 * (1 - 2 * rx); a2 * iy <= b2 * ix; iy++)
     {
-        setPixel(x + ix, y + iy, color);
-        setPixel(x - ix, y + iy, color);
-        setPixel(x + ix, y - iy, color);
-        setPixel(x - ix, y - iy, color);
+        setPixelFast(x + ix, y + iy, color);
+        setPixelFast(x - ix, y + iy, color);
+        setPixelFast(x + ix, y - iy, color);
+        setPixelFast(x - ix, y - iy, color);
         if (sigma >= 0)
         {
             sigma+= fb2 * (1 - ix);
@@ -217,6 +221,7 @@
 }
 void LCD_ST7735::fillRect(int x1, int y1, int x2, int y2, uint16_t fillColor)
 {
+    _spi.prepareFastSPI();
     clipRect(x1, y1, x2, y2);
     int c = ((x2-x1) * (y2-y1)) << 1;
     uint8_t colorHigh = fillColor >> 8;
@@ -231,6 +236,7 @@
 
 void LCD_ST7735::fillRect(int x1, int y1, int x2, int y2, uint16_t borderColor, uint16_t fillColor)
 {
+    _spi.prepareFastSPI();
     if (x1 > x2) swap(x1, x2);
     if (y1 > y2) swap(y1, y2);
     
@@ -249,26 +255,27 @@
 
 void LCD_ST7735::fillCircle(int x, int y, int r, uint16_t borderColor, uint16_t fillColor)
 {
+    _spi.prepareFastSPI();
     int ix = r;
     int iy = 0;
     int err = 1 - r;
     
     while(ix >= iy)
     {
-        setPixel(x - ix, y + iy, borderColor);
-        setPixel(x + ix, y + iy, borderColor);        
+        setPixelFast(x - ix, y + iy, borderColor);
+        setPixelFast(x + ix, y + iy, borderColor);        
         drawHorizLine(x - ix + 1, y + iy, x + ix - 1, fillColor);
         
-        setPixel(x - iy, y + ix, borderColor);
-        setPixel(x + iy, y + ix, borderColor);                
+        setPixelFast(x - iy, y + ix, borderColor);
+        setPixelFast(x + iy, y + ix, borderColor);                
         drawHorizLine(x - iy + 1, y + ix, x + iy - 1, fillColor);
                 
-        setPixel(x - ix, y - iy, borderColor);
-        setPixel(x + ix, y - iy, borderColor);
+        setPixelFast(x - ix, y - iy, borderColor);
+        setPixelFast(x + ix, y - iy, borderColor);
         drawHorizLine(x - ix + 1, y - iy, x + ix - 1, fillColor);
         
-        setPixel(x - iy, y - ix, borderColor);        
-        setPixel(x + iy, y - ix, borderColor);
+        setPixelFast(x - iy, y - ix, borderColor);        
+        setPixelFast(x + iy, y - ix, borderColor);
         drawHorizLine(x - iy + 1, y - ix, x + iy - 1, fillColor);
         iy++;
         if (err < 0)
@@ -285,6 +292,7 @@
 
 void LCD_ST7735::fillEllipse(int x, int y, int rx, int ry, uint16_t borderColor, uint16_t fillColor)
 {
+    _spi.prepareFastSPI();
     int a2 = rx * rx;
     int b2 = ry * ry;
     int fa2 = 4 * a2;
@@ -293,12 +301,12 @@
     int ix, iy, sigma;    
     for (ix = 0, iy = ry, sigma = 2 * b2 + a2 * (1 - 2 * ry); b2 * ix <= a2 * iy; ix++)
     {
-        setPixel(x + ix, y + iy, borderColor);
-        setPixel(x - ix, y + iy, borderColor);
+        setPixelFast(x + ix, y + iy, borderColor);
+        setPixelFast(x - ix, y + iy, borderColor);
         drawHorizLine(x - ix + 1, y + iy, x + ix - 1, fillColor);
         
-        setPixel(x + ix, y - iy, borderColor);
-        setPixel(x - ix, y - iy, borderColor);
+        setPixelFast(x + ix, y - iy, borderColor);
+        setPixelFast(x - ix, y - iy, borderColor);
         drawHorizLine(x - ix + 1, y - iy, x + ix - 1, fillColor);
         
         if (sigma >= 0)
@@ -311,12 +319,12 @@
     
     for (ix = rx, iy = 0, sigma = 2 * a2 + b2 * (1 - 2 * rx); a2 * iy <= b2 * ix; iy++)
     {
-        setPixel(x + ix, y + iy, borderColor);
-        setPixel(x - ix, y + iy, borderColor);
+        setPixelFast(x + ix, y + iy, borderColor);
+        setPixelFast(x - ix, y + iy, borderColor);
         drawHorizLine(x - ix + 1, y + iy, x + ix - 1, fillColor);
         
-        setPixel(x + ix, y - iy, borderColor);
-        setPixel(x - ix, y - iy, borderColor);
+        setPixelFast(x + ix, y - iy, borderColor);
+        setPixelFast(x - ix, y - iy, borderColor);
         drawHorizLine(x - ix + 1, y - iy, x + ix - 1, fillColor);
         if (sigma >= 0)
         {
@@ -329,6 +337,7 @@
 
 void LCD_ST7735::drawBitmap(int x, int y, const uint16_t *pbmp)
 {
+    _spi.prepareFastSPI();
     int w = *pbmp++;
     int h = *pbmp++;
     
@@ -344,6 +353,7 @@
 
 void LCD_ST7735::drawBitmap(int x, int y, const uint16_t *pbmp, int srcX, int srcY, int srcWidth, int srcHeight)
 {
+    _spi.prepareFastSPI();
     int w = *pbmp++;
     int h = *pbmp++;
     
@@ -375,6 +385,7 @@
         
 void LCD_ST7735::drawString(const uint8_t *pFont, int x, int y, const char *pString)
 {
+    _spi.prepareFastSPI();
     char *p = (char*)pString;
     while(*p != 0)
     {
@@ -383,6 +394,13 @@
     }
 }
 
+void LCD_ST7735::setPixelFast(int x, int y, uint16_t color)
+{
+    write(CMD_CASET, (uint8_t[]){0, x, 0, x}, 4);    
+    write(CMD_RASET, (uint8_t[]){0, y, 0, y}, 4);    
+    write(CMD_RAMWR, color);
+}
+
 void LCD_ST7735::drawVertLine(int x1, int y1, int y2, uint16_t color)
 {
     clipRect(x1, y1, x1, y2);
@@ -441,6 +459,7 @@
 
 void LCD_ST7735::initDisplay()
 {
+    _spi.prepareFastSPI();
     reset();
     
     writeCommand(CMD_SLPOUT);
@@ -610,3 +629,4 @@
     _ds = 0; 
     _cs = 1; 
 }
+