ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
cst816_touchscreen.cpp
Go to the documentation of this file.
3
4namespace esphome {
5namespace cst816 {
6
8 if (this->interrupt_pin_ != nullptr) {
9 this->interrupt_pin_->setup();
11 }
12 if (this->read_byte(REG_CHIP_ID, &this->chip_id_)) {
13 switch (this->chip_id_) {
14 case CST820_CHIP_ID:
15 case CST826_CHIP_ID:
16 case CST716_CHIP_ID:
17 case CST816S_CHIP_ID:
18 case CST816D_CHIP_ID:
19 case CST816T_CHIP_ID:
20 break;
21 default:
22 this->mark_failed();
23 this->status_set_error(str_sprintf("Unknown chip ID 0x%02X", this->chip_id_).c_str());
24 return;
25 }
26 this->write_byte(REG_IRQ_CTL, IRQ_EN_MOTION);
27 } else if (!this->skip_probe_) {
28 this->status_set_error("Failed to read chip id");
29 this->mark_failed();
30 return;
31 }
32 if (this->x_raw_max_ == this->x_raw_min_) {
33 this->x_raw_max_ = this->display_->get_native_width();
34 }
35 if (this->y_raw_max_ == this->y_raw_min_) {
36 this->y_raw_max_ = this->display_->get_native_height();
37 }
38}
39
41 if (this->reset_pin_ != nullptr) {
42 this->reset_pin_->setup();
43 this->reset_pin_->digital_write(true);
44 delay(5);
45 this->reset_pin_->digital_write(false);
46 delay(5);
47 this->reset_pin_->digital_write(true);
48 this->set_timeout(30, [this] { this->continue_setup_(); });
49 } else {
50 this->continue_setup_();
51 }
52}
53
55 uint8_t data[13];
56 if (!this->read_bytes(REG_STATUS, data, sizeof data)) {
57 this->status_set_warning();
58 return;
59 }
60 uint8_t num_of_touches = data[REG_TOUCH_NUM] & 3;
61 if (num_of_touches == 0) {
62 return;
63 }
64
65 uint16_t x = encode_uint16(data[REG_XPOS_HIGH] & 0xF, data[REG_XPOS_LOW]);
66 uint16_t y = encode_uint16(data[REG_YPOS_HIGH] & 0xF, data[REG_YPOS_LOW]);
67 ESP_LOGV(TAG, "Read touch %d/%d", x, y);
68 this->add_raw_touch_position_(0, x, y);
69}
70
72 ESP_LOGCONFIG(TAG, "CST816 Touchscreen:");
73 LOG_I2C_DEVICE(this);
74 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
75 LOG_PIN(" Reset Pin: ", this->reset_pin_);
76 ESP_LOGCONFIG(TAG,
77 " X Raw Min: %d, X Raw Max: %d\n"
78 " Y Raw Min: %d, Y Raw Max: %d",
79 this->x_raw_min_, this->x_raw_max_, this->y_raw_min_, this->y_raw_max_);
80 const char *name;
81 switch (this->chip_id_) {
82 case CST820_CHIP_ID:
83 name = "CST820";
84 break;
85 case CST826_CHIP_ID:
86 name = "CST826";
87 break;
88 case CST816S_CHIP_ID:
89 name = "CST816S";
90 break;
91 case CST816D_CHIP_ID:
92 name = "CST816D";
93 break;
94 case CST716_CHIP_ID:
95 name = "CST716";
96 break;
97 case CST816T_CHIP_ID:
98 name = "CST816T";
99 break;
100 default:
101 name = "Unknown";
102 break;
103 }
104 ESP_LOGCONFIG(TAG, " Chip type: %s", name);
105}
106
107} // namespace cst816
108} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message=nullptr)
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
void status_set_error(const char *message=nullptr)
virtual void setup()=0
virtual void digital_write(bool value)=0
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
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition i2c.h:266
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition i2c.h:239
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)
@ INTERRUPT_FALLING_EDGE
Definition gpio.h:42
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string str_sprintf(const char *fmt,...)
Definition helpers.cpp:208
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:173
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:29
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6