Lib for the new LCD Display with ILI9341 controller supporting two displays

Dependents:   CANary_9341_test CANary_9341 CANary

Fork of SPI_TFT_ILI9341 by Peter Drescher

Revision:
9:522e742e62eb
Parent:
8:66cf46ec1225
Child:
10:aca12a61d2b1
--- a/SPI_TFTx2_ILI9341.cpp	Mon May 12 00:53:43 2014 +0000
+++ b/SPI_TFTx2_ILI9341.cpp	Mon May 12 03:18:44 2014 +0000
@@ -24,30 +24,31 @@
 //extern Serial pc;
 //extern DigitalOut xx;     // debug !!
 
-SPI_TFTx2_ILI9341::SPI_TFTx2_ILI9341(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset, PinName dc, const char *name)
-    : _spi(mosi, miso, sclk), _cs(cs), _reset(reset), _dc(dc), GraphicsDisplay(name)
+SPI_TFTx2::SPI_TFTx2(PinName mosi, PinName miso, PinName sclk, PinName cs0, PinName cs1, PinName reset, PinName dc, const char *name)
+    : _spi(mosi, miso, sclk), _cs0(cs0), _cs1(cs1), _reset(reset), _dc(dc), GraphicsDisplay(name)
 {
     clk = sclk;
     orientation = 0;
     char_x = 0;
+    seldisp = 2;
     tft_reset();
 }
 
-int SPI_TFTx2_ILI9341::width()
+int SPI_TFTx2::width()
 {
     if (orientation == 0 || orientation == 2) return 240;
     else return 320;
 }
 
 
-int SPI_TFTx2_ILI9341::height()
+int SPI_TFTx2::height()
 {
     if (orientation == 0 || orientation == 2) return 320;
     else return 240;
 }
 
 
-void SPI_TFTx2_ILI9341::set_orientation(unsigned int o)
+void SPI_TFTx2::set_orientation(unsigned int o)
 {
     orientation = o;
     wr_cmd(0x36);                     // MEMORY_ACCESS_CONTROL
@@ -65,51 +66,95 @@
             _spi.write(0xE8);
             break;
     }
-    _cs = 1; 
+    _cs0 = 1; 
+    _cs1 = 1;
     WindowMax();
 } 
 
+void SPI_TFTx2::set_display(unsigned int o)
+{
+    seldisp = o;
+}
 
 // write command to tft register
 
-void SPI_TFTx2_ILI9341::wr_cmd(unsigned char cmd)
+void SPI_TFTx2::wr_cmd(unsigned char cmd)
 {
     _dc = 0;
-    _cs = 0;
+    if (seldisp == 0) {
+        _cs0 = 0;
+        _cs1 = 1;
+    } else if (seldisp == 1) {
+        _cs0 = 1;
+        _cs1 = 0;
+    } else {
+        _cs0 = 0;
+        _cs1 = 0;
+    }
     _spi.write(cmd);      // mbed lib
     _dc = 1;
 }
 
 
 
-void SPI_TFTx2_ILI9341::wr_dat(unsigned char dat)
+void SPI_TFTx2::wr_dat(unsigned char dat)
 {
-   _spi.write(dat);      // mbed lib
+    if (seldisp == 0) {
+        _cs0 = 0;
+        _cs1 = 1;
+    } else if (seldisp == 1) {
+        _cs0 = 1;
+        _cs1 = 0;
+    } else {
+        _cs0 = 0;
+        _cs1 = 0;
+    }
+    _spi.write(dat);      // mbed lib
 }
 
 
 
 // the ILI9341 can read 
 
-char SPI_TFTx2_ILI9341::rd_byte(unsigned char cmd)
+char SPI_TFTx2::rd_byte(unsigned char cmd)
 {
     char r;
     _dc = 0;
-    _cs = 0;
+    if (seldisp == 0) {
+        _cs0 = 0;
+        _cs1 = 1;
+    } else if (seldisp == 1) {
+        _cs0 = 1;
+        _cs1 = 0;
+    } else {
+        _cs0 = 0;
+        _cs1 = 0;
+    }
     _spi.write(cmd);      // mbed lib
-    _cs = 1;
+    _cs0 = 1;
+    _cs1 = 1;
     r = _spi.write(0xff);
-    _cs = 1;    
+    _cs0 = 1;
+    _cs1 = 1;    
     return(r);
 }
 
 // read 32 bit
-int SPI_TFTx2_ILI9341::rd_32(unsigned char cmd)
+int SPI_TFTx2::rd_32(unsigned char cmd)
 {
     int d;
     char r;
     _dc = 0;
-    _cs = 0;
+    if (seldisp == 0) {
+        _cs0 = 0;
+        _cs1 = 1;
+    } else if (seldisp == 1) {
+        _cs0 = 1;
+        _cs1 = 0;
+    } else {
+        _cs0 = 0;
+        _cs1 = 0;
+    }
     d = cmd;
     d = d << 1;
     _spi.format(9,3);    // we have to add a dummy clock cycle
@@ -124,11 +169,12 @@
     d = (d << 8) | r;
     r = _spi.write(0xff);
     d = (d << 8) | r;
-    _cs = 1;    
+    _cs0 = 1;    
+    _cs1 = 1;
     return(d);
 }
 
-int  SPI_TFTx2_ILI9341::Read_ID(void){
+int  SPI_TFTx2::Read_ID(void){
     int r;
     r = rd_byte(0x0A);
     r = rd_byte(0x0A);
@@ -140,11 +186,12 @@
 
 // Init code based on MI0283QT datasheet
 
-void SPI_TFTx2_ILI9341::tft_reset()
+void SPI_TFTx2::tft_reset()
 {
     _spi.format(8,3);                  // 8 bit spi mode 3
     _spi.frequency(10000000);          // 10 Mhz SPI clock
-    _cs = 1;                           // cs high
+    _cs0 = 1;                          // cs high
+    _cs1 = 1;
     _dc = 1;                           // dc high 
     _reset = 0;                        // display reset
 
@@ -161,20 +208,23 @@
      _spi.write(0x00);
      _spi.write(0x83);
      _spi.write(0x30);
-     _cs = 1;
+     _cs0 = 1;
+     _cs1 = 1;
      
      wr_cmd(0xED);                     
      _spi.write(0x64);
      _spi.write(0x03);
      _spi.write(0x12);
      _spi.write(0x81);
-     _cs = 1;
+     _cs0 = 1;
+     _cs1 = 1;
      
      wr_cmd(0xE8);                     
      _spi.write(0x85);
      _spi.write(0x01);
      _spi.write(0x79);
-     _cs = 1;
+     _cs0 = 1;
+     _cs1 = 1;
      
      wr_cmd(0xCB);                     
      _spi.write(0x39);
@@ -182,54 +232,66 @@
      _spi.write(0x00);
      _spi.write(0x34);
      _spi.write(0x02);
-     _cs = 1;
+     _cs0 = 1;
+     _cs1 = 1;
            
      wr_cmd(0xF7);                     
      _spi.write(0x20);
-     _cs = 1;
+     _cs0 = 1;
+     _cs1 = 1;
            
      wr_cmd(0xEA);                     
      _spi.write(0x00);
      _spi.write(0x00);
-     _cs = 1;
+     _cs0 = 1;
+     _cs1 = 1;
      
      wr_cmd(0xC0);                     // POWER_CONTROL_1
      _spi.write(0x26);
-     _cs = 1;
+     _cs0 = 1;
+     _cs1 = 1;
  
      wr_cmd(0xC1);                     // POWER_CONTROL_2
      _spi.write(0x11);
-     _cs = 1;
+     _cs0 = 1;
+     _cs1 = 1;
      
      wr_cmd(0xC5);                     // VCOM_CONTROL_1
      _spi.write(0x35);
      _spi.write(0x3E);
-     _cs = 1;
+     _cs0 = 1;
+     _cs1 = 1;
      
      wr_cmd(0xC7);                     // VCOM_CONTROL_2
      _spi.write(0xBE);
-     _cs = 1; 
+     _cs0 = 1;
+     _cs1 = 1;
      
      wr_cmd(0x36);                     // MEMORY_ACCESS_CONTROL
      _spi.write(0x48);
-     _cs = 1; 
+     _cs0 = 1;
+     _cs1 = 1;
      
      wr_cmd(0x3A);                     // COLMOD_PIXEL_FORMAT_SET
      _spi.write(0x55);                 // 16 bit pixel 
-     _cs = 1;
+     _cs0 = 1;
+     _cs1 = 1;
      
      wr_cmd(0xB1);                     // Frame Rate
      _spi.write(0x00);
      _spi.write(0x1B);               
-     _cs = 1;
-     
+     _cs0 = 1;
+     _cs1 = 1;
+    
      wr_cmd(0xF2);                     // Gamma Function Disable
      _spi.write(0x08);
-     _cs = 1; 
+     _cs0 = 1;
+     _cs1 = 1;
      
      wr_cmd(0x26);                     
      _spi.write(0x01);                 // gamma set for curve 01/2/04/08
-     _cs = 1; 
+     _cs0 = 1;
+     _cs1 = 1;
      
      wr_cmd(0xE0);                     // positive gamma correction
      _spi.write(0x1F); 
@@ -247,7 +309,8 @@
      _spi.write(0x07);
      _spi.write(0x05); 
      _spi.write(0x00);
-     _cs = 1;
+     _cs0 = 1;
+     _cs1 = 1;
      
      wr_cmd(0xE1);                     // negativ gamma correction
      _spi.write(0x00); 
@@ -265,7 +328,8 @@
      _spi.write(0x38);
      _spi.write(0x3A); 
      _spi.write(0x1F);
-     _cs = 1;
+     _cs0 = 1;
+     _cs1 = 1;
      
      WindowMax ();
      
@@ -277,38 +341,44 @@
       
      wr_cmd(0xB7);                       // entry mode
      _spi.write(0x07);
-     _cs = 1;
+     _cs0 = 1;
+     _cs1 = 1;
      
      wr_cmd(0xB6);                       // display function control
      _spi.write(0x0A);
      _spi.write(0x82);
      _spi.write(0x27);
      _spi.write(0x00);
-     _cs = 1;
+     _cs0 = 1;
+     _cs1 = 1;
      
      wr_cmd(0x11);                     // sleep out
-     _cs = 1;
+     _cs0 = 1;
+     _cs1 = 1;
      
      wait_ms(100);
      
      wr_cmd(0x29);                     // display on
-     _cs = 1;
+     _cs0 = 1;
+     _cs1 = 1;
      
      wait_ms(100);
      
  }
 
 
-void SPI_TFTx2_ILI9341::pixel(int x, int y, int color)
+void SPI_TFTx2::pixel(int x, int y, int color)
 {
     wr_cmd(0x2A);
     _spi.write(x >> 8);
     _spi.write(x);
-    _cs = 1;
+    _cs0 = 1;
+    _cs1 = 1;
     wr_cmd(0x2B);
     _spi.write(y >> 8);
     _spi.write(y);
-    _cs = 1;
+    _cs0 = 1;
+    _cs1 = 1;
     wr_cmd(0x2C);  // send pixel
     #if defined TARGET_KL25Z  // 8 Bit SPI
     _spi.write(color >> 8);
@@ -318,11 +388,12 @@
     _spi.write(color);                              // Write D0..D15
     _spi.format(8,3);
     #endif
-    _cs = 1;
+    _cs0 = 1;
+    _cs1 = 1;
 }
 
 
-void SPI_TFTx2_ILI9341::window (unsigned int x, unsigned int y, unsigned int w, unsigned int h)
+void SPI_TFTx2::window (unsigned int x, unsigned int y, unsigned int w, unsigned int h)
 {
     wr_cmd(0x2A);
     _spi.write(x >> 8);
@@ -330,24 +401,26 @@
     _spi.write((x+w-1) >> 8);
     _spi.write(x+w-1);
     
-    _cs = 1;
+     _cs0 = 1;
+     _cs1 = 1;
     wr_cmd(0x2B);
     _spi.write(y >> 8);
     _spi.write(y);
     _spi.write((y+h-1) >> 8);
     _spi.write(y+h-1);
-    _cs = 1;
+     _cs0 = 1;
+     _cs1 = 1;
 }
 
 
-void SPI_TFTx2_ILI9341::WindowMax (void)
+void SPI_TFTx2::WindowMax (void)
 {
     window (0, 0, width(),  height());
 }
 
 
 
-void SPI_TFTx2_ILI9341::cls (void)
+void SPI_TFTx2::cls (void)
 {
     int pixel = ( width() * height());
     WindowMax();
@@ -366,11 +439,12 @@
         _spi.write(_background);
     _spi.format(8,3);    
     #endif                         
-    _cs = 1; 
+     _cs0 = 1;
+     _cs1 = 1;
 }
 
 
-void SPI_TFTx2_ILI9341::circle(int x0, int y0, int r, int color)
+void SPI_TFTx2::circle(int x0, int y0, int r, int color)
 {
 
     int x = -r, y = 0, err = 2-2*r, e2;
@@ -389,7 +463,7 @@
 
 }
 
-void SPI_TFTx2_ILI9341::fillcircle(int x0, int y0, int r, int color)
+void SPI_TFTx2::fillcircle(int x0, int y0, int r, int color)
 {
     int x = -r, y = 0, err = 2-2*r, e2;
     do {
@@ -405,7 +479,7 @@
 }
 
 
-void SPI_TFTx2_ILI9341::hline(int x0, int x1, int y, int color)
+void SPI_TFTx2::hline(int x0, int x1, int y, int color)
 {
     int w;
     w = x1 - x0 + 1;
@@ -425,12 +499,13 @@
     }
     _spi.format(8,3);
     #endif
-    _cs = 1;
+     _cs0 = 1;
+     _cs1 = 1;
     WindowMax();
     return;
 }
 
-void SPI_TFTx2_ILI9341::vline(int x, int y0, int y1, int color)
+void SPI_TFTx2::vline(int x, int y0, int y1, int color)
 {
     int h;
     h = y1 - y0 + 1;
@@ -448,14 +523,15 @@
     }
     _spi.format(8,3);
     #endif
-    _cs = 1;
+     _cs0 = 1;
+     _cs1 = 1;
     WindowMax();
     return;
 }
 
 
 
-void SPI_TFTx2_ILI9341::line(int x0, int y0, int x1, int y1, int color)
+void SPI_TFTx2::line(int x0, int y0, int x1, int y1, int color)
 {
     //WindowMax();
     int   dx = 0, dy = 0;
@@ -527,7 +603,7 @@
 }
 
 
-void SPI_TFTx2_ILI9341::rect(int x0, int y0, int x1, int y1, int color)
+void SPI_TFTx2::rect(int x0, int y0, int x1, int y1, int color)
 {
 
     if (x1 > x0) hline(x0,x1,y0,color);
@@ -547,7 +623,7 @@
 
 
 
-void SPI_TFTx2_ILI9341::fillrect(int x0, int y0, int x1, int y1, int color)
+void SPI_TFTx2::fillrect(int x0, int y0, int x1, int y1, int color)
 {
 
     int h = y1 - y0 + 1;
@@ -567,13 +643,14 @@
     }
     _spi.format(8,3);
     #endif
-    _cs = 1;
+     _cs0 = 1;
+     _cs1 = 1;
     WindowMax();
     return;
 }
 
 
-void SPI_TFTx2_ILI9341::locate(int x, int y)
+void SPI_TFTx2::locate(int x, int y)
 {
     char_x = x;
     char_y = y;
@@ -581,21 +658,21 @@
 
 
 
-int SPI_TFTx2_ILI9341::columns()
+int SPI_TFTx2::columns()
 {
     return width() / font[1];
 }
 
 
 
-int SPI_TFTx2_ILI9341::rows()
+int SPI_TFTx2::rows()
 {
     return height() / font[2];
 }
 
 
 
-int SPI_TFTx2_ILI9341::_putc(int value)
+int SPI_TFTx2::_putc(int value)
 {
     if (value == '\n') {    // new line
         char_x = 0;
@@ -610,7 +687,7 @@
 }
 
 
-void SPI_TFTx2_ILI9341::character(int x, int y, int c)
+void SPI_TFTx2::character(int x, int y, int c)
 {
     unsigned int hor,vert,offset,bpl,j,i,b;
     unsigned char* zeichen;
@@ -659,7 +736,8 @@
             }
         }
     }
-    _cs = 1;
+     _cs0 = 1;
+     _cs1 = 1;
     #ifndef TARGET_KL25Z  // 16 Bit SPI
     _spi.format(8,3);
     #endif
@@ -670,14 +748,14 @@
 }
 
 
-void SPI_TFTx2_ILI9341::set_font(unsigned char* f)
+void SPI_TFTx2::set_font(unsigned char* f)
 {
     font = f;
 }
 
 
 
-void SPI_TFTx2_ILI9341::Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap)
+void SPI_TFTx2::Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap)
 {
     unsigned int  j;
     int padd;
@@ -714,7 +792,8 @@
         bitmap_ptr -= 2*w;
         bitmap_ptr -= padd;
     }
-    _cs = 1;
+     _cs0 = 1;
+     _cs1 = 1;
     #ifndef TARGET_KL25Z  // 16 Bit SPI 
     _spi.format(8,3);
     #endif
@@ -724,7 +803,7 @@
 
 // local filesystem is not implemented in kinetis board , but you can add a SD card
 
-int SPI_TFTx2_ILI9341::BMP_16(unsigned int x, unsigned int y, const char *Name_BMP)
+int SPI_TFTx2::BMP_16(unsigned int x, unsigned int y, const char *Name_BMP)
 {
 
 #define OffsetPixelWidth    18
@@ -804,7 +883,8 @@
         #endif    
         } 
      }
-    _cs = 1;
+     _cs0 = 1;
+     _cs1 = 1;
     _spi.format(8,3);
     free (line);
     fclose(Image);