|
1 /** |
|
2 * @file calibration.c |
|
3 * @brief TFT screen calibration. |
|
4 */ |
|
5 |
|
6 #include "config.h" |
|
7 |
|
8 |
|
9 static const char *TAG = "calibration"; |
|
10 |
|
11 |
|
12 extern uint16_t tp_xleft; |
|
13 extern uint16_t tp_xright; |
|
14 extern uint16_t tp_ytop; |
|
15 extern uint16_t tp_ybottom; |
|
16 |
|
17 |
|
18 // Distance of the testpoints from the corders. |
|
19 #define CAL_DISTANCE 15 |
|
20 |
|
21 |
|
22 int TS_set_calibration(int xleft, int xright, int ytop, int ybottom) |
|
23 { |
|
24 if ((xleft > xright) || (ybottom > ytop)) { |
|
25 ESP_LOGE(TAG, "Touch Calibration error X=%d..%d Y=%d..%d", xleft, xright, ybottom, ytop); |
|
26 return 1; |
|
27 } |
|
28 |
|
29 tp_xleft = xleft; |
|
30 tp_xright = xright; |
|
31 tp_ytop = ytop; |
|
32 tp_ybottom = ybottom; |
|
33 ESP_LOGI(TAG, "Touch Calibration X=%d..%d Y=%d..%d", xleft, xright, ybottom, ytop); |
|
34 |
|
35 return 0; |
|
36 } |
|
37 |
|
38 |
|
39 |
|
40 void Calibration_Init(void) |
|
41 { |
|
42 _fg = TFT_ORANGE; |
|
43 TFT_setFont(DEJAVU24_FONT, NULL); |
|
44 TFT_print("Scherm calibratie", CENTER, 60); |
|
45 TFT_setFont(DEJAVU18_FONT, NULL); |
|
46 TFT_print("Druk op de oplichtende stippen", CENTER, 90); |
|
47 } |
|
48 |
|
49 |
|
50 |
|
51 /* |
|
52 * Run in a blocking loop |
|
53 */ |
|
54 void Calibration_Loop(void) |
|
55 { |
|
56 int tx, ty, pass, cx[8], cy[8], xl = 0, xh = 0, yl = 0, yh = 0; |
|
57 int offset; |
|
58 |
|
59 pass = 0; |
|
60 ESP_LOGI(TAG, "Touchscreen calibration"); |
|
61 |
|
62 while (1) { |
|
63 switch (pass) { |
|
64 case 0: |
|
65 case 4: TFT_fillCircle(320 - CAL_DISTANCE, 240 - CAL_DISTANCE, 5, TFT_BLACK); |
|
66 TFT_fillCircle(CAL_DISTANCE, CAL_DISTANCE, 5, TFT_YELLOW); |
|
67 xl = 2800; |
|
68 xh = 3800; |
|
69 yl = 2800; |
|
70 yh = 3800; |
|
71 break; |
|
72 case 1: |
|
73 case 5: TFT_fillCircle(CAL_DISTANCE, CAL_DISTANCE, 5, TFT_BLACK); |
|
74 TFT_fillCircle(320 - CAL_DISTANCE, CAL_DISTANCE, 5, TFT_YELLOW); |
|
75 xl = 2800; |
|
76 xh = 3800; |
|
77 yl = 100; |
|
78 yh = 1000; |
|
79 break; |
|
80 case 2: |
|
81 case 6: TFT_fillCircle(320 - CAL_DISTANCE, CAL_DISTANCE, 5, TFT_BLACK); |
|
82 TFT_fillCircle(CAL_DISTANCE, 240 - CAL_DISTANCE, 5, TFT_YELLOW); |
|
83 xl = 100; |
|
84 xh = 1000; |
|
85 yl = 2800; |
|
86 yh = 3800; |
|
87 break; |
|
88 case 3: |
|
89 case 7: TFT_fillCircle(CAL_DISTANCE, 240 - CAL_DISTANCE, 5, TFT_BLACK); |
|
90 TFT_fillCircle(320 - CAL_DISTANCE, 240 - CAL_DISTANCE, 5, TFT_YELLOW); |
|
91 xl = 100; |
|
92 xh = 1000; |
|
93 yl = 100; |
|
94 yh = 1000; |
|
95 break; |
|
96 } |
|
97 if (TFT_read_touch(&tx, &ty, 1)) { // Raw read touch. |
|
98 printf(" Calibrarion loop %d %d %d\n", pass, tx, ty); |
|
99 if ((tx >= xl) && (tx <= xh) && (ty >= yl) && (ty <= yh)) { |
|
100 cx[pass] = tx; |
|
101 cy[pass] = ty; |
|
102 pass++; |
|
103 if (pass == 8) { |
|
104 break; |
|
105 } |
|
106 } else { |
|
107 SoundPlay(SOUND_Warn); |
|
108 } |
|
109 WaitTouchReleased(); |
|
110 } |
|
111 vTaskDelay(100 / portTICK_PERIOD_MS); |
|
112 } |
|
113 |
|
114 TFT_fillScreen(TFT_BLACK); |
|
115 TFT_setFont(DEJAVU18_FONT, NULL); |
|
116 TFT_print("Scherm calibratie is klaar.", CENTER, 60); |
|
117 /* |
|
118 * Calculate the new calibration values. |
|
119 * The testdots were 10 pixels of the corners, |
|
120 * so take that in account. |
|
121 */ |
|
122 offset = (CAL_DISTANCE * 3800) / 240; |
|
123 config.ts_xleft = ((cx[2] + cx[3] + cx[6] + cx[7]) / 4) - offset; |
|
124 config.ts_xright = ((cx[0] + cx[1] + cx[4] + cx[5]) / 4) + offset; |
|
125 offset = (CAL_DISTANCE * 3800) / 320; |
|
126 config.ts_ytop = ((cy[0] + cy[2] + cy[4] + cy[6]) / 4) + offset; |
|
127 config.ts_ybottom = ((cy[1] + cy[3] + cy[5] + cy[7]) / 4) - offset; |
|
128 TS_set_calibration(config.ts_xleft, config.ts_xright, config.ts_ytop, config.ts_ybottom); |
|
129 write_config(); |
|
130 |
|
131 Buttons_Add(130, 200, 60, 40, "Ok", 0); |
|
132 Buttons_Show(); |
|
133 while (1) { |
|
134 vTaskDelay(20 / portTICK_PERIOD_MS); |
|
135 if (Buttons_Scan() == 0) { |
|
136 return; |
|
137 break; |
|
138 } |
|
139 } |
|
140 } |
|
141 |
|
142 |