Fork with support for ILI9341 controller

Dependents:   CANary_9341 CANary

Fork of TOUCH_TFTx2 by Tick Tock

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TOUCH_TFTx2.cpp Source File

TOUCH_TFTx2.cpp

00001 /* mbed library for resistive touch pads
00002  * uses 4 pins - 2 IO and 2 Analog
00003 
00004  * c 2011 Peter Drescher - DC2PD
00005  *
00006  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00007  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00008  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00009  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00010  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00011  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00012  * THE SOFTWARE.
00013  */
00014 
00015 
00016 #include "TOUCH_TFTx2.h"
00017 #include "mbed.h"
00018 #include "Arial12x12.h"
00019 
00020 #define threshold 0x8000  // threshold to detect pressed
00021 #define accuracy 1000 // noise filter
00022 #define numsamp 16 // number of averaging samples
00023 
00024 #if USE_ILI9341== 1
00025 TOUCH_TFTx2::TOUCH_TFTx2(PinName xp, PinName xm, PinName yp, PinName ym,
00026                      PinName mosi, PinName miso, PinName sclk, PinName cs0, 
00027                      PinName cs1, PinName reset, PinName dc, 
00028                      const char* name):
00029         _xp(xp),_xm(xm),_yp(yp),_ym(ym),_ax(xp),_ay(yp),
00030         SPI_TFTx2_ILI9341(mosi,miso,sclk,cs0,cs1,reset,dc,name) {
00031     xa = xp;
00032     ya = yp;
00033 }
00034 #else
00035 TOUCH_TFTx2::TOUCH_TFTx2(PinName xp, PinName xm, PinName yp, PinName ym,
00036                      PinName mosi, PinName miso, PinName sclk, PinName cs0, 
00037                      PinName cs1, PinName reset, 
00038                      const char* name):
00039         _xp(xp),_xm(xm),_yp(yp),_ym(ym),_ax(xp),_ay(yp),
00040         SPI_TFTx2(mosi,miso,sclk,cs0,cs1,reset,name) {
00041     xa = xp;
00042     ya = yp;
00043 }
00044 #endif
00045 
00046 point TOUCH_TFTx2::get_touch() {
00047     unsigned short x1, x2=0, y1, y2=0, i=0, j=0, k;
00048     unsigned long dy , dx, xs1=0, ys1=0, xs2=0, ys2=0;
00049     point p;
00050 
00051     for (k=0; j<numsamp; k++) {
00052         // read y voltage
00053         _ym.input();        //LAJ y- has to be passive
00054         _yp.input();        //LAJ y+ has to be passive
00055         _xp.output();
00056         _xm.output();
00057         switch (orientation) {
00058             case(0):
00059             case(3):
00060                 _xp = 1;
00061                 _xm = 0;
00062                 break;
00063             case(1):
00064             case(2):
00065                 _xp = 0;
00066                 _xm = 1;
00067                 break;
00068         }
00069         AnalogIn Ay(ya);    // we have to call the constructor to switch to analog mode
00070         wait_us(10);
00071         y1 = Ay.read_u16(); // get y voltage
00072         dy = (y1 > y2)? (y1-y2) : (y2-y1);
00073         y2 = y1;
00074 
00075         // read x voltage
00076         _xm.input();        //LAJ x- has to be passive
00077         _xp.input();        //LAJ x+ has to be passive
00078         _yp.output();
00079         _ym.output();
00080         switch (orientation) {
00081             case(0):
00082             case(1):
00083                 _yp = 1;
00084                 _ym = 0;
00085                 break;
00086             case(2):
00087             case(3):
00088                 _yp = 0;
00089                 _ym = 1;
00090                 break;
00091         }
00092         AnalogIn Ax(xa);    // we have to call the constructor to switch to analog mode
00093         wait_us(10);
00094         x1 = Ax.read_u16(); // get x voltage
00095         dx = (x1 > x2)? (x1-x2) : (x2-x1);
00096         x2 = x1;
00097         if(dy<accuracy && dx<accuracy) {
00098             if(k<numsamp/2){
00099                 xs1 += x1;
00100                 ys1 += y1;
00101                 i++;
00102             } else {
00103                 xs2 += x1;
00104                 ys2 += y1;
00105                 j++;
00106             }
00107         }
00108     } // for:next
00109     xs1 = xs1 / i;
00110     ys1 = ys1 / i;
00111     xs2 = xs2 / j;
00112     ys2 = ys2 / j;
00113     dy = (ys1 > ys2)? (ys1-ys2) : (ys2-ys1);
00114     dx = (xs1 > xs2)? (xs1-xs2) : (xs2-xs1);
00115     if(dy<accuracy && dx<accuracy) {
00116 
00117         switch (orientation) {
00118             case(0):
00119             case(2):
00120                 p.y = (xs1+xs2) / 2;  // average
00121                 p.x = (ys1+ys2) / 2;
00122                 break;
00123             case(1):
00124             case(3):
00125                 p.x = (xs1+xs2) / 2;  // average
00126                 p.y = (ys1+ys2) / 2;
00127                 break;
00128         }
00129     } else { // sample average moved too much so discard
00130         p.x = 65535;
00131         p.y = 65535;
00132     }
00133     // debug
00134     //locate(1,160);
00135     //printf("d: %4d x: %5d",dx,p.x);
00136     //locate(1,200);
00137     //printf("d: %4d y: %5d",dy,p.y);
00138     //wait(0.25);
00139     wfi(); //enable touchpad input
00140     return(p);
00141 }//*/
00142 
00143 
00144 
00145 point TOUCH_TFTx2::to_pixel(point a_point) {
00146     static point p;
00147 
00148     if (a_point.x < x_mid) { // left screen
00149         p.x = (a_point.x - x0_off) / x0_pp + 3;
00150         if (p.x > width()-1) p.x = width()-1;
00151         p.y = (a_point.y - y0_off) / y0_pp + 3;
00152     }else{ // right screen
00153         p.x = (a_point.x - x1_off) / x1_pp + 3;
00154         if (p.x > width()-1) p.x = 2*width()-1;
00155         else p.x += width(); // add width to indicate right screen
00156         p.y = (a_point.y - y1_off) / y1_pp + 3;
00157     }
00158     if (p.y > height()-1) p.y = height()-1;
00159     //locate(1,60); //debug
00160     //printf("x: %d\ty: %d",p.x,p.y);
00161     return (p);
00162 }
00163 
00164 bool TOUCH_TFTx2::is_touched(void) {
00165     unsigned short y1;
00166     // read y voltage
00167     _xm.input();        //x- has to be passive
00168     _xp.input();        //x+ has to be passive
00169     _xm.mode(PullDown);
00170     _xp.mode(PullDown);
00171     _yp.output();
00172     _ym.output();
00173     _yp = 1;            //drive y+ high
00174     _ym = 1;            //drive y- high
00175     AnalogIn Ay(xa);    // we have to call the constructor to switch to analog mode
00176     wait_us(10);
00177     y1 = Ay.read_u16(); // get y voltage
00178     return (y1>threshold);
00179 }
00180 
00181 void TOUCH_TFTx2::wfi(void) {
00182     _xm.input();        //x- has to be passive
00183     _xp.input();        //x+ has to be passive
00184     _xm.mode(PullDown);
00185     _xp.mode(PullDown);
00186     _yp.output();
00187     _ym.output();
00188     _yp = 1;            //drive y+ high
00189     _ym = 1;            //drive y- high
00190 }
00191 
00192 void TOUCH_TFTx2::setcal(int _x0off, int _y0off, int _x0pp, int _y0pp, int _x1off, int _y1off, int _x1pp, int _y1pp, int _xmid) {
00193         x0_off = _x0off;
00194         y0_off = _y0off;
00195         x0_pp = _x0pp;
00196         y0_pp = _y0pp;
00197         x1_off = _x1off;
00198         y1_off = _y1off;
00199         x1_pp = _x1pp;
00200         y1_pp = _y1pp;
00201         x_mid = _xmid;
00202 }
00203 
00204 void TOUCH_TFTx2::calibrate(void) {
00205     int i;
00206     int ulx0 = 0, uly0 = 0, brx0 = 0, bry0 = 0;
00207     int ulx1 = 0, uly1 = 0, brx1 = 0, bry1 = 0;
00208     int pos_x, pos_y;
00209     point p;
00210     seldisp=1;       // select right display
00211     set_font((unsigned char*) Arial12x12);
00212     foreground(White);
00213     background(Black);
00214     cls();
00215     line(0,3,6,3,White);
00216     line(3,0,3,6,White);
00217 
00218     // get the center of the screen
00219     pos_x = columns() / 2 - 3;
00220     pos_x = pos_x * font[1];
00221     pos_y = (rows() / 2) - 1;
00222     pos_y = pos_y * font[2];
00223     
00224     //calibrate right screen
00225     locate(pos_x,pos_y);
00226     printf("press cross");
00227     locate(pos_x,pos_y + font[2]);
00228     printf("to calibrate");
00229     do {
00230         wait(0.1);
00231     } while (!is_touched()); //Wait for touch
00232     for (i=0; i<5; i++) {
00233         do {
00234             p = get_touch();
00235         } while (p.x==0 && p.y==0);
00236         ulx1 += p.x;
00237         uly1 += p.y;
00238     }
00239     ulx1 /= 5;
00240     uly1 /= 5;
00241     locate(pos_x,pos_y);
00242     printf("OK         ");
00243     printf("           ");
00244     do {
00245         wait(0.1);
00246     } while (is_touched()); //Wait for no touch
00247 
00248     cls();
00249     line(width() -1, height() - 4,width() - 7,height() -4,White);   // paint cross
00250     line(width() - 4,height() - 1,width() - 4,height() - 7,White);
00251     locate(pos_x,pos_y);
00252     printf("press cross");
00253     locate(pos_x,pos_y + font[2]);
00254     printf("to calibrate");
00255     do {
00256         wait(0.1);
00257     } while (!is_touched()); //Wait for touch
00258     for (i=0; i<5; i++) {
00259         do {
00260             p = get_touch();
00261         } while (p.x==0 && p.y==0);        p  = get_touch();
00262         brx1 += p.x;
00263         bry1 += p.y;
00264     }
00265     brx1 /= 5;
00266     bry1 /= 5;
00267 
00268     locate(pos_x, pos_y);
00269     printf("OK         ");
00270     printf("           ");
00271     do {
00272         wait(0.1);
00273     } while (is_touched()); //Wait for no touch
00274 
00275     cls();
00276     seldisp=0;       // select left display
00277     cls();
00278     line(0,3,6,3,White);
00279     line(3,0,3,6,White);
00280 
00281     // now calibrate left screen
00282     locate(pos_x,pos_y);
00283     printf("press cross");
00284     locate(pos_x,pos_y + font[2]);
00285     printf("to calibrate");
00286     do {
00287         wait(0.1);
00288     } while (!is_touched()); //Wait for touch
00289     for (i=0; i<5; i++) {
00290         do {
00291             p = get_touch();
00292         } while (p.x==0 && p.y==0);
00293         ulx0 += p.x;
00294         uly0 += p.y;
00295     }
00296     ulx0 /= 5;
00297     uly0 /= 5;
00298     locate(pos_x,pos_y);
00299     printf("OK         ");
00300     printf("           ");
00301     do {
00302         wait(0.1);
00303     } while (is_touched()); //Wait for no touch
00304 
00305     cls();
00306     line(width() -1, height() - 4,width() - 7,height() -4,White);   // paint cross
00307     line(width() - 4,height() - 1,width() - 4,height() - 7,White);
00308     locate(pos_x,pos_y);
00309     printf("press cross");
00310     locate(pos_x,pos_y + font[2]);
00311     printf("to calibrate");
00312     do {
00313         wait(0.1);
00314     } while (!is_touched()); //Wait for touch
00315     for (i=0; i<5; i++) {
00316         do {
00317             p = get_touch();
00318         } while (p.x==0 && p.y==0);
00319         brx0 += p.x;
00320         bry0 += p.y;
00321     }
00322     brx0 /= 5;
00323     bry0 /= 5;
00324 
00325     locate(pos_x, pos_y);
00326     printf("OK         ");
00327     printf("           ");
00328     do {
00329         wait(0.1);
00330     } while (is_touched()); //Wait for no touch
00331 
00332     cls();
00333 
00334     x0_off = ulx0;
00335     y0_off = uly0;
00336     x0_pp = (brx0-ulx0) / (width() - 6);
00337     y0_pp = (bry0-uly0) / (height() - 6);
00338     
00339     
00340     x1_off = ulx1;
00341     y1_off = uly1;
00342     x1_pp = (brx1-ulx1) / (width() - 6);
00343     y1_pp = (bry1-uly1) / (height() - 6);
00344     x_mid = (brx0 + ulx1) / 2;
00345     //debug
00346     //printf("x0_off:%d y0_off:%d x0_pp:%d y0_pp:%d\n",x0_off,y0_off,x0_pp,y0_pp);
00347     //printf("x1_off:%d y1_off:%d x1_pp:%d y1_pp:%d\n",x1_off,y1_off,x1_pp,y1_pp);
00348     //printf("x_mid:%d\n",x_mid);
00349 }