Select one of the symbols to view example projects that use it.
 
Outline
Includes
#include "main.h"
Private variables
TS_State
Calibration_Done
A1
A2
B1
B2
aPhysX
aPhysY
aLogX
aLogY
Private function prototypes
Touchscreen_Calibration()
TouchscreenCalibration_SetHint()
GetPhysValues(int16_t, int16_t, int16_t *, int16_t *)
WaitForPressedState(uint8_t)
Calibration_GetX(uint16_t)
Calibration_GetY(uint16_t)
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** ****************************************************************************** * @file Display/LCD_Paint/Src/ts_calibration.c * @author MCD Application Team * @brief This application 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_Applications * @{ *//* ... */ /** @addtogroup LCD_Paint * @{ *//* ... */ Includes /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ static TS_StateTypeDef TS_State; static uint8_t 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 GetPhysValues(int16_t LogX, int16_t LogY, int16_t * pPhysX, int16_t * pPhysY); static void WaitForPressedState(uint8_t Pressed);Private function prototypes /* Private functions ---------------------------------------------------------*/ /** * @brief Performs the TS calibration * @param None * @retval None *//* ... */ void Touchscreen_Calibration(void) { uint8_t status = 0; uint8_t i = 0; TouchscreenCalibration_SetHint(); status = BSP_TS_Init(BSP_LCD_GetXSize(), BSP_LCD_GetYSize()); if (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 initialized", CENTER_MODE); }if (status != TS_OK) { ... } while (1) { if (status == TS_OK) { aLogX[0] = 15; aLogY[0] = 15; aLogX[1] = BSP_LCD_GetXSize() - 15; aLogY[1] = BSP_LCD_GetYSize() - 15; for (i = 0; i < 2; i++) { 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]; Calibration_Done = 1; return; }if (status == TS_OK) { ... } HAL_Delay(5); }while (1) { ... } }{ ... } /** * @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*)"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 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, 5); BSP_LCD_SetTextColor(LCD_COLOR_WHITE); BSP_LCD_FillCircle(LogX, LogY, 2); /* Wait until touch is pressed */ WaitForPressedState(1); BSP_TS_GetState(&TS_State); *pPhysX = TS_State.x; *pPhysY = TS_State.y; /* Wait until touch is released */ WaitForPressedState(0); BSP_LCD_SetTextColor(LCD_COLOR_WHITE); BSP_LCD_FillCircle(LogX, LogY, 5); }{ ... } /** * @brief wait for touch detected * @param Pressed: touch pressed. * @retval None *//* ... */ static void WaitForPressedState(uint8_t Pressed) { TS_StateTypeDef State; do { BSP_TS_GetState(&State); HAL_Delay(10); if (State.TouchDetected == Pressed) { uint16_t TimeStart = HAL_GetTick(); do { BSP_TS_GetState(&State); HAL_Delay(10); if (State.TouchDetected != Pressed) { break; }if (State.TouchDetected != Pressed) { ... } else if ((HAL_GetTick() - 100) > TimeStart) { return; }else if ((HAL_GetTick() - 100) > TimeStart) { ... } ...} while (1); }if (State.TouchDetected == Pressed) { ... } ...} while (1); }{ ... } /** * @brief Calibrate X position * @param x : X position * @retval calibrated x *//* ... */ uint16_t Calibration_GetX(uint16_t x) { return (((A1 * x) + B1)/1000); }{ ... } /** * @brief Calibrate Y position * @param y : Y position * @retval calibrated y *//* ... */ uint16_t Calibration_GetY(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 IsCalibrationDone(void) { return (Calibration_Done); }{ ... } /** * @} *//* ... */ /** * @} *//* ... */