ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
cst816_touchscreen.cpp
Go to the documentation of this file.
3
4namespace esphome::cst816 {
5
7 if (this->interrupt_pin_ != nullptr) {
8 this->interrupt_pin_->setup();
10 }
11
12 if (!this->read_byte(REG_CHIP_ID, &this->chip_id_) && !this->skip_probe_) {
13 this->status_set_error(LOG_STR("Failed to read chip ID"));
14 this->mark_failed();
15 return;
16 }
17
18 // CST826/CST836 return 0 for chip ID, need to read from factory ID register
19 if (this->chip_id_ == 0) {
20 if (!this->read_byte(REG_FACTORY_ID, &this->chip_id_) && !this->skip_probe_) {
21 this->status_set_error(LOG_STR("Failed to read chip ID"));
22 this->mark_failed();
23 return;
24 }
25 }
26
27 switch (this->chip_id_) {
28 case CST716_CHIP_ID:
29 case CST816S_CHIP_ID:
30 case CST816D_CHIP_ID:
31 case CST816T_CHIP_ID:
32 case CST820_CHIP_ID:
33 case CST826_CHIP_ID:
34 case CST836_CHIP_ID:
35 break;
36 default:
37 if (!this->skip_probe_) {
38 ESP_LOGE(TAG, "Unknown chip ID: 0x%02X", this->chip_id_);
39 this->status_set_error(LOG_STR("Unknown chip ID"));
40 this->mark_failed();
41 return;
42 }
43 }
44 this->write_byte(REG_IRQ_CTL, IRQ_EN_MOTION);
45 if (this->x_raw_max_ == this->x_raw_min_) {
46 this->x_raw_max_ = this->display_->get_native_width();
47 }
48 if (this->y_raw_max_ == this->y_raw_min_) {
49 this->y_raw_max_ = this->display_->get_native_height();
50 }
51}
52
54 if (this->reset_pin_ != nullptr) {
55 pulse_reset(this->reset_pin_, {.high_before_ms = 5, .low_ms = 5});
56 this->set_timeout(30, [this] { this->continue_setup_(); });
57 } else {
58 this->continue_setup_();
59 }
60}
61
63 uint8_t data[13];
64 if (!this->read_bytes(REG_STATUS, data, sizeof data)) {
65 this->status_set_warning();
66 return;
67 }
68 uint8_t num_of_touches = data[REG_TOUCH_NUM] & 3;
69 if (num_of_touches == 0) {
70 return;
71 }
72
73 uint16_t x = encode_uint16(data[REG_XPOS_HIGH] & 0xF, data[REG_XPOS_LOW]);
74 uint16_t y = encode_uint16(data[REG_YPOS_HIGH] & 0xF, data[REG_YPOS_LOW]);
75 ESP_LOGV(TAG, "Read touch %d/%d", x, y);
76 this->add_raw_touch_position_(0, x, y);
77}
78
80 ESP_LOGCONFIG(TAG,
81 "CST816 Touchscreen:\n"
82 " X Raw Min: %d, X Raw Max: %d\n"
83 " Y Raw Min: %d, Y Raw Max: %d",
84 this->x_raw_min_, this->x_raw_max_, this->y_raw_min_, this->y_raw_max_);
85 LOG_I2C_DEVICE(this);
86 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
87 LOG_PIN(" Reset Pin: ", this->reset_pin_);
88 const char *name;
89 switch (this->chip_id_) {
90 case CST716_CHIP_ID:
91 name = "CST716";
92 break;
93 case CST816S_CHIP_ID:
94 name = "CST816S";
95 break;
96 case CST816D_CHIP_ID:
97 name = "CST816D";
98 break;
99 case CST816T_CHIP_ID:
100 name = "CST816T";
101 break;
102 case CST820_CHIP_ID:
103 name = "CST820";
104 break;
105 case CST826_CHIP_ID:
106 name = "CST826";
107 break;
108 case CST836_CHIP_ID:
109 name = "CST836";
110 break;
111 default:
112 name = "Unknown";
113 break;
114 }
115 ESP_LOGCONFIG(TAG, " Chip type: %s", name);
116}
117
118} // namespace esphome::cst816
void mark_failed()
Mark this component as failed.
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
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
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:272
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)
@ INTERRUPT_FALLING_EDGE
Definition gpio_pin.h:53
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:1043
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