1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
25
26
29
30
31
32
33
34
35
36
37
38
39
40
41
42
51
52
53
63
64
65
66
67
68
69
73
74
75
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
104
108
113
114
115
116
117
118
119
120
121
122
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
270
271
272
273
274
279
286
287
292
299
300
304
308
309
312
313
/* ... */
#include "main.h"
/* ... */
/* ... */
extern TS_StateTypeDef TS_State;
#define CALIB_RES(val,pte1,ptm1,pte2,ptm2) ((((val-ptm2)*(pte1-pte2))/(ptm1-ptm2))+pte2)
Includes
static uint8_t ts_calibration_done = 0;
static int16_t aPhysX[2], aPhysY[2], aLogX[2], aLogY[2];
typedef union
{
struct
{
uint32_t A1 : 15;
uint32_t B1 : 16;
uint32_t Reserved : 1;
...}b;
uint32_t d32;
...}CALIBRATION_Data1Typedef;
typedef union
{
struct
{
uint32_t A2 : 15;
uint32_t B2 : 16;
uint32_t IsCalibrated : 1;
...}b;
uint32_t d32;
...}CALIBRATION_Data2Typedef;
CALIBRATION_Data1Typedef data1;
CALIBRATION_Data2Typedef data2;
int16_t A1, A2, B1, B2, C1, C2, D1, D2;
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;
data1.d32 = k_BkupRestoreParameter(RTC_BKP_DR0);
data2.d32 = k_BkupRestoreParameter(RTC_BKP_DR1);
if(data2.b.IsCalibrated == 0)
{
TouchscreenCalibration_SetHint();
ts_status = TS_OK;
if (ts_status != TS_OK)
{
BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
BSP_LCD_SetTextColor(LCD_COLOR_RED);
BSP_LCD_DisplayStringAt(0, 240 - 95, (uint8_t *)"ERROR", CENTER_MODE);
BSP_LCD_DisplayStringAt(0, 240 - 80, (uint8_t *)"Touchscreen cannot be calibrated", CENTER_MODE);
if(ts_status == TS_ERROR)
{
BSP_LCD_DisplayStringAt(0, 240 - 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
{
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++) { ... }
data1.b.A1 = aPhysX[0];
data1.b.B1 = aPhysX[1];
k_BkupSaveParameter(RTC_BKP_DR0, data1.d32);
data2.b.A2 = aPhysY[0];
data2.b.B2 = aPhysY[1];
data2.b.IsCalibrated = 1;
k_BkupSaveParameter(RTC_BKP_DR1, data2.d32);
}else { ... }
}if (data2.b.IsCalibrated == 0) { ... }
ts_calibration_done = 1;
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, 100, (uint8_t *)"Touchscreen", CENTER_MODE);
BSP_LCD_DisplayStringAt(0, 120, (uint8_t *)"Calibration", CENTER_MODE);
}{ ... }
/* ... */
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(100);
}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(100);
}if (!exitFirstLevelWhileLoopReq) { ... }
...}
while (!exitSecondLevelWhileLoopReq);
}{ ... }
/* ... */
uint16_t TouchScreen_Get_Calibrated_X(uint16_t x)
{
int32_t ret=CALIB_RES(x,20,data1.b.A1,220,data1.b.B1);
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,20,data2.b.A2,220,data2.b.B2);
if (ret<0) ret=0;
else if (ret>240) ret=239;
return ret;
}{ ... }
/* ... */
uint8_t TouchScreen_IsCalibrationDone(void)
{
return (ts_calibration_done);
}{ ... }
/* ... */
/* ... */