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 = 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 = 2000; 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 //int temp = p.x; 00106 //p.x = p.y; 00107 //p.y = temp; 00108 //locate(0,50); 00109 //printf("x: %6i y: %6i",p.x,p.y); 00110 return touch; 00111 } 00112 00113 void SeeedStudioTFTv2::calibrate(void) 00114 { 00115 int i; 00116 int a = 0,b = 0,c = 0, d = 0; 00117 int pos_x = 0, pos_y = 0; 00118 point p; 00119 00120 cls(); 00121 foreground(White); // set chars to white 00122 line(0,3,6,3,White); 00123 line(3,0,3,6,White); 00124 if (font) 00125 { 00126 // get the center of the screen 00127 pos_x = columns() / 2 - 3; 00128 pos_x = pos_x * font[1]; 00129 pos_y = (rows() / 2) - 1; 00130 pos_y = pos_y * font[2]; 00131 locate(pos_x,pos_y); 00132 printf("press cross "); 00133 locate(pos_x,pos_y + font[2]); 00134 printf("to calibrate "); 00135 } 00136 for (i=0; i<5; i++) { 00137 while (getTouch(p) != YES) 00138 /*nothing*/; 00139 a += p.x; 00140 b += p.y; 00141 } 00142 a = a / 5; 00143 b = b / 5; 00144 if (font) 00145 { 00146 locate(pos_x,pos_y); 00147 printf("ok "); 00148 locate(pos_x,pos_y + font[2]); 00149 printf("release touch "); 00150 } 00151 while (getTouch(p) != NO) 00152 /*nothing*/; 00153 cls(); 00154 line(width() -5, height() - 8,width() - 5,height() -1,White); // paint cross 00155 line(width() - 8,height() - 5,width() - 1,height() - 5,White); 00156 if (font) 00157 { 00158 locate(pos_x,pos_y); 00159 printf("press cross "); 00160 locate(pos_x,pos_y + font[2]); 00161 printf("to calibrate "); 00162 } 00163 for (i=0; i<5; i++) { 00164 while (getTouch(p) != YES) 00165 /*nothing*/; 00166 c+= p.x; 00167 d+= p.y; 00168 } 00169 c = c / 5; 00170 d = d / 5; 00171 x_off = a; 00172 y_off = b; 00173 i = c-a; // delta x 00174 pp_tx = i / (width() - 6); 00175 i = d-b; // delta y 00176 pp_ty = i / (height() - 6); 00177 if (font) 00178 { 00179 locate(pos_x,pos_y); 00180 printf("Calibrated "); 00181 locate(pos_x,pos_y + font[2]); 00182 printf("x %6i %4i", x_off, pp_tx); 00183 locate(pos_x,pos_y + 2*font[2]); 00184 printf("y %6i %4i", y_off, pp_ty); 00185 } 00186 while (getTouch(p) != NO) 00187 /*nothing*/; 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 Sun Jul 17 2022 01:29:19 by
1.7.2
