Modified version of the DmTftLibrary, optimized for the LPC4088 Experiment Base Board

Dependents:   lpc4088_ebb_dm_calc lpc4088_ebb_dm_bubbles

Fork of DmTftLibrary by Display Module

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 
00061     default:
00062       break;
00063   }
00064   return calibrationMatrix;
00065 }
00066 
00067 
00068 bool DmTouchCalibration::getTouchReferencePoints(Point displayRefPoint[], Point touchRefPoint[], uint16_t tftWidth, uint16_t tftHeight) {
00069   _touch->setPrecison(60);
00070   displayRefPoint[0].x = (tftWidth / 2);
00071   displayRefPoint[0].y = (tftHeight / 2);
00072   displayRefPoint[1].x = 1 * (tftWidth / 5);
00073   displayRefPoint[1].y = 1 * (tftHeight / 5);
00074   displayRefPoint[2].x = 4 * (tftWidth / 5);
00075   displayRefPoint[2].y = 1 * (tftHeight / 5);
00076   displayRefPoint[3].x = 4 * (tftWidth / 5);
00077   displayRefPoint[3].y = 4 * (tftHeight / 5);
00078   displayRefPoint[4].x = 1 * (tftWidth / 5);
00079   displayRefPoint[4].y = 4 * (tftHeight / 5);
00080 
00081   for(int n = 0; n < 5; n++) {
00082     drawCalibPoint(displayRefPoint[n].x, displayRefPoint[n].y);
00083     if (!getRawTouch(touchRefPoint[n].x, touchRefPoint[n].y)) {
00084       return false;
00085     }
00086     releaseTouch(displayRefPoint[n].x, displayRefPoint[n].y);
00087   }
00088 
00089   _touch->setPrecison(3);
00090   return true;
00091 }
00092 
00093 bool DmTouchCalibration::getRawTouch(uint16_t& x, uint16_t& y) const {
00094   bool touched = false;
00095   bool haveAllSamples = false;
00096 
00097   while(!touched) {
00098     if (_touch->isTouched()) {
00099       //Serial.println("is touched");
00100       haveAllSamples = _touch->getMiddleXY(x, y);
00101         
00102       // As many samples are take during calibration it is important to
00103       // make sure that the screen is still touched. If the user has let
00104       // go already then the gathered samples are not good enough.
00105       touched = haveAllSamples && _touch->isTouched();
00106     }
00107   }
00108   return haveAllSamples;
00109 }
00110 
00111 void DmTouchCalibration::drawCalibPoint(uint16_t x, uint16_t y) const {
00112   const uint8_t lineSize = 5;
00113   _tft->drawHorizontalLine(x-lineSize, y, lineSize*2, RED);
00114   _tft->drawVerticalLine(x, y-lineSize, lineSize*2, RED);
00115 }
00116 
00117 void DmTouchCalibration::releaseTouch(uint16_t x, uint16_t y) const {
00118   _tft->drawCircle(x, y, 10, YELLOW);
00119   delay(100);
00120   _tft->fillCircle(x, y, 10, GREEN);
00121   delay(100);
00122   _tft->fillCircle(x, y, 10, BLACK);
00123   delay(300);
00124   _touch->waitForTouchRelease();
00125 }
00126 
00127 CalibrationMatrix DmTouchCalibration::calculateCalibrationMatrix(Point displayRefPoint[], Point touchRefPoint[]) {
00128   double A = 0.0, B = 0.0, C = 0.0, D = 0.0, E = 0.0, F = 0.0;
00129   double d = 0.0, dx1 = 0.0, dx2 = 0.0, dx3 = 0.0, dy1 = 0.0, dy2 = 0.0, dy3 = 0.0;
00130   uint32_t X2_1 = 0, X2_2 = 0, X2_3 = 0, X2_4 = 0, X2_5 = 0;
00131   uint32_t Y2_1 = 0, Y2_2 = 0, Y2_3 = 0, Y2_4 = 0, Y2_5 = 0;
00132   uint32_t XxY_1 = 0, XxY_2 = 0, XxY_3 = 0, XxY_4 = 0, XxY_5 = 0;
00133   uint32_t XxXd_1 = 0, XxXd_2 = 0, XxXd_3 = 0, XxXd_4 = 0, XxXd_5 = 0;
00134   uint32_t YxXd_1 = 0, YxXd_2 = 0, YxXd_3 = 0, YxXd_4 = 0, YxXd_5 = 0;
00135   uint32_t XxYd_1 = 0, XxYd_2 = 0, XxYd_3 = 0, XxYd_4 = 0, XxYd_5 = 0;
00136   uint32_t YxYd_1 = 0, YxYd_2 = 0, YxYd_3 = 0, YxYd_4 = 0, YxYd_5 = 0;
00137   uint32_t alfa = 0, beta = 0, chi = 0, Kx = 0, Ky = 0, Lx = 0, Ly = 0;
00138   uint16_t epsilon = 0, fi = 0, Mx = 0, My = 0;
00139 
00140   X2_1 = ((uint32_t)touchRefPoint[0].x * touchRefPoint[0].x);
00141   X2_2 = ((uint32_t)touchRefPoint[1].x * touchRefPoint[1].x);
00142   X2_3 = ((uint32_t)touchRefPoint[2].x * touchRefPoint[2].x);
00143   X2_4 = ((uint32_t)touchRefPoint[3].x * touchRefPoint[3].x);
00144   X2_5 = ((uint32_t)touchRefPoint[4].x * touchRefPoint[4].x);
00145 
00146   Y2_1 = ((uint32_t)touchRefPoint[0].y * touchRefPoint[0].y);
00147   Y2_2 = ((uint32_t)touchRefPoint[1].y * touchRefPoint[1].y);
00148   Y2_3 = ((uint32_t)touchRefPoint[2].y * touchRefPoint[2].y);
00149   Y2_4 = ((uint32_t)touchRefPoint[3].y * touchRefPoint[3].y);
00150   Y2_5 = ((uint32_t)touchRefPoint[4].y * touchRefPoint[4].y);
00151 
00152   XxY_1 = ((uint32_t)touchRefPoint[0].x * touchRefPoint[0].y);
00153   XxY_2 = ((uint32_t)touchRefPoint[1].x * touchRefPoint[1].y);
00154   XxY_3 = ((uint32_t)touchRefPoint[2].x * touchRefPoint[2].y);
00155   XxY_4 = ((uint32_t)touchRefPoint[3].x * touchRefPoint[3].y);
00156   XxY_5 = ((uint32_t)touchRefPoint[4].x * touchRefPoint[4].y);
00157 
00158   XxXd_1 = ((uint32_t)touchRefPoint[0].x * displayRefPoint[0].x);
00159   XxXd_2 = ((uint32_t)touchRefPoint[1].x * displayRefPoint[1].x);
00160   XxXd_3 = ((uint32_t)touchRefPoint[2].x * displayRefPoint[2].x);
00161   XxXd_4 = ((uint32_t)touchRefPoint[3].x * displayRefPoint[3].x);
00162   XxXd_5 = ((uint32_t)touchRefPoint[4].x * displayRefPoint[4].x);
00163 
00164   YxXd_1 = ((uint32_t)touchRefPoint[0].y * displayRefPoint[0].x);
00165   YxXd_2 = ((uint32_t)touchRefPoint[1].y * displayRefPoint[1].x);
00166   YxXd_3 = ((uint32_t)touchRefPoint[2].y * displayRefPoint[2].x);
00167   YxXd_4 = ((uint32_t)touchRefPoint[3].y * displayRefPoint[3].x);
00168   YxXd_5 = ((uint32_t)touchRefPoint[4].y * displayRefPoint[4].x);
00169 
00170   XxYd_1 = ((uint32_t)touchRefPoint[0].x * displayRefPoint[0].y);
00171   XxYd_2 = ((uint32_t)touchRefPoint[1].x * displayRefPoint[1].y);
00172   XxYd_3 = ((uint32_t)touchRefPoint[2].x * displayRefPoint[2].y);
00173   XxYd_4 = ((uint32_t)touchRefPoint[3].x * displayRefPoint[3].y);
00174   XxYd_5 = ((uint32_t)touchRefPoint[4].x * displayRefPoint[4].y);
00175 
00176   YxYd_1 = ((uint32_t)touchRefPoint[0].y * displayRefPoint[0].y);
00177   YxYd_2 = ((uint32_t)touchRefPoint[1].y * displayRefPoint[1].y);
00178   YxYd_3 = ((uint32_t)touchRefPoint[2].y * displayRefPoint[2].y);
00179   YxYd_4 = ((uint32_t)touchRefPoint[3].y * displayRefPoint[3].y);
00180   YxYd_5 = ((uint32_t)touchRefPoint[4].y * displayRefPoint[4].y);
00181 
00182   alfa = X2_1 + X2_2 + X2_3 + X2_4 + X2_5;
00183   beta = Y2_1 + Y2_2 + Y2_3 + Y2_4 + Y2_5;
00184   chi = XxY_1 + XxY_2 + XxY_3 + XxY_4 + XxY_5;
00185   epsilon = touchRefPoint[0].x + touchRefPoint[1].x + touchRefPoint[2].x + touchRefPoint[3].x + touchRefPoint[4].x;
00186   fi = touchRefPoint[0].y + touchRefPoint[1].y + touchRefPoint[2].y + touchRefPoint[3].y + touchRefPoint[4].y;
00187   Kx = XxXd_1 + XxXd_2 + XxXd_3 + XxXd_4 + XxXd_5;
00188   Ky = XxYd_1 + XxYd_2 + XxYd_3 + XxYd_4 + XxYd_5;
00189   Lx = YxXd_1 + YxXd_2 + YxXd_3 + YxXd_4 + YxXd_5;
00190   Ly = YxYd_1 + YxYd_2 + YxYd_3 + YxYd_4 + YxYd_5;
00191   Mx = displayRefPoint[0].x + displayRefPoint[1].x + displayRefPoint[2].x + displayRefPoint[3].x + displayRefPoint[4].x;
00192   My = displayRefPoint[0].y + displayRefPoint[1].y + displayRefPoint[2].y + displayRefPoint[3].y + displayRefPoint[4].y;
00193   d = 5 * ( ((double)alfa * beta) - ((double)chi * chi) ) + 2 * ((double)chi * epsilon * fi) - ((double)alfa * fi * fi ) - ( (double)beta * epsilon * epsilon );
00194   dx1 = 5 * ( ((double)Kx * beta) - ((double)Lx * chi) ) + ((double)fi * ( ((double)Lx * epsilon) - ((double)Kx * fi) )) + ((double)Mx * ( ((double)chi * fi) - ((double)beta * epsilon) ));
00195   dx2 = 5 * ( ((double)Lx * alfa) - ((double)Kx * chi) ) + ((double)epsilon * ( ((double)Kx * fi) - ((double)Lx * epsilon) )) + ((double)Mx * ( ((double)chi * epsilon) - ((double)alfa * fi) ));
00196   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) ));
00197   dy1 = 5 * ( ((double)Ky * beta) - ((double)Ly * chi) ) + ((double)fi * ( ((double)Ly * epsilon) - ((double)Ky * fi) )) + ((double)My * ( ((double)chi * fi) - ((double)beta * epsilon) ));
00198   dy2 = 5 * ( ((double)Ly * alfa) - ((double)Ky * chi) ) + ((double)epsilon * ( ((double)Ky * fi) - ((double)Ly * epsilon) )) + ((double)My * ( ((double)chi * epsilon) - ((double)alfa * fi) ));
00199   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) ));
00200 
00201   A = dx1 / d;
00202   B = dx2 / d;
00203   C = dx3 / d;
00204   D = dy1 / d;
00205   E = dy2 / d;
00206   F = dy3 / d;
00207 
00208   CalibrationMatrix calibrationData;
00209   calibrationData.a = (int32_t)(A * _touch->rescaleFactor());
00210   calibrationData.b = (int32_t)(B * _touch->rescaleFactor());
00211   calibrationData.c = (int32_t)(C * _touch->rescaleFactor());
00212   calibrationData.d = (int32_t)(D * _touch->rescaleFactor());
00213   calibrationData.e = (int32_t)(E * _touch->rescaleFactor());
00214   calibrationData.f = (int32_t)(F * _touch->rescaleFactor());
00215 
00216   return calibrationData;
00217 }
00218 
00219 void DmTouchCalibration::drawCalibration(CalibrationMatrix calibrationMatrix) {
00220   printHeadline("CalibrationData: ");
00221   printHeadline("a: ");
00222   printNumber(calibrationMatrix.a);
00223   printHeadline("b: ");
00224   printNumber(calibrationMatrix.b);
00225   printHeadline("c: ");
00226   printNumber(calibrationMatrix.c);
00227   printHeadline("d: ");
00228   printNumber(calibrationMatrix.d);
00229   printHeadline("e: ");
00230   printNumber(calibrationMatrix.e);
00231   printHeadline("f: ");
00232   printNumber(calibrationMatrix.f);
00233 }
00234 
00235 void DmTouchCalibration::printHeadline(char* text) {
00236   _textRow += 20;
00237   _tft->drawString(5, _textRow, text);
00238 }
00239 
00240 void DmTouchCalibration::printNumber(int32_t number) const {
00241   _tft->drawNumber(20, _textRow, number, 10, false);
00242 }
00243 
00244 void DmTouchCalibration::printCalibration(CalibrationMatrix calibrationMatrix) const {
00245 #if defined (DM_TOOLCHAIN_ARDUINO)
00246   Serial.println(F("-------- Calibration data ------"));
00247   Serial.println(F("CalibrationMatrix calibrationMatrix;"));
00248   Serial.print(F("calibrationMatrix.a = "));
00249   Serial.print(calibrationMatrix.a);
00250   Serial.println(F(";"));
00251   Serial.print(F("calibrationMatrix.b = "));
00252   Serial.print(calibrationMatrix.b);
00253   Serial.println(F(";"));
00254   Serial.print(F("calibrationMatrix.c = "));
00255   Serial.print(calibrationMatrix.c);
00256   Serial.println(F(";"));
00257   Serial.print(F("calibrationMatrix.d = "));
00258   Serial.print(calibrationMatrix.d);
00259   Serial.println(F(";"));
00260   Serial.print(F("calibrationMatrix.e = "));
00261   Serial.print(calibrationMatrix.e);
00262   Serial.println(F(";"));
00263   Serial.print(F("calibrationMatrix.f = "));
00264   Serial.print(calibrationMatrix.f);
00265   Serial.println(F(";"));
00266   Serial.println("-------------------------------");
00267 #endif
00268 }