Select one of the symbols to view example projects that use it.
 
Outline
Includes
#include "main.h"
Private variables
ts_calibration_done
A1
A2
B1
B2
aPhysX
aPhysY
aLogX
aLogY
Private function prototypes
Touchscreen_Calibration()
TouchscreenCalibration_SetHint()
TouchScreen_Calibration_GetPhysValues(int16_t, int16_t, int16_t *, int16_t *)
TouchScreen_Calibration_WaitForPressedState(uint8_t)
TouchScreen_Get_Calibrated_X(uint16_t)
TouchScreen_Get_Calibrated_Y(uint16_t)
TouchScreen_IsCalibrationDone()
Files
loading...
CodeScopeSTM32 Libraries and SamplesLCD_PaintSrc/ts_calibration.c
 
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** ****************************************************************************** * @file LCD_Paint/Src/ts_calibration.c * @author MCD Application Team * @brief This example code shows how to calibrate the touchscreen. ****************************************************************************** * @attention * * Copyright (c) 2017 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** *//* ... */ /* Includes ------------------------------------------------------------------*/ #include "main.h" /** @addtogroup STM32F4xx_HAL_Examples * @{ *//* ... */ /** @addtogroup BSP * @{ *//* ... */ /* Imported variables */ extern TS_StateTypeDef TS_State; Includes /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ 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 /* Private function prototypes -----------------------------------------------*/ 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 /* Private functions ---------------------------------------------------------*/ /** * @brief Performs the TS calibration * @param None * @retval Status (TS_OK = 0/ TS_ERROR = 1 / TS_TIMEOUT = 1 / TS_DEVICE_NOT_FOUND = 3) *//* ... */ 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(); /* Start touchscreen internal calibration and configuration + start */ 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 { /* TS_DEVICE_NOT_FOUND */ BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 65, (uint8_t *)"Touchscreen Not Found", CENTER_MODE); }else { ... } }if (ts_status != TS_OK) { ... } else { /* status == TS_OK */ BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 65, (uint8_t *)"FT6x06 internal calibration passed", CENTER_MODE); /* Get touch points for SW calibration processing */ 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++) { ... } /* Compute calibration coefficients */ 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); }{ ... } /** * @brief Display calibration hint * @param None * @retval None *//* ... */ static void TouchscreenCalibration_SetHint(void) { /* Clear the LCD */ BSP_LCD_Clear(LCD_COLOR_WHITE); /* Set Touchscreen Demo description */ BSP_LCD_SetTextColor(LCD_COLOR_BLACK); BSP_LCD_SetBackColor(LCD_COLOR_WHITE); BSP_LCD_SetFont(&Font12); BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() / 2 - 27, (uint8_t *)"Before using the Touchscreen", CENTER_MODE); BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() / 2 - 12, (uint8_t *)"you need to calibrate it.", CENTER_MODE); BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() / 2 + 3, (uint8_t *)"WAIT until end of internal calibration", CENTER_MODE); BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() / 2 + 18, (uint8_t *)"THEN Press on the black circles", CENTER_MODE); }{ ... } /** * @brief Get Physical position * @param LogX : logical X position * @param LogY : logical Y position * @param pPhysX : Physical X position * @param pPhysY : Physical Y position * @retval None *//* ... */ static void TouchScreen_Calibration_GetPhysValues(int16_t LogX, int16_t LogY, int16_t * pPhysX, int16_t * pPhysY) { /* Draw the ring */ BSP_LCD_SetTextColor(LCD_COLOR_BLACK); BSP_LCD_FillCircle(LogX, LogY, 20); BSP_LCD_SetTextColor(LCD_COLOR_WHITE); BSP_LCD_FillCircle(LogX, LogY, 10); /* Wait until pressed state on the touch panel */ TouchScreen_Calibration_WaitForPressedState(1); /* Return as physical touch values the positions of first touch, even if double touched occurred */ *pPhysX = TS_State.touchX[0]; *pPhysY = TS_State.touchY[0]; /* Wait until touch is released on touch panel */ TouchScreen_Calibration_WaitForPressedState(0); BSP_LCD_SetTextColor(LCD_COLOR_WHITE); BSP_LCD_FillCircle(LogX, LogY, 20); }{ ... } /** * @brief TouchScreen_Calibration_WaitForPressedState : wait until a particular press/depress action * The function is managing anti-rebound : that is the awaited state when detected * needs to be stable for a sufficient time (timeout time), otherwise a new sense to search * for awaited state is performed. When awaited state is found and state is stable for timeout * duration, the function is exited. * @param uint8_t Pressed : Awaited pressed state * - Await touch (single/multiple) detection if Pressed == 1 * - Await no touch detection if Pressed == 0 * @retval None *//* ... */ static void TouchScreen_Calibration_WaitForPressedState(uint8_t Pressed) { uint16_t TimeStart = 0; uint8_t status = TS_OK; uint32_t exitFirstLevelWhileLoopReq = 0; /* By default no exit request from first level while loop */ uint32_t exitSecondLevelWhileLoopReq = 0; /* By default no exit request from second level while loop */ /* First level while loop entry */ do { /* reset exit second level while loop in case it was set */ exitSecondLevelWhileLoopReq = 0; /* Sense of touch state from touch IC until get the awaited state in parameter 'Pressed' */ 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)))) { /* Got awaited press state */ /* Record in 'TimeStart' the time of awaited touch event for anti-rebound calculation */ /* The state should persist for a minimum sufficient time */ TimeStart = HAL_GetTick(); /* Is state of the touch changing ? */ /* Second level while loop entry */ do { /* New sense of touch state from touch IC : to evaluate if state was stable */ status = BSP_TS_GetState(&TS_State); if(status == TS_OK) { /* Is there a state change compared since having found the awaited state ? */ if (((Pressed == 0) && ((TS_State.touchDetected == 1) || (TS_State.touchDetected == 2))) || ((Pressed == 1) && ((TS_State.touchDetected == 0)))) { /* Too rapid state change => anti-rebound management : restart first touch search */ exitSecondLevelWhileLoopReq = 1; /* exit request from second level while loop */ }if (((Pressed == 0) && ((TS_State.touchDetected == 1) || (TS_State.touchDetected == 2))) || ((Pressed == 1) && ((TS_State.touchDetected == 0)))) { ... } else if ((HAL_GetTick() - 100) > TimeStart) { /* State have not changed for the timeout duration (stable touch for 100 ms) */ /* This means the touch state is stable : can exit function */ /* found valid touch, exit both while levels */ exitSecondLevelWhileLoopReq = 1; exitFirstLevelWhileLoopReq = 1; }else if ((HAL_GetTick() - 100) > TimeStart) { ... } /* Wait 10 ms before next sense of touch at next loop iteration */ HAL_Delay(10); }if (status == TS_OK) { ... } /* of if(status == TS_OK) */ ...} while (!exitSecondLevelWhileLoopReq); }if (((Pressed == 0) && (TS_State.touchDetected == 0)) || ((Pressed == 1) && ((TS_State.touchDetected == 1) || (TS_State.touchDetected == 2)))) { ... } /* of if (((Pressed == 0) && .... */ }if (status == TS_OK) { ... } /* of if(status == TS_OK) */ if(!exitFirstLevelWhileLoopReq) { /* Wait some time before next sense of touch at next loop iteration */ HAL_Delay(10); }if (!exitFirstLevelWhileLoopReq) { ... } ...} while (!exitSecondLevelWhileLoopReq); }{ ... } /** * @brief Calibrate x position (to obtain X = calibrated(x)) * @param x : X position * @retval calibrated x *//* ... */ uint16_t TouchScreen_Get_Calibrated_X(uint16_t x) { return (((A1 * x) + B1)/1000); }{ ... } /** * @brief Calibrate Y position * @param y : Y position * @retval calibrated y *//* ... */ uint16_t TouchScreen_Get_Calibrated_Y(uint16_t y) { return (((A2 * y) + B2) / 1000); }{ ... } /**check if the TS is calibrated * @param None * @retval calibration state (1 : calibrated / 0: no) *//* ... */ uint8_t TouchScreen_IsCalibrationDone(void) { return (ts_calibration_done); }{ ... } /** * @} *//* ... */ /** * @} *//* ... */