Repository navigation
Expand file tree
/
Copy pathtouch.cpp
More file actions
112 lines (98 loc) · 3.36 KB
/
Copy pathtouch.cpp
File metadata and controls
112 lines (98 loc) · 3.36 KB
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
#include "touch.h"
#include <Arduino.h>
#include <Wire.h>
#include "board.h"
#include "config.h"
static constexpr uint8_t ADDRESS = 0x15;
static constexpr uint8_t REG_FINGER_COUNT = 0x02; // followed by X high, X low, Y high, Y low
static constexpr uint8_t REG_DISABLE_AUTO_SLEEP = 0xFE;
static constexpr uint32_t POLL_EVERY_MS = 10;
static bool cancelled = false; // current press is ignored until the finger lifts
static uint32_t suppressUntilMs = 0;
static bool readPoint(bool *touching, int16_t *x, int16_t *y) {
Wire1.beginTransmission(ADDRESS);
Wire1.write(REG_FINGER_COUNT);
if (Wire1.endTransmission(false) != 0) return false;
uint8_t data[5];
if (Wire1.requestFrom(static_cast<uint16_t>(ADDRESS), sizeof(data)) != sizeof(data)) return false;
for (uint8_t &value : data) value = Wire1.read();
*touching = data[0] != 0;
*x = ((data[1] & 0x0F) << 8) | data[2];
*y = ((data[3] & 0x0F) << 8) | data[4];
return true;
}
static TouchGesture swipe(int16_t dx, int16_t dy) {
if (abs(dx) >= abs(dy)) {
if (abs(dx) < TOUCH_SWIPE_MIN_PX) return TouchGesture::None;
return dx > 0 ? TouchGesture::SwipeRight : TouchGesture::SwipeLeft;
}
if (abs(dy) < TOUCH_SWIPE_MIN_PX) return TouchGesture::None;
return dy > 0 ? TouchGesture::SwipeDown : TouchGesture::SwipeUp;
}
bool touchBegin() {
Wire1.begin(pins::TOUCH_SDA, pins::TOUCH_SCL, 400000);
// The controller expects a pulse on INT, then a hardware reset.
pinMode(pins::TOUCH_INT, OUTPUT);
digitalWrite(pins::TOUCH_INT, HIGH);
delay(1);
digitalWrite(pins::TOUCH_INT, LOW);
delay(1);
pinMode(pins::TOUCH_INT, INPUT);
pinMode(pins::TOUCH_RST, OUTPUT);
digitalWrite(pins::TOUCH_RST, LOW);
delay(10);
digitalWrite(pins::TOUCH_RST, HIGH);
delay(300);
// Otherwise the chip sleeps after a few idle seconds and stops answering.
Wire1.beginTransmission(ADDRESS);
Wire1.write(REG_DISABLE_AUTO_SLEEP);
Wire1.write(0xFF);
return Wire1.endTransmission() == 0;
}
void touchSuppress() {
cancelled = true;
suppressUntilMs = millis() + TOUCH_IGNORE_AFTER_TURN_MS;
}
TouchUpdate touchPoll() {
static uint32_t lastPollMs = 0;
static bool down = false;
static bool moved = false;
static bool longFired = false;
static int16_t startX = 0, startY = 0, lastX = 0, lastY = 0;
static uint32_t downAtMs = 0;
TouchUpdate update = {TouchGesture::None, 0, 0};
const uint32_t now = millis();
if (now - lastPollMs < POLL_EVERY_MS) return update;
lastPollMs = now;
bool touching = false;
int16_t x = lastX, y = lastY;
if (!readPoint(&touching, &x, &y)) touching = false;
if (touching && !down) {
down = true;
moved = false;
longFired = false;
cancelled = static_cast<int32_t>(now - suppressUntilMs) < 0;
startX = lastX = x;
startY = lastY = y;
downAtMs = now;
} else if (touching && cancelled) {
lastX = x;
lastY = y;
} else if (touching) {
update.dx = x - lastX;
update.dy = y - lastY;
lastX = x;
lastY = y;
if (abs(x - startX) > TOUCH_TAP_SLOP_PX || abs(y - startY) > TOUCH_TAP_SLOP_PX) moved = true;
if (!moved && !longFired && now - downAtMs >= BUTTON_LONG_PRESS_MS) {
longFired = true;
update.gesture = TouchGesture::LongPress;
}
} else if (down) {
down = false;
if (!longFired && !cancelled) {
update.gesture = moved ? swipe(lastX - startX, lastY - startY) : TouchGesture::Tap;
}
}
return update;
}