1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
| // URTouch_Calibration
// Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved
// web: http://www.RinkyDinkElectronics.com/
//
// This program can be used to calibrate the touchscreen
// of the display modules.
// This program requires the UTFT library and a touch
// screen module that is compatible with UTFT.
//
// It is assumed that the display module is connected to an
// appropriate shield or that you know how to change the pin
// numbers in the setup.
//
// Instructions will be given on the display.
//
#include <UTFT.h>
#include <URTouch.h>
// Define the orientation of the touch screen. Further
// information can be found in the instructions.
#define TOUCH_ORIENTATION PORTRAIT
//On définit les broches pour la partie affichage
#define TFT_CLK A0 //13
#define TFT_MISO A1 //12
#define TFT_MOSI A2 //11
#define TFT_CS 0 // On change juste cette broche pour piloter aussi le module Ethernet avec le bus SPI
#define TFT_DC 9
#define TFT_RST 8
UTFT myGLCD(ILI9341_S5P,TFT_MOSI,TFT_CLK,TFT_CS,TFT_RST,TFT_DC);
#define TFT_PWM_LED 6 // Un transistor PNP permet de piloter le rétroéclairage
//On définit les broches pour la partie tactile
#define t_IRQ 5
#define t_MISO 7 // La broche 4 est utilisée pour le lecteur SD de l'ethernet Shield
#define t_MOSI 3
#define t_CS 2
#define t_SCK 1
URTouch myTouch(t_SCK, t_CS, t_MOSI, t_MISO, t_IRQ);
// ************************************
// DO NOT EDIT ANYTHING BELOW THIS LINE
// ************************************
// Declare which fonts we will be using
extern uint8_t SmallFont[];
uint32_t cx, cy;
uint32_t rx[8], ry[8];
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()
{
pinMode(A0, OUTPUT);
pinMode(A2, OUTPUT);
analogWrite(TFT_PWM_LED,128);
myGLCD.InitLCD();
myGLCD.clrScr();
myGLCD.setFont(SmallFont);
myTouch.InitTouch(TOUCH_ORIENTATION);
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 = 5000;
int failcount = 0;
int cnt = 0;
uint32_t tx=0;
uint32_t ty=0;
boolean OK = false;
while (OK == false)
{
myGLCD.setColor(255, 255, 255);
myGLCD.print(F("* PRESS *"), CENTER, text_y_center);
while (myTouch.dataAvailable() == false) {}
myGLCD.print(F("* HOLD! *"), CENTER, text_y_center);
while ((myTouch.dataAvailable() == true) && (cnt<iter) && (failcount<10000))
{
myTouch.calibrateRead();
if (!((myTouch.TP_X==65535) || (myTouch.TP_Y==65535)))
{
tx += myTouch.TP_X;
ty += myTouch.TP_Y;
cnt++;
}
else
failcount++;
}
if (cnt>=iter)
{
OK = true;
}
else
{
tx = 0;
ty = 0;
cnt = 0;
}
if (failcount>=10000)
fail();
}
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[i]=cx;
ry[i]=cy;
while (myTouch.dataAvailable() == true) {}
}
void waitForTouch()
{
while (myTouch.dataAvailable() == true) {}
while (myTouch.dataAvailable() == false) {}
while (myTouch.dataAvailable() == true) {}
}
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(F("URTouch Calibration"), CENTER, 1);
myGLCD.setBackColor(0, 0, 0);
if (dispx==220)
{
myGLCD.print(F("Use a stylus or something"), LEFT, 30);
myGLCD.print(F("similar to touch as close"), LEFT, 42);
myGLCD.print(F("to the center of the"), LEFT, 54);
myGLCD.print(F("highlighted crosshair as"), LEFT, 66);
myGLCD.print(F("possible. Keep as still as"), LEFT, 78);
myGLCD.print(F("possible and keep holding"), LEFT, 90);
myGLCD.print(F("until the highlight is"), LEFT, 102);
myGLCD.print(F("removed. Repeat for all"), LEFT, 114);
myGLCD.print(F("crosshairs in sequence."), LEFT, 126);
myGLCD.print(F("Touch screen to continue"), CENTER, 162);
}
else
{
myGLCD.print(F("INSTRUCTIONS"), CENTER, 30);
myGLCD.print(F("Use a stylus or something similar to"), LEFT, 50);
myGLCD.print(F("touch as close to the center of the"), LEFT, 62);
myGLCD.print(F("highlighted crosshair as possible. Keep"), LEFT, 74);
myGLCD.print(F("as still as possible and keep holding"), LEFT, 86);
myGLCD.print(F("until the highlight is removed. Repeat"), LEFT, 98);
myGLCD.print(F("for all crosshairs in sequence."), LEFT, 110);
myGLCD.print(F("Further instructions will be displayed"), LEFT, 134);
myGLCD.print(F("when the calibration is complete."), LEFT, 146);
myGLCD.print(F("Do NOT use your finger as a calibration"), LEFT, 170);
myGLCD.print(F("stylus or the result WILL BE imprecise."), LEFT, 182);
myGLCD.print(F("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(F("URTouch Calibration"), CENTER, 1);
myGLCD.setBackColor(0, 0, 0);
if (dispx==220)
{
myGLCD.print(F("To use the new calibration"), LEFT, 30);
myGLCD.print(F("settings you must edit the"), LEFT, 42);
myGLCD.setColor(160, 160, 255);
myGLCD.print(F("URTouchCD.h"), LEFT, 54);
myGLCD.setColor(255, 255, 255);
myGLCD.print(F("file and change"), 88, 54);
myGLCD.print(F("the following values. The"), LEFT, 66);
myGLCD.print(F("values are located right"), LEFT, 78);
myGLCD.print(F("below the opening comment."), LEFT, 90);
myGLCD.print(F("CAL_X"), LEFT, 110);
myGLCD.print(F("CAL_Y"), LEFT, 122);
myGLCD.print(F("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(F("CALIBRATION COMPLETE"), CENTER, 30);
myGLCD.print(F("To use the new calibration"), LEFT, 50);
myGLCD.print(F("settings you must edit the"), LEFT, 62);
myGLCD.setColor(160, 160, 255);
myGLCD.print(F("URTouchCD.h"), LEFT, 74);
myGLCD.setColor(255, 255, 255);
myGLCD.print(F("file and change"), 88, 74);
myGLCD.print(F("the following values."), LEFT, 86);
myGLCD.print(F("The values are located right"), LEFT, 98);
myGLCD.print(F("below the opening comment in"), LEFT, 110);
myGLCD.print(F("the file."), LEFT, 122);
myGLCD.print(F("CAL_X"), LEFT, 150);
myGLCD.print(F("CAL_Y"), LEFT, 162);
myGLCD.print(F("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 fail()
{
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(F("URTouch Calibration FAILED"), CENTER, 1);
myGLCD.setBackColor(0, 0, 0);
myGLCD.print(F("Unable to read the position"), LEFT, 30);
myGLCD.print(F("of the press. This is a"), LEFT, 42);
myGLCD.print(F("hardware issue and can"), 88, 54);
myGLCD.print(F("not be corrected in"), LEFT, 66);
myGLCD.print(F("software."), LEFT, 78);
while(true) {};
}
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(F("***********"), CENTER, text_y_center-12);
myGLCD.print(F("***********"), 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 - (px*10);
crx = crx + (px*10);
}
else
{
clx = clx + (px*10);
crx = crx - (px*10);
}
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 - (py*10);
cby = cby + (py*10);
}
else
{
cty = cty + (py*10);
cby = cby - (py*10);
}
calx = (long(clx)<<14) + long(crx);
caly = (long(cty)<<14) + long(cby);
if (TOUCH_ORIENTATION == LANDSCAPE)
cals = cals + (1L<<31);
done();
while(true) {}
} |
Partager