Modified to work with two displays
Dependents: touch2 default CANary_9341_test CANary_merge
Fork of Touch_tft by
Diff: TOUCH_TFTx2.cpp
- Revision:
- 7:9cc43f20e365
- Parent:
- 6:a91b668b058a
- Child:
- 8:fd0abf6a7f59
--- a/TOUCH_TFTx2.cpp Sun Feb 17 16:44:06 2013 +0000 +++ b/TOUCH_TFTx2.cpp Tue Feb 19 18:18:56 2013 +0000 @@ -17,7 +17,7 @@ #include "mbed.h" #define threshold 0x8000 // threshold to detect pressed -#define accuracy 300 // noise filter +#define accuracy 1000 // noise filter #define numsamp 16 // number of averaging samples TOUCH_TFTx2::TOUCH_TFTx2(PinName xp, PinName xm, PinName yp, PinName ym, @@ -30,7 +30,7 @@ } point TOUCH_TFTx2::get_touch() { - unsigned short x1, x2, y1, y2, i=0, j=0, k; + unsigned short x1, x2=0, y1, y2=0, i=0, j=0, k; unsigned long dy , dx, xs1=0, ys1=0, xs2=0, ys2=0; point p; @@ -113,8 +113,8 @@ break; } } else { // sample average moved too much so discard - p.x = 0; - p.y = 0; + p.x = 65535; + p.y = 65535; } // debug //locate(1,80); @@ -126,6 +126,55 @@ return(p); }//*/ + + +point TOUCH_TFTx2::to_pixel(point a_point) { + static point p; + + 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.y > height()-1) p.y = height()-1; + //locate(1,60); //debug + //printf("x: %d\ty: %d",p.x,p.y); + return (p); +} + +bool TOUCH_TFTx2::is_touched(void) { + unsigned short y1; + // read y voltage + _xm.input(); //x- has to be passive + _xp.input(); //x+ has to be passive + _xm.mode(PullDown); + _xp.mode(PullDown); + _yp.output(); + _ym.output(); + _yp = 1; //drive y+ high + _ym = 1; //drive y- high + AnalogIn Ay(xa); // we have to call the constructor to switch to analog mode + wait_us(10); + y1 = Ay.read_u16(); // get y voltage + return (y1>threshold); +} + +void TOUCH_TFTx2::wfi(void) { + _xm.input(); //x- has to be passive + _xp.input(); //x+ has to be passive + _xm.mode(PullDown); + _xp.mode(PullDown); + _yp.output(); + _ym.output(); + _yp = 1; //drive y+ high + _ym = 1; //drive y- high +} + void TOUCH_TFTx2::calibrate(void) { int i; int ulx0 = 0, uly0 = 0, brx0 = 0, bry0 = 0; @@ -276,58 +325,8 @@ y1_pp = (bry1-uly1) / (height() - 6); x_mid = (brx0 + ulx1) / 2; //debug - printf("x0_off:%d y0_off:%d x0_pp:%d y0_pp:%d\n",x0_off,y0_off,x0_pp,y0_pp); - printf("x1_off:%d y1_off:%d x1_pp:%d y1_pp:%d\n",x1_off,y1_off,x1_pp,y1_pp); - printf("x_mid:%d\n",x_mid); + //printf("x0_off:%d y0_off:%d x0_pp:%d y0_pp:%d\n",x0_off,y0_off,x0_pp,y0_pp); + //printf("x1_off:%d y1_off:%d x1_pp:%d y1_pp:%d\n",x1_off,y1_off,x1_pp,y1_pp); + //printf("x_mid:%d\n",x_mid); } } - - -point TOUCH_TFTx2::to_pixel(point a_point) { - point p; - - 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); -} - -bool TOUCH_TFTx2::is_touched(void) { - unsigned short y1; - // read y voltage - _xm.input(); //x- has to be passive - _xp.input(); //x+ has to be passive - _xm.mode(PullDown); - _xp.mode(PullDown); - _yp.output(); - _ym.output(); - _yp = 1; //drive y+ high - _ym = 1; //drive y- high - AnalogIn Ay(xa); // we have to call the constructor to switch to analog mode - wait_us(10); - y1 = Ay.read_u16(); // get y voltage - return (y1>threshold); -} - -void TOUCH_TFTx2::wfi(void) { - _xm.input(); //x- has to be passive - _xp.input(); //x+ has to be passive - _xm.mode(PullDown); - _xp.mode(PullDown); - _yp.output(); - _ym.output(); - _yp = 1; //drive y+ high - _ym = 1; //drive y- high -}