3
4
5
6
7
8
9
10
11
12
13
14
15
17#include "TinyGPU/Emulation.h"
18#include "TinyGPU/Input/TouchDriverCommon.h"
23
24
25
26
27
28
29
30
31
32
36 uint16_t zThreshold = 400)
37 :
spi_(
spi), csPin_(csPin), irqPin_(irqPin), zThreshold_(zThreshold) {}
44 pinMode(csPin_, OUTPUT);
45 digitalWrite(csPin_, HIGH);
48 pinMode(irqPin_, INPUT_PULLUP);
56 return digitalRead(irqPin_) == LOW;
60
61
67 spi_.beginTransaction(SPISettings(kSpiClock, MSBFIRST, SPI_MODE0));
69 digitalWrite(csPin_, LOW);
72
73
74 const uint16_t z1 = transfer12(0xB1);
75 const uint16_t z2 = transfer12(0xC1);
78
79
80
81
82
83
84
86 static_cast<int32_t>(z1) + 4095 -
static_cast<int32_t>(z2);
88 if (z <
static_cast<int32_t>(zThreshold_)) {
94
95
96
97
98
99
100
101
102
103
104 (
void)transfer12(0xD0);
105 const uint16_t rawX = transfer12(0xD0);
107 (
void)transfer12(0x90);
108 const uint16_t rawY = transfer12(0x90);
113
114
115 const uint16_t pressure =
static_cast<uint16_t>(
clamp32(z
, 0
, 4095
));
118 static_cast<int16_t>(rawY), pressure);
128 static constexpr uint32_t kSpiClock = 2000000UL;
133 uint16_t zThreshold_;
135 uint16_t transfer12(uint8_t command) {
136 spi_.transfer(command);
138 const uint8_t high = spi_.transfer(0x00);
140 const uint8_t low = spi_.transfer(0x00);
142 return static_cast<uint16_t>(
143 ((
static_cast<uint16_t>(high) << 8) |
static_cast<uint16_t>(low)) >> 3);
146 void endTransaction() {
147 digitalWrite(csPin_, HIGH);
148 spi_.endTransaction();
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
184
185
186
187
188
191 : xp_(xPlusPin), xm_(xMinusPin), yp_(yPlusPin), ym_(yMinusPin) {}
194 if (xp_ < 0 || xm_ < 0 || yp_ < 0 || ym_ < 0) {
204
205
206
207
208
212 pinMode(xm_, OUTPUT);
213 digitalWrite(xm_, LOW);
215 pinMode(yp_, INPUT_PULLUP);
217 const bool touched = digitalRead(yp_) == LOW;
228 const int16_t rawX = readAxis(
true);
229 const int16_t rawY = readAxis(
false);
251
252
253
254
255
256
257
258
259 int16_t readAxis(
bool driveXAxis) {
260 const int8_t drivePlus = driveXAxis ? xp_ : yp_;
261 const int8_t driveMinus = driveXAxis ? xm_ : ym_;
262 const int8_t sensePin = driveXAxis ? yp_ : xp_;
263 const int8_t senseOtherPin = driveXAxis ? ym_ : xm_;
265 pinMode(senseOtherPin, INPUT);
267 pinMode(drivePlus, OUTPUT);
268 pinMode(driveMinus, OUTPUT);
269 digitalWrite(drivePlus, HIGH);
270 digitalWrite(driveMinus, LOW);
272 pinMode(sensePin, INPUT);
275
276
277
278
279 (
void)analogRead(sensePin);
280 return static_cast<int16_t>(analogRead(sensePin));
285
286
287
288
289
290
291
292
293
294
295
296
303 :
wire_(
wire), rstPin_(rstPin), irqPin_(irqPin) {}
307 pinMode(rstPin_, OUTPUT);
309 digitalWrite(rstPin_, LOW);
312 digitalWrite(rstPin_, HIGH);
317 pinMode(irqPin_, INPUT_PULLUP);
321
322
323
324
325 wire_.beginTransmission(I2C_ADDR);
327 return wire_.endTransmission() == 0;
331 if (irqPin_ >= 0 && digitalRead(irqPin_) == HIGH) {
337 if (!readRegisters(0x02, buffer,
sizeof(buffer))) {
341 const uint8_t touches = buffer[0] & 0x0F;
347 if (irqPin_ >= 0 && digitalRead(irqPin_) == HIGH) {
352
353
354
355
356
357
358
359
362 if (!readRegisters(0x02, buffer,
sizeof(buffer))) {
366 const uint8_t touches = buffer[0] & 0x0F;
372 const uint16_t rawX =
373 (
static_cast<uint16_t>(buffer[1] & 0x0F) << 8) | buffer[2];
375 const uint16_t rawY =
376 (
static_cast<uint16_t>(buffer[3] & 0x0F) << 8) | buffer[4];
379
380
381
383 static_cast<int16_t>(rawY), 255);
393 bool readRegisters(uint8_t startRegister, uint8_t* buffer, size_t length) {
394 if (buffer ==
nullptr || length == 0) {
398 wire_.beginTransmission(I2C_ADDR);
399 wire_.write(startRegister);
401 if (wire_.endTransmission(
false) != 0) {
405 const size_t received =
406 wire_.requestFrom(
static_cast<
int>(I2C_ADDR),
static_cast<
int>(length));
408 if (received != length) {
410
411
412 while (wire_.available()) {
419 for (size_t i = 0; i < length; ++i) {
420 if (!wire_.available()) {
424 buffer[i] =
static_cast<uint8_t>(wire_.read());
437
438
439
440
441
442
443
444
445
446
447
448
449
450
457 :
wire_(
wire), rstPin_(rstPin), irqPin_(irqPin) {}
461 pinMode(rstPin_, OUTPUT);
463 digitalWrite(rstPin_, LOW);
466 digitalWrite(rstPin_, HIGH);
471 pinMode(irqPin_, INPUT_PULLUP);
475
476
477 wire_.beginTransmission(I2C_ADDR);
479 return wire_.endTransmission() == 0;
483 if (irqPin_ >= 0 && digitalRead(irqPin_) == HIGH) {
487 uint8_t fingerNum = 0;
489 if (!readRegisters(0x02, &fingerNum, 1)) {
493 return fingerNum != 0;
497 if (irqPin_ >= 0 && digitalRead(irqPin_) == HIGH) {
502
503
504
505
506
507
508
509
510
513 if (!readRegisters(0x01, buffer,
sizeof(buffer))) {
517 const uint8_t fingerNum = buffer[1];
519 if (fingerNum == 0) {
523 const uint16_t rawX =
524 (
static_cast<uint16_t>(buffer[2] & 0x0F) << 8) | buffer[3];
526 const uint16_t rawY =
527 (
static_cast<uint16_t>(buffer[4] & 0x0F) << 8) | buffer[5];
530 static_cast<int16_t>(rawY), 255);
540 bool readRegisters(uint8_t startRegister, uint8_t* buffer, size_t length) {
541 if (buffer ==
nullptr || length == 0) {
545 wire_.beginTransmission(I2C_ADDR);
546 wire_.write(startRegister);
549
550
551
552 if (wire_.endTransmission(
false) != 0) {
556 const size_t received =
557 wire_.requestFrom(
static_cast<
int>(I2C_ADDR),
static_cast<
int>(length));
559 if (received != length) {
560 while (wire_.available()) {
567 for (size_t i = 0; i < length; ++i) {
568 if (!wire_.available()) {
572 buffer[i] =
static_cast<uint8_t>(wire_.read());
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
623 int8_t irqPin = -1, uint8_t i2cAddr =
I2C_ADDR_1)
624 :
wire_(
wire), rstPin_(rstPin), irqPin_(irqPin), addr_(i2cAddr) {}
629
630
631
632
633 pinMode(rstPin_, OUTPUT);
636 pinMode(irqPin_, OUTPUT);
637 digitalWrite(irqPin_, addr_ == I2C_ADDR_1 ? LOW : HIGH);
640 digitalWrite(rstPin_, LOW);
644 digitalWrite(irqPin_, addr_ == I2C_ADDR_1 ? LOW : HIGH);
648 digitalWrite(rstPin_, HIGH);
652 digitalWrite(irqPin_, LOW);
658 pinMode(irqPin_, INPUT_PULLUP);
663 wire_.beginTransmission(addr_);
664 if (wire_.endTransmission() != 0) {
669 uint8_t checksum = 0;
670 readRegister(kRegConfigChecksum, checksum);
671 writeRegister(kRegConfigChecksum, checksum);
672 writeRegister(kRegConfigFresh, 0x01);
680 readRegister(kRegStatus, status);
681 const bool bufferReady = (status & 0x80) != 0;
682 cachedTouches_ = status & 0x0F;
684 if (bufferReady && cachedTouches_ > 0) {
685 const uint8_t pointsToRead =
686 cachedTouches_ > 2 ? 2 : cachedTouches_;
688 for (uint8_t i = 0; i < pointsToRead; ++i) {
690 if (readRegisters(kRegPoint0 + i * 7, buf,
sizeof(buf))) {
691 const uint16_t rawX = (
static_cast<uint16_t>(buf[2]) << 8) | buf[1];
692 const uint16_t rawY = (
static_cast<uint16_t>(buf[4]) << 8) | buf[3];
694 static_cast<int16_t>(rawY), 255);
699 writeRegister(kRegStatus, 0x00);
700 return cachedTouches_ > 0;
704 if (cachedTouches_ == 0)
return false;
705 outPoint = cachedPoints_[0];
710 if (cachedTouches_ < 2)
return false;
711 outPoint = cachedPoints_[1];
716 static constexpr uint16_t kRegConfigChecksum = 0x80FF;
717 static constexpr uint16_t kRegConfigFresh = 0x8100;
718 static constexpr uint16_t kRegStatus = 0x814E;
719 static constexpr uint16_t kRegPoint0 = 0x814F;
725 uint8_t cachedTouches_ = 0;
726 Point cachedPoints_[2];
728 bool readRegister(uint16_t reg, uint8_t& value) {
729 return readRegisters(reg, &value, 1);
732 bool readRegisters(uint16_t startRegister, uint8_t* buffer, size_t length) {
733 if (buffer ==
nullptr || length == 0) {
737 wire_.beginTransmission(addr_);
738 wire_.write(
static_cast<uint8_t>(startRegister >> 8));
739 wire_.write(
static_cast<uint8_t>(startRegister & 0xFF));
741 if (wire_.endTransmission(
false) != 0) {
745 const size_t received =
746 wire_.requestFrom(
static_cast<
int>(addr_),
static_cast<
int>(length));
748 if (received != length) {
749 while (wire_.available()) {
755 for (size_t i = 0; i < length; ++i) {
756 if (!wire_.available()) {
759 buffer[i] =
static_cast<uint8_t>(wire_.read());
765 void writeRegister(uint16_t reg, uint8_t value) {
766 wire_.beginTransmission(addr_);
767 wire_.write(
static_cast<uint8_t>(reg >> 8));
768 wire_.write(
static_cast<uint8_t>(reg & 0xFF));
770 wire_.endTransmission();
Bit-banged 4-wire resistive touch panel driver.
Definition: TouchDriverArduino.h:181
bool isTouched() override
Return whether the panel currently appears touched.
Definition: TouchDriverArduino.h:202
bool getPoint(Point &outPoint) override
Read the current touch point.
Definition: TouchDriverArduino.h:223
TouchDriverBitBang(int8_t xPlusPin, int8_t xMinusPin, int8_t yPlusPin, int8_t yMinusPin)
Definition: TouchDriverArduino.h:189
bool begin() override
Initialize the controller.
Definition: TouchDriverArduino.h:193
CST816S capacitive touch controller.
Definition: TouchDriverArduino.h:451
bool isTouched() override
Return whether the panel currently appears touched.
Definition: TouchDriverArduino.h:482
bool getPoint(Point &outPoint) override
Read the current touch point.
Definition: TouchDriverArduino.h:496
bool begin() override
Initialize the controller.
Definition: TouchDriverArduino.h:459
TouchDriverCST816S(TwoWire &wire=Wire, int8_t rstPin=-1, int8_t irqPin=-1)
Definition: TouchDriverArduino.h:455
static constexpr uint8_t I2C_ADDR
Definition: TouchDriverArduino.h:453
FT6236 / FT6336 capacitive touch controller.
Definition: TouchDriverArduino.h:297
bool isTouched() override
Return whether the panel currently appears touched.
Definition: TouchDriverArduino.h:330
bool getPoint(Point &outPoint) override
Read the current touch point.
Definition: TouchDriverArduino.h:346
TouchDriverFT6236(TwoWire &wire=Wire, int8_t rstPin=-1, int8_t irqPin=-1)
Definition: TouchDriverArduino.h:301
bool begin() override
Initialize the controller.
Definition: TouchDriverArduino.h:305
static constexpr uint8_t I2C_ADDR
Definition: TouchDriverArduino.h:299
GT911 capacitive touch controller.
Definition: TouchDriverArduino.h:617
bool isTouched() override
Return whether the panel currently appears touched.
Definition: TouchDriverArduino.h:678
static constexpr uint8_t I2C_ADDR_2
Definition: TouchDriverArduino.h:620
bool getPoint(Point &outPoint) override
Read the current touch point.
Definition: TouchDriverArduino.h:703
static constexpr uint8_t I2C_ADDR_1
Definition: TouchDriverArduino.h:619
TouchDriverGT911(TwoWire &wire=Wire, int8_t rstPin=-1, int8_t irqPin=-1, uint8_t i2cAddr=I2C_ADDR_1)
Definition: TouchDriverArduino.h:622
bool begin() override
Initialize the controller.
Definition: TouchDriverArduino.h:626
bool getSecondPoint(Point &outPoint) override
Read a second, simultaneous touch point, for multi-touch gestures (pinch/rotate).
Definition: TouchDriverArduino.h:709
XPT2046 resistive touch controller.
Definition: TouchDriverArduino.h:33
bool isTouched() override
Return whether the panel currently appears touched.
Definition: TouchDriverArduino.h:54
TouchDriverXPT2046(SPIClass &spi, int8_t csPin, int8_t irqPin=-1, uint16_t zThreshold=400)
Definition: TouchDriverArduino.h:35
bool getPoint(Point &outPoint) override
Read the current touch point.
Definition: TouchDriverArduino.h:66
uint16_t getZThreshold() const
Definition: TouchDriverArduino.h:125
bool begin() override
Initialize the controller.
Definition: TouchDriverArduino.h:39
void setZThreshold(uint16_t threshold)
Definition: TouchDriverArduino.h:123
Base class for touch controllers.
Definition: TouchDriverCommon.h:72
Point mapCoordinates(int16_t rawX, int16_t rawY, uint16_t pressure=255) const
Convert raw controller coordinates into logical display space.
Definition: TouchDriverCommon.h:208
static int32_t clamp32(int32_t value, int32_t minimum, int32_t maximum)
Clamp an integer to an inclusive range.
Definition: TouchDriverCommon.h:159
Definition: DSIBusESP32.h:19
void delay(unsigned long ms)
Definition: EmulationDesktop.h:52
Touch point.
Definition: TouchDriverCommon.h:32