Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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 = 17252;// 17252 16605 108755 108000 00045 y_off = 22330;// 22330 105819 97167 22000 00046 pp_tx = 378 ; // 378 289 -390 -291 00047 pp_ty = 261; // 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 { int Xperso, Yperso; 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,20); 00106 printf("x1: %6i",p.x); 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 00137 a += p.x; 00138 b += p.y; 00139 } 00140 a = a / 5; 00141 b = b / 5; 00142 if (font) 00143 { 00144 locate(pos_x,pos_y); 00145 printf("ok "); 00146 locate(pos_x,pos_y + font[2]); 00147 printf("release touch "); 00148 } 00149 while (getTouch(p) != NO) 00150 /*nothing*/; 00151 cls(); 00152 line(width() -5, height() - 8,width() - 5,height() -1,White); // paint cross 00153 line(width() - 8,height() - 5,width() - 1,height() - 5,White); 00154 if (font) 00155 { 00156 locate(pos_x,pos_y); 00157 printf("press cross "); 00158 locate(pos_x,pos_y + font[2]); 00159 printf("to calibrate "); 00160 } 00161 for (i=0; i<5; i++) { 00162 while (getTouch(p) != YES) 00163 /*nothing*/; 00164 c+= p.x; 00165 d+= p.y; 00166 } 00167 c = c / 5; 00168 d = d / 5; 00169 x_off = a; 00170 y_off = b; 00171 i = c-a; // delta x 00172 pp_tx = i / (width() - 6); 00173 i = d-b; // delta y 00174 pp_ty = i / (height() - 6); 00175 if (font) 00176 { 00177 locate(pos_x,pos_y); 00178 printf("Calibrated "); 00179 locate(pos_x,pos_y + font[2]); 00180 printf("x %6i %4i", x_off, pp_tx); 00181 locate(pos_x,pos_y + 2*font[2]); 00182 printf("y %6i %4i", y_off, pp_ty); 00183 } 00184 while (getTouch(p) != NO) 00185 /*nothing*/; 00186 printf("on continu"); 00187 wait(5); 00188 cls(); 00189 } 00190 00191 point SeeedStudioTFTv2::toPixel(point p) 00192 { 00193 p.x -= x_off; 00194 p.x /= pp_tx; 00195 int w = width(); 00196 if (p.x > w) p.x = w; 00197 if (p.x < 0) p.x = 0; 00198 p.y -= y_off; 00199 p.y /= pp_ty; 00200 int h = height(); 00201 if (p.y > h) p.y = h; 00202 if (p.y < 0) p.y = 0; 00203 return (p); 00204 } 00205 00206 bool SeeedStudioTFTv2::getPixel(point& p) 00207 { 00208 TOUCH touch = getTouch(p); 00209 p = toPixel(p); 00210 return touch == YES; 00211 }
Generated on Thu Jul 14 2022 22:44:05 by
1.7.2
