-
Notifications
You must be signed in to change notification settings - Fork 223
/
TouchControllerWS.cpp
108 lines (90 loc) · 2.55 KB
/
TouchControllerWS.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
#include "TouchControllerWS.h"
TouchControllerWS::TouchControllerWS(XPT2046_Touchscreen *touchScreen) {
this->touchScreen = touchScreen;
}
bool TouchControllerWS::loadCalibration() {
// always use this to "mount" the filesystem
bool result = SPIFFS.begin();
Serial.print("SPIFFS opened: ");
Serial.println(result ? "OK" : "Failed");
// this opens the file in read-mode
File f = SPIFFS.open("/calibration.txt", "r");
if (!f) {
return false;
} else {
//Lets read line by line from the file
String dxStr = f.readStringUntil('\n');
String dyStr = f.readStringUntil('\n');
String axStr = f.readStringUntil('\n');
String ayStr = f.readStringUntil('\n');
dx = dxStr.toFloat();
dy = dyStr.toFloat();
ax = axStr.toInt();
ay = ayStr.toInt();
}
f.close();
return true;
}
bool TouchControllerWS::saveCalibration() {
bool result = SPIFFS.begin();
// open the file in write mode
File f = SPIFFS.open("/calibration.txt", "w");
if (!f) {
Serial.println("file creation failed");
return false;
}
// now write two lines in key/value style with end-of-line characters
f.println(dx);
f.println(dy);
f.println(ax);
f.println(ay);
f.close();
return true;
}
void TouchControllerWS::startCalibration(CalibrationCallback *calibrationCallback) {
state = 0;
this->calibrationCallback = calibrationCallback;
}
void TouchControllerWS::continueCalibration() {
TS_Point p = touchScreen->getPoint();
if (state == 0) {
(*calibrationCallback)(10, 10);
if (touchScreen->touched()) {
p1 = p;
state++;
lastStateChange = millis();
}
} else if (state == 1) {
(*calibrationCallback)(230, 310);
if (touchScreen->touched() && (millis() - lastStateChange > 1000)) {
p2 = p;
state++;
lastStateChange = millis();
dx = 240.0 / abs(p1.y - p2.y);
dy = 320.0 / abs(p1.x - p2.x);
ax = p1.y < p2.y ? p1.y : p2.y;
ay = p1.x < p2.x ? p1.x : p2.x;
}
}
}
bool TouchControllerWS::isCalibrationFinished() {
return state == 2;
}
bool TouchControllerWS::isTouched() {
return touchScreen->touched();
}
bool TouchControllerWS::isTouched(int16_t debounceMillis) {
if (touchScreen->touched() && millis() - lastTouched > debounceMillis) {
lastTouched = millis();
return true;
}
return false;
}
TS_Point TouchControllerWS::getPoint() {
TS_Point p = touchScreen->getPoint();
int x = (p.y - ax) * dx;
int y = 320 - (p.x - ay) * dy;
p.x = x;
p.y = y;
return p;
}