ESPHome 2025.5.0
Loading...
Searching...
No Matches
cst226_touchscreen.cpp
Go to the documentation of this file.
2
3namespace esphome {
4namespace cst226 {
5
6static const char *const TAG = "cst226.touchscreen";
7
9 ESP_LOGCONFIG(TAG, "Setting up CST226 Touchscreen...");
10 if (this->reset_pin_ != nullptr) {
11 this->reset_pin_->setup();
12 this->reset_pin_->digital_write(true);
13 delay(5);
14 this->reset_pin_->digital_write(false);
15 delay(5);
16 this->reset_pin_->digital_write(true);
17 this->set_timeout(30, [this] { this->continue_setup_(); });
18 } else {
19 this->continue_setup_();
20 }
21}
22
24 uint8_t data[28];
25 if (!this->read_bytes(CST226_REG_STATUS, data, sizeof data)) {
26 this->status_set_warning();
27 this->skip_update_ = true;
28 return;
29 }
31 if (data[0] == 0x83 && data[1] == 0x17 && data[5] == 0x80) {
32 this->update_button_state_(true);
33 return;
34 }
35 this->update_button_state_(false);
36 if (data[6] != 0xAB || data[0] == 0xAB || data[5] == 0x80) {
37 this->skip_update_ = true;
38 return;
39 }
40 uint8_t num_of_touches = data[5] & 0x7F;
41 if (num_of_touches == 0 || num_of_touches > 5) {
42 this->write_byte(0, 0xAB);
43 return;
44 }
45
46 size_t index = 0;
47 for (uint8_t i = 0; i != num_of_touches; i++) {
48 uint8_t id = data[index] >> 4;
49 int16_t x = (data[index + 1] << 4) | ((data[index + 3] >> 4) & 0x0F);
50 int16_t y = (data[index + 2] << 4) | (data[index + 3] & 0x0F);
51 int16_t z = data[index + 4];
52 this->add_raw_touch_position_(id, x, y, z);
53 ESP_LOGV(TAG, "Read touch %d: %d/%d", id, x, y);
54 index += 5;
55 if (i == 0)
56 index += 2;
57 }
58}
59
60bool CST226Touchscreen::read16_(uint16_t addr, uint8_t *data, size_t len) {
61 if (this->read_register16(addr, data, len) != i2c::ERROR_OK) {
62 ESP_LOGE(TAG, "Read data from 0x%04X failed", addr);
63 this->mark_failed();
64 return false;
65 }
66 return true;
67}
69 uint8_t buffer[8];
70 if (this->interrupt_pin_ != nullptr) {
71 this->interrupt_pin_->setup();
73 }
74 buffer[0] = 0xD1;
75 if (this->write_register16(0xD1, buffer, 1) != i2c::ERROR_OK) {
76 ESP_LOGE(TAG, "Write byte to 0xD1 failed");
77 this->mark_failed();
78 return;
79 }
80 delay(10);
81 if (this->read16_(0xD204, buffer, 4)) {
82 uint16_t chip_id = buffer[2] + (buffer[3] << 8);
83 uint16_t project_id = buffer[0] + (buffer[1] << 8);
84 ESP_LOGCONFIG(TAG, "Chip ID %X, project ID %x", chip_id, project_id);
85 }
86 if (this->x_raw_max_ == 0 || this->y_raw_max_ == 0) {
87 if (this->read16_(0xD1F8, buffer, 4)) {
88 this->x_raw_max_ = buffer[0] + (buffer[1] << 8);
89 this->y_raw_max_ = buffer[2] + (buffer[3] << 8);
90 if (this->swap_x_y_)
91 std::swap(this->x_raw_max_, this->y_raw_max_);
92 } else {
93 this->x_raw_max_ = this->display_->get_native_width();
94 this->y_raw_max_ = this->display_->get_native_height();
95 }
96 }
97 this->setup_complete_ = true;
98 ESP_LOGCONFIG(TAG, "CST226 Touchscreen setup complete");
99}
101 if (this->button_touched_ == state)
102 return;
103 this->button_touched_ = state;
104 for (auto *listener : this->button_listeners_)
105 listener->update_button(state);
106}
107
109 ESP_LOGCONFIG(TAG, "CST226 Touchscreen:");
110 LOG_I2C_DEVICE(this);
111 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
112 LOG_PIN(" Reset Pin: ", this->reset_pin_);
113}
114
115} // namespace cst226
116} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message="unspecified")
void status_clear_warning()
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.cpp:72
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:221
int get_native_height()
Get the native (original) height of the display in pixels.
Definition display.h:223
ErrorCode write_register16(uint16_t a_register, const uint8_t *data, size_t len, bool stop=true)
write an array of bytes to a specific register in the I²C device
Definition i2c.cpp:34
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition i2c.h:266
ErrorCode read_register16(uint16_t a_register, uint8_t *data, size_t len, bool stop=true)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:17
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:216
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:0
bool z
Definition msa3xx.h:1
@ INTERRUPT_FALLING_EDGE
Definition gpio.h:42
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:301
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:28
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6