-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathM5Touch.cpp
156 lines (130 loc) · 3.9 KB
/
M5Touch.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
#include <M5Tough.h>
// M5Touch class
/* static */ M5Touch* M5Touch::instance;
M5Touch::M5Touch() {
if (!instance) instance = this;
}
void M5Touch::begin() {
Wire1.begin(21, 22);
pinMode(CST_INT, INPUT);
Wire1.beginTransmission((uint8_t)CST_DEVICE_ADDR);
Wire1.write(0x5A); /// (INT mode change)
Wire1.write(0x5A);
Wire1.endTransmission();
}
bool M5Touch::ispressed()
{
return (digitalRead(CST_INT) == LOW);
}
// Single register read and write
uint8_t M5Touch::readRegister(uint8_t reg) {
Wire1.beginTransmission((uint8_t)CST_DEVICE_ADDR);
Wire1.write(reg);
Wire1.endTransmission();
Wire1.requestFrom((uint8_t)CST_DEVICE_ADDR, uint8_t(1));
return Wire1.read();
}
void M5Touch::writeRegister(uint8_t reg, uint8_t value) {
Wire1.beginTransmission(CST_DEVICE_ADDR);
Wire1.write(reg);
Wire1.write((uint8_t)value);
Wire1.endTransmission();
}
// Reading size bytes into data
void M5Touch::readRegisters(uint8_t reg, uint8_t size, uint8_t* data) {
Wire1.beginTransmission((uint8_t)CST_DEVICE_ADDR);
Wire1.write(reg);
Wire1.endTransmission();
Wire1.requestFrom((uint8_t)CST_DEVICE_ADDR, size);
for (uint8_t i = 0; i < size; i++) data[i] = Wire1.read();
}
// This is normally called from update()
bool M5Touch::read() {
// true if real read, not a "come back later"
wasRead = false;
// true is something actually changed on the touchpad
changed = false;
// Return immediately if read() is called more frequently than the
// touch sensor updates. This prevents unnecessary I2C reads, and the
// data can also get corrupted if reads are too close together.
if (millis() - _lastRead < _interval) return false;
_lastRead = millis();
Point p[2];
uint8_t pts = 0;
uint8_t p0f = 0;
if (ispressed())
{
uint8_t data[11];
readRegisters(0x02, 11, data);
pts = data[0];
if (pts > 2) return false;
if (pts) {
// Read the data. Never mind trying to read the "weight" and
// "size" properties or using the built-in gestures: they
// are always set to zero.
p0f = (data[3] >> 4) ? 1 : 0;
p[0].x = ((data[1] << 8) | data[2]) & 0x0fff;
p[0].y = ((data[3] << 8) | data[4]) & 0x0fff;
if (p[0].x >= TOUCH_W || p[0].y >= TOUCH_H) return false;
if (pts == 2) {
p[1].x = ((data[7] << 8) | data[8]) & 0x0fff;
p[1].y = ((data[9] << 8) | data[10]) & 0x0fff;
if (p[1].x >= TOUCH_W || p[1].y >= TOUCH_H) return false;
}
}
}
#ifdef TFT
p[0].rotate(TFT->rotation);
p[1].rotate(TFT->rotation);
#endif /* TFT */
if (p[0] != point[0] || p[1] != point[1]) {
changed = true;
point[0] = p[0];
point[1] = p[1];
points = pts;
point0finger = p0f;
}
wasRead = true;
return true;
}
Point M5Touch::getPressPoint() {
read();
if (point[0]) return point[0];
return Point(-1, -1); // -1, -1 is old API's definition of invalid
}
void M5Touch::update() { read(); }
void M5Touch::dump() {
uint8_t data[256] = {0};
readRegisters(0x00, 0x80, data);
readRegisters(0x80, 0x80, data + 0x80);
Serial.printf("\n ");
for (uint8_t i = 0; i < 16; i++) Serial.printf(".%1X ", i);
Serial.printf("\n");
for (uint16_t i = 0; i < 0x100; i++) {
if (!(i % 16)) Serial.printf("\n%1X. ", i / 16);
Serial.printf("%02X ", data[i]);
}
Serial.printf("\n\n\n");
}
// HotZone class (for compatibility with older M5Tough code)
HotZone::HotZone(int16_t x0_, int16_t y0_, int16_t x1_, int16_t y1_,
void (*fun_)() /* = nullptr */
)
: Zone(x0_, y0_, x1_ - x0_, y1_ - y0_) {
fun = fun_;
}
void HotZone::setZone(int16_t x0_, int16_t y0_, int16_t x1_, int16_t y1_,
void (*fun_)() /*= nullptr */
) {
set(x0_, y0_, x1_ - x0_, y1_ - y0_);
fun = fun_;
}
bool HotZone::inHotZone(Point& p) { return contains(p); }
bool HotZone::inHotZoneDoFun(Point& p) {
if (contains(p)) {
if (fun) fun();
return true;
} else {
return false;
}
}