ESPHome 2026.6.0-dev
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 this->reset_pin_->setup();
56 this->reset_pin_->digital_write(true);
57 delay(5);
58 this->reset_pin_->digital_write(false);
59 delay(5);
60 this->reset_pin_->digital_write(true);
61 this->set_timeout(30, [this] { this->continue_setup_(); });
62 } else {
63 this->continue_setup_();
64 }
65}
66
68 uint8_t data[13];
69 if (!this->read_bytes(REG_STATUS, data, sizeof data)) {
70 this->status_set_warning();
71 return;
72 }
73 uint8_t num_of_touches = data[REG_TOUCH_NUM] & 3;
74 if (num_of_touches == 0) {
75 return;
76 }
77
78 uint16_t x = encode_uint16(data[REG_XPOS_HIGH] & 0xF, data[REG_XPOS_LOW]);
79 uint16_t y = encode_uint16(data[REG_YPOS_HIGH] & 0xF, data[REG_YPOS_LOW]);
80 ESP_LOGV(TAG, "Read touch %d/%d", x, y);
81 this->add_raw_touch_position_(0, x, y);
82}
83
85 ESP_LOGCONFIG(TAG,
86 "CST816 Touchscreen:\n"
87 " X Raw Min: %d, X Raw Max: %d\n"
88 " Y Raw Min: %d, Y Raw Max: %d",
89 this->x_raw_min_, this->x_raw_max_, this->y_raw_min_, this->y_raw_max_);
90 LOG_I2C_DEVICE(this);
91 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
92 LOG_PIN(" Reset Pin: ", this->reset_pin_);
93 const char *name;
94 switch (this->chip_id_) {
95 case CST716_CHIP_ID:
96 name = "CST716";
97 break;
98 case CST816S_CHIP_ID:
99 name = "CST816S";
100 break;
101 case CST816D_CHIP_ID:
102 name = "CST816D";
103 break;
104 case CST816T_CHIP_ID:
105 name = "CST816T";
106 break;
107 case CST820_CHIP_ID:
108 name = "CST820";
109 break;
110 case CST826_CHIP_ID:
111 name = "CST826";
112 break;
113 case CST836_CHIP_ID:
114 name = "CST836";
115 break;
116 default:
117 name = "Unknown";
118 break;
119 }
120 ESP_LOGCONFIG(TAG, " Chip type: %s", name);
121}
122
123} // namespace esphome::cst816
void mark_failed()
Mark this component as failed.
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:493
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:330
int get_native_height()
Get the native (original) height of the display in pixels.
Definition display.h:332
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:265
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:240
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:217
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:51
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:859
void HOT delay(uint32_t ms)
Definition hal.cpp:85
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6