1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
23
24
25
26
36
37
38
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
62
66
71
72
73
74
75
76
77
78
79
80
81
82
83
87
88
89
90
91
92
93
94
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
227
228
229
230
231
236
243
244
249
256
257
261
/* ... */
#include "main.h"
#include "ts_calibration.h"
Includes
static uint8_t ts_calibration_done = 0;
static int16_t aPhysX[2], aPhysY[2], aLogX[2], aLogY[2];
extern TS_StateTypeDef TS_State;
#define CALIB_RES(val,pte1,ptm1,pte2,ptm2) ((((val-ptm2)*(pte1-pte2))/(ptm1-ptm2))+pte2)
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;
TouchscreenCalibration_SetHint();
ts_status = BSP_TS_Init(BSP_LCD_GetXSize(), BSP_LCD_GetYSize());
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] = 20;
aLogY[0] = 20;
aLogX[1] = BSP_LCD_GetXSize() - 20;
aLogY[1] = BSP_LCD_GetYSize() - 20;
for (i = 0; i < 2; i++)
{
TouchScreen_Calibration_GetPhysValues(aLogX[i], aLogY[i], &aPhysX[i], &aPhysY[i]);
}for (i = 0; i < 2; i++) { ... }
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)
{
int32_t ret=CALIB_RES(x,aLogX[0],aPhysX[0],aLogX[1],aPhysX[1]);
if (ret<0) ret=0;
else if (ret>240) ret=239;
return ret;
}{ ... }
/* ... */
uint16_t TouchScreen_Get_Calibrated_Y(uint16_t y)
{
int32_t ret=CALIB_RES(y,aLogY[0],aPhysY[0],aLogY[1],aPhysY[1]);
if (ret<0) ret=0;
else if (ret>240) ret=239;
return ret;
}{ ... }
/* ... */
uint8_t TouchScreen_IsCalibrationDone(void)
{
return (ts_calibration_done);
}{ ... }