-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
111 lines (78 loc) · 2.02 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
#include "Arduino.h"
#include "Adafruit_GFX.h"
#include "MCUFRIEND_kbv.h"
#include "TouchScreen.h"
#include "SPI.h"
#include "SD.h"
#include "constants.h"
MCUFRIEND_kbv tft;
TouchScreen ts(XP, YP, XM, YM, 300);
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 convert(uint16_t *x, uint16_t *y) {
*x = constrain(*x, XBEGIN, XEND);
*y = constrain(*y, YBEGIN, YEND);
*x = map(*x, XBEGIN, XEND, 0, 319);
*y = map(*y, YBEGIN, YEND, 0, 479);
}
void setup() {
Serial.begin(9600);
tft.begin(0x9486);
tft.fillScreen(BLACK);
if (!SD.begin(10)) {
Serial.println("SD Card initialization failed");
while (1);
}
if (SD.exists("LOGS.TXT")) {
Serial.println("Clearing previous logs");
SD.remove("LOGS.TXT");
}
tft.fillRect(0, 0, 16, 16, GREEN);
}
void loop() {
uint16_t tx, ty;
if (valid_touch()) { // keep checking for a valid touch
// read the coordinates and convert them to pixel-coordinates
get_touch(&tx, &ty);
convert(&tx, &ty);
// open the file (report the error if it could not be opened)
// and paint the square red
File logs = SD.open("LOGS.TXT", FILE_WRITE);
if (!logs) {
Serial.println("Error opening/creating logs file");
while (1);
}
tft.fillRect(0, 0, 16, 16, RED);
logs.print(millis()); // timestamp
logs.print(',');
logs.print(tx); // x-coordinate
logs.print(',');
logs.println(ty); // y-coordinate
// close the file, paint the square green and
// wait for a second before reading the screen again
logs.close();
tft.fillRect(0, 0, 16, 16, GREEN);
delay(1000);
}
}