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