SPI based library for the ST7735 LCD controller.

Dependents:   RayCastingEngine RETRO_LCD_PerformanceTest RETRO_loop_test RETRO_RickGame ... more

Revision:
4:88d22437119c
Parent:
3:451148656b76
Child:
5:21c987ee68d2
--- a/LCD_ST7735.cpp	Sat Sep 20 19:09:16 2014 +0000
+++ b/LCD_ST7735.cpp	Sun Sep 21 07:01:15 2014 +0000
@@ -79,9 +79,11 @@
 {
     clipRect(0, 0, _width - 1, _height - 1);
     beginBatchCommand(CMD_RAMWR);
+    uint8_t colorHigh = color >> 8;
+    uint8_t colorLow = color;
     for(int i = 0; i < 128 * 160 * 2; ++i)
     {
-        writeBatchData(color);
+        writeBatchData(colorHigh, colorLow);
     }
     endBatchCommand();
 }
@@ -213,6 +215,19 @@
         sigma += a2 * ((4 * iy) + 6);
     }
 }
+void LCD_ST7735::fillRect(int x1, int y1, int x2, int y2, uint16_t fillColor)
+{
+    clipRect(x1, y1, x2, y2);
+    int c = ((x2-x1) * (y2-y1)) << 1;
+    uint8_t colorHigh = fillColor >> 8;
+    uint8_t colorLow = fillColor;
+    beginBatchCommand(CMD_RAMWR);
+    while(c--)
+    {
+        writeBatchData(colorHigh, colorLow);
+    }
+    endBatchCommand();
+}
 
 void LCD_ST7735::fillRect(int x1, int y1, int x2, int y2, uint16_t borderColor, uint16_t fillColor)
 {
@@ -222,10 +237,12 @@
     drawRect(x1, y1, x2, y2, borderColor);
     clipRect(x1 + 1, y1 + 1, x2 - 1, y2 - 1);
     int c = ((x2-x1-2) * (y2-y1-2)) << 1;
+    uint8_t colorHigh = fillColor >> 8;
+    uint8_t colorLow = fillColor;
     beginBatchCommand(CMD_RAMWR);
     while(c--)
     {
-        writeBatchData(fillColor);
+        writeBatchData(colorHigh, colorLow);
     }
     endBatchCommand();
 }
@@ -346,12 +363,14 @@
 
 void LCD_ST7735::setForegroundColor(uint16_t color)
 {
-    _foregroundColor = color;
+    _foregroundColorHigh = color >> 8;
+    _foregroundColorLow = color;
 }
 
 void LCD_ST7735::setBackgroundColor(uint16_t color)
 {
-    _backgroundColor = color;
+    _backgroundColorHigh = color >> 8;
+    _backgroundColorLow = color;
 }
         
 void LCD_ST7735::drawString(const uint8_t *pFont, int x, int y, const char *pString)
@@ -368,9 +387,12 @@
 {
     clipRect(x1, y1, x1, y2);
     beginBatchCommand(CMD_RAMWR);
-    for (int i = 0; i < (y2 - y1) * 2; ++i)
+    int c = (y2 - y1) << 1;
+    uint8_t colorHigh = color >> 8;
+    uint8_t colorLow = color;
+    for (int i = 0; i < c; ++i)
     {
-        writeBatchData(color);
+        writeBatchData(colorHigh, colorLow);        
     }
     endBatchCommand();
 }
@@ -379,9 +401,12 @@
 {
     clipRect(x1, y1, x2, y1);
     beginBatchCommand(CMD_RAMWR);
-    for (int i = 0; i < (x2 - x1) * 2; ++i)
+    int c = (x2 - x1) << 1;
+    uint8_t colorHigh = color >> 8;
+    uint8_t colorLow = color;
+    for (int i = 0; i < c; ++i)
     {
-        writeBatchData(color);
+        writeBatchData(colorHigh, colorLow);
     }
     endBatchCommand();
 }
@@ -398,9 +423,15 @@
         for(int c = 0; c < 8; ++c)
         {
             if (b & 0x80)
-                writeBatchData(_foregroundColor);
+            {
+                writeBatchData(_foregroundColorHigh);
+                writeBatchData(_foregroundColorLow);
+            }
             else
-                writeBatchData(_backgroundColor);
+            {
+                writeBatchData(_backgroundColorHigh);
+                writeBatchData(_backgroundColorLow);
+            }
                 
             b <<= 1;
         }
@@ -487,16 +518,16 @@
 
 void LCD_ST7735::clipRect(int x1, int y1, int x2, int y2)
 {
-    uint8_t x1l = (uint8_t)(x1 & 0xff);
-    uint8_t x1h = (uint8_t)((x1 >> 8) & 0xff);
-    uint8_t x2l = (uint8_t)(x2 & 0xff);
-    uint8_t x2h = (uint8_t)((x2 >> 8) & 0xff);
+    uint8_t x1l = (uint8_t)x1;
+    uint8_t x1h = (uint8_t)(x1 >> 8);
+    uint8_t x2l = (uint8_t)x2;
+    uint8_t x2h = (uint8_t)(x2 >> 8);
     write(CMD_CASET, (uint8_t[]){x1h, x1l, x2h, x2l}, 4);    
     
-    uint8_t y1l = (uint8_t)(y1 & 0xff);
-    uint8_t y1h = (uint8_t)((y1 >> 8) & 0xff);
-    uint8_t y2l = (uint8_t)(y2 & 0xff);
-    uint8_t y2h = (uint8_t)((y2 >> 8) & 0xff);
+    uint8_t y1l = (uint8_t)y1;
+    uint8_t y1h = (uint8_t)(y1 >> 8);
+    uint8_t y2l = (uint8_t)y2;
+    uint8_t y2h = (uint8_t)(y2 >> 8);
     write(CMD_RASET, (uint8_t[]){y1h, y1l, y2h, y2l}, 4);    
 }
 
@@ -528,8 +559,8 @@
     _cs = 0;
     _spi.write(cmd);   
     _ds = 1;        
-    _spi.write((data >> 8) & 0xff);
-    _spi.write(data & 0xff);
+    _spi.write(data >> 8);
+    _spi.write(data);
     _ds = 0; 
     _cs = 1;
 }
@@ -556,14 +587,21 @@
     _spi.write(data);
 }
 
+void LCD_ST7735::writeBatchData(uint8_t dataHigh, uint8_t dataLow)
+{
+    _spi.write(dataHigh);
+    _spi.write(dataLow);
+}
+
+
 void LCD_ST7735::writeBatchData(uint16_t data)
 {
-    _spi.write((data >> 8) & 0xff);
-    _spi.write(data & 0xff); 
+    _spi.write(data >> 8);
+    _spi.write(data); 
 }
 
 void LCD_ST7735::endBatchCommand()
 {
     _ds = 0; 
     _cs = 1; 
-}
\ No newline at end of file
+}