ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
cst226_touchscreen.cpp
Go to the documentation of this file.
2
3namespace esphome::cst226 {
4
5static const char *const TAG = "cst226.touchscreen";
6
8 if (this->reset_pin_ != nullptr) {
9 this->reset_pin_->setup();
10 this->reset_pin_->digital_write(true);
11 delay(5);
12 this->reset_pin_->digital_write(false);
13 delay(5);
14 this->reset_pin_->digital_write(true);
15 this->set_timeout(30, [this] { this->continue_setup_(); });
16 } else {
17 this->continue_setup_();
18 }
19}
20
22 uint8_t data[28];
23 if (!this->read_bytes(CST226_REG_STATUS, data, sizeof data)) {
24 this->status_set_warning();
25 this->skip_update_ = true;
26 return;
27 }
29 if (data[0] == 0x83 && data[1] == 0x17 && data[5] == 0x80) {
30 this->update_button_state_(true);
31 return;
32 }
33 this->update_button_state_(false);
34 if (data[6] != 0xAB || data[0] == 0xAB || data[5] == 0x80) {
35 this->skip_update_ = true;
36 return;
37 }
38 uint8_t num_of_touches = data[5] & 0x7F;
39 if (num_of_touches == 0 || num_of_touches > 5) {
40 this->write_byte(0, 0xAB);
41 return;
42 }
43
44 size_t index = 0;
45 for (uint8_t i = 0; i != num_of_touches; i++) {
46 uint8_t id = data[index] >> 4;
47 int16_t x = (data[index + 1] << 4) | ((data[index + 3] >> 4) & 0x0F);
48 int16_t y = (data[index + 2] << 4) | (data[index + 3] & 0x0F);
49 int16_t z = data[index + 4];
50 this->add_raw_touch_position_(id, x, y, z);
51 ESP_LOGV(TAG, "Read touch %d: %d/%d", id, x, y);
52 index += 5;
53 if (i == 0)
54 index += 2;
55 }
56}
57
58bool CST226Touchscreen::read16_(uint16_t addr, uint8_t *data, size_t len) {
59 if (this->read_register16(addr, data, len) != i2c::ERROR_OK) {
60 ESP_LOGE(TAG, "Read data from 0x%04X failed", addr);
61 this->mark_failed();
62 return false;
63 }
64 return true;
65}
67 uint8_t buffer[8];
68 if (this->interrupt_pin_ != nullptr) {
69 this->interrupt_pin_->setup();
71 }
72 buffer[0] = 0xD1;
73 if (this->write_register16(0xD1, buffer, 1) != i2c::ERROR_OK) {
74 ESP_LOGE(TAG, "Write byte to 0xD1 failed");
75 this->mark_failed();
76 return;
77 }
78 delay(10);
79 if (this->read16_(0xD204, buffer, 4)) {
80 uint16_t chip_id = buffer[2] + (buffer[3] << 8);
81 uint16_t project_id = buffer[0] + (buffer[1] << 8);
82 ESP_LOGCONFIG(TAG, "Chip ID %X, project ID %x", chip_id, project_id);
83 }
84 if (this->x_raw_max_ == 0 || this->y_raw_max_ == 0) {
85 if (this->read16_(0xD1F8, buffer, 4)) {
86 this->x_raw_max_ = buffer[0] + (buffer[1] << 8);
87 this->y_raw_max_ = buffer[2] + (buffer[3] << 8);
88 if (this->swap_x_y_)
89 std::swap(this->x_raw_max_, this->y_raw_max_);
90 } else {
91 this->x_raw_max_ = this->display_->get_native_width();
92 this->y_raw_max_ = this->display_->get_native_height();
93 }
94 }
95 this->setup_complete_ = true;
96}
98 if (this->button_touched_ == state)
99 return;
100 this->button_touched_ = state;
101 for (auto *listener : this->button_listeners_)
102 listener->update_button(state);
103}
104
106 ESP_LOGCONFIG(TAG, "CST226 Touchscreen:");
107 LOG_I2C_DEVICE(this);
108 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
109 LOG_PIN(" Reset Pin: ", this->reset_pin_);
110}
111
112} // namespace esphome::cst226
void mark_failed()
Mark this component as failed.
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:493
void status_clear_warning()
Definition component.h:289
virtual void setup()=0
virtual void digital_write(bool value)=0
std::vector< CST226ButtonListener * > button_listeners_
bool read16_(uint16_t addr, uint8_t *data, size_t len)
int get_native_width()
Get the native (original) width of the display in pixels.
Definition display.h:330
int get_native_height()
Get the native (original) height of the display in pixels.
Definition display.h:332
ErrorCode write_register16(uint16_t a_register, const uint8_t *data, size_t len) const
write an array of bytes to a specific register in the I²C device
Definition i2c.cpp:43
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:265
ErrorCode read_register16(uint16_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:29
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition i2c.h:217
void attach_interrupt_(InternalGPIOPin *irq_pin, esphome::gpio::InterruptType type)
Call this function to send touch points to the on_touch listener and the binary_sensors.
void add_raw_touch_position_(uint8_t id, int16_t x_raw, int16_t y_raw, int16_t z_raw=0)
bool state
Definition fan.h:2
bool z
Definition msa3xx.h:1
@ INTERRUPT_FALLING_EDGE
Definition gpio.h:51
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14
const void size_t len
Definition hal.h:64
void HOT delay(uint32_t ms)
Definition hal.cpp:85
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6