ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
cst226_touchscreen.cpp
Go to the documentation of this file.
2
3namespace esphome::cst226 {
4
5ESPHOME_LOG_TAG(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->status_comm_result(this->read_bytes(CST226_REG_STATUS, data, sizeof data))) {
24 this->skip_update_ = true;
25 return;
26 }
27 if (data[0] == 0x83 && data[1] == 0x17 && data[5] == 0x80) {
28 this->update_button_state_(true);
29 return;
30 }
31 this->update_button_state_(false);
32 if (data[6] != 0xAB || data[0] == 0xAB || data[5] == 0x80) {
33 this->skip_update_ = true;
34 return;
35 }
36 uint8_t num_of_touches = data[5] & 0x7F;
37 if (num_of_touches == 0 || num_of_touches > 5) {
38 this->write_byte(0, 0xAB);
39 return;
40 }
41
42 size_t index = 0;
43 for (uint8_t i = 0; i != num_of_touches; i++) {
44 uint8_t id = data[index] >> 4;
45 int16_t x = (data[index + 1] << 4) | ((data[index + 3] >> 4) & 0x0F);
46 int16_t y = (data[index + 2] << 4) | (data[index + 3] & 0x0F);
47 int16_t z = data[index + 4];
48 this->add_raw_touch_position_(id, x, y, z);
49 ESP_LOGV(TAG, "Read touch %d: %d/%d", id, x, y);
50 index += 5;
51 if (i == 0)
52 index += 2;
53 }
54}
55
56bool CST226Touchscreen::read16_(uint16_t addr, uint8_t *data, size_t len) {
57 if (this->read_register16(addr, data, len) != i2c::ERROR_OK) {
58 ESP_LOGE(TAG, "Read data from 0x%04X failed", addr);
59 this->mark_failed();
60 return false;
61 }
62 return true;
63}
65 uint8_t buffer[8];
66 if (this->interrupt_pin_ != nullptr) {
67 this->interrupt_pin_->setup();
69 }
70 buffer[0] = 0xD1;
71 if (this->write_register16(0xD1, buffer, 1) != i2c::ERROR_OK) {
72 ESP_LOGE(TAG, "Write byte to 0xD1 failed");
73 this->mark_failed();
74 return;
75 }
76 delay(10);
77 if (this->read16_(0xD204, buffer, 4)) {
78 uint16_t chip_id = buffer[2] + (buffer[3] << 8);
79 uint16_t project_id = buffer[0] + (buffer[1] << 8);
80 ESP_LOGCONFIG(TAG, "Chip ID %X, project ID %x", chip_id, project_id);
81 }
82 if (this->x_raw_max_ == 0 || this->y_raw_max_ == 0) {
83 if (this->read16_(0xD1F8, buffer, 4)) {
84 this->x_raw_max_ = buffer[0] + (buffer[1] << 8);
85 this->y_raw_max_ = buffer[2] + (buffer[3] << 8);
86 if (this->swap_x_y_)
87 std::swap(this->x_raw_max_, this->y_raw_max_);
88 } else {
89 this->x_raw_max_ = this->display_->get_native_width();
90 this->y_raw_max_ = this->display_->get_native_height();
91 }
92 }
93 this->setup_complete_ = true;
94}
96 if (this->button_touched_ == state)
97 return;
98 this->button_touched_ = state;
99 for (auto *listener : this->button_listeners_)
100 listener->update_button(state);
101}
102
104 ESP_LOGCONFIG(TAG, "CST226 Touchscreen:");
105 LOG_I2C_DEVICE(this);
106 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
107 LOG_PIN(" Reset Pin: ", this->reset_pin_);
108}
109
110} // namespace esphome::cst226
void mark_failed()
Mark this component as failed.
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
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:341
int get_native_height()
Get the native (original) height of the display in pixels.
Definition display.h:343
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:46
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
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:32
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:249
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
uint32_t buffer
Definition gif.cpp:50
bool z
Definition msa3xx.h:1
ESPHOME_LOG_TAG(TAG, "CST226.binary_sensor")
@ INTERRUPT_FALLING_EDGE
Definition gpio_pin.h:53
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
const void size_t len
Definition hal.h:68
void HOT delay(uint32_t ms)
Definition hal.cpp:85
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6