aitendo SX032QVGA008

Dependencies:   SPI_TFT_ILI9341ext

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SX032QVGA008.cpp Source File

SX032QVGA008.cpp

00001 /* mbed library for aitendo SX032QVGA08 TFT module with resistive touch panel.
00002  * Original library is "SpeedStudioTFT" by 2014 Copyright (c) Seeed Technology Inc.
00003  * I made by it to the reference.
00004  *
00005  * (1) Because there was some mistake in the source, I was corrected.
00006  * (2) Also, I changed to the source you have to assume FRDM
00007  * (3) I changed BackLight is PWM controlled.
00008  */
00009 
00010 
00011 #include "mbed.h"
00012 #include "SX032QVGA008.h"
00013 
00014 SX032QVGA008::SX032QVGA008(PinName xp, PinName xm, PinName yp, PinName ym,
00015                                    PinName mosi, PinName miso, PinName sclk,
00016                                    PinName csTft, PinName dcTft, PinName resTft, PinName blTft,
00017                                    PinName csSd):
00018 #ifdef USE_SDCARD
00019     SDFileSystem(mosi,miso,sclk,csSd, "sdc"),
00020 #endif
00021     SPI_TFT_ILI9341ext(mosi,miso,sclk,csTft,resTft,dcTft, "tft"),
00022     bl(blTft)
00023 {
00024 #ifndef USE_SDCARD
00025     // sd card
00026     DigitalOut cs(csSd);
00027     cs = 1;
00028 #endif
00029     // backlight PWM controll
00030     bl.period(0.002);
00031     font = NULL;
00032     // touch screen pins
00033     _xp = xp;
00034     _yp = yp;
00035     _xm = xm;
00036     _ym = ym;
00037     // default touch calibration
00038     // orientation     //      0      1      2      3
00039     x_off = 108000;  //  17252  16605 108755 108000
00040     y_off =  22000;  //  22330 105819  97167  22000
00041     pp_tx =   -291;  //    378    289   -390   -291
00042     pp_ty =    356;  //    261   -355   -239    356
00043 }
00044 
00045 void SX032QVGA008::setBacklight(float duty)
00046 {
00047     bl = duty;
00048 }
00049 
00050 int SX032QVGA008::readTouch(PinName p, PinName m, PinName a, PinName i)
00051 {
00052     DigitalOut _p(p);
00053     _p = 1;
00054     DigitalOut _m(m);
00055     _m = 0;
00056     AnalogIn   _a(a);
00057     AnalogIn   _i(i); // this pin has to be high Z (DigitalIn may also work)
00058     wait_us(10);
00059     return _a.read_u16();
00060 }
00061 
00062 SX032QVGA008::TOUCH SX032QVGA008::getTouch(point& p)
00063 {
00064     int y2 = readTouch(_xp,_xm,_yp,_ym);
00065     int x2 = readTouch(_yp,_ym,_xp,_xm);
00066     int y1 = readTouch(_xp,_xm,_yp,_ym);
00067     int x1 = readTouch(_yp,_ym,_xp,_xm);
00068     int xd = x1 - x2;
00069     int yd = y1 - y2;
00070     xd = (xd > 0) ? xd : -xd;
00071 //    yd = (yd > 0) ? xd : -xd;
00072     yd = (yd > 0) ? yd : -yd;
00073     p.x = x1 + x2;
00074     p.y = y1 + y2;
00075 #if 0
00076     DigitalOut _p(_xp);
00077     _p = 1;
00078     DigitalOut _m(_ym);
00079     _m = 0;
00080     AnalogIn   _ax(_xm);
00081     AnalogIn   _ay(_yp);
00082     wait_us(20);
00083     int ax = _ax.read_u16();
00084     int ay = _ay.read_u16();
00085     float z = 0;
00086     z  = (float)ay / ax / x / 2 * 0x10000;
00087 #endif
00088     const int th = 8000;
00089     const int df =  100;
00090     TOUCH touch;
00091     if (x1 < th || x2 < th ||
00092             y1 < th || y2 < th) {
00093         p.x = 0;
00094         p.y = 0;
00095         touch = NO;
00096     } else if (xd > df || yd > df) {
00097         touch = MAYBE;
00098     } else {
00099         touch = YES;
00100     }
00101     //locate(0,50);
00102     //printf("x: %6i y: %6i",p.x,p.y);
00103     return touch;
00104 }
00105 
00106 void SX032QVGA008::calibrate(void)
00107 {
00108     int i;
00109     int a = 0,b = 0,c = 0, d = 0;
00110     int pos_x = 0, pos_y = 0;
00111     point p;
00112 
00113     cls();
00114     foreground(White);    // set chars to white
00115     line(0,3,6,3,White);
00116     line(3,0,3,6,White);
00117     if (font)
00118     {
00119         // get the center of the screen
00120         pos_x = columns() / 2 - 3;
00121         pos_x = pos_x * font[1];
00122         pos_y = (rows() / 2) - 1;
00123         pos_y = pos_y * font[2];
00124         locate(pos_x,pos_y);
00125         printf("press cross    ");
00126         locate(pos_x,pos_y + font[2]);
00127         printf("to calibrate   ");
00128     }
00129     for (i=0; i<5; i++) {
00130         while (getTouch(p) != YES)
00131             /*nothing*/;
00132         a += p.x;
00133         b += p.y;
00134     }
00135     a = a / 5;
00136     b = b / 5;
00137     if (font)
00138     {
00139         locate(pos_x,pos_y);
00140         printf("ok             ");
00141         locate(pos_x,pos_y + font[2]);
00142         printf("release touch  ");
00143     }
00144     while (getTouch(p) != NO)
00145         /*nothing*/;
00146     cls();
00147     line(width() -5, height() - 8,width() - 5,height() -1,White);   // paint cross
00148     line(width() - 8,height() - 5,width() - 1,height() - 5,White);
00149     if (font)
00150     {
00151         locate(pos_x,pos_y);
00152         printf("press cross    ");
00153         locate(pos_x,pos_y + font[2]);
00154         printf("to calibrate   ");
00155     }
00156     for (i=0; i<5; i++) {
00157         while (getTouch(p) != YES)
00158             /*nothing*/;
00159         c+= p.x;
00160         d+= p.y;
00161     }
00162     c = c / 5;
00163     d = d / 5;
00164     x_off = a;
00165     y_off = b;
00166     i = c-a;  // delta x
00167     pp_tx = i / (width() - 6);
00168     i = d-b;  // delta y
00169     pp_ty = i / (height() - 6);
00170     if (font)
00171     {
00172         locate(pos_x,pos_y);
00173         printf("Calibrated     ");
00174         locate(pos_x,pos_y + font[2]);
00175         printf("x %6i %4i", x_off, pp_tx);
00176         locate(pos_x,pos_y + 2*font[2]);
00177         printf("y %6i %4i", y_off, pp_ty);
00178     }
00179     while (getTouch(p) != NO)
00180         /*nothing*/;
00181     cls();
00182 }
00183 
00184 point SX032QVGA008::toPixel(point p)
00185 {
00186     p.x -= x_off;
00187     p.x /= pp_tx;
00188     int w = width();
00189     if (p.x > w) p.x = w;
00190     if (p.x < 0) p.x = 0;
00191     p.y -= y_off;
00192     p.y /= pp_ty;
00193     int h = height();
00194     if (p.y > h) p.y = h;
00195     if (p.y < 0) p.y = 0;
00196     return (p);
00197 }
00198 
00199 bool SX032QVGA008::getPixel(point& p)
00200 {
00201     TOUCH touch = getTouch(p);
00202     p = toPixel(p);
00203     return touch == YES;
00204 }