Basically i glued Peter Drescher and Simon Ford libs in a GraphicsDisplay class, then derived TFT or LCD class (which inherits Protocols class), then the most derived ones (Inits), which are per-display and are the only part needed to be adapted to diff hw.
Fork of UniGraphic by
Touch.cpp
00001 /************************************************************************************************** 00002 ***** ***** 00003 ***** Name: Touch.cpp ***** 00004 ***** Ver.: 1.0 ***** 00005 ***** Date: 04/01/2013 ***** 00006 ***** Auth: Frank Vannieuwkerke ***** 00007 ***** Erik Olieman ***** 00008 ***** Func: Touch driver for use with ADS7843 ***** 00009 ***** ***** 00010 ***** Code based on Carlos E. Vidales tutorial : ***** 00011 ***** How To Calibrate Touch Screens ***** 00012 ***** www.embedded.com/design/configurable-systems/4023968/How-To-Calibrate-Touch-Screens ***** 00013 ***** ***** 00014 **************************************************************************************************/ 00015 00016 #include "Touch.h" 00017 #include "mbed.h" 00018 #include "Arial12x12.h" 00019 00020 #define THRESHOLD 2 00021 00022 TouchScreenADS7843::TouchScreenADS7843(PinName tp_mosi, PinName tp_miso, PinName tp_sclk, PinName tp_cs, PinName tp_irq, TFT *_LCD) 00023 : LCD(_LCD), _tp_spi(tp_mosi, tp_miso, tp_sclk), _tp_cs(tp_cs), _tp_irq(tp_irq) 00024 { 00025 DisplaySample[0].x=45; 00026 DisplaySample[0].y=45; 00027 DisplaySample[1].x=45; 00028 DisplaySample[1].y=270; 00029 DisplaySample[2].x=190; 00030 DisplaySample[2].y=190; 00031 ScreenSample[0].x=45; 00032 ScreenSample[0].y=45; 00033 ScreenSample[1].x=45; 00034 ScreenSample[1].y=270; 00035 ScreenSample[2].x=190; 00036 ScreenSample[2].y=190; 00037 _tp_cs=1; 00038 _tp_spi.frequency(500000); 00039 _tp_spi.format(8,0); // 8 bit spi mode 0 00040 } 00041 00042 int TouchScreenADS7843::Read_XY(unsigned char XY) 00043 { 00044 unsigned char msb, lsb; 00045 unsigned int Temp; 00046 00047 Temp=0; 00048 _tp_cs=0; 00049 wait_us(SPI_RD_DELAY); 00050 _tp_spi.write(XY); 00051 wait_us(SPI_RD_DELAY); 00052 msb = _tp_spi.write(0x00); // msb 00053 wait_us(SPI_RD_DELAY); 00054 lsb = _tp_spi.write(0x00); // lsb 00055 _tp_cs=1; 00056 Temp = ((msb << 8 ) | lsb); 00057 Temp >>= 3; 00058 Temp &= 0xfff; 00059 Temp /= 4; // Scaling : return value range must be between 0 and 1024 00060 return(Temp); 00061 } 00062 00063 00064 void TouchScreenADS7843::TP_GetAdXY(int *x,int *y) 00065 { 00066 int adx,ady; 00067 adx = Read_XY(CHX); 00068 wait_us(1); 00069 ady = Read_XY(CHY); 00070 *x = adx; 00071 *y = ady; 00072 } 00073 00074 void TouchScreenADS7843::TP_DrawPoint(unsigned int Xpos,unsigned int Ypos, unsigned int color) 00075 { 00076 // LCD->wr_reg(0x03, 0x1030); 00077 // LCD->WindowMax(); 00078 LCD->pixel(Xpos,Ypos,color); 00079 LCD->pixel(Xpos+1,Ypos,color); 00080 LCD->pixel(Xpos,Ypos+1,color); 00081 LCD->pixel(Xpos+1,Ypos+1,color); 00082 } 00083 00084 void TouchScreenADS7843::DrawCross(unsigned int Xpos,unsigned int Ypos) 00085 { 00086 // LCD->rect(Xpos-7,Ypos+7,Xpos+7,Ypos-7, Red); 00087 00088 LCD->line(Xpos-15,Ypos,Xpos-2,Ypos,White); //left 00089 LCD->line(Xpos+2,Ypos,Xpos+15,Ypos,White); 00090 LCD->line(Xpos,Ypos-15,Xpos,Ypos-2,White); 00091 LCD->line(Xpos,Ypos+2,Xpos,Ypos+15,White); 00092 00093 LCD->line(Xpos-15,Ypos+15,Xpos-7,Ypos+15,DarkGrey); 00094 LCD->line(Xpos-15,Ypos+7,Xpos-15,Ypos+15,DarkGrey); 00095 00096 LCD->line(Xpos-15,Ypos-15,Xpos-7,Ypos-15,DarkGrey); 00097 LCD->line(Xpos-15,Ypos-7,Xpos-15,Ypos-15,DarkGrey); 00098 00099 LCD->line(Xpos+7,Ypos+15,Xpos+15,Ypos+15,DarkGrey); 00100 LCD->line(Xpos+15,Ypos+7,Xpos+15,Ypos+15,DarkGrey); 00101 00102 LCD->line(Xpos+7,Ypos-15,Xpos+15,Ypos-15,DarkGrey); 00103 LCD->line(Xpos+15,Ypos-15,Xpos+15,Ypos-7,DarkGrey); 00104 } 00105 00106 unsigned char TouchScreenADS7843::Read_Ads7843(Coordinate * screenPtr) 00107 { 00108 int m0,m1,m2,TP_X[1],TP_Y[1],temp[3]; 00109 uint8_t count=0; 00110 int buffer[2][9]={{0},{0}}; 00111 if (screenPtr == NULL) screenPtr = &screen; 00112 do 00113 { 00114 TP_GetAdXY(TP_X,TP_Y); 00115 buffer[0][count]=TP_X[0]; 00116 buffer[1][count]=TP_Y[0]; 00117 count++; 00118 } 00119 while(!_tp_irq && (count < 9)); 00120 if(count==9) 00121 { 00122 temp[0]=(buffer[0][0]+buffer[0][1]+buffer[0][2])/3; 00123 temp[1]=(buffer[0][3]+buffer[0][4]+buffer[0][5])/3; 00124 temp[2]=(buffer[0][6]+buffer[0][7]+buffer[0][8])/3; 00125 m0=temp[0]-temp[1]; 00126 m1=temp[1]-temp[2]; 00127 m2=temp[2]-temp[0]; 00128 m0=m0>0?m0:(-m0); 00129 m1=m1>0?m1:(-m1); 00130 m2=m2>0?m2:(-m2); 00131 if( (m0>THRESHOLD) && (m1>THRESHOLD) && (m2>THRESHOLD) ) return 0; 00132 if(m0<m1) 00133 { 00134 if(m2<m0) 00135 screenPtr->x=(temp[0]+temp[2])/2; 00136 else 00137 screenPtr->x=(temp[0]+temp[1])/2; 00138 } 00139 else if(m2<m1) 00140 screenPtr->x=(temp[0]+temp[2])/2; 00141 else 00142 screenPtr->x=(temp[1]+temp[2])/2; 00143 00144 temp[0]=(buffer[1][0]+buffer[1][1]+buffer[1][2])/3; 00145 temp[1]=(buffer[1][3]+buffer[1][4]+buffer[1][5])/3; 00146 temp[2]=(buffer[1][6]+buffer[1][7]+buffer[1][8])/3; 00147 m0=temp[0]-temp[1]; 00148 m1=temp[1]-temp[2]; 00149 m2=temp[2]-temp[0]; 00150 m0=m0>0?m0:(-m0); 00151 m1=m1>0?m1:(-m1); 00152 m2=m2>0?m2:(-m2); 00153 if( (m0>THRESHOLD) && (m1>THRESHOLD) && (m2>THRESHOLD) ) return 0; 00154 00155 if(m0<m1) 00156 { 00157 if(m2<m0) 00158 screenPtr->y=(temp[0]+temp[2])/2; 00159 else 00160 screenPtr->y=(temp[0]+temp[1])/2; 00161 } 00162 else if(m2<m1) 00163 screenPtr->y=(temp[0]+temp[2])/2; 00164 else 00165 screenPtr->y=(temp[1]+temp[2])/2; 00166 return 1; 00167 } 00168 return 0; 00169 } 00170 00171 uint8_t TouchScreenADS7843::setCalibrationMatrix( Coordinate * displayPtr, 00172 Coordinate * screenPtr, 00173 Matrix * matrixPtr) 00174 { 00175 uint8_t retTHRESHOLD = 0 ; 00176 // K = (Xs0 - Xs2)*(Ys1 - Ys2) - (Xs1 - Xs2)*(Ys0 - Ys2) 00177 matrixPtr->Divider = ((screenPtr[0].x - screenPtr[2].x) * (screenPtr[1].y - screenPtr[2].y)) - 00178 ((screenPtr[1].x - screenPtr[2].x) * (screenPtr[0].y - screenPtr[2].y)) ; 00179 if( matrixPtr->Divider == 0 ) 00180 { 00181 retTHRESHOLD = 1; 00182 } 00183 else 00184 { 00185 // (Xd0 - Xd2)*(Ys1 - Ys2) - (Xd1 - Xd2)*(Ys0 - Ys2) 00186 // A = --------------------------------------------------- 00187 // K 00188 matrixPtr->An = ((displayPtr[0].x - displayPtr[2].x) * (screenPtr[1].y - screenPtr[2].y)) - 00189 ((displayPtr[1].x - displayPtr[2].x) * (screenPtr[0].y - screenPtr[2].y)) ; 00190 // (Xs0 - Xs2)*(Xd1 - Xd2) - (Xd0 - Xd2)*(Xs1 - Xs2) 00191 // B = --------------------------------------------------- 00192 // K 00193 matrixPtr->Bn = ((screenPtr[0].x - screenPtr[2].x) * (displayPtr[1].x - displayPtr[2].x)) - 00194 ((displayPtr[0].x - displayPtr[2].x) * (screenPtr[1].x - screenPtr[2].x)) ; 00195 // Ys0*(Xs2*Xd1 - Xs1*Xd2) + Ys1*(Xs0*Xd2 - Xs2*Xd0) + Ys2*(Xs1*Xd0 - Xs0*Xd1) 00196 // C = ---------------------------------------------------------------------------- 00197 // K 00198 matrixPtr->Cn = (screenPtr[2].x * displayPtr[1].x - screenPtr[1].x * displayPtr[2].x) * screenPtr[0].y + 00199 (screenPtr[0].x * displayPtr[2].x - screenPtr[2].x * displayPtr[0].x) * screenPtr[1].y + 00200 (screenPtr[1].x * displayPtr[0].x - screenPtr[0].x * displayPtr[1].x) * screenPtr[2].y ; 00201 // (Yd0 - Yd2)*(Ys1 - Ys2) - (Yd1 - Yd2)*(Ys0 - Ys2) 00202 // D = --------------------------------------------------- 00203 // K 00204 matrixPtr->Dn = ((displayPtr[0].y - displayPtr[2].y) * (screenPtr[1].y - screenPtr[2].y)) - 00205 ((displayPtr[1].y - displayPtr[2].y) * (screenPtr[0].y - screenPtr[2].y)) ; 00206 // (Xs0 - Xs2)*(Yd1 - Yd2) - (Yd0 - Yd2)*(Xs1 - Xs2) 00207 // E = --------------------------------------------------- 00208 // K 00209 matrixPtr->En = ((screenPtr[0].x - screenPtr[2].x) * (displayPtr[1].y - displayPtr[2].y)) - 00210 ((displayPtr[0].y - displayPtr[2].y) * (screenPtr[1].x - screenPtr[2].x)) ; 00211 // Ys0*(Xs2*Yd1 - Xs1*Yd2) + Ys1*(Xs0*Yd2 - Xs2*Yd0) + Ys2*(Xs1*Yd0 - Xs0*Yd1) 00212 // F = ---------------------------------------------------------------------------- 00213 // K 00214 matrixPtr->Fn = (screenPtr[2].x * displayPtr[1].y - screenPtr[1].x * displayPtr[2].y) * screenPtr[0].y + 00215 (screenPtr[0].x * displayPtr[2].y - screenPtr[2].x * displayPtr[0].y) * screenPtr[1].y + 00216 (screenPtr[1].x * displayPtr[0].y - screenPtr[0].x * displayPtr[1].y) * screenPtr[2].y ; 00217 } 00218 return( retTHRESHOLD ) ; 00219 } 00220 00221 uint8_t TouchScreenADS7843::getDisplayPoint(void) 00222 { 00223 uint8_t retTHRESHOLD = 0 ; 00224 00225 if( matrix.Divider != 0 ) 00226 { 00227 // XD = AX+BY+C 00228 display.x = ( (matrix.An * screen.x) + 00229 (matrix.Bn * screen.y) + 00230 matrix.Cn 00231 ) / matrix.Divider ; 00232 // YD = DX+EY+F 00233 display.y = ( (matrix.Dn * screen.x) + 00234 (matrix.En * screen.y) + 00235 matrix.Fn 00236 ) / matrix.Divider ; 00237 } 00238 else 00239 { 00240 retTHRESHOLD = 1; 00241 } 00242 return(retTHRESHOLD); 00243 } 00244 00245 void TouchScreenADS7843::TouchPanel_Calibrate(void) 00246 { 00247 uint8_t i; 00248 Coordinate screen_cal; 00249 setCalibrationMatrix( &DisplaySample[0],&ScreenSample[0],&matrix) ; 00250 LCD->set_font((unsigned char*) Arial12x12); 00251 for(i=0;i<3;i++) 00252 { 00253 LCD->cls(); 00254 LCD->locate(10,10); 00255 LCD->printf("Touch crosshair to calibrate"); 00256 wait_ms(500); 00257 DrawCross(DisplaySample[i].x,DisplaySample[i].y); 00258 do {} while (!Read_Ads7843(&screen_cal)); 00259 ScreenSample[i].x= screen_cal.x;ScreenSample[i].y= screen_cal.y; 00260 } 00261 setCalibrationMatrix( &DisplaySample[0],&ScreenSample[0],&matrix) ; 00262 LCD->cls(); 00263 } 00264 00265 void TouchScreenADS7843::GetCalibration(Matrix * matrixPtr, Coordinate * screenPtr) 00266 { 00267 uint8_t i; 00268 Matrix * mp1; 00269 mp1 = &matrix; 00270 *matrixPtr = *mp1; 00271 for(i=0;i<3;i++) 00272 { 00273 screenPtr[i].x = ScreenSample[i].x; 00274 screenPtr[i].y = ScreenSample[i].y; 00275 } 00276 } 00277 00278 void TouchScreenADS7843::SetCalibration(Matrix * matrixPtr, Coordinate * screenPtr) 00279 { 00280 uint8_t i; 00281 Matrix * mp1; 00282 mp1 = &matrix; 00283 *mp1 = *matrixPtr; 00284 for(i=0;i<3;i++) 00285 { 00286 ScreenSample[i].x = screenPtr[i].x; 00287 ScreenSample[i].y = screenPtr[i].y; 00288 } 00289 setCalibrationMatrix( &DisplaySample[0],&ScreenSample[0],&matrix) ; 00290 }
Generated on Wed Jul 13 2022 21:19:22 by 1.7.2