SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
custom_callback_touch.ino
Go to the documentation of this file.
1
33#include <TouchDrv.hpp>
34#include <IoExpanderDrv.hpp>
35#include <SensorWireHelper.h>
36
37#ifndef TOUCH_SDA
38#define TOUCH_SDA 8
39#endif
40
41#ifndef TOUCH_SCL
42#define TOUCH_SCL 48
43#endif
44
45#ifndef TOUCH_IRQ
46#define TOUCH_IRQ 1
47#endif
48
49#ifndef TOUCH_RST
50#define TOUCH_RST 1
51#endif
52
53// Use the TouchDrvInterface base class for automatic discovery and use of multi-touch devices
55// T-RGB uses XL9555 as the reset control of the touch screen
57
58int16_t x[5], y[5];
59
60// Expander gpio 1
62
74bool i2c_wr_function(uint8_t addr, uint8_t reg, uint8_t *buf, size_t len, bool writeReg, bool isWrite)
75{
76 if (isWrite) {
77 Wire.beginTransmission(addr);
78 if (writeReg) {
79 Wire.write(reg);
80 }
81 if (buf && len > 0) {
82 Wire.write(buf, len);
83 }
84 return (Wire.endTransmission() == 0);
85 } else {
86 if (writeReg) {
87 Wire.beginTransmission(addr);
88 Wire.write(reg);
89 Wire.endTransmission();
90 }
91 Wire.requestFrom(addr, static_cast<uint8_t>(len));
92 for (size_t i = 0; i < len; ++i) {
93 if (Wire.available()) {
94 buf[i] = Wire.read();
95 } else {
96 return false;
97 }
98 }
99 return true;
100 }
101}
102
103
104uint32_t hal_callback(SensorCommCustomHal::Operation op, void *param1, void *param2)
105{
106 switch (op) {
107 // Set GPIO mode
109 uint8_t pin = reinterpret_cast<uintptr_t>(param1);
110 uint8_t mode = reinterpret_cast<uintptr_t>(param2);
111 pinMode(pin, mode);
112 }
113 break;
114 // Set GPIO level
116 uint8_t pin = reinterpret_cast<uintptr_t>(param1);
117 uint8_t level = reinterpret_cast<uintptr_t>(param2);
118 digitalWrite(pin, level);
119 }
120 break;
121 // Read GPIO level
123 uint8_t pin = reinterpret_cast<uintptr_t>(param1);
124 return digitalRead(pin);
125 }
126 break;
127 // Get the current running milliseconds
129 return millis();
130
131 // Delay in milliseconds
133 if (param1) {
134 uint32_t ms = reinterpret_cast<uintptr_t>(param1);
135 delay(ms);
136 }
137 }
138 break;
139 // Delay in microseconds
141 uint32_t us = reinterpret_cast<uintptr_t>(param1);
142 delayMicroseconds(us);
143 }
144 break;
145 default:
146 break;
147 }
148 return 0;
149}
150
151void TouchDrvDigitalWrite(uint8_t gpio, uint8_t level)
152{
153 if (gpio & 0x80) {
154 expander.digitalWrite(gpio & 0x7F, level);
155 } else {
156 digitalWrite(gpio, level);
157 }
158}
159
160uint8_t TouchDrvDigitalRead(uint8_t gpio)
161{
162 if (gpio & 0x80) {
163 return expander.digitalRead(gpio & 0x7F);
164 } else {
165 return digitalRead(gpio);
166 }
167}
168
169void TouchDrvPinMode(uint8_t gpio, uint8_t mode)
170{
171 if (gpio & 0x80) {
172 expander.pinMode(gpio & 0x7F, mode);
173 } else {
174 pinMode(gpio, mode);
175 }
176}
177
179{
180 // Add the highest bit to indicate that the GPIO expander is used, not the ESP's GPIO
181 const uint8_t touch_reset_pin = expander_io_tp_reset | 0x80;
182 const uint8_t touch_irq_pin = TOUCH_IRQ;
183 bool result = false;
184
185 touchDrv = new TouchDrvCSTXXX();
187 touchDrv->setPins(touch_reset_pin, touch_irq_pin);
188 result = touchDrv->begin(i2c_wr_function, hal_callback, CST816_SLAVE_ADDRESS);
189 if (result) {
190 const char *model = touchDrv->getModelName();
191 Serial.print("Successfully initialized "); Serial.print(model);
192 Serial.print(",using "); Serial.print(model); Serial.println(" Driver!");
193 return true;
194 }
195 delete touchDrv;
196
197 touchDrv = new TouchDrvGT911();
199 touchDrv->setPins(touch_reset_pin, touch_irq_pin);
200 result = touchDrv->begin(i2c_wr_function, hal_callback, GT911_SLAVE_ADDRESS_L);
201 if (result) {
202 const char *model = touchDrv->getModelName();
203 Serial.print("Successfully initialized "); Serial.print(model);
204 Serial.print(",using "); Serial.print(model); Serial.println(" Driver!");
205 return true;
206 }
207 delete touchDrv;
208
209 touchDrv = new TouchDrvFT6X36();
211 touchDrv->setPins(touch_reset_pin, touch_irq_pin);
212 result = touchDrv->begin(i2c_wr_function, hal_callback, FT3267_SLAVE_ADDRESS);
213 if (result) {
214 TouchDrvFT6X36 *tmp = static_cast<TouchDrvFT6X36 *>(touchDrv);
215 tmp->interruptPolling();
216 const char *model = touchDrv->getModelName();
217 Serial.print("Successfully initialized "); Serial.print(model);
218 Serial.print(",using "); Serial.print(model); Serial.println(" Driver!");
219 return true;
220 }
221 delete touchDrv;
222
223 Serial.println("Unable to find touch device.");
224
225 touchDrv = NULL;
226
227 return false;
228}
229
230void setup()
231{
232 Serial.begin(115200);
233 while (!Serial);
234 Serial.println("Start!");
235
236#if (defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_STM32)) && !defined(ARDUINO_ARCH_MBED)
237 Wire.setSCL(TOUCH_SCL);
238 Wire.setSDA(TOUCH_SDA);
239 Wire.begin();
240#elif defined(ARDUINO_ARCH_NRF52)
241 Wire.setPins(TOUCH_SDA, TOUCH_SCL);
242 Wire.begin();
243#elif defined(ARDUINO_ARCH_ESP32)
244 Wire.begin(TOUCH_SDA, TOUCH_SCL);
245#else
246 Wire.begin();
247#endif
248
249 SensorWireHelper::dumpDevices(Wire);
250
251 // Use unspecified address to test the address discovery function.
252 // T-RGB XL9555 uses 0X20 device address
253 if (!expander.begin(i2c_wr_function, XL9555_UNKNOWN_ADDRESS)) {
254 Serial.println("Failed to find XL9555 - check your wiring!");
255 while (1) {
256 delay(1000);
257 }
258 }
259
260 if (!setupTouchDrv()) {
261 while (1) {
262 delay(3000);
263 }
264 }
265
266}
267
268void loop()
269{
270 if (touchDrv->isPressed()) {
271 TouchPoints touch_points = touchDrv->getTouchPoints();
272 if (touch_points.hasPoints()) {
273 for (int i = 0; i < touch_points.getPointCount(); ++i) {
274 const TouchPoint &point = touch_points.getPoint(i);
275 Serial.print("X[");
276 Serial.print(i);
277 Serial.print("]:");
278 Serial.print(point.x);
279 Serial.print(" ");
280 Serial.print(" Y[");
281 Serial.print(i);
282 Serial.print("]:");
283 Serial.print(point.y);
284 Serial.print(" ");
285 }
286 Serial.println();
287 }
288 }
289 delay(5);
290}
291
292
293
294
@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.
virtual bool isPressed(uint32_t filter_ms=0)
Check if any touch point is currently pressed.
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.
#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
uint32_t hal_callback(SensorCommCustomHal::Operation op, void *param1, void *param2)
int16_t y[5]
uint8_t TouchDrvDigitalRead(uint8_t gpio)
bool i2c_wr_function(uint8_t addr, uint8_t reg, uint8_t *buf, size_t len, bool writeReg, bool isWrite)
i2c_wr_function
#define TOUCH_IRQ
uint8_t expander_io_tp_reset
void loop()
IoExpanderXL9555 expander
uint8_t i
char buf[64]
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.