ESPHome 2026.11.0b6
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 pulse_reset(this->reset_pin_, {.high_before_ms = 5, .low_ms = 5});
10 this->set_timeout(30, [this] { this->continue_setup_(); });
11 } else {
12 this->continue_setup_();
13 }
14}
15
17 uint8_t data[28];
18 if (!this->status_comm_result(this->read_bytes(CST226_REG_STATUS, data, sizeof data))) {
19 this->skip_update_ = true;
20 return;
21 }
22 if (data[0] == 0x83 && data[1] == 0x17 && data[5] == 0x80) {
23 this->update_button_state_(true);
24 return;
25 }
26 this->update_button_state_(false);
27 if (data[6] != 0xAB || data[0] == 0xAB || data[5] == 0x80) {
28 this->skip_update_ = true;
29 return;
30 }
31 uint8_t num_of_touches = data[5] & 0x7F;
32 if (num_of_touches == 0 || num_of_touches > 5) {
33 this->write_byte(0, 0xAB);
34 return;
35 }
36
37 size_t index = 0;
38 for (uint8_t i = 0; i != num_of_touches; i++) {
39 uint8_t id = data[index] >> 4;
40 int16_t x = (data[index + 1] << 4) | ((data[index + 3] >> 4) & 0x0F);
41 int16_t y = (data[index + 2] << 4) | (data[index + 3] & 0x0F);
42 int16_t z = data[index + 4];
43 this->add_raw_touch_position_(id, x, y, z);
44 ESP_LOGV(TAG, "Read touch %d: %d/%d", id, x, y);
45 index += 5;
46 if (i == 0)
47 index += 2;
48 }
49}
50
51bool CST226Touchscreen::read16_(uint16_t addr, uint8_t *data, size_t len) {
52 if (this->read_register16(addr, data, len) != i2c::ERROR_OK) {
53 ESP_LOGE(TAG, "Read data from 0x%04X failed", addr);
54 this->mark_failed();
55 return false;
56 }
57 return true;
58}
60 uint8_t buffer[8];
61 if (this->interrupt_pin_ != nullptr) {
62 this->interrupt_pin_->setup();
64 }
65 buffer[0] = 0xD1;
66 if (this->write_register16(0xD1, buffer, 1) != i2c::ERROR_OK) {
67 ESP_LOGE(TAG, "Write byte to 0xD1 failed");
68 this->mark_failed();
69 return;
70 }
71 delay(10);
72 if (this->read16_(0xD204, buffer, 4)) {
73 uint16_t chip_id = buffer[2] + (buffer[3] << 8);
74 uint16_t project_id = buffer[0] + (buffer[1] << 8);
75 ESP_LOGCONFIG(TAG, "Chip ID %X, project ID %x", chip_id, project_id);
76 }
77 if (this->x_raw_max_ == 0 || this->y_raw_max_ == 0) {
78 if (this->read16_(0xD1F8, buffer, 4)) {
79 this->x_raw_max_ = buffer[0] + (buffer[1] << 8);
80 this->y_raw_max_ = buffer[2] + (buffer[3] << 8);
81 if (this->swap_x_y_)
82 std::swap(this->x_raw_max_, this->y_raw_max_);
83 } else {
84 this->x_raw_max_ = this->display_->get_native_width();
85 this->y_raw_max_ = this->display_->get_native_height();
86 }
87 }
88 this->setup_complete_ = true;
89}
91 if (this->button_touched_ == state)
92 return;
93 this->button_touched_ = state;
94 for (auto *listener : this->button_listeners_)
95 listener->update_button(state);
96}
97
99 ESP_LOGCONFIG(TAG, "CST226 Touchscreen:");
100 LOG_I2C_DEVICE(this);
101 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
102 LOG_PIN(" Reset Pin: ", this->reset_pin_);
103}
104
105} // 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
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:63
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:49
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
ESPHOME_ALWAYS_INLINE void pulse_reset(GPIOPin *pin, ResetPulse pulse)
Set up an active-low reset pin and drive it through pulse.
Definition gpio_pin.h:163
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6