Select one of the symbols to view example projects that use it.
 
Outline
#include "calibration.h"
#include "rtc.h"
#include "GUI.h"
CALIBRATION_Data1Typedef
CALIBRATION_Data2Typedef
CALIBRATION_Done
_acPos
A1
A2
B1
B2
data1
data2
_WaitForPressedState(int)
_DispStringCentered(const char *)
_GetPhysValues(int, int, int *, int *, const char *)
_Explain()
CALIBRATION_Check()
CALIBRATION_IsDone()
CALIBRATION_GetX(uint16_t)
CALIBRATION_GetY(uint16_t)
Files
loading...
CodeScopeSTM32 Libraries and SamplesSTemWin_SampleDemoSTemWin/Target/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
285
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/********************************************************************* * SEGGER Microcontroller GmbH & Co. KG * * Solutions for real time microcontroller applications * ********************************************************************** * * * (c) 1996 - 2015 SEGGER Microcontroller GmbH & Co. KG * * * * Internet: www.segger.com Support: support@segger.com * * * ********************************************************************** ** emWin V5.28 - Graphical user interface for embedded applications ** All Intellectual Property rights in the Software belongs to SEGGER. emWin is protected by international copyright laws. Knowledge of the source code may not be used to write a similar product. This file may only be used in accordance with the following terms: The software has been licensed to STMicroelectronics International N.V. whose registered office is situated at Plan-les-Ouates, Geneva, 39 Chemin du Champ des Filles, Switzerland solely for the purposes of creating libraries for STMicroelectronics ARM Cortex�-M-based 32-bit microcontroller products, sublicensed and distributed by STMicroelectronics under the terms and conditions of the End User License Agreement supplied with the software. The use of the software on a stand-alone basis or for any purpose other than to operate with the specified microcontroller is prohibited and subject to a separate license agreement. Full source code is available at: www.segger.com We appreciate your understanding and fairness. ---------------------------------------------------------------------- File : TOUCH_Calibrate.c Purpose : Demonstrates how a touch screen can be calibrated at run time ---------------------------END-OF-HEADER------------------------------ *//* ... */ #include "calibration.h" #include "rtc.h" #include "GUI.h" 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; uint32_t CALIBRATION_Done = 0; /******************************************************************** * * Static data * ********************************************************************* *//* ... */ static const char * _acPos[] = { "(upper left position)", "(lower right position)" ...}; int32_t A1, A2, B1, B2; CALIBRATION_Data1Typedef data1; CALIBRATION_Data2Typedef data2; /********************************************************************* * * Static code * ********************************************************************** *//* ... */ /********************************************************************* * * _WaitForPressedState * * Purpose: * Waits until the touch is in the given pressed state for at least 250 ms *//* ... */ static void _WaitForPressedState(int Pressed) { GUI_PID_STATE State; do { GUI_TOUCH_GetState(&State); GUI_Delay(10); if (State.Pressed == Pressed) { int TimeStart = GUI_GetTime(); do { GUI_TOUCH_GetState(&State); GUI_Delay(10); if (State.Pressed != Pressed) { break; }if (State.Pressed != Pressed) { ... } else if ((GUI_GetTime() - 150) > TimeStart) { return; }else if ((GUI_GetTime() - 150) > TimeStart) { ... } ...} while (1); }if (State.Pressed == Pressed) { ... } ...} while (1); }{ ... } /********************************************************************* * * _DispStringCentered * * Purpose: * Shows the given text horizontally and vertically centered *//* ... */ static void _DispStringCentered(const char * pString) { GUI_RECT Rect; Rect.x0 = Rect.y0 = 0; Rect.x1 = LCD_GetXSize() - 1; Rect.y1 = LCD_GetYSize() - 1; GUI_DispStringInRect(pString, &Rect, GUI_TA_HCENTER | GUI_TA_VCENTER); }{ ... } /********************************************************************* * * _GetPhysValues * * Purpose: * Asks the user to press the touch screen at the given position * and returns the physical A/D values *//* ... */ static void _GetPhysValues(int LogX, int LogY, int * pPhysX, int * pPhysY, const char * pString) { char acText[] = "Press here"; GUI_RECT Rect; GUI_PID_STATE State; int FontSizeY, Align; FontSizeY = GUI_GetFontSizeY(); GUI_Clear(); GUI_SetColor(GUI_BLACK); _DispStringCentered("Runtime calibration,\n" "please touch the corners of the screen\n" "at the center of the rings."); /* Ask user to press the touch */ /* Calculate the rectangle for the string */ if (LogX < LCD_GetXSize() / 2) { Rect.x0 = LogX + 25; Rect.x1 = LCD_GetXSize(); Rect.y0 = LogY - FontSizeY + 5; Rect.y1 = LogY + FontSizeY + 5; Align = GUI_TA_LEFT; }if (LogX < LCD_GetXSize() / 2) { ... } else { Rect.x0 = 0; Rect.x1 = LogX - 25; Rect.y0 = LogY - FontSizeY - 5; Rect.y1 = LogY + FontSizeY - 5; Align = GUI_TA_RIGHT; }else { ... } /* Show the text nearby the ring */ GUI_DispStringInRect(acText, &Rect, Align | GUI_TA_TOP); GUI_DispStringInRect(pString, &Rect, Align | GUI_TA_BOTTOM); /* Draw the ring */ GUI_FillCircle(LogX, LogY, 10); GUI_SetColor(GUI_WHITE); GUI_FillCircle(LogX, LogY, 7); GUI_SetColor(GUI_BLACK); /* Wait until touch is pressed */ _WaitForPressedState(1); GUI_TOUCH_GetState(&State); *pPhysX = State.x; *pPhysY = State.y; /* Wait until touch is released */ _WaitForPressedState(0); }{ ... } /******************************************************************** * * _Explain * * Purpose: * Shows a text to give a short explanation of the sample program *//* ... */ static void _Explain(void) { _DispStringCentered("At first time, you need to\n" "calibrate the Touch screen\n" "Please press the touch\n" "screen to continue..."); GUI_DispStringHCenterAt("Touch screen Calibration", LCD_GetXSize() / 2, 5); _WaitForPressedState(1); _WaitForPressedState(0); }{ ... } /********************************************************************* * * Public code * ********************************************************************** *//* ... */ /********************************************************************* * * MainTask *//* ... */ void CALIBRATION_Check(void) { int aPhysX[2], aPhysY[2], aLogX[2], aLogY[2], i; data1.d32 = BACKUP_RestoreParameter(RTC_BKP_DR0); data2.d32 = BACKUP_RestoreParameter(RTC_BKP_DR1); A2 = data2.b.A2 ; B2 = data2.b.B2 ; A1 = data1.b.A1 ; B1 = data1.b.B1 ; if(data2.b.IsCalibrated == 0) { GUI_SetBkColor(GUI_WHITE); GUI_Clear(); GUI_SetColor(GUI_BLACK); GUI_SetFont(&GUI_Font20B_ASCII); _Explain(); /* Set the logical values */ aLogX[0] = 15; aLogY[0] = 15; aLogX[1] = LCD_GetXSize() - 15; aLogY[1] = LCD_GetYSize() - 15; /* Get the physical values of the AD converter for 2 positions */ for (i = 0; i < 2; i++) { _GetPhysValues(aLogX[i], aLogY[i], &aPhysX[i], &aPhysY[i], _acPos[i]); }for (i = 0; i < 2; i++) { ... } /* Use the physical values to calibrate the touch screen */ 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]; data1.b.A1 = A1; data1.b.B1 = B1; BACKUP_SaveParameter(RTC_BKP_DR0, data1.d32); data2.b.A2 = A2; data2.b.B2 = B2; data2.b.IsCalibrated = 1; BACKUP_SaveParameter(RTC_BKP_DR1, data2.d32); /* Display the result */ GUI_CURSOR_Show(); GUI_Clear(); _DispStringCentered("Touch screen has been\n" "calibrated. Please use\n" "the cursor to test\n" "the calibration..."); }if (data2.b.IsCalibrated == 0) { ... } CALIBRATION_Done = 1; GUI_Delay(1000); }{ ... } uint8_t CALIBRATION_IsDone(void) { return CALIBRATION_Done; }{ ... } uint16_t CALIBRATION_GetX(uint16_t x) { return (((A1 * x) + B1)/1000); }{ ... } uint16_t CALIBRATION_GetY(uint16_t y) { return (((A2 * y) + B2)/1000); }{ ... } /*************************** End of file ****************************/