// UTouch_Calibration (C)2012 Henning Karlsen
#include <UTFT.h>
#include <UTouch.h>
// Define the orientation of the touch screen. Further
// information can be found in the instructions.
#define TOUCH_ORIENTATION PORTRAIT
// Declare which fonts we will be using
extern uint8_t SmallFont[];
// Uncomment the next two lines for the Arduino Mega
UTFT myGLCD(SSD1963_800,38,39,40,41); // Remember to change the model parameter to suit your display module!
UTouch myTouch(6,5,4,3,2);
// ************************************
// DO NOT EDIT ANYTHING BELOW THIS LINE
// ************************************
uint32_t cx, cy;
uint32_t rx[10], ry[10];
uint32_t clx, crx, cty, cby;
float px, py;
int dispx, dispy, text_y_center;
uint32_t calx, caly, cals;
char buf[13];
void setup()
{
myGLCD.InitLCD();
myGLCD.clrScr();
myGLCD.setFont(SmallFont);
myTouch.InitTouch(TOUCH_ORIENTATION);
myTouch.setPrecision(PREC_LOW);
dispx=myGLCD.getDisplayXSize();
dispy=myGLCD.getDisplayYSize();
text_y_center=(dispy/2)-6;
}
void drawCrossHair(int x, int y)
{
myGLCD.drawRect(x-10, y-10, x+10, y+10);
myGLCD.drawLine(x-5, y, x+5, y);
myGLCD.drawLine(x, y-5, x, y+5);
}
void readCoordinates()
{
int iter = 2000;
int cnt = 0;
uint32_t tx=0;
uint32_t ty=0;
boolean OK = false;
while (OK == false)
{
myGLCD.setColor(255, 255, 255);
myGLCD.print("* PRESS ", CENTER, text_y_center);
while (myTouch.dataAvailable() == false) {}
myGLCD.print(" HOLD! *", CENTER, text_y_center);
while ((myTouch.dataAvailable() == true) && (cnt<iter))
{
myTouch.read();
tx = myTouch.TP_X;
ty = myTouch.TP_Y;
cnt++;
}
if (cnt>=iter)
{
OK = true;
}
else
{
tx = 0;
ty = 0;
cnt = 0;
}
}
cx = tx / iter;
cy = ty / iter;
}
void calibrate(int x, int y, int i)
{
myGLCD.setColor(255, 255, 255);
drawCrossHair(x,y);
myGLCD.setBackColor(255, 0, 0);
readCoordinates();
myGLCD.setColor(255, 255, 255);
myGLCD.print("* RELEASE ", CENTER, text_y_center);
myGLCD.setColor(80, 80, 80);
drawCrossHair(x,y);
rx=cx;*
_ ry*=cy;_
_ while (myTouch.dataAvailable() == true)_
_ {_
_ myTouch.read();_
_ }_
_}_
void waitForTouch()
_{_
_ while (myTouch.dataAvailable() == true)_
_ {_
_ myTouch.read();_
_ }_
_ while (myTouch.dataAvailable() == false) {}_
_ while (myTouch.dataAvailable() == true)_
_ {_
_ myTouch.read();_
_ }_
_}_
void toHex(uint32_t num)
_{_
_ buf[0] = '0';_
_ buf[1] = 'x';_
_ buf[10] = 'U';_
_ buf[11] = 'L';_
_ buf[12] = 0;_
_ for (int zz=9; zz>1; zz--)_
_ {_
_ if ((num & 0xF) > 9)_
_ buf[zz] = (num & 0xF) + 55;_
_ else*_
* buf[zz] = (num & 0xF) + 48;*
* num=num>>4;*
* }*
}
void startup()
{
* myGLCD.setColor(255, 0, 0);*
* myGLCD.fillRect(0, 0, dispx-1, 13);*
* myGLCD.setColor(255, 255, 255);*
* myGLCD.setBackColor(255, 0, 0);*
* myGLCD.drawLine(0, 14, dispx-1, 14);*
* myGLCD.print("UTouch Calibration", CENTER, 1);*
* myGLCD.setBackColor(0, 0, 0);*
* if (dispx==220)*
* { *
* myGLCD.print("Use a stylus or something", LEFT, 30);*
* myGLCD.print("similar to touch as close", LEFT, 42);*
* myGLCD.print("to the center of the", LEFT, 54);*
* myGLCD.print("highlighted crosshair as", LEFT, 66);*
* myGLCD.print("possible. Keep as still as", LEFT, 78);*
* myGLCD.print("possible and keep holding", LEFT, 90);*
* myGLCD.print("until the highlight is", LEFT, 102);*
* myGLCD.print("removed. Repeat for all", LEFT, 114);*
* myGLCD.print("crosshairs in sequence.", LEFT, 126);*
* myGLCD.print("Touch screen to continue", CENTER, 162);*
* }*
* else*
* {*
* myGLCD.print("INSTRUCTIONS", CENTER, 30);*
* myGLCD.print("Use a stylus or something similar to", LEFT, 50);*
* myGLCD.print("touch as close to the center of the", LEFT, 62);*
* myGLCD.print("highlighted crosshair as possible. Keep", LEFT, 74);*
* myGLCD.print("as still as possible and keep holding", LEFT, 86);*
* myGLCD.print("until the highlight is removed. Repeat", LEFT, 98);*
* myGLCD.print("for all crosshairs in sequence.", LEFT, 110);*
* myGLCD.print("Further instructions will be displayed", LEFT, 134);*
* myGLCD.print("when the calibration is complete.", LEFT, 146);*
* myGLCD.print("Do NOT use your finger as a calibration", LEFT, 170);*
* myGLCD.print("stylus or the result WILL BE imprecise.", LEFT, 182);*
* myGLCD.print("Touch screen to continue", CENTER, 226);*
* }*
* waitForTouch();*
* myGLCD.clrScr();*
}
void done()
{
* myGLCD.clrScr();*
* myGLCD.setColor(255, 0, 0);*
* myGLCD.fillRect(0, 0, dispx-1, 13);*
* myGLCD.setColor(255, 255, 255);*
* myGLCD.setBackColor(255, 0, 0);*
* myGLCD.drawLine(0, 14, dispx-1, 14);*
* myGLCD.print("UTouch Calibration", CENTER, 1);*
* myGLCD.setBackColor(0, 0, 0);*
* if (dispx==220)*
* { *
* myGLCD.print("To use the new calibration", LEFT, 30);*
* myGLCD.print("settings you must edit the", LEFT, 42);*
* myGLCD.setColor(160, 160, 255);*
* myGLCD.print("UTouchCD.h", LEFT, 54);*
* myGLCD.setColor(255, 255, 255);*
* myGLCD.print("file and change", 88, 54);*
* myGLCD.print("the following values. The", LEFT, 66);*
* myGLCD.print("values are located right", LEFT, 78);*
* myGLCD.print("below the opening comment.", LEFT, 90);*
* myGLCD.print("CAL_X", LEFT, 110);
myGLCD.print("CAL_Y", LEFT, 122);
myGLCD.print("CAL_S", LEFT, 134);
_ toHex(calx);_
_ myGLCD.print(buf, 75, 110);_
_ toHex(caly);_
_ myGLCD.print(buf, 75, 122);_
_ toHex(cals);_
_ myGLCD.print(buf, 75, 134);_
_ }_
_ else*_
* { *
* myGLCD.print("CALIBRATION COMPLETE", CENTER, 30);*
* myGLCD.print("To use the new calibration", LEFT, 50);*
* myGLCD.print("settings you must edit the", LEFT, 62);*
* myGLCD.setColor(160, 160, 255);*
* myGLCD.print("UTouchCD.h", LEFT, 74);*
* myGLCD.setColor(255, 255, 255);*
* myGLCD.print("file and change", 88, 74);*
* myGLCD.print("the following values.", LEFT, 86);*
* myGLCD.print("The values are located right", LEFT, 98);*
* myGLCD.print("below the opening comment in", LEFT, 110);*
* myGLCD.print("the file.", LEFT, 122);*
* myGLCD.print("CAL_X", LEFT, 150);
myGLCD.print("CAL_Y", LEFT, 162);
myGLCD.print("CAL_S", LEFT, 174);
_ toHex(calx);_
_ myGLCD.print(buf, 75, 150);_
_ toHex(caly);_
_ myGLCD.print(buf, 75, 162);_
_ toHex(cals);_
_ myGLCD.print(buf, 75, 174);_
_ }*_
}
void loop()
{
* startup();*
* myGLCD.setColor(80, 80, 80);*
* drawCrossHair(dispx-11, 10);*
* drawCrossHair(dispx/2, 10);*
* drawCrossHair(10, 10);*
* drawCrossHair(dispx-11, dispy/2);*
* drawCrossHair(10, dispy/2);*
* drawCrossHair(dispx-11, dispy-11);*
* drawCrossHair(dispx/2, dispy-11);*
* drawCrossHair(10, dispy-11);*
* myGLCD.setColor(255, 255, 255);*
* myGLCD.setBackColor(255, 0, 0);*
myGLCD.print("", CENTER, text_y_center-12);
myGLCD.print("", CENTER, text_y_center+12);
* calibrate(10, 10, 0);*
* calibrate(10, dispy/2, 1);*
* calibrate(10, dispy-11, 2);*
* calibrate(dispx/2, 10, 3);*
* calibrate(dispx/2, dispy-11, 4);*
* calibrate(dispx-11, 10, 5);*
* calibrate(dispx-11, dispy/2, 6);*
* calibrate(dispx-11, dispy-11, 7);*
* if (TOUCH_ORIENTATION == LANDSCAPE)
_ cals=(long(dispx-1)<<12)+(dispy-1);_
_ else*_
* cals=(long(dispy-1)<<12)+(dispx-1);*
* if (TOUCH_ORIENTATION == PORTRAIT)
_ px = abs(((float(rx[2]+rx[4]+rx[7])/3)-(float(rx[0]+rx[3]+rx[5])/3))/(dispy-20)); // PORTRAIT*_
* else*
* px = abs(((float(rx[5]+rx[6]+rx[7])/3)-(float(rx[0]+rx[1]+rx[2])/3))/(dispy-20)); // LANDSCAPE*
* if (TOUCH_ORIENTATION == PORTRAIT)
_ {_
_ clx = (((rx[0]+rx[3]+rx[5])/3)); // PORTRAIT*_
* crx = (((rx[2]+rx[4]+rx[7])/3)); // PORTRAIT*
* }*
* else*
* {*
* clx = (((rx[0]+rx[1]+rx[2])/3)); // LANDSCAPE*
* crx = (((rx[5]+rx[6]+rx[7])/3)); // LANDSCAPE*
* }*
* if (clx<crx)*
* {*
_ clx = clx - (px10);
crx = crx + (px10);
* }*
* else*
* {*
clx = clx + (px10);
crx = crx - (px10);_
* }*
* if (TOUCH_ORIENTATION == PORTRAIT)
_ py = abs(((float(ry[5]+ry[6]+ry[7])/3)-(float(ry[0]+ry[1]+ry[2])/3))/(dispx-20)); // PORTRAIT*_
* else*
* py = abs(((float(ry[0]+ry[3]+ry[5])/3)-(float(ry[2]+ry[4]+ry[7])/3))/(dispx-20)); // LANDSCAPE*
* if (TOUCH_ORIENTATION == PORTRAIT)
_ {_
_ cty = (((ry[5]+ry[6]+ry[7])/3)); // PORTRAIT*_
* cby = (((ry[0]+ry[1]+ry[2])/3)); // PORTRAIT*
* }*
* else*
* {*
* cty = (((ry[0]+ry[3]+ry[5])/3)); // LANDSCAPE*
* cby = (((ry[2]+ry[4]+ry[7])/3)); // LANDSCAPE*
* }*
* if (cty<cby)*
* {*
_ cty = cty - (py10);
cby = cby + (py10);
* }*
* else*
* {*
cty = cty + (py10);
cby = cby - (py10);_
* }*
* calx = (long(clx)<<14) + long(crx);*
* caly = (long(cty)<<14) + long(cby);*
* if (TOUCH_ORIENTATION == LANDSCAPE)
_ cals = cals + (1L<<31);_
_ done();_
_ while(true) {}_
_}[/quote]*_