SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
cstxxx_get_point.ino
Go to the documentation of this file.
1
30#include <TouchDrv.hpp>
31
32#ifndef TOUCH_SDA
33#define TOUCH_SDA 3
34#endif
35
36#ifndef TOUCH_SCL
37#define TOUCH_SCL 2
38#endif
39
40#ifndef TOUCH_IRQ
41#define TOUCH_IRQ 21
42#endif
43
44#ifndef TOUCH_RST
45#define TOUCH_RST 16
46#endif
47
49volatile bool isPressed = false;
50
51void scanDevices(void)
52{
53 byte error, address;
54 int nDevices = 0;
55 Serial.println("Scanning for I2C devices ...");
56 for (address = 0x01; address < 0x7f; address++) {
57 Wire.beginTransmission(address);
58 error = Wire.endTransmission();
59 if (error == 0) {
60 Serial.print("I2C device found at address 0x");
61 Serial.println(address, HEX);
62 nDevices++;
63 } else if (error != 2) {
64 Serial.print("Error ");
65 Serial.print(error);
66 Serial.print(" at address 0x");
67 Serial.println(address, HEX);
68 }
69 }
70 if (nDevices == 0) {
71 Serial.println("No I2C devices found");
72 }
73}
74
75void setup()
76{
77 Serial.begin(115200);
78 while (!Serial);
79
80#if TOUCH_RST != -1
81 pinMode(TOUCH_RST, OUTPUT);
82 digitalWrite(TOUCH_RST, LOW);
83 delay(30);
84 digitalWrite(TOUCH_RST, HIGH);
85 delay(50);
86 // delay(1000);
87#endif
88
89 // Search for known CSTxxx device addresses
90 uint8_t address = 0xFF;
91
92#if (defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_STM32)) && !defined(ARDUINO_ARCH_MBED)
93 Wire.setSCL(TOUCH_SCL);
94 Wire.setSDA(TOUCH_SDA);
95 Wire.begin();
96#elif defined(ARDUINO_ARCH_NRF52)
97 Wire.setPins(TOUCH_SDA, TOUCH_SCL);
98 Wire.begin();
99#elif defined(ARDUINO_ARCH_ESP32)
100 Wire.begin(TOUCH_SDA, TOUCH_SCL);
101#else
102 Wire.begin();
103#endif
104
105 // Scan I2C devices
106 scanDevices();
107
108 Wire.beginTransmission(CST816_SLAVE_ADDRESS);
109 if (Wire.endTransmission() == 0) {
110 address = CST816_SLAVE_ADDRESS;
111 }
112 // CST226 and CST328 , CST3240 same i2c address
113 Wire.beginTransmission(CST226SE_SLAVE_ADDRESS);
114 if (Wire.endTransmission() == 0) {
115 address = CST226SE_SLAVE_ADDRESS;
116 }
117 Wire.beginTransmission(CST328_SLAVE_ADDRESS);
118 if (Wire.endTransmission() == 0) {
119 address = CST328_SLAVE_ADDRESS;
120 }
121 while (address == 0xFF) {
122 Serial.println("Could't find touch chip!"); delay(1000);
123 }
124
126
127 /*
128 * Support type.
129 * TouchDrv_UNKNOWN : Judging by identification ID
130 * TouchDrv_CST8XX : CST816X,CST716,CST820
131 * TouchDrv_CST226 : CST226X,CST328,CST3240
132 * TouchDrv_CST92XX : CST9217,CST9220
133 * TouchDrv_CST3530 : CST3530
134 */
135 // Can choose fixed touch model or automatic identification by ID
136 // touch.setTouchDrvModel(TouchDrv_CST8XX);
137 // touch.setTouchDrvModel(TouchDrv_CST226);
138 // touch.setTouchDrvModel(TouchDrv_CST92XX);
139 // touch.setTouchDrvModel(TouchDrv_CST3530);
140
141 // For touchscreens without an RST pin, the actual model may not be
142 // accurately detected, or some undefined behavior may occur.
143
144 // Support CST81X CST226 CST9217 CST9220 CST3530 ....
145 bool result = touch.begin(Wire, address, TOUCH_SDA, TOUCH_SCL);
146 if (result == false) {
147 while (1) {
148 Serial.println("Failed to initialize CST series touch, please check the connection...");
149 delay(1000);
150 }
151 }
152
153 // T-Display-S3 CST328 touch panel, touch button coordinates are is 85 , 360
154 // touch.setCenterButtonCoordinate(85, 360);
155
156 // T-Display-AMOLED 1.91 Inch CST816T touch panel, touch button coordinates is 600, 120.
157 // touch.setCenterButtonCoordinate(600, 120); // Only suitable for AMOLED 1.91 inch
158
159 // T-Display-Bar Inch CST820 touch panel, touch button coordinates is 30,400
160 // touch.setCenterButtonCoordinate(30, 400); // Only suitable for T-Display-Bar
161
162 // Depending on the touch panel, not all touch panels have touch buttons.
163 touch.setHomeButtonCallback([](void *user_data) {
164 Serial.println("Home key pressed!");
165 }, NULL);
166
167
168 // Unable to obtain coordinates after turning on sleep
169 // CST816T sleep current = 1.1 uA
170 // CST226SE sleep current = 60 uA
171 // touch.sleep();
172
173 // Set touch max xy
174 // touch.setMaxCoordinates(536, 240);
175
176 // Set swap xy
177 // touch.setSwapXY(true);
178
179 // Set mirror xy
180 // touch.setMirrorXY(true, true);
181
182 //Register touch plane interrupt pin
183 attachInterrupt(TOUCH_IRQ, +[]() {
184 isPressed = true;
185 }, FALLING);
186
187 Serial.println("Touch Info:");
188 Serial.print("Model: "); Serial.println(touch.getModelName());
189 Serial.print("ID: 0x"); Serial.println(touch.getChipID(), HEX);
190 Serial.print("Max Touch Points: "); Serial.println(touch.getSupportTouchPoint());
191 uint16_t resX = touch.getResolutionX();
192 uint16_t resY = touch.getResolutionY();
193 if (resX == 0 || resY == 0) {
194 Serial.println("The touch driver not support get touch resolution,please use setResolution() to set touch resolution.");
195 // touch.setResolution(480, 320);
196 } else {
197 Serial.print("Resolution: "); Serial.print(resX); Serial.print(" x "); Serial.println(resY);
198 }
199 delay(3000);
200}
201
202void loop()
203{
204 if (isPressed) {
205 isPressed = false;
206 TouchPoints touch_points = touch.getTouchPoints();
207 if (touch_points.hasPoints()) {
208 for (int i = 0; i < touch_points.getPointCount(); ++i) {
209 const TouchPoint &point = touch_points.getPoint(i);
210 Serial.print("ID: ");
211 Serial.print(point.id);
212 Serial.print(" ");
213 Serial.print("X: ");
214 Serial.print(point.x);
215 Serial.print(" ");
216 Serial.print("Y: ");
217 Serial.print(point.y);
218 Serial.print(" ");
219 Serial.print("Pressure: ");
220 Serial.print(point.pressure);
221 Serial.print(" ");
222 Serial.print("Event: ");
223 switch (point.event) {
224 case 0: Serial.print("Released"); break;
225 case 6: Serial.print("Pressed"); break;
226 default: Serial.print("Unknown"); break;
227 }
228 Serial.println();
229 }
230 Serial.println();
231 }
232 }
233 delay(5);
234}
@license MIT License
uint16_t getResolutionY() override
Gets the touch point resolution.
uint8_t getSupportTouchPoint() override
Gets the number of supported touch points.
void setHomeButtonCallback(TouchDrvInterface::HomeButtonCallback callback, void *user_data=NULL) override
Set the home button callback.
bool begin(SensorCommCustom::CustomCallback callback, SensorCommCustomHal::CustomHalCallback hal_callback, uint8_t addr) override
Initialize the touch driver for custom communication.
const char * getModelName() override
Get the model name.
const TouchPoints & getTouchPoints() override
Get the touch points.
uint32_t getChipID() override
Get the chip ID.
uint16_t getResolutionX() override
Gets the touch point resolution.
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.
#define TOUCH_SDA
volatile bool isPressed
void scanDevices(void)
#define TOUCH_RST
TouchDrvCSTXXX touch
void setup()
#define TOUCH_SCL
#define TOUCH_IRQ
void loop()
uint8_t i
Represents a single touch point with its properties.
uint16_t pressure
Pressure value (0 if not supported by the chip).
uint8_t event
Event type based on actual hardware feedback, most touch devices do not support.
uint8_t id
Touch point ID for multi‑touch tracking.
uint16_t x
X coordinate of the touch point.
uint16_t y
Y coordinate of the touch point.