1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
24
25
28
29
30
31
32
33
34
35
39
43
44
45
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
74
78
83
84
85
86
87
88
89
90
91
92
93
94
95
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
246
247
248
249
250
251
256
260
261
266
270
271
275
279
280
283
284
/* ... */
#include "main.h"
/* ... */
/* ... */
extern TS_StateTypeDef TS_State;
Includes
static uint8_t ts_calibration_done = 0;
static int16_t A1, A2, B1, B2;
static int16_t aPhysX[2], aPhysY[2], aLogX[2], aLogY[2];Private variables
static void TouchscreenCalibration_SetHint(void);
static void TouchScreen_Calibration_GetPhysValues(int16_t LogX, int16_t LogY, int16_t * pPhysX, int16_t * pPhysY) ;
static void TouchScreen_Calibration_WaitForPressedState(uint8_t Pressed) ;Private function prototypes
/* ... */
uint8_t Touchscreen_Calibration(void)
{
uint8_t ts_status = TS_OK;
uint8_t i;
uint16_t ts_SizeX;
uint16_t ts_SizeY;
ts_SizeX = BSP_LCD_GetXSize();
ts_SizeY = BSP_LCD_GetYSize();
TouchscreenCalibration_SetHint();
ts_status = BSP_TS_Init(ts_SizeX, ts_SizeY);
if (ts_status != TS_OK)
{
BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
BSP_LCD_SetTextColor(LCD_COLOR_RED);
BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 95, (uint8_t *)"ERROR", CENTER_MODE);
BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 80, (uint8_t *)"Touchscreen cannot be calibrated", CENTER_MODE);
if(ts_status == TS_ERROR)
{
BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 65, (uint8_t *)"Touchscreen undefined error", CENTER_MODE);
}if (ts_status == TS_ERROR) { ... }
else if(ts_status == TS_TIMEOUT)
{
BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 65, (uint8_t *)"Touchscreen Timeout", CENTER_MODE);
}else if (ts_status == TS_TIMEOUT) { ... }
else
{
BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 65, (uint8_t *)"Touchscreen Not Found", CENTER_MODE);
}else { ... }
}if (ts_status != TS_OK) { ... }
else
{
BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 65, (uint8_t *)"FT6x06 internal calibration passed", CENTER_MODE);
aLogX[0] = 40;
aLogY[0] = 40;
aLogX[1] = BSP_LCD_GetXSize() - 40;
aLogY[1] = BSP_LCD_GetYSize() - 40;
for (i = 0; i < 2; i++)
{
TouchScreen_Calibration_GetPhysValues(aLogX[i], aLogY[i], &aPhysX[i], &aPhysY[i]);
}for (i = 0; i < 2; i++) { ... }
A1 = (1000 * ( aLogX[1] - aLogX[0])) / ( aPhysX[1] - aPhysX[0]);
B1 = (1000 * aLogX[0]) - A1 * aPhysX[0];
A2 = (1000 * ( aLogY[1] - aLogY[0])) / ( aPhysY[1] - aPhysY[0]);
B2 = (1000 * aLogY[0]) - A2 * aPhysY[0];
ts_calibration_done = 1;
}else { ... }
return (ts_status);
}{ ... }
/* ... */
static void TouchscreenCalibration_SetHint(void)
{
BSP_LCD_Clear(LCD_COLOR_WHITE);
BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
BSP_LCD_SetFont(&Font20);
BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() / 2 - 35, (uint8_t *)"Before using the Touchscreen", CENTER_MODE);
BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() / 2 - 15, (uint8_t *)"you need to calibrate it.", CENTER_MODE);
BSP_LCD_SetFont(&Font24);
BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() / 2 + 7, (uint8_t *)"WAIT until the black circle appears", CENTER_MODE);
BSP_LCD_SetFont(&Font20);
BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() / 2 + 30, (uint8_t *)"THEN Press precisely on the black circles", CENTER_MODE);
BSP_LCD_SetFont(&Font12);
}{ ... }
/* ... */
static void TouchScreen_Calibration_GetPhysValues(int16_t LogX, int16_t LogY, int16_t * pPhysX, int16_t * pPhysY)
{
BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
BSP_LCD_FillCircle(LogX, LogY, 20);
BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
BSP_LCD_FillCircle(LogX, LogY, 10);
TouchScreen_Calibration_WaitForPressedState(1);
*pPhysX = TS_State.touchX[0];
*pPhysY = TS_State.touchY[0];
TouchScreen_Calibration_WaitForPressedState(0);
BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
BSP_LCD_FillCircle(LogX, LogY, 20);
}{ ... }
/* ... */
static void TouchScreen_Calibration_WaitForPressedState(uint8_t Pressed)
{
uint16_t TimeStart = 0;
uint8_t status = TS_OK;
uint32_t exitFirstLevelWhileLoopReq = 0;
uint32_t exitSecondLevelWhileLoopReq = 0;
do
{
exitSecondLevelWhileLoopReq = 0;
status = BSP_TS_GetState(&TS_State);
if(status == TS_OK)
{
if (((Pressed == 0) && (TS_State.touchDetected == 0)) ||
((Pressed == 1) && ((TS_State.touchDetected == 1) || (TS_State.touchDetected == 2))))
{
TimeStart = HAL_GetTick();
do
{
status = BSP_TS_GetState(&TS_State);
if(status == TS_OK)
{
if (((Pressed == 0) && ((TS_State.touchDetected == 1) || (TS_State.touchDetected == 2))) ||
((Pressed == 1) && ((TS_State.touchDetected == 0))))
{
exitSecondLevelWhileLoopReq = 1;
}if (((Pressed == 0) && ((TS_State.touchDetected == 1) || (TS_State.touchDetected == 2))) || ((Pressed == 1) && ((TS_State.touchDetected == 0)))) { ... }
else if ((HAL_GetTick() - 100) > TimeStart)
{
exitSecondLevelWhileLoopReq = 1;
exitFirstLevelWhileLoopReq = 1;
}else if ((HAL_GetTick() - 100) > TimeStart) { ... }
HAL_Delay(10);
}if (status == TS_OK) { ... }
...}
while (!exitSecondLevelWhileLoopReq);
}if (((Pressed == 0) && (TS_State.touchDetected == 0)) || ((Pressed == 1) && ((TS_State.touchDetected == 1) || (TS_State.touchDetected == 2)))) { ... }
}if (status == TS_OK) { ... }
if(!exitFirstLevelWhileLoopReq)
{
HAL_Delay(10);
}if (!exitFirstLevelWhileLoopReq) { ... }
...}
while (!exitSecondLevelWhileLoopReq);
}{ ... }
/* ... */
uint16_t TouchScreen_Get_Calibrated_X(uint16_t x)
{
return (((A1 * x) + B1) / 1000);
}{ ... }
/* ... */
uint16_t TouchScreen_Get_Calibrated_Y(uint16_t y)
{
return (((A2 * y) + B2) / 1000);
}{ ... }
/* ... */
uint8_t TouchScreen_IsCalibrationDone(void)
{
return (ts_calibration_done);
}{ ... }
/* ... */
/* ... */