Fork with support for ILI9341 controller

Dependents:   CANary_9341 CANary

Fork of TOUCH_TFTx2 by Tick Tock

Revision:
4:a3cd26c97b76
Parent:
3:3db7309b6146
Child:
5:a9890c586a64
--- a/TOUCH_TFTx2.cpp	Sun Feb 03 15:22:44 2013 +0000
+++ b/TOUCH_TFTx2.cpp	Mon Feb 11 02:14:15 2013 +0000
@@ -16,7 +16,7 @@
 #include "TOUCH_TFTx2.h"
 #include "mbed.h"
 
-#define threshold 0x2000  // threshold to detect pressed 
+#define threshold 0x1000  // threshold to detect pressed 
 
 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,12 +28,14 @@
 }
 
 point TOUCH_TFTx2::get_touch() {
-    unsigned short x1 = 0,x2 = 0, y1 = 0, y2 = 0;
+    unsigned short x1, x2, y1, y2;
     unsigned int s1 = 0,s2 = 0,d1 , d2;
     point p;
 
     do {
         // read y voltage
+        _ym.input();        //LAJ y- has to be passive
+        _yp.input();        //LAJ y+ has to be passive
         _xp.output();
         _xm.output();
         switch (orientation) {
@@ -48,21 +50,22 @@
                 _xm = 1;
                 break;
         }
-        _ym.input();        // y- have to be passive
         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 (((y1 < 8000) && (d1 < 2000)) || ((y1 > 8000) && (d1 < 150))) s1 ++;
+        if (d1 < 3000) s1 ++;
         else {
-            if (s1 > 0) s1 --;
+            s1 = 0;
         }
         y2 = y1;
         // debug
-        //locate(1,7);
+        //locate(1,80);
         //printf("d: %4d y: %5d s1: %4d",d1,y1,s1);
 
         // read x voltage
+        _xm.input();        //LAJ x- has to be passive
+        _xp.input();        //LAJ x+ has to be passive
         _yp.output();
         _ym.output();
         switch (orientation) {
@@ -77,20 +80,19 @@
                 _ym = 1;
                 break;
         }
-        _xm.input();        // x- have to be passive
         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 (((x1 < 8000) && (d2 < 2000)) || ((x1 > 8000) && (d2 < 150))) s2 ++;
+        if (d2 < 3000) s2 ++;
         else {
-            if (s2 > 0) s2 --;
+            s2 = 0;
         }
         x2 = x1;
         // debug
-        //locate(1,8);
+        //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):
@@ -113,6 +115,7 @@
     int pos_x, pos_y;
     point p;
 
+    seldisp=1;       // select right display
     cls();
     line(0,3,6,3,White);
     line(3,0,3,6,White);
@@ -122,25 +125,23 @@
     pos_x = pos_x * font[1];
     pos_y = (rows() / 2) - 1;
     pos_y = pos_y * font[2];
-
     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
     for (i=0; i<5; i++) {
-        do {
-            p = get_touch();
-        } while (p.x < threshold | p.y < threshold);  // wait for touch
-        a += p.x;
+        p = get_touch();
         b += p.y;
     }
-    a = a / 5;
     b = b / 5;
     locate(pos_x,pos_y);
     printf("OK         ");
     do {
-        p = get_touch();
-    } while (p.y > threshold & p.x > threshold); // wait for no touch
+        wait(0.1);
+        } while (is_touched()); //Wait for no touch
 
     cls();
     line(width() -5, height() - 8,width() - 5,height() -1,White);   // paint cross
@@ -149,21 +150,70 @@
     printf("press cross");
     locate(pos_x,pos_y + font[2]);
     printf("to calibrate");
+    do {
+        wait(0.1);
+        } while (!is_touched()); //Wait for touch
     for (i=0; i<5; i++) {
-        do {
-            p  = get_touch();
-        } while (p.y < threshold | p.x < threshold);  // wait for touch
+        p  = get_touch();
         c+= p.x;
+    }
+    c = c / 5;
+
+    locate(pos_x, pos_y);
+    printf("OK         ");
+    do {
+        wait(0.1);
+        } while (is_touched()); //Wait for no touch
+
+    cls();
+    seldisp=0;       // select left display
+    cls();
+    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];
+    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
+    for (i=0; i<5; i++) {
+        p = get_touch();
+        a += p.x;
+    }
+    a = a / 5;
+    locate(pos_x,pos_y);
+    printf("OK         ");
+    do {
+        wait(0.1);
+        } 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);
+    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
+    for (i=0; i<5; i++) {
+        p  = get_touch();
         d+= p.y;
     }
-    c = c / 5;
     d = d / 5;
 
     locate(pos_x, pos_y);
     printf("OK         ");
     do {
-        p = get_touch();
-    } while (p.y > threshold & p.x > threshold); // wait for no touch
+        wait(0.1);
+        } while (is_touched()); //Wait for no touch
 
     cls();
 
@@ -185,10 +235,24 @@
     if (p.x > width()) p.x = width();
     p.y = (a_point.y - y_off) / pp_ty;
     if (p.y > height()) p.y = height();
+    //locate(1,60); //debug
+    //printf("x: %d\ty: %d",p.x,p.y);
     return (p);
 }
 
-bool TOUCH_TFTx2::is_touched(point a) {
-    if (a.x > threshold & a.y > threshold) return(true);
-    else return(false);
+bool TOUCH_TFTx2::is_touched(void) {
+    unsigned short y1;
+    // read y voltage
+    _ym.input();        //y- has to be passive
+    _yp.input();        //y+ has to be passive
+    _ym.mode(PullUp);
+    _yp.mode(PullUp);
+    _xp.output();
+    _xm.output();
+    _xp = 0;            //drive x+ low
+    _xm = 0;            //drive x- low
+    AnalogIn Ay(ya);    // we have to call the constructor to switch to analog mode
+    wait_us(10);
+    y1 = Ay.read_u16(); // get y voltage
+    return (y1<threshold);
 }