-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTftCalibration.ino
280 lines (236 loc) · 9.45 KB
/
TftCalibration.ino
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
/*
Name: TftCalibration.ino
Created: 11/23/2016 8:26:03 PM
Author: parents
*/
#if defined(ARDUINO) && ARDUINO >= 100
#include <RA8875.h>
#include <SPI.h>
#include <EEPROM.h>
#include "arduino.h"
#include "TftCalibration.h"
#else
#include "WProgram.h"
#endif
// Definitions for connecting the RA8875 Board
// Library only supports hardware SPI at this time
// Connect SCLK to UNO Digital #13 (Hardware SPI clock)
// Connect MISO to UNO Digital #12 (Hardware SPI MISO)
// Connect MOSI to UNO Digital #11 (Hardware SPI MOSI)
#define RA8875_INT 2
#define RA8875_CS 10
#define RA8875_RESET 9
#define EEPROM_CALIBRATION_LOCATION 100 // Calibration settings
RA8875 tft = RA8875(RA8875_CS, RA8875_RESET); // 800x600 TFT Display
//This function will write a 4 byte (32bit) long to the eeprom at
//the specified address to address + 3.
void EEPROMWritelong(int address, int32_t value)
{
//Decomposition from a long to 4 bytes by using bitshift.
//One = Most significant -> Four = Least significant byte
byte four = (value & 0xFF);
byte three = ((value >> 8) & 0xFF);
byte two = ((value >> 16) & 0xFF);
byte one = ((value >> 24) & 0xFF);
// Write the 4 bytes into the eeprom memory. Update function only writes if value was updated
EEPROM.update(address, one);
EEPROM.update(address + 1, two);
EEPROM.update(address + 2, three);
EEPROM.update(address + 3, four);
}
/**************************************************************************
@brief Calculates the difference between the touch screen and the actual screen co-ordinates, taking into account misalignment
and any physical offset of the touch screen.
**************************************************************************/
int setCalibrationMatrix(tsPoint_t * displayPtr, tsPoint_t * screenPtr, tsMatrix_t * matrixPtr)
{
int retValue = 0;
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 = -1;
}
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);
}
/**************************************************************************/
/*!
@brief Waits for a touch event
*/
/**************************************************************************/
void waitForTouchEvent(RA8875* disp, tsPoint_t * point)
{
uint16_t x, y;
/* Clear the touch data object and placeholder variables */
memset(point, 0, sizeof(tsPoint_t));
disp->touchEnable(true);
/* Clear any previous interrupts to avoid false buffered reads */
disp->touchReadAdc(&x, &y);
delay(1);
/* Wait around for a new touch event (INT pin goes low) */
while (digitalRead(RA8875_INT)) {}
/* Make sure this is really a touch event */
if (disp->touched())
{
// We're reading the raw register data, not the Sumotoy calibrated coordinates.
// The Sumotoy calibration routine was highly inaccurate,
// So we're using the Adafruit calibration code instead
disp->touchReadAdc(&x, &y);
point->x = x;
point->y = y;
Serial.print("Raw Point: x="); Serial.print(x); Serial.print(" Y="); Serial.println(y);
}
else
{
point->x = 0;
point->y = 0;
}
}
//**************************************************************************/
/*!
@brief Renders the calibration screen with an appropriately
placed test point and waits for a touch event
*/
/**************************************************************************/
tsPoint_t renderCalibrationScreen(RA8875* disp, uint16_t x, uint16_t y, uint16_t radius)
{
tsPoint_t point = { 0, 0 };
bool valid = false;
disp->fillWindow(RA8875_BLACK);
disp->fillWindow(RA8875_WHITE);
// Draw some explanatory text
disp->setFont(INT);
disp->setTextColor(RA8875_BLACK, RA8875_WHITE);
disp->setFontScale(3);
disp->setCursor(80, 50); disp->print(F(" Screen Calibration "));
disp->setCursor(115, 125); disp->print(F(" Press the Circle "));
// Draw the calibration circle on the screen
disp->drawCircle(x, y, radius + 2, RA8875_BLACK);
disp->fillCircle(x, y, radius + 2, RA8875_BLACK);
/* Keep polling until the TS event flag is valid */
valid = false;
while (!valid)
{
waitForTouchEvent(disp, &point);
if (point.x || point.y)
{
valid = true;
}
}
return point;
}
/**************************************************************************/
/*!
@brief Starts the screen calibration process. Each corner will be
tested, meaning that each boundary (top, left, right and
bottom) will be tested twice and the readings averaged.
*/
/**************************************************************************/
void tsCalibrate(RA8875* disp, tsMatrix_t *matrix)
{
// Screen Point references used in screen calibration routines
tsPoint_t _tsLCDPoints[3];
tsPoint_t _tsTSPoints[3];
//tsMatrix_t _tsMatrix;
tsPoint_t data;
// Screen not yet calibrated. Create new calibration data
/* --------------- Welcome Screen --------------- */
data = renderCalibrationScreen(disp, disp->width() / 2, disp->height() / 2, 5);
delay(250);
/* ----------------- First Dot ------------------ */
// 10% over and 10% down
data = renderCalibrationScreen(disp, disp->width() / 10, disp->height() / 10, 5);
_tsLCDPoints[0].x = disp->width() / 10;
_tsLCDPoints[0].y = disp->height() / 10;
_tsTSPoints[0].x = data.x;
_tsTSPoints[0].y = data.y;
delay(250);
/* ---------------- Second Dot ------------------ */
// 50% over and 90% down
data = renderCalibrationScreen(disp, disp->width() / 2, disp->height() - disp->height() / 10, 5);
_tsLCDPoints[1].x = disp->width() / 2;
_tsLCDPoints[1].y = disp->height() - disp->height() / 10;
_tsTSPoints[1].x = data.x;
_tsTSPoints[1].y = data.y;
delay(250);
/* ---------------- Third Dot ------------------- */
// 90% over and 50% down
data = renderCalibrationScreen(disp, disp->width() - disp->width() / 10, disp->height() / 2, 5);
_tsLCDPoints[2].x = disp->width() - disp->width() / 10;
_tsLCDPoints[2].y = disp->height() / 2;
_tsTSPoints[2].x = data.x;
_tsTSPoints[2].y = data.y;
delay(250);
/* Clear the screen */
disp->fillWindow(RA8875_WHITE);
// Do matrix calculations for calibration and store to EEPROM
//setCalibrationMatrix(&_tsLCDPoints[0], &_tsTSPoints[0], &_tsMatrix);
setCalibrationMatrix(&_tsLCDPoints[0], &_tsTSPoints[0], matrix);
Serial.println("0 Degree Matrix Code:"); Serial.println("");
Serial.println("typedef struct");
Serial.println("{");
Serial.println("int32_t An,");
Serial.println("\tBn,");
Serial.println("\tCn,");
Serial.println("\tDn,");
Serial.println("\tEn,");
Serial.println("\tFn,");
Serial.println("\tDivider;");
Serial.println("} tsMatrix_t;");
Serial.print("tsMatrix_t _tsMatrix = {");
Serial.print(matrix->An); Serial.print(", ");
Serial.print(matrix->Bn); Serial.print(", ");
Serial.print(matrix->Cn); Serial.print(", ");
Serial.print(matrix->Dn); Serial.print(", ");
Serial.print(matrix->En); Serial.print(", ");
Serial.print(matrix->Fn); Serial.print(", ");
Serial.print(matrix->Divider); Serial.println("}; "); Serial.println("");
}
void writeEEPROMData(tsMatrix_t *matrix, int startLocation)
{
// Store calibration data to EEPROM
EEPROMWritelong(startLocation, (int32_t)matrix->An);
EEPROMWritelong(startLocation + 4, (int32_t)matrix->Bn);
EEPROMWritelong(startLocation + 8, (int32_t)matrix->Cn);
EEPROMWritelong(startLocation + 12, (int32_t)matrix->Dn);
EEPROMWritelong(startLocation + 16, (int32_t)matrix->En);
EEPROMWritelong(startLocation + 20, (int32_t)matrix->Fn);
EEPROMWritelong(startLocation + 24, (int32_t)matrix->Divider);
}
void setup() {
Serial.begin(9600);
tsMatrix_t matrix;
Serial.println("Begin setup()");
/* Initialize the TFT display */
tft.begin(Adafruit_800x480);
tft.useINT(RA8875_INT);
tft.touchBegin();
tft.enableISR(true);
tft.setRotation(0);
tsCalibrate(&tft, &matrix);
writeEEPROMData(&matrix, EEPROM_CALIBRATION_LOCATION);
tft.setRotation(2);
tsCalibrate(&tft, &matrix);
writeEEPROMData(&matrix, EEPROM_CALIBRATION_LOCATION + 28);
Serial.println("Ending setup()");
}
// the loop function runs over and over again until power down or reset
void loop() {
}