1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
32
33
43
44
45
46
47
48
49
50
54
55
56
57
58
59
64
83
84
89
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
153
154
155
156
157
158
159
160
161
162
163
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
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
238
239
243
244
248
249
252
253
/* ... */
#include "k_calibration.h"
#include "k_rtc.h"
#include "GUI.h"
/* ... */
/* ... */
Includes
typedef struct
{
uint32_t A;
uint32_t B;
uint32_t IsCalibrated;
...}CALIBRATION_DataTypedef;
Private typedef
uint32_t CALIBRATION_Done = 0;
static const char * _acPos[] = {
"(upper left position)",
"(lower right position)"
...};
int32_t A1, A2, B1, B2;
CALIBRATION_DataTypedef data1;
CALIBRATION_DataTypedef data2;
/* ... */
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);
}{ ... }
/* ... */
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);
}{ ... }
/* ... */
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 screen\n"
"at the center of the ring.");
Rect.y0 = LogY - FontSizeY;
Rect.y1 = LogY + FontSizeY;
if (LogX < LCD_GetXSize() / 2) {
Rect.x0 = LogX + 15;
Rect.x1 = LCD_GetXSize();
Align = GUI_TA_LEFT;
}if (LogX < LCD_GetXSize() / 2) { ... } else {
Rect.x0 = 0;
Rect.x1 = LogX - 15;
Align = GUI_TA_RIGHT;
}else { ... }
GUI_DispStringInRect(acText, &Rect, Align | GUI_TA_TOP);
GUI_DispStringInRect(pString, &Rect, Align | GUI_TA_BOTTOM);
GUI_FillCircle(LogX - 5, LogY - 5, 5);
GUI_SetColor(GUI_WHITE);
GUI_FillCircle(LogX + 5, LogY + 5, 2);
GUI_SetColor(GUI_BLACK);
_WaitForPressedState(1);
GUI_TOUCH_GetState(&State);
*pPhysX = State.x;
*pPhysY = State.y;
_WaitForPressedState(0);
}{ ... }
/* ... */
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);
}{ ... }
/* ... */
void k_CalibrationInit(void)
{
int aPhysX[2], aPhysY[2], aLogX[2], aLogY[2], i;
data2.A= k_BkupRestoreParameter(RTC_BKP_DR15);
data2.B = k_BkupRestoreParameter(RTC_BKP_DR16);
data1.A = k_BkupRestoreParameter(RTC_BKP_DR17);
data1.B = k_BkupRestoreParameter(RTC_BKP_DR18);
data2.IsCalibrated = k_BkupRestoreParameter(RTC_BKP_DR19);
A2 = data2.A;
B2 = data2.B;
A1 = data1.A;
B1 = data1.B;
if (data2.IsCalibrated == 0)
{
GUI_SetBkColor(GUI_WHITE);
GUI_Clear();
GUI_SetColor(GUI_BLACK);
GUI_SetFont(&GUI_Font13B_ASCII);
_Explain();
aLogX[0] = 15;
aLogY[0] = 15;
aLogX[1] = LCD_GetXSize() - 15;
aLogY[1] = LCD_GetYSize() - 15;
for (i = 0; i < 2; i++) {
_GetPhysValues(aLogX[i], aLogY[i], &aPhysX[i], &aPhysY[i], _acPos[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];
data1.A = A1;
data1.B = B1;
k_BkupSaveParameter(RTC_BKP_DR17, data1.A);
k_BkupSaveParameter(RTC_BKP_DR18, data1.B);
data2.A = A2;
data2.B = B2;
data2.IsCalibrated = 1;
k_BkupSaveParameter(RTC_BKP_DR15, data2.A);
k_BkupSaveParameter(RTC_BKP_DR16, data2.B);
k_BkupSaveParameter(RTC_BKP_DR19, data2.IsCalibrated);
GUI_Clear();
_DispStringCentered("Touch screen has been\n"
"calibrated. Please use\n"
"the cursor to test\n"
"the calibration...");
}if (data2.IsCalibrated == 0) { ... }
CALIBRATION_Done = 1;
GUI_Delay(1000);
}{ ... }
uint8_t k_CalibrationIsDone(void)
{
return CALIBRATION_Done;
}{ ... }
uint16_t k_CalibrationGetX(uint16_t x)
{
return ((((A1 * x) + B1)/1000));
}{ ... }
uint16_t k_CalibrationGetY(uint16_t y)
{
return (((A2 * y) + B2)/1000);
}{ ... }
/* ... */
/* ... */