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