TFT Touch base

Dependencies:   TFTv2 mbed

Fork of TFT_test_NUCLEO-F411RE by Motoo Tanaka

Files at this revision

API Documentation at this revision

Comitter:
richnash
Date:
Mon Jul 30 14:18:06 2018 +0000
Parent:
0:cd5e3d371b54
Commit message:
base version of TFT demo for F767ZI

Changed in this revision

SPI_STMPE610.lib Show diff for this revision Revisions of this file
TFTv2.lib Show annotated file Show diff for this revision Revisions of this file
UniGraphic.lib Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r cd5e3d371b54 -r 0a7005226e61 SPI_STMPE610.lib
--- a/SPI_STMPE610.lib	Thu Nov 19 13:34:57 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/Rhyme/code/SPI_STMPE610/#21f92cef6dca
diff -r cd5e3d371b54 -r 0a7005226e61 TFTv2.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TFTv2.lib	Mon Jul 30 14:18:06 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/richnash/code/TFTv2/#2b9b3c325d48
diff -r cd5e3d371b54 -r 0a7005226e61 UniGraphic.lib
--- a/UniGraphic.lib	Thu Nov 19 13:34:57 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/teams/GraphicsDisplay/code/UniGraphic/#1bdfb971b2c1
diff -r cd5e3d371b54 -r 0a7005226e61 main.cpp
--- a/main.cpp	Thu Nov 19 13:34:57 2015 +0000
+++ b/main.cpp	Mon Jul 30 14:18:06 2018 +0000
@@ -1,3 +1,6 @@
+/* Adapted from other examples to work on a F767ZI
+ */
+ 
 /* mbed main.cpp to test adafruit 2.8" TFT LCD shiled w Touchscreen
  * Copyright (c) 2014, 2015 Motoo Tanaka @ Design Methodology Lab
  *
@@ -28,50 +31,244 @@
 #include "Arial28x28.h"
 #include "Arial43x48_numb.h"
 
-#define PIN_MOSI        PA_7
-#define PIN_MISO        PA_6  
-#define PIN_SCLK        PA_5 
-#define PIN_CS_TFT      PB_6 
-#define PIN_DC_TFT      PC_7 
-#define PIN_BL_TFT      PA_8 
-#define PIN_CS_SD       PB_5 
-#define PIN_CS_TSC      PA_9 
-#define PIN_TSC_INTR    PA_8
 #define PIN_RESET_TFT   PC_13 /* place holder */
-#define DEVICE_NAME     "F411RE"
+
+// ------------- FROM WORKING VERSION
+
+#define PIN_MOSI        D11
+#define PIN_MISO        D12
+#define PIN_SCLK        D13
+#define PIN_CS_TFT      D5
+#define PIN_DC_TFT      D6
+#define PIN_BL_TFT      D7
+#define PIN_CS_SD       D4
+
+// =====================================
+
+#define PIN_XP          A2
+#define PIN_XM          A0
+#define PIN_YP          A3
+#define PIN_YM          A1
+
+
+struct point {
+    int x;
+    int y;
+};
 
-#ifndef TARGET_NECLEO_F411RE
-#define TARGET_NECLEO_F411RE 
-#endif
+void calibrate(void);
+bool getPixel(point& p);
+point toPixel(point p);
+
+typedef enum { YES, MAYBE, NO } TOUCH;
+
+TOUCH getTouch(point& p);
+
+int readTouch(PinName p, PinName m, PinName a, PinName i);
+
+
+int x_off = 22071;
+int y_off = 18707;
+int pp_tx = 373;
+int pp_ty = 297;
+
+// =====================================
+
+DigitalOut backlight(PIN_BL_TFT) ;
+
+ILI9341 TFT(SPI_8, 10000000, PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TFT, PIN_RESET_TFT, PIN_DC_TFT, "Seeed2.8") ;
+
+int page = 1 ;
+int numPage = 3 ;
+
+Serial logger(SERIAL_TX, SERIAL_RX);
+
+
+/// ================================================================================================
 
 
-DigitalOut backlight(PB_3) ;
-DigitalOut tsc_cs(PA_9, 1) ;
-DigitalOut tft_cs(PB_6, 1) ;
+int readTouch(PinName p, PinName m, PinName a, PinName i)
+{
+    DigitalOut _p(p);
+    _p = 1;
+    DigitalOut _m(m);
+    _m = 0;
+    AnalogIn   _a(a);
+    AnalogIn   _i(i); // this pin has to be high Z (DigitalIn may also work)
+    wait_us(10);
+    return _a.read_u16();
+}
+
+TOUCH getTouch(point& p)
+{
+    int y2 = readTouch(PIN_XP,PIN_XM,PIN_YP,PIN_YM);
+    int x2 = readTouch(PIN_YP,PIN_YM,PIN_XP,PIN_XM);
+    int y1 = readTouch(PIN_XP,PIN_XM,PIN_YP,PIN_YM);
+    int x1 = readTouch(PIN_YP,PIN_YM,PIN_XP,PIN_XM);
+    int xd = x1 - x2;
+    int yd = y1 - y2;
+    xd = (xd > 0) ? xd : -xd;
+    yd = (yd > 0) ? xd : -xd;
+    p.x = x1 + x2;
+    p.y = y1 + y2;
+    
+    const int th = 8000;
+    const int df =  100;
+    TOUCH touch;
+    if (x1 < th || x2 < th ||
+            y1 < th || y2 < th) {
+        p.x = 0;
+        p.y = 0;
+        touch = NO;
+    } else if (xd > df || yd > df) {
+        touch = MAYBE;
+    } else {
+        touch = YES;
+    }
+    //TFT.locate(0,50);
+    //TFT.printf("x: %6i y: %6i",p.x,p.y);
+    return touch;
+}
 
-ILI9341 TFT(SPI_8, 10000000, 
-    PIN_MOSI, PIN_MISO,  PIN_SCLK, 
-    PIN_CS_TFT, PIN_RESET_TFT, PIN_DC_TFT, "Adafruit2.8") ;
-SPI_STMPE610 TSC(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TSC) ;
+void calibrate(void)
+{
+    int i;
+    int a = 0,b = 0,c = 0, d = 0;
+    int pos_x = 0, pos_y = 0;
+    point p;
+    
+    backlight = 0 ;
+    //TFT.BusEnable(true) ;
+    
+    TFT.background(Black);
+    wait(0.1) ;
+    TFT.foreground(White);
+    wait(0.1) ;
+    TFT.cls() ;
+    wait(0.1) ;
+    TFT.set_font((unsigned char*) Arial12x12);
+    TFT.locate(90,0);
+    TFT.printf("Graphics");
+    
+    TFT.line(0,3,6,3,White);
+    TFT.line(3,0,3,6,White);
+    
+    backlight = 1 ;
+    
+    //if (font)
+    //{
+        // get the center of the screen
+        pos_x = TFT.columns() / 2 - 3;
+        pos_x = pos_x * Arial12x12[1];
+        pos_y = (TFT.rows() / 2) - 1;
+        pos_y = pos_y * Arial12x12[2];
+        TFT.locate(pos_x,pos_y);
+        TFT.printf("press cross    ");
+        TFT.locate(pos_x,pos_y + Arial12x12[2]);
+        TFT.printf("to calibrate   ");
+    //}
+    
+    for (i=0; i<5; i++) {
+        while (getTouch(p) != YES)
+            /*nothing*/;
+        a += p.x;
+        b += p.y;
+    }
+    a = a / 5;
+    b = b / 5;
+    //if (font)
+    //{
+        TFT.locate(pos_x,pos_y);
+        TFT.printf("ok             ");
+        TFT.locate(pos_x,pos_y + Arial12x12[2]);
+        TFT.printf("release touch  ");
+    //}
+    while (getTouch(p) != NO)
+        /*nothing*/;
+    
+    TFT.cls();
+    TFT.line(TFT.width() -5, TFT.height() - 8,TFT.width() - 5,TFT.height() -1,White);   // paint cross
+    TFT.line(TFT.width() - 8,TFT.height() - 5,TFT.width() - 1,TFT.height() - 5,White);
+    //if (font)
+    //{
+        TFT.locate(pos_x,pos_y);
+        TFT.printf("press cross    ");
+        TFT.locate(pos_x,pos_y + Arial12x12[2]);
+        TFT.printf("to calibrate   ");
+    //}
+    for (i=0; i<5; i++) {
+        while (getTouch(p) != YES)
+            /*nothing*/;
+        c+= p.x;
+        d+= p.y;
+    }
+    c = c / 5;
+    d = d / 5;
+    x_off = a;
+    y_off = b;
+    i = c-a;  // delta x
+    pp_tx = i / (TFT.width() - 6);
+    i = d-b;  // delta y
+    pp_ty = i / (TFT.height() - 6);
+    //if (font)
+    //{
+        TFT.locate(pos_x,pos_y);
+        TFT.printf("Calibrated     ");
+        TFT.locate(pos_x,pos_y + Arial12x12[2]);
+        TFT.printf("x %6i %4i", x_off, pp_tx);
+        TFT.locate(pos_x,pos_y + 2*Arial12x12[2]);
+        TFT.printf("y %6i %4i", y_off, pp_ty);
+    //}
+    while (getTouch(p) != NO)
+        /*nothing*/;
+    TFT.cls();
+    
+    //TFT.BusEnable(false) ;
+    
+    printf("x_off:%6i pp_tx:%4i \n\r", x_off, pp_tx);
+    printf("y_off:%6i pp_ty:%4i \n\r", y_off, pp_ty);
+}
 
-int page = 0 ;
-int numPage = 2 ;
+point toPixel(point p)
+{
+    p.x -= x_off;
+    p.x /= pp_tx;
+    int w = TFT.width();
+    if (p.x > w) p.x = w;
+    if (p.x < 0) p.x = 0;
+    p.y -= y_off;
+    p.y /= pp_ty;
+    int h = TFT.height();
+    if (p.y > h) p.y = h;
+    if (p.y < 0) p.y = 0;
+    return (p);
+}
+
+bool getPixel(point& p)
+{
+    TOUCH touch = getTouch(p);
+    p = toPixel(p);
+    return touch == YES;
+}
+
+
+// ===================================================
 
 void initTFT(void)
 {
     //Configure the display driver
-    TFT.BusEnable(true) ;
+    //TFT.BusEnable(true) ;
     TFT.FastWindow(true) ;
     TFT.background(Black);
     TFT.foreground(White);
     wait(0.01) ;
     TFT.cls();
-    TFT.BusEnable(false) ;
+    //TFT.BusEnable(false) ;
 }
 
 void screen1(void) // Welcome Screen
 {
-    TFT.BusEnable(true) ;
+    //TFT.BusEnable(true) ;
     backlight = 0 ;
     TFT.background(White) ;
     wait(0.1) ;
@@ -103,7 +300,7 @@
     TFT.printf("and hold right side of screen") ;
     TFT.locate(30, 300) ;
     TFT.printf("until the next screen starts") ;
-    TFT.BusEnable(false) ;
+    //TFT.BusEnable(false) ;
     backlight = 1 ;
 }
 
@@ -112,7 +309,7 @@
     //Draw some graphics
     int i, x[2], y[2] ;
     backlight = 0 ;
-    TFT.BusEnable(true) ;
+    //TFT.BusEnable(true) ;
     TFT.background(Black);
     wait(0.1) ;
     TFT.foreground(White);
@@ -159,20 +356,37 @@
     TFT.printf("simple graphics drawing") ;
     TFT.locate(20, 290) ;
     TFT.printf("capability is provided") ;
-    TFT.BusEnable(false) ;
+    //TFT.BusEnable(false) ;
     backlight = 1 ;
 }    
 
-double clip(double src)
+void screen3(void) // Graphics
 {
-    double value ;
-    value = src ;
-    if (value < 0.0) {
-        value = 0.0 ;
-    } else if (value > 2.0) {
-        value = 2.0 ;
-    }
-    return( value ) ;
+    //int rowX;
+    int rowY;
+    //int x = Terminal6x8[1];
+    int y = Terminal6x8[2];
+    
+    //Draw some text
+    backlight = 0 ;
+    
+    TFT.background(Black);
+    wait(0.1) ;
+    TFT.foreground(White);
+    wait(0.1) ;
+    TFT.cls() ;
+    wait(0.1) ;
+    
+    backlight = 1 ;
+    
+    TFT.set_font((unsigned char*) Terminal6x8);
+    
+    for( rowY=0; rowY<100; ++rowY )
+        {
+        TFT.locate(0, (rowY%25)*y);
+        TFT.printf("%3d TFT width = %d, height = %d", rowY, TFT.width(), TFT.height()) ;
+        }
+    
 }
 
 void incPage(void)
@@ -193,28 +407,26 @@
     
 int main()
 {
-    uint16_t x, y, z ;
+    point p;
+    
+    // setup the pc serial logger
+    logger.baud(115200);
+    logger.printf("\r\n\r\n<<<<<<<<< TFT SCREEN TEST >>>>>>>>>>>\r\n" );
+    
     int prevPage = 99 ;
     bool waitTouch = false ;
     
-    printf("Hello World\n") ;
+    initTFT() ;
     
-    tsc_cs = 1 ;
-    tft_cs = 0 ;
-    initTFT() ;
-    tft_cs = 1 ;
-
-    tsc_cs = 0 ;
-    TSC.spi_format(8, 1) ; // for Nucleo F411RE
-    tsc_cs = 1 ;
+    printf("Program Started!\n\r") ;
+    printf("TFT width = %d, height = %d\n\r", TFT.width(), TFT.height()) ;
     
- //   screen0() ;
-        
-    printf("Program Started!\n\r") ;
+    //calibrate();
     
     for(;;) {
-//        printf("TFT width = %d, height = %d\n\r", TFT.width(), TFT.height()) ;
-        tft_cs = 0 ;
+        
+        printf("page %d of %d\r\n", page, numPage );
+        
         switch(page) {
         case 0:
             if (prevPage != page) {
@@ -228,26 +440,33 @@
             }
             waitTouch = true ;
             break ;
+        case 2:
+            if (prevPage != page) {
+                screen3() ; 
+            }
+            waitTouch = true ;
+            break ;
         default:
             page = 0 ; 
             break ;
         }
         prevPage = page ;
-
-        tft_cs = 1 ;
-        do {
-            tsc_cs = 0 ;
-            TSC.getRAWPoint(&x, &y, &z) ;
-            if ((x != 0)||(y != 0) || (z != 0)) {
-                if (x < 1000) { // left
+        
+        do  {
+            if( getPixel(p) ) 
+                {
+                printf("TFT Touch x = %d, y = %d\n\r", p.x, p.y) ;
+                
+                if (p.x < 100) 
+                    { // left
                     decPage() ;
-                } else if (x > 3000) { // right
+                    } 
+                else if (p.x > 150) 
+                    { // right
                     incPage() ;
-                }
+                    }
                 waitTouch = false ;
-            }
-            tsc_cs = 1 ;
-        } while(waitTouch != false) ;
- //       wait(1) ;
+                }
+            } while(waitTouch != false) ;
     }
 }
diff -r cd5e3d371b54 -r 0a7005226e61 mbed.bld
--- a/mbed.bld	Thu Nov 19 13:34:57 2015 +0000
+++ b/mbed.bld	Mon Jul 30 14:18:06 2018 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/9296ab0bfc11
\ No newline at end of file
+https://os.mbed.com/users/mbed_official/code/mbed/builds/a7c7b631e539
\ No newline at end of file