Added methods and features

Fork of SPI_TFT_ILI9341 by Peter Drescher

Revision:
10:2d505d14b7eb
Parent:
9:6d30a225a5c7
Child:
11:7aabc3810093
--- a/SPI_TFT_ILI9341.cpp	Mon Apr 07 20:25:09 2014 +0000
+++ b/SPI_TFT_ILI9341.cpp	Sat Apr 12 20:33:24 2014 +0000
@@ -25,8 +25,19 @@
 
 SPI_TFT_ILI9341::SPI_TFT_ILI9341(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset, PinName dc, const char *name)
 //WH    : _spi(mosi, miso, sclk), _cs(cs), _reset(reset), _dc(dc), GraphicsDisplay(name)
-    : GraphicsDisplay(name), _spi(mosi, miso, sclk), _cs(cs), _reset(reset), _dc(dc) 
+    : GraphicsDisplay(name), _spi(mosi, miso, sclk), _cs(cs), _dc(dc) 
 {
+
+  // The hardware Reset pin is optional. Test and make sure whether it exists or not to prevent illegal access.
+  if (reset != NC) {
+    _reset = new DigitalOut(reset, 1);   //Construct new pin, Deactivated 
+//    _reset->write(1);                 //Deactivate    
+  }
+  else {
+    // No Hardware Reset pin       
+    _reset = NULL;                 //Construct dummy pin     
+  }  
+
 //WH    clk = sclk;
 //WH    orientation = 0;
     _origin = Origin_LeftTop;  
@@ -35,10 +46,22 @@
     _transparancy = false;    
 //    set_font(Arial12x12); //Default font    
 //    set_font(FONT8x8); //Default font, shame it doesnt fit format.. waste of flash space at moment
-    
+  
     tft_reset();
 }
 
+
+/** Destruct a SPI_TFT LCD object
+  *
+  * @param  none
+  * @return none
+  */ 
+SPI_TFT_ILI9341::~SPI_TFT_ILI9341() {
+  if (_reset != NULL) {delete _reset;}  // HW Reset pin
+}
+
+
+
 int SPI_TFT_ILI9341::width()
 {
 //    if (orientation == 0 || orientation == 2) return 240;
@@ -110,6 +133,16 @@
   _transparancy = state;  
 }
 
+// HW Reset to tft
+void SPI_TFT_ILI9341::_hwreset()
+{
+  // _reset is an optional pin which defaults to NC. Make sure it does not hang mbed lib
+  if (_reset != NULL) {_reset->write(0);}  //Clear _reset pin
+  wait_us(50);
+  if (_reset != NULL) {_reset->write(1);}  //Set _reset pin  
+  wait_ms(5);
+}
+
 
 // write command to tft register
 void SPI_TFT_ILI9341::wr_cmd(unsigned char cmd)
@@ -187,18 +220,15 @@
 //WH    _spi.format(8,3);                  // 8 bit spi Mode 3
     _spi.format(8,0);                  // 8 bit spi mode 0    
     
-    _spi.frequency(10000000);           // 10 Mhz SPI ... should work on current version of mbed F103 lib after fix for HSI/HSE...
 //    _spi.frequency(4000000);          // 4 Mhz SPI clock
 //    _spi.frequency(8000000);          // 8 Mhz SPI clock
-    
+    _spi.frequency(10000000);           // 10 Mhz SPI ... works on current version of mbed F103 lib after fix for HSI/HSE...
+        
     _cs = 1;                           // cs high
     _dc = 1;                           // dc high 
 
-    _reset = 0;                        // display reset
-    wait_us(50);
-    _reset = 1;                        // end hardware reset
-    wait_ms(5);
-     
+    _hwreset();                        // HW reset 
+         
 //WH    wr_cmd(0x01);                     // SW reset  
     wr_cmd(ILI9341_DISPLAY_RST);      // SW reset      
     wait_ms(5);
@@ -495,6 +525,71 @@
     
 #endif
 
+
+
+void SPI_TFT_ILI9341::newcls (void)
+{  
+    int pixels = height() * width();
+    int i;
+    int color = _background;
+#if (SPI_16 != 1)    
+    int msb, lsb;        
+#endif    
+ 
+    window(0,0,width(),height());
+
+    wr_cmd(ILI9341_GRAM);  // send pixel         
+
+#if (SPI_16 == 1)    
+    // 16 Bit SPI
+    _spi.format(16,0);   // switch to 16 bit Mode 0
+
+    //unroll loop in chunks of 8 pixels
+    for (i = 0; i < (pixels>>3); i++) {
+      _spi.write(color);
+      _spi.write(color);
+      _spi.write(color);
+      _spi.write(color); 
+      
+      _spi.write(color);
+      _spi.write(color);
+      _spi.write(color);
+      _spi.write(color);                                          
+    }  
+
+    //remainder
+    for (i = 0; i < (pixels & 0x07); i++)
+      _spi.write(color);
+
+    _spi.format(8,0);    // switch back to 8 bit Mode 0 
+#else
+    // 8 Bit SPI
+    msb = color >> 8;
+    lsb = color & 0xff;
+
+    for (i = 0; i < (pixels>>3); i+=8) {
+      _spi.write(msb); _spi.write(lsb);
+      _spi.write(msb); _spi.write(lsb);
+      _spi.write(msb); _spi.write(lsb);
+      _spi.write(msb); _spi.write(lsb);
+      
+      _spi.write(msb); _spi.write(lsb);
+      _spi.write(msb); _spi.write(lsb);
+      _spi.write(msb); _spi.write(lsb);
+      _spi.write(msb); _spi.write(lsb);                                          
+    }
+    
+    for (i = 0; i < (pixels & 0x07); i++) {
+      _spi.write(msb); _spi.write(lsb);
+    }    
+#endif        
+
+    _cs = 1; 
+}
+
+
+
+
 void SPI_TFT_ILI9341::circle(int x0, int y0, int r, int color)
 {
 
@@ -673,7 +768,9 @@
 void SPI_TFT_ILI9341::hline(int x0, int x1, int y, int color)
 {
     int i, w;
+#if (SPI_16 != 1)    
     int msb, lsb;
+#endif    
     w = x1 - x0 + 1;
     window(x0,y,w,1);
 //    wr_cmd(0x2C);  // send pixel
@@ -730,7 +827,9 @@
 void SPI_TFT_ILI9341::vline(int x, int y0, int y1, int color)
 {
     int i, h;
+#if (SPI_16 != 1)    
     int msb, lsb;
+#endif    
     
     h = y1 - y0 + 1;
     window(x,y0,1,h);
@@ -886,17 +985,22 @@
     int h = y1 - y0 + 1; 
     int w = x1 - x0 + 1; 
     int pixels = h * w;
-    int i, msb, lsb;        
+    int i;
+#if (SPI_16 != 1)    
+    int msb, lsb;        
+#endif    
  
     window(x0,y0,w,h);
 //    wr_cmd(0x2C);  // send pixel
     wr_cmd(ILI9341_GRAM);  // send pixel         
 
-#if (SPI_16 == 1)
+#if (SPI_16 == 1)    
     // 16 Bit SPI
     _spi.format(16,0);   // switch to 16 bit Mode 0
+
     for (i = 0; i < pixels; i++)
       _spi.write(color);
+
     _spi.format(8,0);    // switch back to 8 bit Mode 0 
 #else
     // 8 Bit SPI