TinyGPU
Loading...
Searching...
No Matches
TouchDriverTFTeSPI.h
Go to the documentation of this file.
1#pragma once
2#include <TFT_eSPI.h> // https://github.com/Bodmer/TFT_eSPI
3
4#include "TinyGPU/Input/TouchDriverCommon.h"
5
6namespace tinygpu {
7
8/**
9 * @brief Touch driver that reads through Bodmer's TFT_eSPI
10 * (https://github.com/Bodmer/TFT_eSPI) resistive touch support
11 * (TFT_eSPI::getTouch()) instead of one of this library's own touch
12 * controller drivers (TouchDriverXPT2046, ...) - the companion to
13 * DisplayDriverTFTeSPI, for a panel already wired up and calibrated
14 * through TFT_eSPI.
15 *
16 * TFT_eSPI is a separate, optional Arduino library - install it alongside
17 * TinyGPU and `#include <TinyGPU/Input/TouchDriverTFTeSPI.h>` explicitly
18 * (this header is not pulled in by TinyGPU/Input/TouchDriver.h or
19 * TinyGPU.h) to opt in.
20 *
21 * TFT_eSPI::getTouch() already returns calibrated, rotation-corrected
22 * screen-pixel coordinates (matching the display's current
23 * TFT_eSPI::setRotation()), so - like TouchDriverSDL - this driver has no
24 * use for TouchDriver's own CalibrationData/mapCoordinates() raw-ADC
25 * pipeline; leave setCalibration() unset. Calibration itself is entirely
26 * TFT_eSPI's job: call tft.setTouch(calData) with data from
27 * tft.calibrateTouch(...) (or a hardcoded panel-specific array) before
28 * using this driver, the same "driver does not own bus/calibration
29 * setup, the application does" division of responsibility
30 * TouchDriverArduino.h's drivers use for their own SPI/I2C buses.
31 *
32 * Like every TouchDriver, isTouched() and getPoint() do not implicitly
33 * loop or block. isTouched() is where the actual TFT_eSPI::getTouch()
34 * read happens (and its result cached), so - as with this library's other
35 * touch drivers - call it exactly once per loop() iteration.
36 */
38 public:
39 /// Wraps an existing TFT_eSPI instance (ownership not taken - construct
40 /// and keep it alive for as long as this driver is used, the same way
41 /// DisplayDriverTFTeSPI takes its TFT_eSPI& rather than owning one).
42 /// `threshold` is forwarded to TFT_eSPI::getTouch() as its pressure
43 /// threshold (raw ADC units - see TFT_eSPI's own documentation for what
44 /// your panel's controller considers a real touch).
45 explicit TouchDriverTFTeSPI(TFT_eSPI& tft, uint16_t threshold = 600)
46 : tft_(tft), threshold_(threshold) {}
47
48 /// No-op: TFT_eSPI itself is initialized (and touch-calibrated) by the
49 /// application before constructing this driver - see class comment.
50 bool begin() override { return true; }
51
52 bool isTouched() override {
53 uint16_t x = 0;
54 uint16_t y = 0;
55 down_ = tft_.getTouch(&x, &y, threshold_) != 0;
56 if (down_) {
57 lastX_ = static_cast<int16_t>(x);
58 lastY_ = static_cast<int16_t>(y);
59 }
60 return down_;
61 }
62
63 bool getPoint(Point& outPoint) override {
64 if (!down_) return false;
65 outPoint = mapCoordinates(lastX_, lastY_, 255);
66 return true;
67 }
68
69 private:
70 TFT_eSPI& tft_;
71 uint16_t threshold_;
72 int16_t lastX_ = 0;
73 int16_t lastY_ = 0;
74 bool down_ = false;
75};
76
77} // namespace tinygpu
Touch driver that reads through Bodmer's TFT_eSPI (https://github.com/Bodmer/TFT_eSPI) resistive touc...
Definition: TouchDriverTFTeSPI.h:37
bool isTouched() override
Return whether the panel currently appears touched.
Definition: TouchDriverTFTeSPI.h:52
bool getPoint(Point &outPoint) override
Read the current touch point.
Definition: TouchDriverTFTeSPI.h:63
bool begin() override
Definition: TouchDriverTFTeSPI.h:50
TouchDriverTFTeSPI(TFT_eSPI &tft, uint16_t threshold=600)
Definition: TouchDriverTFTeSPI.h:45
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
Definition: DSIBusESP32.h:19
Touch point.
Definition: TouchDriverCommon.h:32