-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
189 lines (131 loc) · 3.94 KB
/
main.cpp
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
#include "Arduino.h"
#include "Adafruit_GFX.h"
#include "MCUFRIEND_kbv.h"
#include "TouchScreen.h"
#include "constants.h"
const int CROSSHAIR_LEFT = CROSSHAIR_W / 2;
const int CROSSHAIR_RIGHT = DISPLAY_WIDTH - 1 - (CROSSHAIR_W / 2);
const int CROSSHAIR_TOP = CROSSHAIR_H / 2;
const int CROSSHAIR_BOTTOM = DISPLAY_HEIGHT - 1 - (CROSSHAIR_H / 2);
const int crosshair_coors[][2] = {
{CROSSHAIR_LEFT, CROSSHAIR_TOP},
{CROSSHAIR_RIGHT, CROSSHAIR_TOP},
{CROSSHAIR_LEFT, CROSSHAIR_BOTTOM},
{CROSSHAIR_RIGHT, CROSSHAIR_BOTTOM},
};
const int NUM_CROSSHAIRS = sizeof(crosshair_coors) / sizeof(crosshair_coors[0]);
MCUFRIEND_kbv tft;
TouchScreen ts(XP, YP, XM, YM, 300);
void draw_crosshair(uint16_t x, uint16_t y, uint16_t clr) {
const int x1 = x - (CROSSHAIR_W >> 1);
const int x2 = x + (CROSSHAIR_W >> 1);
const int y1 = y - (CROSSHAIR_H >> 1);
const int y2 = y + (CROSSHAIR_H >> 1);
tft.drawRect(x1, y1, CROSSHAIR_W, CROSSHAIR_H, clr);
tft.drawLine(x1, y1, x2, y2, clr);
tft.drawLine(x1, y2, x2, y1, clr);
}
void to_display_mode() {
pinMode(XM, OUTPUT);
pinMode(XP, OUTPUT);
pinMode(YM, OUTPUT);
pinMode(YP, OUTPUT);
}
bool valid_touch() {
TSPoint p = ts.getPoint();
to_display_mode();
return (PRESSURE_LO <= p.z) && (p.z <= PRESSURE_HI);
}
void get_touch(uint16_t *xptr, uint16_t *yptr) {
TSPoint p;
while (1) {
p = ts.getPoint();
if ((PRESSURE_LO <= p.z) && (p.z <= PRESSURE_HI)) {
break;
}
}
*xptr = p.x;
*yptr = p.y;
to_display_mode();
}
void setup() {
uint16_t touch_coors[NUM_CROSSHAIRS][2];
uint16_t xbeginf = 0;
uint16_t xendf = 0;
uint16_t ybeginf = 0;
uint16_t yendf = 0;
uint16_t xbegin = 0;
uint16_t xend = 0;
uint16_t ybegin = 0;
uint16_t yend = 0;
Serial.begin(115200);
tft.begin(0x9486);
tft.fillScreen(BLACK);
for (int i = 0; i < NUM_CROSSHAIRS; ++i) {
uint16_t cx = crosshair_coors[i][0];
uint16_t cy = crosshair_coors[i][1];
uint32_t ax = 0;
uint32_t ay = 0;
uint16_t tx;
uint16_t ty;
for (uint16_t _ = 0; _ < CALIBRATION_TOUCH_CNT; ++_) {
draw_crosshair(cx, cy, RED);
get_touch(&tx, &ty);
ax += tx;
ay += ty;
draw_crosshair(cx, cy, GREEN);
delay(CALIBRATION_TOUCH_PERIOD_MS);
}
touch_coors[i][0] = ax / CALIBRATION_TOUCH_CNT;
touch_coors[i][1] = ay / CALIBRATION_TOUCH_CNT;
draw_crosshair(cx, cy, WHITE);
while (valid_touch());
delay(1000);
}
for (int i = 0; i < NUM_CROSSHAIRS; ++i) {
uint16_t cx = crosshair_coors[i][0];
uint16_t cy = crosshair_coors[i][1];
uint16_t tx = touch_coors[i][0];
uint16_t ty = touch_coors[i][1];
if (cx == (CROSSHAIR_W >> 1)) {
xbeginf += tx;
}
else if (cx == (DISPLAY_WIDTH - 1 - (CROSSHAIR_W >> 1))) {
xendf += tx;
}
if (cy == (CROSSHAIR_H >> 1)) {
ybeginf += ty;
}
else if (cy == (DISPLAY_HEIGHT - 1 - (CROSSHAIR_H >> 1))) {
yendf += ty;
}
}
xbeginf /= 2;
xendf /= 2;
ybeginf /= 2;
yendf /= 2;
xbegin = map(0, CROSSHAIR_LEFT, CROSSHAIR_RIGHT, xbeginf, xendf);
xend = map(DISPLAY_WIDTH - 1, CROSSHAIR_LEFT, CROSSHAIR_RIGHT, xbeginf, xendf);
ybegin = map(DISPLAY_HEIGHT - 1, CROSSHAIR_TOP, CROSSHAIR_BOTTOM, ybeginf, yendf);
yend = map(0, CROSSHAIR_TOP, CROSSHAIR_BOTTOM, ybeginf, yendf);
Serial.println("Paste this into constants.h");
Serial.println();
Serial.print("const int XBEGIN = "); Serial.println(xbegin);
Serial.print("const int XEND = "); Serial.println(xend);
Serial.print("const int YBEGIN = "); Serial.println(ybegin);
Serial.print("const int YEND = "); Serial.println(yend);
to_display_mode();
tft.setTextSize(2);
tft.setTextColor(WHITE);
tft.setCursor(0, 100);
tft.print(" const int XBEGIN = ");
tft.println(xbegin);
tft.print(" const int XEND = ");
tft.println(xend);
tft.print(" const int YBEGIN = ");
tft.println(ybegin);
tft.print(" const int YEND = ");
tft.println(yend);
}
void loop() {
}