This commit is contained in:
Ben Meadors
2026-04-18 11:12:05 -05:00
parent a277108c84
commit e589de2d6e
2 changed files with 24 additions and 23 deletions
@@ -10,13 +10,14 @@ CSE_CST328 tsPanel = CSE_CST328(EINK_WIDTH, EINK_HEIGHT, &Wire, CST328_PIN_RST,
static bool is_cst3530 = false; static bool is_cst3530 = false;
volatile bool touch_isr = false; volatile bool touch_isr = false;
#define CST3530_ADDR 0x1A #define CST3530_ADDR 0x1A
bool read_cst3530_touch(int16_t *x, int16_t *y) { bool read_cst3530_touch(int16_t *x, int16_t *y)
{
uint8_t buffer[9] = {0}; uint8_t buffer[9] = {0};
uint8_t r_cmd[] = {0xD0, 0x07, 0x00, 0x00}; uint8_t r_cmd[] = {0xD0, 0x07, 0x00, 0x00};
uint8_t clear_cmd[] = {0xD0, 0x00, 0x02, 0xAB}; uint8_t clear_cmd[] = {0xD0, 0x00, 0x02, 0xAB};
Wire.beginTransmission(CST3530_ADDR); Wire.beginTransmission(CST3530_ADDR);
Wire.write(r_cmd, sizeof(r_cmd)); Wire.write(r_cmd, sizeof(r_cmd));
if (Wire.endTransmission() != 0) { if (Wire.endTransmission() != 0) {
@@ -41,13 +42,13 @@ bool read_cst3530_touch(int16_t *x, int16_t *y) {
} }
uint8_t touch_points = buffer[3] & 0x0F; uint8_t touch_points = buffer[3] & 0x0F;
if (touch_points == 0 || touch_points > 1) { if (touch_points == 0 || touch_points > 1) {
LOG_DEBUG("CST3530 touch points invalid: %d", touch_points); LOG_DEBUG("CST3530 touch points invalid: %d", touch_points);
return false; return false;
} }
*x = buffer[4] + ((uint16_t)(buffer[7] & 0x0F) << 8); *x = buffer[4] + ((uint16_t)(buffer[7] & 0x0F) << 8);
*y = buffer[5] + ((uint16_t)(buffer[7] & 0xF0) << 4); *y = buffer[5] + ((uint16_t)(buffer[7] & 0xF0) << 4);
// LOG_DEBUG("CST3530 touch: num:%d x=%d,y=%d", touch_points, *x, *y); // LOG_DEBUG("CST3530 touch: num:%d x=%d,y=%d", touch_points, *x, *y);
@@ -63,13 +64,13 @@ bool read_cst3530_touch(int16_t *x, int16_t *y) {
bool readTouch(int16_t *x, int16_t *y) bool readTouch(int16_t *x, int16_t *y)
{ {
if(is_cst3530){ if (is_cst3530) {
if(touch_isr){ if (touch_isr) {
touch_isr = false; touch_isr = false;
return read_cst3530_touch(x, y); return read_cst3530_touch(x, y);
} }
return false; return false;
}else{ } else {
if (tsPanel.getTouches()) { if (tsPanel.getTouches()) {
*x = tsPanel.getPoint(0).x; *x = tsPanel.getPoint(0).x;
*y = tsPanel.getPoint(0).y; *y = tsPanel.getPoint(0).y;
@@ -79,8 +80,8 @@ bool readTouch(int16_t *x, int16_t *y)
return false; return false;
} }
static void IRAM_ATTR touchInterruptHandler()
static void IRAM_ATTR touchInterruptHandler(){ {
touch_isr = true; touch_isr = true;
} }
@@ -98,30 +99,30 @@ void lateInitVariant()
int retry = 5; int retry = 5;
uint8_t buffer[7]; uint8_t buffer[7];
uint8_t r_cmd[] = {0x0d0,0x03,0x00,0x00}; uint8_t r_cmd[] = {0x0d0, 0x03, 0x00, 0x00};
// Probe touch chip // Probe touch chip
while(retry--) { while (retry--) {
Wire.beginTransmission(CST3530_ADDR); Wire.beginTransmission(CST3530_ADDR);
Wire.write(r_cmd, sizeof(r_cmd)); Wire.write(r_cmd, sizeof(r_cmd));
if(Wire.endTransmission() == 0){ if (Wire.endTransmission() == 0) {
Wire.requestFrom((int)CST3530_ADDR,7); Wire.requestFrom((int)CST3530_ADDR, 7);
Wire.readBytes(buffer,7); Wire.readBytes(buffer, 7);
if(buffer[2] == 0xCA && buffer[3] == 0xCA){ if (buffer[2] == 0xCA && buffer[3] == 0xCA) {
LOG_DEBUG("CST3530 detected"); LOG_DEBUG("CST3530 detected");
is_cst3530 = true; is_cst3530 = true;
// The CST3530 will automatically enter sleep mode; // The CST3530 will automatically enter sleep mode;
// polling should not be used, but rather an interrupt method should be employed. // polling should not be used, but rather an interrupt method should be employed.
pinMode(CST328_PIN_INT, INPUT); pinMode(CST328_PIN_INT, INPUT);
attachInterrupt(digitalPinToInterrupt(CST328_PIN_INT), touchInterruptHandler, FALLING); attachInterrupt(digitalPinToInterrupt(CST328_PIN_INT), touchInterruptHandler, FALLING);
break; break;
}else{ } else {
LOG_DEBUG("CST3530 not response ~!"); LOG_DEBUG("CST3530 not response ~!");
} }
} }
uint8_t cmd1[] = {0xD0,0x00,0x04,0x00}; uint8_t cmd1[] = {0xD0, 0x00, 0x04, 0x00};
Wire.beginTransmission(CST3530_ADDR); Wire.beginTransmission(CST3530_ADDR);
Wire.write(cmd1, sizeof(cmd1)); Wire.write(cmd1, sizeof(cmd1));
Wire.endTransmission(); Wire.endTransmission();
+1 -1
View File
@@ -5,7 +5,7 @@
#define PIN_EINK_RES 16 #define PIN_EINK_RES 16
#define PIN_EINK_SCLK 36 #define PIN_EINK_SCLK 36
#define PIN_EINK_MOSI 47 #define PIN_EINK_MOSI 47
#define TFT_BL 45 // option , default not backlight #define TFT_BL 45 // option , default not backlight
#define I2C_SDA SDA #define I2C_SDA SDA
#define I2C_SCL SCL #define I2C_SCL SCL