#include #include #include /* Calibrate call touch_calibrate(). Routine set calibrate matrix and save to eeprom. Calibrate using 3 point calibrate. First point is 24,32. Second point is 215,160. Third point is 120,287. Touch screen api will read matrix at init class. No need to call calibrate again. */ lcdFont LCD; touchscreen TS; void showHeader(void) { LCD.Color = WHITE; // graphic color LCD.FgColor = GREEN; // text foreground color LCD.BkColor = RED; // text background color LCD.TextBox( 0, 250, 59, 279, "scrX", ALINE_CENTER | BORDER_RECT | BORDER_FILL | BEVEL(8)); LCD.TextBox( 60, 250, 119, 279, "scrY", ALINE_CENTER | BORDER_RECT | BORDER_FILL | BEVEL(8)); LCD.TextBox(120, 250, 179, 279, "lcdX", ALINE_CENTER | BORDER_RECT | BORDER_FILL | BEVEL(8)); LCD.TextBox(180, 250, 239, 279, "lcdY", ALINE_CENTER | BORDER_RECT | BORDER_FILL | BEVEL(8)); } void showPoint(void) { char sTemp[8]; TS.readPoint(); if (TS.screenPoint.x > 0) { LCD.Color = WHITE; LCD.FgColor = GREEN; // text foreground color LCD.BkColor = RED; // text background color ltoa(TS.screenPoint.x, sTemp, 10); LCD.TextBox( 0, 280, 59, 310, sTemp, ALINE_CENTER | BORDER_RECT | BORDER_FILL | BEVEL(8)); ltoa(TS.screenPoint.y, sTemp, 10); LCD.TextBox( 60, 280, 119, 310, sTemp, ALINE_CENTER | BORDER_RECT | BORDER_FILL | BEVEL(8)); ltoa(TS.displayPoint.x, sTemp, 10); LCD.TextBox(120, 280, 179, 310, sTemp, ALINE_CENTER | BORDER_RECT | BORDER_FILL | BEVEL(8)); ltoa(TS.displayPoint.y, sTemp, 10); LCD.TextBox(180, 280, 239, 310, sTemp, ALINE_CENTER | BORDER_RECT | BORDER_FILL | BEVEL(8)); if (TS.displayPoint.y > LCD.getMaxY()) { LCD.Clear(BLACK); showHeader(); } if ((TS.displayPoint.x > 4) && (TS.displayPoint.y > 4) && (TS.displayPoint.x < LCD.getMaxX()-5) && (TS.displayPoint.y < LCD.getMaxY()-5)) LCD.CircleFill(TS.displayPoint.x, TS.displayPoint.y, 2); } } int setCalibrationMatrix( POINT * displayPtr, POINT * screenPtr, MATRIX * matrixPtr) { int retValue = 1 ; matrixPtr->Divider = ((screenPtr[0].x - screenPtr[2].x) * (screenPtr[1].y - screenPtr[2].y)) - ((screenPtr[1].x - screenPtr[2].x) * (screenPtr[0].y - screenPtr[2].y)) ; if( matrixPtr->Divider == 0 ) { retValue = 0 ; } else { matrixPtr->An = ((displayPtr[0].x - displayPtr[2].x) * (screenPtr[1].y - screenPtr[2].y)) - ((displayPtr[1].x - displayPtr[2].x) * (screenPtr[0].y - screenPtr[2].y)) ; matrixPtr->Bn = ((screenPtr[0].x - screenPtr[2].x) * (displayPtr[1].x - displayPtr[2].x)) - ((displayPtr[0].x - displayPtr[2].x) * (screenPtr[1].x - screenPtr[2].x)) ; matrixPtr->Cn = (screenPtr[2].x * displayPtr[1].x - screenPtr[1].x * displayPtr[2].x) * screenPtr[0].y + (screenPtr[0].x * displayPtr[2].x - screenPtr[2].x * displayPtr[0].x) * screenPtr[1].y + (screenPtr[1].x * displayPtr[0].x - screenPtr[0].x * displayPtr[1].x) * screenPtr[2].y ; matrixPtr->Dn = ((displayPtr[0].y - displayPtr[2].y) * (screenPtr[1].y - screenPtr[2].y)) - ((displayPtr[1].y - displayPtr[2].y) * (screenPtr[0].y - screenPtr[2].y)) ; matrixPtr->En = ((screenPtr[0].x - screenPtr[2].x) * (displayPtr[1].y - displayPtr[2].y)) - ((displayPtr[0].y - displayPtr[2].y) * (screenPtr[1].x - screenPtr[2].x)) ; matrixPtr->Fn = (screenPtr[2].x * displayPtr[1].y - screenPtr[1].x * displayPtr[2].y) * screenPtr[0].y + (screenPtr[0].x * displayPtr[2].y - screenPtr[2].x * displayPtr[0].y) * screenPtr[1].y + (screenPtr[1].x * displayPtr[0].y - screenPtr[0].x * displayPtr[1].y) * screenPtr[2].y ; } return( retValue ) ; } void touch_calibrate(void) { unsigned char press; POINT screenSample[3]; //array of input points POINT displaySample[3] = {{24,32},{215,160},{120,287}}; //array of expected correct answers TS.getMatrix(); POINT capturePoint; //array of valid input points LCD.Color = WHITE; // graphic color LCD.FgColor = WHITE; // text foreground color LCD.BkColor = BLACK; // text background color LCD.TextBox(0, 0, LCD.getMaxX(), 30, "Calibrate", ALINE_CENTER); for (uint8_t i=0; i<3; i++) { // draw touch point LCD.Color = WHITE; LCD.Circle(displaySample[i].x, displaySample[i].y , 4); press = 0; TS.screenPoint.x = 0; TS.screenPoint.y = 0; while(!press) { capturePoint.x = TS.screenPoint.x; capturePoint.y = TS.screenPoint.y; TS.sampling(&TS.screenPoint, 50); if (TS.screenPoint.x > 0) { // Is stable point? if ((capturePoint.x == TS.screenPoint.x) && (capturePoint.y == TS.screenPoint.y)) press = 1; } } // got a point screenSample[i].x = TS.screenPoint.x; screenSample[i].y = TS.screenPoint.y; // mark complete point LCD.Circle(displaySample[i].x, displaySample[i].y,3); delay(20); LCD.Circle(displaySample[i].x, displaySample[i].y,2); delay(20); LCD.Circle(displaySample[i].x, displaySample[i].y,1); delay(60); LCD.Color = WHITE; LCD.Circle(displaySample[i].x, displaySample[i].y,4); delay(20); LCD.Circle(displaySample[i].x, displaySample[i].y,3); delay(20); LCD.Circle(displaySample[i].x, displaySample[i].y,2); delay(20); LCD.Circle(displaySample[i].x, displaySample[i].y,1); delay(250); } LCD.Clear(BLACK); LCD.TextBox(0, 0, LCD.getMaxX(), 30, "Calibrating", ALINE_CENTER); setCalibrationMatrix(&displaySample[0], &screenSample[0], &TS.matrix); // calibration TS.setMatrix(); // save matrix to eeprom LCD.Clear(BLACK); LCD.TextBox(0, 0, LCD.getMaxX(), 30, "Calibrated", ALINE_CENTER); delay(1000); LCD.Clear(BLACK); LCD.Color = WHITE; LCD.Circle(displaySample[0].x, displaySample[0].y , 4); LCD.Circle(displaySample[1].x, displaySample[1].y , 4); LCD.Circle(displaySample[2].x, displaySample[2].y , 4); } void demoColor() { uint16_t color[]={0xf800,0x001f,0xffe0,0x0000,0xffff,0x07ff,0xf81f,0x07e0}; for(uint8_t n=0; n<8; n++) { LCD.Color = color[n]; LCD.RectangleFill(0, 40*n, LCD.getMaxX(), 40*(n+1)-1, 0); } LCD.Color = WHITE; LCD.Rectangle(0+40, 0+40, LCD.getMaxX()-40, LCD.getMaxY()-40, 10); LCD.Color = BLACK; LCD.Rectangle(0+50, 0+50, LCD.getMaxX()-50, LCD.getMaxY()-50, 10); LCD.Color = RED; LCD.Circle(LCD.getMaxX() / 2, LCD.getMaxY() / 2, LCD.getMaxX()/4 + 40); LCD.Color = GREEN; LCD.CircleFill(LCD.getMaxX() / 2, LCD.getMaxY() / 2, LCD.getMaxX()/4 + 30); LCD.FgColor = WHITE; LCD.BkColor = RED; LCD.TextBox(0,0, LCD.getMaxX(), 30, "BL-TFT240320PLUS", ALINE_CENTER); LCD.FgColor = RED; LCD.BkColor = GREEN; LCD.TextBox(0, LCD.getMaxY()-30, LCD.getMaxX(), LCD.getMaxY(), "www.circuitidea.com", ALINE_CENTER); delay(5000); LCD.Clear(BLACK); } void setup() { TS.Reset(); LCD.Reset(); LCD.Clear(BLACK); LCD.Font(f20x19); // demoColor(); touch_calibrate(); // use for calibrate only showHeader(); } void loop() { showPoint(); }