SPI based library for the ST7735 LCD controller.

Dependents:   RayCastingEngine RETRO_LCD_PerformanceTest RETRO_loop_test RETRO_RickGame ... more

Files at this revision

API Documentation at this revision

Comitter:
taylorza
Date:
Sat Sep 20 04:28:41 2014 +0000
Parent:
0:7b3fb3085867
Child:
2:43f08d03a7e2
Commit message:
Added support for:
; 1. Drawing ellipses and drawing filled ellipses.
; 2. Drawing a bitmap from a subsection of an existing bitmap

Changed in this revision

LCD_ST7735.cpp Show annotated file Show diff for this revision Revisions of this file
LCD_ST7735.h Show annotated file Show diff for this revision Revisions of this file
--- a/LCD_ST7735.cpp	Fri Sep 19 02:43:29 2014 +0000
+++ b/LCD_ST7735.cpp	Sat Sep 20 04:28:41 2014 +0000
@@ -16,7 +16,7 @@
         _spi(mosiPin, misoPin, clkPin)        
 {        
     _spi.format(8, 3);
-    _spi.frequency(12000000);
+    _spi.frequency(15000000);
     
     initDisplay();
     clearScreen();
@@ -131,6 +131,43 @@
     }
 }
 
+void LCD_ST7735::drawEllipse(int x, int y, int rx, int ry, uint16_t color)
+{
+    int a2 = rx * rx;
+    int b2 = ry * ry;
+    int fa2 = 4 * a2;
+    int fb2 = 4 * b2;
+    
+    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);
+        if (sigma >= 0)
+        {
+            sigma+= fa2 * (1 - iy);
+            iy--;
+        }
+        sigma += b2 * ((4 * ix) + 6);
+    }
+    
+    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);
+        if (sigma >= 0)
+        {
+            sigma+= fb2 * (1 - ix);
+            ix--;
+        }
+        sigma += a2 * ((4 * iy) + 6);
+    }
+}
+
 void LCD_ST7735::fillRect(int x1, int y1, int x2, int y2, uint16_t borderColor, uint16_t fillColor)
 {
     if (x1 > x2) swap(x1, x2);
@@ -183,6 +220,50 @@
     }
 }
 
+void LCD_ST7735::fillEllipse(int x, int y, int rx, int ry, uint16_t borderColor, uint16_t fillColor)
+{
+    int a2 = rx * rx;
+    int b2 = ry * ry;
+    int fa2 = 4 * a2;
+    int fb2 = 4 * b2;
+    
+    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);
+        drawHorizLine(x - ix + 1, y + iy, x + ix - 1, fillColor);
+        
+        setPixel(x + ix, y - iy, borderColor);
+        setPixel(x - ix, y - iy, borderColor);
+        drawHorizLine(x - ix + 1, y - iy, x + ix - 1, fillColor);
+        
+        if (sigma >= 0)
+        {
+            sigma+= fa2 * (1 - iy);
+            iy--;
+        }
+        sigma += b2 * ((4 * ix) + 6);
+    }
+    
+    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);
+        drawHorizLine(x - ix + 1, y + iy, x + ix - 1, fillColor);
+        
+        setPixel(x + ix, y - iy, borderColor);
+        setPixel(x - ix, y - iy, borderColor);
+        drawHorizLine(x - ix + 1, y - iy, x + ix - 1, fillColor);
+        if (sigma >= 0)
+        {
+            sigma+= fb2 * (1 - ix);
+            ix--;
+        }
+        sigma += a2 * ((4 * iy) + 6);
+    }
+}
+
 void LCD_ST7735::drawBitmap(int x, int y, const uint16_t *pbmp)
 {
     int w = *pbmp++;
@@ -198,6 +279,25 @@
     endBatchCommand();
 }
 
+void LCD_ST7735::drawBitmap(int x, int y, const uint16_t *pbmp, int srcX, int srcY, int srcWidth, int srcHeight)
+{
+    int w = *pbmp++;
+    int h = *pbmp++;
+    
+    clip(x, y, srcWidth, srcHeight);
+    beginBatchCommand(CMD_RAMWR);    
+    const uint16_t *p = pbmp + srcX + (srcY * w);
+    for(int iy = 0; iy < srcHeight; ++iy)
+    {
+        for(int ix = 0; ix < srcWidth; ++ix)
+        {
+            writeBatchData(*(p + ix));
+        }
+        p += w;
+    } 
+    endBatchCommand();
+}
+
 void LCD_ST7735::setForegroundColor(uint16_t color)
 {
     _foregroundColor = color;
--- a/LCD_ST7735.h	Fri Sep 19 02:43:29 2014 +0000
+++ b/LCD_ST7735.h	Sat Sep 20 04:28:41 2014 +0000
@@ -72,6 +72,15 @@
         */
         void drawCircle(int x, int y, int r, uint16_t color);
         
+        /** Draw an ellipse on the display
+         * @param x The X coordinate of the center of the ellipse
+         * @param y The Y coordinate of the center of the ellipse
+         * @param rx The X radius of the ellipse
+         * @param ry The X radius of the ellipse
+         * @param color The color used to draw the ellipse
+        */
+        void drawEllipse(int x, int y, int rx, int ry, uint16_t color);
+        
         /** Draw a filled rectangle on the display 
          * @param x1 The X coordinate of the upper left corner
          * @param y1 The Y coordinate of the upper left corner
@@ -90,6 +99,16 @@
         */
         void fillCircle(int x, int y, int r, uint16_t borderColor, uint16_t fillColor);
         
+        /** Draw a filled ellipse on the display
+         * @param x The X coordinate of the center of the ellipse
+         * @param y The Y coordinate of the center of the ellipse
+         * @param rx The X radius of the ellipse
+         * @param ry The X radius of the ellipse
+         * @param borderColor The color used to draw the circumference of the circle
+         * @param fillColor The color used to fill the circle
+        */
+        void fillEllipse(int x, int y, int rx, int ry, uint16_t borderColor, uint16_t fillColor);
+        
         /** Draw a bitmap on the screen 
          * @param x The X coordinate location to draw the bitmap.
          * @param y The Y coordinate location to draw the bitmap.
@@ -100,6 +119,20 @@
         */
         void drawBitmap(int x, int y, const uint16_t *pbmp);
         
+        /** Extracts a portion of a bitmap and draws it on the screen 
+         * @param x The X coordinate location to draw the bitmap.
+         * @param y The Y coordinate location to draw the bitmap.
+         * @param pbmp Pointer to the bitmap.
+         * @param srcX X offset into the source bitmap of the portion to extract
+         * @param srcY Y offset into the source bitmap of the portion to extract
+         * @param srcWidth Width of the bitmap portion to draw
+         * @param srcHeight Height of the bitmap portion to draw
+         * @note The bitmap is an single dimensional uint8_t (unsigned short) array. 
+         * The first to elements of the array indicate the width and height of the bitmap repectively.
+         * The rest of the entries int the array make up the pixel data for the array.
+        */
+        void drawBitmap(int x, int y, const uint16_t *pbmp, int srcX, int srcY, int srcWidth, int srcHeight);
+        
         /** Set the foreground color used to render text
          * @param color Color used when drawing text to the display
          * @note The color can be changed multiple times to render text in various colors on the display