SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
touch/TouchDrvInterface.hpp
Go to the documentation of this file.
1
30#pragma once
31
32#include "../SensorBuildOpt.h"
33#if !SENSORLIB_EXCLUDE_TOUCH_COMMON
34
36#include "TouchPoints.hpp"
37
47{
48public:
49
50 struct TouchPinsCfg {
51 int rstPin; // Reset pin number, -1 if not used
52 int irqPin; // Interrupt pin number, -1 if not used
53 int rstActiveLevel; // Active level for reset pin (HIGH or LOW)
54 int irqTriggerLevel; // Trigger level for interrupt pin (HIGH or LOW)
55 int rstHoldTimeMs; // Minimum hold time for reset signal in milliseconds
56 int rstReleaseTimeMs; // Minimum release time after reset in milliseconds
57 };
58
59 struct TouchConfig {
60 bool swapXY; // Whether to swap X and Y coordinates
61 bool mirrorX; // Whether to mirror X coordinates
62 bool mirrorY; // Whether to mirror Y coordinates
63 bool scalingEnabled; // Whether scaling is enabled
64 uint16_t xMax; // Maximum X coordinate value from the hardware
65 uint16_t yMax; // Maximum Y coordinate value from the hardware
66 uint16_t resolutionX; // Physical resolution of the touch panel (used for scaling)
67 uint16_t resolutionY; // Physical resolution of the touch panel (used for scaling)
68 float scaleX; // Scaling factor for X coordinates
69 float scaleY; // Scaling factor for Y coordinates
70 };
71
76 using HomeButtonCallback = void(*)(void *user_data);
77
85 _halWriteCallback(nullptr), _halReadCallback(nullptr),
86 _chipID(0x00), _HButtonCallback(nullptr), _userData(nullptr), _maxTouchPoints(0),
88 _lastPulse(0), _pinsCfg({-1, -1, LOW, LOW, 10, 10}),
89 _touchConfig({false, false, false, false, 0, 0, 0, 0, 1.0f, 1.0f})
90 {
91 }
92
98 virtual ~TouchDrvInterface() = default;
99
100// *INDENT-OFF*
101#if defined(ARDUINO)
112 virtual bool begin(TwoWire &wire, uint8_t addr, int sda = -1, int scl = -1)
113 {
114 return I2CDeviceWithHal::begin(wire, addr, sda, scl);
115 }
116
117#elif defined(ESP_PLATFORM)
118
119#if defined(SENSORLIB_USE_I2C_LEGACY)
120
131 virtual bool begin(i2c_port_t port_num, uint8_t addr, int sda, int scl)
132 {
133 return I2CDeviceWithHal::begin(port_num, addr, sda, scl);
134 }
135
136#else
137
146 virtual bool begin(i2c_master_bus_handle_t handle, uint8_t addr)
147 {
148 return I2CDeviceWithHal::begin(handle, addr);
149 }
150
151#endif //SENSORLIB_USE_I2C_LEGACY
152#endif //ARDUINO
153
168 uint8_t addr)
169 {
170 return I2CDeviceWithHal::begin(callback, hal_callback, addr);
171 }
172
186 _halModeCallback = mode_cb;
187 _halWriteCallback = write_cb;
188 _halReadCallback = read_cb;
189 }
190
196 virtual uint8_t getSupportTouchPoint() {
197 return _maxTouchPoints;
198 }
199
206 virtual void getResolution(uint16_t &x, uint16_t &y) {
209 }
210
216 virtual uint16_t getResolutionX() {
218 }
219
225 virtual uint16_t getResolutionY() {
227 }
228
240 virtual bool initImpl(uint8_t param) = 0;
241
247 virtual void reset()
248 {
249 if (_pinsCfg.rstPin != -1) {
250 // Perform hardware reset using the reset pin
251 hal->digitalWrite(_pinsCfg.rstPin, _pinsCfg.rstActiveLevel);
252 hal->delay(_pinsCfg.rstHoldTimeMs);
253 hal->digitalWrite(_pinsCfg.rstPin, !_pinsCfg.rstActiveLevel);
255 }
256 // If no reset pin is available, derived classes can override this method to implement a software reset if supported.
257 }
258
265 virtual void sleep() = 0;
266
273 virtual void wakeup()
274 {
275 reset();
276 }
277
288 virtual uint8_t getPoint(int16_t *x_array, int16_t *y_array, uint8_t get_point = 1) __attribute__((deprecated("use getTouchPoints instead of getPoint")))
289 {
291
292 if (x_array == nullptr || y_array == nullptr) {
293 return data.getPointCount();
294 }
295 if (data.hasPoints()) {
296 uint8_t pointsToCopy = (get_point < data.getPointCount()) ? get_point : data.getPointCount();
297 for (int i = 0; i < pointsToCopy; i++) {
298 const TouchPoint &pt = data.getPoint(i);
299 x_array[i] = pt.x;
300 y_array[i] = pt.y;
301 }
302 }
303 return data.getPointCount();
304 }
305
312 virtual const TouchPoints& getTouchPoints() = 0;
313
323 virtual bool isPressed(uint32_t filter_ms = 0)
324 {
325 if (_pinsCfg.irqPin != -1) {
326 if (hal->digitalRead(_pinsCfg.irqPin) == _pinsCfg.irqTriggerLevel) {
327 if(filter_ms == 0){
328 return true;
329 }
330 uint32_t now = hal->millis();
331 if (now - _lastPulse > filter_ms) {
332 _lastPulse = now;
333 return true;
334 }
335 }
336 return false;
337 }
338 return getTouchPoints().hasPoints();
339 }
340
346 virtual const char *getModelName() = 0;
347
353 virtual uint32_t getChipID() {
354 return _chipID;
355 }
356
366 virtual void setPins(int rst, int irq) {
367 _pinsCfg.irqPin = irq;
368 _pinsCfg.rstPin = rst;
369 }
370
376 virtual const TouchPinsCfg& getTouchPinsCfg() const { return _pinsCfg; }
377
383 virtual void setSwapXY(bool swap) {
384 _touchConfig.swapXY = swap;
385 }
386
392 virtual bool isSwapXY() const { return _touchConfig.swapXY; }
393
403 virtual void setMirrorXY(bool mirrorX, bool mirrorY) {
404 _touchConfig.mirrorX = mirrorX;
405 _touchConfig.mirrorY = mirrorY;
406 }
407
413 virtual bool isMirrorX() const { return _touchConfig.mirrorX; }
414
420 virtual bool isMirrorY() const { return _touchConfig.mirrorY; }
421
432 virtual void setMaxCoordinates(uint16_t x, uint16_t y) {
435 }
436
443 virtual void getMaxCoordinates(uint16_t &x, uint16_t &y) {
446 }
447
453 virtual uint16_t getMaxX() { return _touchConfig.xMax; }
454
459 virtual uint16_t getMaxY() { return _touchConfig.yMax; }
460
466
476 virtual void setResolution(uint16_t width, uint16_t height) {
478 _touchConfig.resolutionY = height;
479 }
480
491 virtual void setTargetResolution(uint16_t width, uint16_t height) {
495 } else {
499 }
500 _touchConfig.xMax = width;
501 _touchConfig.yMax = height;
502 }
503
516 virtual void updateXY(TouchPoints &points) const
517 {
518 uint8_t count = points.getPointCount();
519 if (count == 0){
520 return;
521 }
522 for (uint8_t i = 0; i < count; ++i) {
523 TouchPoint &pt = points.getPoint(i);
524
525 // Swap XY
526 if (_touchConfig.swapXY) {
527 uint16_t tmp = pt.x;
528 pt.x = pt.y;
529 pt.y = tmp;
530 }
531
532 // Apply scaling if target resolution set
534 pt.x = static_cast<uint16_t>(pt.x * _touchConfig.scaleX + 0.5f);
535 pt.y = static_cast<uint16_t>(pt.y * _touchConfig.scaleY + 0.5f);
536 }
537
538 // Mirror X
540 pt.x = _touchConfig.xMax - pt.x;
541 }
542
543 // Mirror Y
545 pt.y = _touchConfig.yMax - pt.y;
546 }
547
548 // Clamp to display bounds
549 if(_touchConfig.xMax != 0) {
550 if (pt.x > _touchConfig.xMax) {
551 pt.x = _touchConfig.xMax;
552 }
553 }
554 if(_touchConfig.yMax != 0) {
555 if (pt.y > _touchConfig.yMax) {
556 pt.y = _touchConfig.yMax;
557 }
558 }
559 }
560 }
561
571 virtual void setHomeButtonCallback(HomeButtonCallback cb, void *user_data) {
572 _HButtonCallback = cb;
573 _userData = user_data;
574 }
575
586 virtual void setCenterButtonCoordinate(int16_t x, int16_t y) {
589 }
590
591// *INDENT-ON*
592
593protected:
594
598
599 uint32_t _chipID;
601 void *_userData;
605 uint32_t _lastPulse;
609
610private:
616 virtual void afterCommReady() override
617 {
618 if (_halModeCallback) {
619 hal->setCustomMode(_halModeCallback);
620 }
621 if (_halWriteCallback) {
622 hal->setCustomWrite(_halWriteCallback);
623 }
624 if (_halReadCallback) {
625 hal->setCustomRead(_halReadCallback);
626 }
627 initGpioPins();
628 }
629
636 virtual void initGpioPins()
637 {
638 if (_pinsCfg.rstPin != -1) {
639 hal->pinMode(_pinsCfg.rstPin, OUTPUT);
640 }
641 if (_pinsCfg.irqPin != -1) {
642 hal->pinMode(_pinsCfg.irqPin, INPUT);
643 }
644 reset();
645 }
646};
647
648#endif
@license MIT License
@license MIT License
std::unique_ptr< SensorHal > hal
bool begin(SensorCommCustom::CustomCallback cb, SensorCommCustomHal::CustomHalCallback hal_cb, uint8_t addr)
Initialize the sensor using custom callback interface.
uint32_t(*)(Operation op, void *param1, void *param2) CustomHalCallback
bool(*)(uint8_t addr, uint8_t reg, uint8_t *buf, size_t len, bool writeReg, bool isWrite) CustomCallback
void(*)(uint8_t pin, uint8_t mode) CustomMode
Set pin mode (INPUT/OUTPUT, etc.)
void(*)(uint8_t pin, uint8_t level) CustomWrite
Set pin level (0/1)
uint8_t(*)(uint8_t pin) CustomRead
Read pin level.
Abstract base class for touch screen drivers.
uint32_t _chipID
Chip identification value.
TouchConfig _touchConfig
Configuration for coordinate transformations.
virtual bool isSwapXY() const
Check if X and Y coordinates are swapped.
virtual bool isMirrorX() const
Check if X coordinates are mirrored.
virtual void getResolution(uint16_t &x, uint16_t &y)
Get the raw resolution (maximum coordinate values) of the touch panel.
virtual bool isScalingEnabled()
Check if coordinate scaling is enabled.
virtual uint16_t getMaxX()
Get the maximum X coordinate (e.g., display width).
virtual void setMaxCoordinates(uint16_t x, uint16_t y)
Set the maximum coordinate values used for mirroring.
void(*)(void *user_data) HomeButtonCallback
Callback type for home button events.
SensorHalCustom::CustomMode _halModeCallback
Custom GPIO mode callback.
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 void getMaxCoordinates(uint16_t &x, uint16_t &y)
Get the maximum coordinates used for mirroring.
virtual void setCenterButtonCoordinate(int16_t x, int16_t y)
Set the expected screen coordinates of a hardware home button.
virtual void reset()
Reset the touch controller.
void * _userData
User data for home button callback.
virtual const TouchPoints & getTouchPoints()=0
Get the current touch points (pure virtual).
virtual uint8_t getPoint(int16_t *x_array, int16_t *y_array, uint8_t get_point=1) __attribute__((deprecated("use getTouchPoints instead of getPoint")))
Retrieve touch point coordinates (deprecated).
virtual void updateXY(TouchPoints &points) const
Apply coordinate transformations (swap, scale, mirror) to all touch points.
virtual bool initImpl(uint8_t param)=0
Device-specific initialization implementation (pure virtual).
virtual uint16_t getMaxY()
Get the maximum Y coordinate (e.g., display height).
virtual const char * getModelName()=0
Get the human-readable model name of the touch controller.
virtual bool isMirrorY() const
Check if Y coordinates are mirrored.
TouchPinsCfg _pinsCfg
Configuration for reset and interrupt pins.
virtual bool begin(SensorCommCustom::CustomCallback callback, SensorCommCustomHal::CustomHalCallback hal_callback, uint8_t addr)
Initialize the touch driver with custom communication callbacks.
virtual uint16_t getResolutionX()
Get the raw maximum X coordinate of the touch panel.
int16_t _center_btn_y
Y coordinate of home button center.
virtual void setResolution(uint16_t width, uint16_t height)
Set the raw resolution of the touch panel (physical maximum coordinates).
virtual void setMirrorXY(bool mirrorX, bool mirrorY)
Enable or disable mirroring of X and/or Y coordinates.
virtual uint8_t getSupportTouchPoint()
Get the maximum number of touch points supported by the hardware.
virtual void setPins(int rst, int irq)
Set the hardware reset and interrupt pin numbers.
virtual const TouchPinsCfg & getTouchPinsCfg() const
Get the current touch pins configuration.
TouchDrvInterface()
Construct a new TouchDrvInterface object.
int16_t _center_btn_x
X coordinate of home button center.
SensorHalCustom::CustomWrite _halWriteCallback
Custom GPIO write callback.
virtual ~TouchDrvInterface()=default
Destroy the TouchDrvInterface object.
virtual uint32_t getChipID()
Get the chip identifier (e.g., ID register value).
virtual bool isPressed(uint32_t filter_ms=0)
Check if any touch point is currently pressed.
virtual void setHomeButtonCallback(HomeButtonCallback cb, void *user_data)
Register a callback for home button events.
virtual uint16_t getResolutionY()
Get the raw maximum Y coordinate of the touch panel.
virtual void sleep()=0
Put the touch controller into low-power sleep mode (pure virtual).
virtual void setTargetResolution(uint16_t width, uint16_t height)
Set the target display resolution to map touch coordinates to screen pixels.
TouchPoints _touchPoints
Touch points data.
SensorHalCustom::CustomRead _halReadCallback
Custom GPIO read callback.
uint8_t _maxTouchPoints
Maximum supported touch points.
virtual void wakeup()
Wake the touch controller from sleep mode (pure virtual).
HomeButtonCallback _HButtonCallback
Home button callback function.
uint32_t _lastPulse
Timestamp of the last touch event.
virtual void setSwapXY(bool swap)
Enable or disable swapping of X and Y coordinates.
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.
uint32_t hal_callback(SensorCommCustomHal::Operation op, void *param1, void *param2)
uint8_t i
__attribute__((weak)) void setupPower()
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.
int16_t x[5]
int16_t y[5]