Fork with support for ILI9341 controller

Dependents:   CANary_9341 CANary

Fork of TOUCH_TFTx2 by Tick Tock

Revision:
5:a9890c586a64
Parent:
4:a3cd26c97b76
Child:
6:a91b668b058a
--- a/TOUCH_TFTx2.cpp	Mon Feb 11 02:14:15 2013 +0000
+++ b/TOUCH_TFTx2.cpp	Thu Feb 14 00:26:54 2013 +0000
@@ -16,7 +16,9 @@
 #include "TOUCH_TFTx2.h"
 #include "mbed.h"
 
-#define threshold 0x1000  // threshold to detect pressed 
+#define threshold 0x1000  // threshold to detect pressed
+#define accuracy 300 // noise filter
+#define numsamp 16 // number of averaging samples
 
 TOUCH_TFTx2::TOUCH_TFTx2(PinName xp, PinName xm, PinName yp, PinName ym,
                      PinName mosi, PinName miso, PinName sclk, PinName cs0, PinName cs1, PinName reset,const char* name):
@@ -28,11 +30,11 @@
 }
 
 point TOUCH_TFTx2::get_touch() {
-    unsigned short x1, x2, y1, y2;
-    unsigned int s1 = 0,s2 = 0,d1 , d2;
+    unsigned short x1, x2, y1, y2, i=0, j=0, k;
+    unsigned long dy , dx, xs1=0, ys1=0, xs2=0, ys2=0;
     point p;
 
-    do {
+    for (k=0; j<numsamp; k++) {
         // read y voltage
         _ym.input();        //LAJ y- has to be passive
         _yp.input();        //LAJ y+ has to be passive
@@ -53,15 +55,8 @@
         AnalogIn Ay(ya);    // we have to call the constructor to switch to analog mode
         wait_us(10);
         y1 = Ay.read_u16(); // get y voltage
-        d1 = (y1 > y2)? (y1-y2) : (y2-y1);
-        if (d1 < 3000) s1 ++;
-        else {
-            s1 = 0;
-        }
+        dy = (y1 > y2)? (y1-y2) : (y2-y1);
         y2 = y1;
-        // debug
-        //locate(1,80);
-        //printf("d: %4d y: %5d s1: %4d",d1,y1,s1);
 
         // read x voltage
         _xm.input();        //LAJ x- has to be passive
@@ -83,35 +78,57 @@
         AnalogIn Ax(xa);    // we have to call the constructor to switch to analog mode
         wait_us(10);
         x1 = Ax.read_u16(); // get x voltage
-        d2 = (x1 > x2)? (x1-x2) : (x2-x1);
-        if (d2 < 3000) s2 ++;
-        else {
-            s2 = 0;
-        }
+        dx = (x1 > x2)? (x1-x2) : (x2-x1);
         x2 = x1;
-        // debug
-        //locate(1,100);
-        //printf("d: %4d x: %5d s2: %4d",d2,x1,s2);
-        //wait(0.25);
-    } while (s1 < 3 || s2 < 3); // read until we have three samples close together
-    switch (orientation) {
-        case(0):
-        case(2):
-            p.y = (x1+x2) / 2;  // average of two sample
-            p.x = (y1+y2) / 2;
-            break;
-        case(1):
-        case(3):
-            p.x = (x1+x2) / 2;  // average of two sample
-            p.y = (y1+y2) / 2;
-            break;
+        if(dy<accuracy && dx<accuracy) {
+            if(k<8){
+                xs1 += x1;
+                ys1 += y1;
+                i++;
+            } else {
+                xs2 += x1;
+                ys2 += y1;
+                j++;
+            }
+        }
+    } // for:next
+    xs1 = xs1 / i;
+    ys1 = ys1 / i;
+    xs2 = xs2 / j;
+    ys2 = ys2 / j;
+    dy = (ys1 > ys2)? (ys1-ys2) : (ys2-ys1);
+    dx = (xs1 > xs2)? (xs1-xs2) : (xs2-xs1);
+    if(dy<accuracy && dx<accuracy) {
+
+        switch (orientation) {
+            case(0):
+            case(2):
+                p.y = (xs1+xs2) / 2;  // average
+                p.x = (ys1+ys2) / 2;
+                break;
+            case(1):
+            case(3):
+                p.x = (xs1+xs2) / 2;  // average
+                p.y = (ys1+ys2) / 2;
+                break;
+        }
+    } else { // sample average moved too much so discard
+        p.x = 0;
+        p.y = 0;
     }
+    // debug
+    //locate(1,80);
+    //printf("d: %4d y: %5d",dy,p.y);
+    //locate(1,100);
+    //printf("d: %4d x: %5d",dx,p.x);
+    //wait(0.25);
     return(p);
-}
+}//*/
 
 void TOUCH_TFTx2::calibrate(void) {
     int i;
-    int a = 0,b = 0,c = 0, d = 0;
+    int ulx0 = 0, uly0 = 0, brx0 = 0, bry0 = 0;
+    int ulx1 = 0, uly1 = 0, brx1 = 0, bry1 = 0;
     int pos_x, pos_y;
     point p;
 
@@ -125,45 +142,57 @@
     pos_x = pos_x * font[1];
     pos_y = (rows() / 2) - 1;
     pos_y = pos_y * font[2];
+    
+    //calibrate right screen
     locate(pos_x,pos_y);
     printf("press cross");
     locate(pos_x,pos_y + font[2]);
     printf("to calibrate");
     do {
         wait(0.1);
-        } while (!is_touched()); //Wait for touch
+    } while (!is_touched()); //Wait for touch
     for (i=0; i<5; i++) {
-        p = get_touch();
-        b += p.y;
+        do {
+            p = get_touch();
+        } while (p.x==0 && p.y==0);
+        ulx1 += p.x;
+        uly1 += p.y;
     }
-    b = b / 5;
+    ulx1 /= 5;
+    uly1 /= 5;
     locate(pos_x,pos_y);
     printf("OK         ");
+    printf("           ");
     do {
         wait(0.1);
-        } while (is_touched()); //Wait for no touch
+    } while (is_touched()); //Wait for no touch
 
     cls();
-    line(width() -5, height() - 8,width() - 5,height() -1,White);   // paint cross
-    line(width() - 8,height() - 5,width() - 1,height() - 5,White);
+    line(width() -1, height() - 4,width() - 7,height() -4,White);   // paint cross
+    line(width() - 4,height() - 1,width() - 4,height() - 7,White);
     locate(pos_x,pos_y);
     printf("press cross");
     locate(pos_x,pos_y + font[2]);
     printf("to calibrate");
     do {
         wait(0.1);
-        } while (!is_touched()); //Wait for touch
+    } while (!is_touched()); //Wait for touch
     for (i=0; i<5; i++) {
-        p  = get_touch();
-        c+= p.x;
+        do {
+            p = get_touch();
+        } while (p.x==0 && p.y==0);        p  = get_touch();
+        brx1 += p.x;
+        bry1 += p.y;
     }
-    c = c / 5;
+    brx1 /= 5;
+    bry1 /= 5;
 
     locate(pos_x, pos_y);
     printf("OK         ");
+    printf("           ");
     do {
         wait(0.1);
-        } while (is_touched()); //Wait for no touch
+    } while (is_touched()); //Wait for no touch
 
     cls();
     seldisp=0;       // select left display
@@ -171,70 +200,90 @@
     line(0,3,6,3,White);
     line(3,0,3,6,White);
 
-    // get the center of the screen
-    pos_x = columns() / 2 - 3;
-    pos_x = pos_x * font[1];
-    pos_y = (rows() / 2) - 1;
-    pos_y = pos_y * font[2];
+    // now calibrate left screen
     locate(pos_x,pos_y);
     printf("press cross");
     locate(pos_x,pos_y + font[2]);
     printf("to calibrate");
     do {
         wait(0.1);
-        } while (!is_touched()); //Wait for touch
+    } while (!is_touched()); //Wait for touch
     for (i=0; i<5; i++) {
-        p = get_touch();
-        a += p.x;
+        do {
+            p = get_touch();
+        } while (p.x==0 && p.y==0);
+        ulx0 += p.x;
+        uly0 += p.y;
     }
-    a = a / 5;
+    ulx0 /= 5;
+    uly0 /= 5;
     locate(pos_x,pos_y);
     printf("OK         ");
+    printf("           ");
     do {
         wait(0.1);
-        } while (is_touched()); //Wait for no touch
+    } while (is_touched()); //Wait for no touch
+
     cls();
-    line(width() -5, height() - 8,width() - 5,height() -1,White);   // paint cross
-    line(width() - 8,height() - 5,width() - 1,height() - 5,White);
+    line(width() -1, height() - 4,width() - 7,height() -4,White);   // paint cross
+    line(width() - 4,height() - 1,width() - 4,height() - 7,White);
     locate(pos_x,pos_y);
     printf("press cross");
     locate(pos_x,pos_y + font[2]);
     printf("to calibrate");
     do {
         wait(0.1);
-        } while (!is_touched()); //Wait for touch
+    } while (!is_touched()); //Wait for touch
     for (i=0; i<5; i++) {
-        p  = get_touch();
-        d+= p.y;
+        do {
+            p = get_touch();
+        } while (p.x==0 && p.y==0);
+        brx0 += p.x;
+        bry0 += p.y;
     }
-    d = d / 5;
+    brx0 /= 5;
+    bry0 /= 5;
 
     locate(pos_x, pos_y);
     printf("OK         ");
+    printf("           ");
     do {
         wait(0.1);
-        } while (is_touched()); //Wait for no touch
+    } while (is_touched()); //Wait for no touch
 
     cls();
 
-    x_off = a;
-    y_off = b;
+    x0_off = ulx0;
+    y0_off = uly0;
+    x0_pp = (brx0-ulx0) / (width() - 6);
+    y0_pp = (bry0-uly0) / (height() - 6);
+    
+    
+    x1_off = ulx1;
+    y1_off = uly1;
+    x1_pp = (brx1-ulx1) / (width() - 6);
+    y1_pp = (bry1-uly1) / (height() - 6);
+    x_mid = (brx0 + ulx1) / 2;
 
-    i = c-a;  // delta x
-    pp_tx = i / (width() - 6);
-
-    i = d-b;  // delta y
-    pp_ty = i / (height() - 6);
 }
 
 
 point TOUCH_TFTx2::to_pixel(point a_point) {
     point p;
 
-    p.x = (a_point.x - x_off) / pp_tx;
-    if (p.x > width()) p.x = width();
-    p.y = (a_point.y - y_off) / pp_ty;
-    if (p.y > height()) p.y = height();
+    if (a_point.x < x_mid) { // left screen
+        p.x = (a_point.x - x0_off) / x0_pp + 3;
+        if (p.x > width()-1) p.x = width()-1;
+        p.y = (a_point.y - y0_off) / y0_pp + 3;
+    }else{ // right screen
+        p.x = (a_point.x - x1_off) / x1_pp + 3;
+        if (p.x > width()-1) p.x = 2*width()-1;
+        else p.x += width(); // add width to indicate right screen
+        p.y = (a_point.y - y1_off) / y1_pp + 3;
+    }
+    if (p.x<0) p.x=0;
+    if (p.y<0) p.y=0;
+    if (p.y > height()-1) p.y = height()-1;
     //locate(1,60); //debug
     //printf("x: %d\ty: %d",p.x,p.y);
     return (p);