Driver Library for our displays

Dependents:   dm_bubbles dm_calc dm_paint dm_sdcard_with_adapter ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DmTouchCalibration.cpp Source File

DmTouchCalibration.cpp

00001 /**********************************************************************************************
00002  Copyright (c) 2014 DisplayModule. All rights reserved.
00003 
00004  Redistribution and use of this source code, part of this source code or any compiled binary
00005  based on this source code is permitted as long as the above copyright notice and following
00006  disclaimer is retained.
00007 
00008  DISCLAIMER:
00009  THIS SOFTWARE IS SUPPLIED "AS IS" WITHOUT ANY WARRANTIES AND SUPPORT. DISPLAYMODULE ASSUMES
00010  NO RESPONSIBILITY OR LIABILITY FOR THE USE OF THE SOFTWARE.
00011  ********************************************************************************************/
00012 
00013 #include "DmTouchCalibration.h"
00014 
00015 DmTouchCalibration::DmTouchCalibration(DmTftBase *tftBase, DmTouch *touch) {
00016   _tft = tftBase;
00017   _touch = touch;
00018   _textRow = 20;
00019 }
00020 
00021 // For best result, create a calibration for each display
00022 CalibrationMatrix DmTouchCalibration::getDefaultCalibrationData(DmTouch::Display disp) {
00023   CalibrationMatrix calibrationMatrix = {0};
00024   switch (disp) {
00025     case DmTouch::DM_TFT28_103:
00026       calibrationMatrix.a = 67548;    //    63787;
00027       calibrationMatrix.b = -625;     //     -138;
00028       calibrationMatrix.c = -16854644;//-15921157;
00029       calibrationMatrix.d = 362;      //     -244;
00030       calibrationMatrix.e = 89504;    //    89313;
00031       calibrationMatrix.f = -14380636;//-10726623;
00032       break;
00033 
00034     case DmTouch::DM_TFT24_104:
00035       calibrationMatrix.a = -71855;
00036       calibrationMatrix.b = 2147;
00037       calibrationMatrix.c = 259719524;
00038       calibrationMatrix.d = -1339;
00039       calibrationMatrix.e = -91012;
00040       calibrationMatrix.f = 354268832;
00041       break;
00042 
00043     case DmTouch::DM_TFT28_105:
00044       calibrationMatrix.a = 65521;
00045       calibrationMatrix.b = -253;
00046       calibrationMatrix.c = -11813673;
00047       calibrationMatrix.d = -439;
00048       calibrationMatrix.e = 89201;
00049       calibrationMatrix.f = -10450920;
00050       break;
00051 
00052     case DmTouch::DM_TFT35_107:
00053       calibrationMatrix.a = 91302;    //    85984;
00054       calibrationMatrix.b = 817;      //      451;
00055       calibrationMatrix.c = -26296117;//-16494041;
00056       calibrationMatrix.d = -1877;    //     2308;
00057       calibrationMatrix.e = 73762;    //    65173;
00058       calibrationMatrix.f = -26384255;//-19179080;
00059       break;
00060     case DmTouch::DM_TFT43_108:   // or DM_TFT43_110
00061       calibrationMatrix.a = 541307;
00062       calibrationMatrix.b = -4288;
00063       calibrationMatrix.c = -36678732;
00064       calibrationMatrix.d = 2730;
00065       calibrationMatrix.e = 321714;
00066       calibrationMatrix.f = -31439472;    
00067       break;
00068     case DmTouch::DM_TFT50_111:   // or  DM_TFT50_112
00069       calibrationMatrix.a = 875894;
00070       calibrationMatrix.b = 1655;
00071       calibrationMatrix.c = -53695309;
00072       calibrationMatrix.d = -993;
00073       calibrationMatrix.e = 544421;
00074       calibrationMatrix.f = -41496753;                    
00075       break;   
00076     default:
00077       break;
00078   }
00079   return calibrationMatrix;
00080 }
00081 
00082 
00083 bool DmTouchCalibration::getTouchReferencePoints(Point displayRefPoint[], Point touchRefPoint[], uint16_t tftWidth, uint16_t tftHeight) {
00084   _touch->setPrecison(60);
00085   displayRefPoint[0].x = (tftWidth / 2);
00086   displayRefPoint[0].y = (tftHeight / 2);
00087   displayRefPoint[1].x = 1 * (tftWidth / 5);
00088   displayRefPoint[1].y = 1 * (tftHeight / 5);
00089   displayRefPoint[2].x = 4 * (tftWidth / 5);
00090   displayRefPoint[2].y = 1 * (tftHeight / 5);
00091   displayRefPoint[3].x = 4 * (tftWidth / 5);
00092   displayRefPoint[3].y = 4 * (tftHeight / 5);
00093   displayRefPoint[4].x = 1 * (tftWidth / 5);
00094   displayRefPoint[4].y = 4 * (tftHeight / 5);
00095 
00096   for(int n = 0; n < 5; n++) {
00097     drawCalibPoint(displayRefPoint[n].x, displayRefPoint[n].y);
00098     if (!getRawTouch(touchRefPoint[n].x, touchRefPoint[n].y)) {
00099       return false;
00100     }
00101     releaseTouch(displayRefPoint[n].x, displayRefPoint[n].y);
00102   }
00103 
00104   _touch->setPrecison(3);
00105   return true;
00106 }
00107 
00108 bool DmTouchCalibration::getRawTouch(uint16_t& x, uint16_t& y) const {
00109   bool touched = false;
00110   bool haveAllSamples = false;
00111 
00112   while(!touched) {
00113     if (_touch->isTouched()) {
00114       //Serial.println("is touched");
00115       haveAllSamples = _touch->getMiddleXY(x, y);
00116         
00117       // As many samples are take during calibration it is important to
00118       // make sure that the screen is still touched. If the user has let
00119       // go already then the gathered samples are not good enough.
00120       touched = haveAllSamples && _touch->isTouched();
00121     }
00122   }
00123   return haveAllSamples;
00124 }
00125 
00126 void DmTouchCalibration::drawCalibPoint(uint16_t x, uint16_t y) const {
00127   const uint8_t lineSize = 5;
00128   _tft->drawHorizontalLine(x-lineSize, y, lineSize*2, RED);
00129   _tft->drawVerticalLine(x, y-lineSize, lineSize*2, RED);
00130 }
00131 
00132 void DmTouchCalibration::releaseTouch(uint16_t x, uint16_t y) const {
00133   _tft->drawCircle(x, y, 10, YELLOW);
00134   delay(100);
00135   _tft->fillCircle(x, y, 10, GREEN);
00136   delay(100);
00137   _tft->fillCircle(x, y, 10, BLACK);
00138   delay(300);
00139   _touch->waitForTouchRelease();
00140 }
00141 
00142 CalibrationMatrix DmTouchCalibration::calculateCalibrationMatrix(Point displayRefPoint[], Point touchRefPoint[]) {
00143   double A = 0.0, B = 0.0, C = 0.0, D = 0.0, E = 0.0, F = 0.0;
00144   double d = 0.0, dx1 = 0.0, dx2 = 0.0, dx3 = 0.0, dy1 = 0.0, dy2 = 0.0, dy3 = 0.0;
00145   uint32_t X2_1 = 0, X2_2 = 0, X2_3 = 0, X2_4 = 0, X2_5 = 0;
00146   uint32_t Y2_1 = 0, Y2_2 = 0, Y2_3 = 0, Y2_4 = 0, Y2_5 = 0;
00147   uint32_t XxY_1 = 0, XxY_2 = 0, XxY_3 = 0, XxY_4 = 0, XxY_5 = 0;
00148   uint32_t XxXd_1 = 0, XxXd_2 = 0, XxXd_3 = 0, XxXd_4 = 0, XxXd_5 = 0;
00149   uint32_t YxXd_1 = 0, YxXd_2 = 0, YxXd_3 = 0, YxXd_4 = 0, YxXd_5 = 0;
00150   uint32_t XxYd_1 = 0, XxYd_2 = 0, XxYd_3 = 0, XxYd_4 = 0, XxYd_5 = 0;
00151   uint32_t YxYd_1 = 0, YxYd_2 = 0, YxYd_3 = 0, YxYd_4 = 0, YxYd_5 = 0;
00152   uint32_t alfa = 0, beta = 0, chi = 0, Kx = 0, Ky = 0, Lx = 0, Ly = 0;
00153   uint16_t epsilon = 0, fi = 0, Mx = 0, My = 0;
00154 
00155   X2_1 = ((uint32_t)touchRefPoint[0].x * touchRefPoint[0].x);
00156   X2_2 = ((uint32_t)touchRefPoint[1].x * touchRefPoint[1].x);
00157   X2_3 = ((uint32_t)touchRefPoint[2].x * touchRefPoint[2].x);
00158   X2_4 = ((uint32_t)touchRefPoint[3].x * touchRefPoint[3].x);
00159   X2_5 = ((uint32_t)touchRefPoint[4].x * touchRefPoint[4].x);
00160 
00161   Y2_1 = ((uint32_t)touchRefPoint[0].y * touchRefPoint[0].y);
00162   Y2_2 = ((uint32_t)touchRefPoint[1].y * touchRefPoint[1].y);
00163   Y2_3 = ((uint32_t)touchRefPoint[2].y * touchRefPoint[2].y);
00164   Y2_4 = ((uint32_t)touchRefPoint[3].y * touchRefPoint[3].y);
00165   Y2_5 = ((uint32_t)touchRefPoint[4].y * touchRefPoint[4].y);
00166 
00167   XxY_1 = ((uint32_t)touchRefPoint[0].x * touchRefPoint[0].y);
00168   XxY_2 = ((uint32_t)touchRefPoint[1].x * touchRefPoint[1].y);
00169   XxY_3 = ((uint32_t)touchRefPoint[2].x * touchRefPoint[2].y);
00170   XxY_4 = ((uint32_t)touchRefPoint[3].x * touchRefPoint[3].y);
00171   XxY_5 = ((uint32_t)touchRefPoint[4].x * touchRefPoint[4].y);
00172 
00173   XxXd_1 = ((uint32_t)touchRefPoint[0].x * displayRefPoint[0].x);
00174   XxXd_2 = ((uint32_t)touchRefPoint[1].x * displayRefPoint[1].x);
00175   XxXd_3 = ((uint32_t)touchRefPoint[2].x * displayRefPoint[2].x);
00176   XxXd_4 = ((uint32_t)touchRefPoint[3].x * displayRefPoint[3].x);
00177   XxXd_5 = ((uint32_t)touchRefPoint[4].x * displayRefPoint[4].x);
00178 
00179   YxXd_1 = ((uint32_t)touchRefPoint[0].y * displayRefPoint[0].x);
00180   YxXd_2 = ((uint32_t)touchRefPoint[1].y * displayRefPoint[1].x);
00181   YxXd_3 = ((uint32_t)touchRefPoint[2].y * displayRefPoint[2].x);
00182   YxXd_4 = ((uint32_t)touchRefPoint[3].y * displayRefPoint[3].x);
00183   YxXd_5 = ((uint32_t)touchRefPoint[4].y * displayRefPoint[4].x);
00184 
00185   XxYd_1 = ((uint32_t)touchRefPoint[0].x * displayRefPoint[0].y);
00186   XxYd_2 = ((uint32_t)touchRefPoint[1].x * displayRefPoint[1].y);
00187   XxYd_3 = ((uint32_t)touchRefPoint[2].x * displayRefPoint[2].y);
00188   XxYd_4 = ((uint32_t)touchRefPoint[3].x * displayRefPoint[3].y);
00189   XxYd_5 = ((uint32_t)touchRefPoint[4].x * displayRefPoint[4].y);
00190 
00191   YxYd_1 = ((uint32_t)touchRefPoint[0].y * displayRefPoint[0].y);
00192   YxYd_2 = ((uint32_t)touchRefPoint[1].y * displayRefPoint[1].y);
00193   YxYd_3 = ((uint32_t)touchRefPoint[2].y * displayRefPoint[2].y);
00194   YxYd_4 = ((uint32_t)touchRefPoint[3].y * displayRefPoint[3].y);
00195   YxYd_5 = ((uint32_t)touchRefPoint[4].y * displayRefPoint[4].y);
00196 
00197   alfa = X2_1 + X2_2 + X2_3 + X2_4 + X2_5;
00198   beta = Y2_1 + Y2_2 + Y2_3 + Y2_4 + Y2_5;
00199   chi = XxY_1 + XxY_2 + XxY_3 + XxY_4 + XxY_5;
00200   epsilon = touchRefPoint[0].x + touchRefPoint[1].x + touchRefPoint[2].x + touchRefPoint[3].x + touchRefPoint[4].x;
00201   fi = touchRefPoint[0].y + touchRefPoint[1].y + touchRefPoint[2].y + touchRefPoint[3].y + touchRefPoint[4].y;
00202   Kx = XxXd_1 + XxXd_2 + XxXd_3 + XxXd_4 + XxXd_5;
00203   Ky = XxYd_1 + XxYd_2 + XxYd_3 + XxYd_4 + XxYd_5;
00204   Lx = YxXd_1 + YxXd_2 + YxXd_3 + YxXd_4 + YxXd_5;
00205   Ly = YxYd_1 + YxYd_2 + YxYd_3 + YxYd_4 + YxYd_5;
00206   Mx = displayRefPoint[0].x + displayRefPoint[1].x + displayRefPoint[2].x + displayRefPoint[3].x + displayRefPoint[4].x;
00207   My = displayRefPoint[0].y + displayRefPoint[1].y + displayRefPoint[2].y + displayRefPoint[3].y + displayRefPoint[4].y;
00208   d = 5 * ( ((double)alfa * beta) - ((double)chi * chi) ) + 2 * ((double)chi * epsilon * fi) - ((double)alfa * fi * fi ) - ( (double)beta * epsilon * epsilon );
00209   dx1 = 5 * ( ((double)Kx * beta) - ((double)Lx * chi) ) + ((double)fi * ( ((double)Lx * epsilon) - ((double)Kx * fi) )) + ((double)Mx * ( ((double)chi * fi) - ((double)beta * epsilon) ));
00210   dx2 = 5 * ( ((double)Lx * alfa) - ((double)Kx * chi) ) + ((double)epsilon * ( ((double)Kx * fi) - ((double)Lx * epsilon) )) + ((double)Mx * ( ((double)chi * epsilon) - ((double)alfa * fi) ));
00211   dx3 = ((double)Kx * ( ((double)chi * fi) - ((double)beta * epsilon) )) + ((double)Lx * ( ((double)chi * epsilon) - ((double)alfa * fi) )) + ((double)Mx * ( ((double)alfa * beta) - ((double)chi * chi) ));
00212   dy1 = 5 * ( ((double)Ky * beta) - ((double)Ly * chi) ) + ((double)fi * ( ((double)Ly * epsilon) - ((double)Ky * fi) )) + ((double)My * ( ((double)chi * fi) - ((double)beta * epsilon) ));
00213   dy2 = 5 * ( ((double)Ly * alfa) - ((double)Ky * chi) ) + ((double)epsilon * ( ((double)Ky * fi) - ((double)Ly * epsilon) )) + ((double)My * ( ((double)chi * epsilon) - ((double)alfa * fi) ));
00214   dy3 = ((double)Ky * ( ((double)chi * fi) - ((double)beta * epsilon) )) + ((double)Ly * ( ((double)chi * epsilon) - ((double)alfa * fi) )) + ((double)My * ( ((double)alfa * beta) - ((double)chi * chi) ));
00215 
00216   A = dx1 / d;
00217   B = dx2 / d;
00218   C = dx3 / d;
00219   D = dy1 / d;
00220   E = dy2 / d;
00221   F = dy3 / d;
00222 
00223   CalibrationMatrix calibrationData;
00224   calibrationData.a = (int32_t)(A * _touch->rescaleFactor());
00225   calibrationData.b = (int32_t)(B * _touch->rescaleFactor());
00226   calibrationData.c = (int32_t)(C * _touch->rescaleFactor());
00227   calibrationData.d = (int32_t)(D * _touch->rescaleFactor());
00228   calibrationData.e = (int32_t)(E * _touch->rescaleFactor());
00229   calibrationData.f = (int32_t)(F * _touch->rescaleFactor());
00230   return calibrationData;
00231 }
00232 
00233 void DmTouchCalibration::drawCalibration(CalibrationMatrix calibrationMatrix) {
00234   printHeadline("CalibrationData: ");
00235   printHeadline("a: ");
00236   printNumber(calibrationMatrix.a);
00237   printHeadline("b: ");
00238   printNumber(calibrationMatrix.b);
00239   printHeadline("c: ");
00240   printNumber(calibrationMatrix.c);
00241   printHeadline("d: ");
00242   printNumber(calibrationMatrix.d);
00243   printHeadline("e: ");
00244   printNumber(calibrationMatrix.e);
00245   printHeadline("f: ");
00246   printNumber(calibrationMatrix.f);
00247 }
00248 
00249 void DmTouchCalibration::printHeadline(char* text) {
00250   _textRow += 20;
00251   _tft->drawString(5, _textRow, text);
00252 }
00253 
00254 void DmTouchCalibration::printNumber(int32_t number) const {
00255   _tft->drawNumber(20, _textRow, number, 10, false);
00256 }
00257 
00258 void DmTouchCalibration::printCalibration(CalibrationMatrix calibrationMatrix) const {
00259 #if defined (DM_TOOLCHAIN_ARDUINO)
00260   Serial.println(F("-------- Calibration data ------"));
00261   Serial.println(F("CalibrationMatrix calibrationMatrix;"));
00262   Serial.print(F("calibrationMatrix.a = "));
00263   Serial.print(calibrationMatrix.a);
00264   Serial.println(F(";"));
00265   Serial.print(F("calibrationMatrix.b = "));
00266   Serial.print(calibrationMatrix.b);
00267   Serial.println(F(";"));
00268   Serial.print(F("calibrationMatrix.c = "));
00269   Serial.print(calibrationMatrix.c);
00270   Serial.println(F(";"));
00271   Serial.print(F("calibrationMatrix.d = "));
00272   Serial.print(calibrationMatrix.d);
00273   Serial.println(F(";"));
00274   Serial.print(F("calibrationMatrix.e = "));
00275   Serial.print(calibrationMatrix.e);
00276   Serial.println(F(";"));
00277   Serial.print(F("calibrationMatrix.f = "));
00278   Serial.print(calibrationMatrix.f);
00279   Serial.println(F(";"));
00280   Serial.println("-------------------------------");
00281 #endif
00282 }