SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
touch_interface.ino
Go to the documentation of this file.
1
31#include <TouchDrv.hpp>
32#include <IoExpanderDrv.hpp>
33#include <SensorWireHelper.h>
34
35#ifndef TOUCH_SDA
36#define TOUCH_SDA 8
37#endif
38
39#ifndef TOUCH_SCL
40#define TOUCH_SCL 48
41#endif
42
43#ifndef TOUCH_IRQ
44#define TOUCH_IRQ 1
45#endif
46
47#ifndef TOUCH_RST
48#define TOUCH_RST 1
49#endif
50
51// Use the TouchDrvInterface base class for automatic discovery and use of multi-touch devices
53// T-RGB uses XL9555 as the reset control of the touch screen
55
56int16_t x[5], y[5];
57
58// Expander gpio 1
60
61void TouchDrvDigitalWrite(uint8_t gpio, uint8_t level)
62{
63 if (gpio & 0x80) {
64 expander.digitalWrite(gpio & 0x7F, level);
65 } else {
66 digitalWrite(gpio, level);
67 }
68}
69
70uint8_t TouchDrvDigitalRead(uint8_t gpio)
71{
72 if (gpio & 0x80) {
73 return expander.digitalRead(gpio & 0x7F);
74 } else {
75 return digitalRead(gpio);
76 }
77}
78
79void TouchDrvPinMode(uint8_t gpio, uint8_t mode)
80{
81 if (gpio & 0x80) {
82 expander.pinMode(gpio & 0x7F, mode);
83 } else {
84 pinMode(gpio, mode);
85 }
86}
87
89{
90 // Add the highest bit to indicate that the GPIO expander is used, not the ESP's GPIO
91 const uint8_t touch_reset_pin = expander_io_tp_reset | 0x80;
92 const uint8_t touch_irq_pin = TOUCH_IRQ;
93 bool result = false;
94
97 touchDrv->setPins(touch_reset_pin, touch_irq_pin);
98 result = touchDrv->begin(Wire, CST816_SLAVE_ADDRESS, TOUCH_SDA, TOUCH_SCL);
99 if (result) {
100 const char *model = touchDrv->getModelName();
101 Serial.print("Successfully initialized "); Serial.print(model);
102 Serial.print(",using "); Serial.print(model); Serial.println(" Driver!");
103 return true;
104 }
105 delete touchDrv;
106
107 touchDrv = new TouchDrvGT911();
109 touchDrv->setPins(touch_reset_pin, touch_irq_pin);
110 result = touchDrv->begin(Wire, GT911_SLAVE_ADDRESS_L, TOUCH_SDA, TOUCH_SCL);
111 if (result) {
112 const char *model = touchDrv->getModelName();
113 Serial.print("Successfully initialized "); Serial.print(model);
114 Serial.print(",using "); Serial.print(model); Serial.println(" Driver!");
115 return true;
116 }
117 delete touchDrv;
118
119 touchDrv = new TouchDrvFT6X36();
121 touchDrv->setPins(touch_reset_pin, touch_irq_pin);
122 result = touchDrv->begin(Wire, FT3267_SLAVE_ADDRESS, TOUCH_SDA, TOUCH_SCL);
123 if (result) {
124 TouchDrvFT6X36 *tmp = static_cast<TouchDrvFT6X36 *>(touchDrv);
125 tmp->interruptPolling();
126 const char *model = touchDrv->getModelName();
127 Serial.print("Successfully initialized "); Serial.print(model);
128 Serial.print(",using "); Serial.print(model); Serial.println(" Driver!");
129 return true;
130 }
131 delete touchDrv;
132
133 Serial.println("Unable to find touch device.");
134
135 touchDrv = NULL;
136
137 return false;
138}
139
140void setup()
141{
142 Serial.begin(115200);
143 while (!Serial);
144 Serial.println("Start!");
145
146#if (defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_STM32)) && !defined(ARDUINO_ARCH_MBED)
147 Wire.setSCL(TOUCH_SCL);
148 Wire.setSDA(TOUCH_SDA);
149 Wire.begin();
150#elif defined(ARDUINO_ARCH_NRF52)
151 Wire.setPins(TOUCH_SDA, TOUCH_SCL);
152 Wire.begin();
153#elif defined(ARDUINO_ARCH_ESP32)
154 Wire.begin(TOUCH_SDA, TOUCH_SCL);
155#else
156 Wire.begin();
157#endif
158
159 SensorWireHelper::dumpDevices(Wire);
160
161 // Use unspecified address to test the address discovery function.
162 // T-RGB XL9555 uses 0X20 device address
163 if (!expander.begin(Wire, XL9555_UNKNOWN_ADDRESS, TOUCH_SDA, TOUCH_SCL)) {
164 Serial.println("Failed to find XL9555 - check your wiring!");
165 while (1) {
166 delay(1000);
167 }
168 }
169
170 if (!setupTouchDrv()) {
171 while (1) {
172 delay(3000);
173 }
174 }
175
176}
177
178void loop()
179{
180 TouchPoints touch_points = touchDrv->getTouchPoints();
181 if (touch_points.hasPoints()) {
182 for (int i = 0; i < touch_points.getPointCount(); ++i) {
183
184 const TouchPoint &point = touch_points.getPoint(i);
185
186 Serial.print("X[");
187 Serial.print(i);
188 Serial.print("]:");
189 Serial.print(point.x);
190 Serial.print(" ");
191 Serial.print(" Y[");
192 Serial.print(i);
193 Serial.print("]:");
194 Serial.print(point.y);
195 Serial.print(" ");
196 }
197 Serial.println();
198 }
199 delay(5);
200}
201
202
203
204
@license MIT License
@license MIT License
@license MIT License
uint8_t level
bool begin(SensorCommCustom::CustomCallback callback, uint8_t addr)
Initialize the sensor using custom callback interface.
bool digitalRead(uint8_t pin)
Read the digital value from an input pin.
void pinMode(uint8_t pin, uint8_t mode)
Set the direction of a GPIO pin.
void digitalWrite(uint8_t pin, bool value)
Write a digital value to an output pin.
Concrete driver for the XL9555 16‑bit I2C GPIO expander.
void interruptPolling(void)
Polling mode for touch interrupt.
Abstract base class for touch screen drivers.
virtual void setGpioCallback(SensorHalCustom::CustomMode mode_cb, SensorHalCustom::CustomWrite write_cb, SensorHalCustom::CustomRead read_cb)
Set custom GPIO callbacks for pin control (e.g., reset, interrupt).
virtual const TouchPoints & getTouchPoints()=0
Get the current touch points (pure virtual).
virtual const char * getModelName()=0
Get the human-readable model name of the touch controller.
virtual bool begin(SensorCommCustom::CustomCallback callback, SensorCommCustomHal::CustomHalCallback hal_callback, uint8_t addr)
Initialize the touch driver with custom communication callbacks.
virtual void setPins(int rst, int irq)
Set the hardware reset and interrupt pin numbers.
Container for touch point data and optional gesture information.
uint8_t getPointCount() const
Returns the number of stored touch points.
TouchPoint & getPoint(uint8_t index)
Gets a reference to a touch point at the specified index.
bool hasPoints() const
Checks whether any touch points are stored.
uint8_t i
Represents a single touch point with its properties.
uint16_t x
X coordinate of the touch point.
uint16_t y
Y coordinate of the touch point.
#define TOUCH_SDA
bool setupTouchDrv()
void TouchDrvPinMode(uint8_t gpio, uint8_t mode)
void setup()
#define TOUCH_SCL
void TouchDrvDigitalWrite(uint8_t gpio, uint8_t level)
int16_t x[5]
TouchDrvInterface * touchDrv
int16_t y[5]
uint8_t TouchDrvDigitalRead(uint8_t gpio)
#define TOUCH_IRQ
uint8_t expander_io_tp_reset
void loop()
IoExpanderXL9555 expander