SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
cst3530_get_point.ino
Go to the documentation of this file.
1
30#include <TouchDrv.hpp>
31#include <SensorWireHelper.h>
32
33#ifndef TOUCH_SDA
34#define TOUCH_SDA 13
35#endif
36
37#ifndef TOUCH_SCL
38#define TOUCH_SCL 14
39#endif
40
41#ifndef TOUCH_IRQ
42#define TOUCH_IRQ 12
43#endif
44
45#ifndef TOUCH_RST
46#define TOUCH_RST 38
47#endif
48
50volatile bool isPressed = false;
51
52void setup()
53{
54 Serial.begin(115200);
55 while (!Serial);
56
57#if TOUCH_RST != -1
58 pinMode(TOUCH_RST, OUTPUT);
59 digitalWrite(TOUCH_RST, LOW);
60 delay(30);
61 digitalWrite(TOUCH_RST, HIGH);
62 delay(50);
63#endif
64
65#if (defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_STM32)) && !defined(ARDUINO_ARCH_MBED)
66 Wire.setSCL(TOUCH_SCL);
67 Wire.setSDA(TOUCH_SDA);
68 Wire.begin();
69#elif defined(ARDUINO_ARCH_NRF52)
70 Wire.setPins(TOUCH_SDA, TOUCH_SCL);
71 Wire.begin();
72#elif defined(ARDUINO_ARCH_ESP32)
73 Wire.begin(TOUCH_SDA, TOUCH_SCL);
74#else
75 Wire.begin();
76#endif
77
78 // Scan I2C devices
79 SensorWireHelper::dumpDevices(Wire);
80
81
82 uint8_t touchAddress = 0x1A; // Other device addresses are determined by the touch firmware and are generally 0X5A by default.
83 // uint8_t touchAddress = 0x5A; // The device address is determined according to the actual situation. Not all device addresses are 0X5A. There can also be other customized device addresses.
84
86 bool result = touch.begin(Wire, touchAddress, TOUCH_SDA, TOUCH_SCL);
87 if (result == false) {
88 Serial.println("touch is not online...");
89 while (1)delay(1000);
90 }
91 Serial.print("Model :"); Serial.println(touch.getModelName());
92
93
94 // Serial.println("Enter touch sleep mode.");
95 // touch.sleep();
96
97 // int i = 10;
98 // while (i--) {
99 // Serial.print("Wake up after ");
100 // Serial.print(i);
101 // Serial.println(" seconds");
102 // delay(1000);
103 // }
104
105 // Serial.println("Wakeup touch");
106 // touch.wakeup();
107
108 // Set touch max xy
109 // touch.setMaxCoordinates(240, 296);
110
111 // Set swap xy
112 // touch.setSwapXY(true);
113
114 // Set mirror xy
115 // touch.setMirrorXY(true, true);
116
117 //Register touch plane interrupt pin
118 attachInterrupt(TOUCH_IRQ, +[]() {
119 isPressed = true;
120 }, FALLING);
121
122 Serial.println("Touch Info:");
123 Serial.print("Model: "); Serial.println(touch.getModelName());
124 Serial.print("ID: 0x"); Serial.println(touch.getChipID(), HEX);
125 Serial.print("Max Touch Points: "); Serial.println(touch.getSupportTouchPoint());
126 uint16_t resX = touch.getResolutionX();
127 uint16_t resY = touch.getResolutionY();
128 if (resX == 0 || resY == 0) {
129 Serial.println("The touch driver not support get touch resolution,please use setResolution() to set touch resolution.");
130 // touch.setResolution(480, 320);
131 } else {
132 Serial.print("Resolution: "); Serial.print(resX); Serial.print(" x "); Serial.println(resY);
133 }
134 delay(3000);
135}
136
137void loop()
138{
139 if (isPressed) {
140 isPressed = false;
141 TouchPoints touch_points = touch.getTouchPoints();
142 if (touch_points.hasPoints()) {
143 for (int i = 0; i < touch_points.getPointCount(); ++i) {
144 const TouchPoint &point = touch_points.getPoint(i);
145 Serial.print("X[");
146 Serial.print(i);
147 Serial.print("]:");
148 Serial.print(point.x);
149 Serial.print(" ");
150 Serial.print(" Y[");
151 Serial.print(i);
152 Serial.print("]:");
153 Serial.print(point.y);
154 Serial.print(" ");
155 }
156 Serial.println();
157 }
158 }
159 delay(30);
160}
@license MIT License
@license MIT License
const char * getModelName() override
Get the model name.
const TouchPoints & getTouchPoints() override
Get the touch points.
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.
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 uint32_t getChipID()
Get the chip identifier (e.g., ID register value).
virtual uint16_t getResolutionY()
Get the raw maximum Y coordinate of the touch panel.
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
volatile bool isPressed
#define TOUCH_RST
void setup()
#define TOUCH_SCL
TouchDrvCST3530 touch
#define TOUCH_IRQ
void loop()
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.