ESPHome 2025.5.0
Loading...
Searching...
No Matches
cst816_touchscreen.cpp
Go to the documentation of this file.
2
3namespace esphome {
4namespace cst816 {
5
7 if (this->interrupt_pin_ != nullptr) {
8 this->interrupt_pin_->setup();
10 }
11 if (this->read_byte(REG_CHIP_ID, &this->chip_id_)) {
12 switch (this->chip_id_) {
13 case CST820_CHIP_ID:
14 case CST826_CHIP_ID:
15 case CST716_CHIP_ID:
16 case CST816S_CHIP_ID:
17 case CST816D_CHIP_ID:
18 case CST816T_CHIP_ID:
19 break;
20 default:
21 this->mark_failed();
22 this->status_set_error(str_sprintf("Unknown chip ID 0x%02X", this->chip_id_).c_str());
23 return;
24 }
25 this->write_byte(REG_IRQ_CTL, IRQ_EN_MOTION);
26 } else if (!this->skip_probe_) {
27 this->status_set_error("Failed to read chip id");
28 this->mark_failed();
29 return;
30 }
31 if (this->x_raw_max_ == this->x_raw_min_) {
32 this->x_raw_max_ = this->display_->get_native_width();
33 }
34 if (this->y_raw_max_ == this->y_raw_min_) {
35 this->y_raw_max_ = this->display_->get_native_height();
36 }
37 ESP_LOGCONFIG(TAG, "CST816 Touchscreen setup complete");
38}
39
41 ESP_LOGCONFIG(TAG, "Setting up CST816 Touchscreen...");
42 if (this->reset_pin_ != nullptr) {
43 this->reset_pin_->setup();
44 this->reset_pin_->digital_write(true);
45 delay(5);
46 this->reset_pin_->digital_write(false);
47 delay(5);
48 this->reset_pin_->digital_write(true);
49 this->set_timeout(30, [this] { this->continue_setup_(); });
50 } else {
51 this->continue_setup_();
52 }
53}
54
56 uint8_t data[13];
57 if (!this->read_bytes(REG_STATUS, data, sizeof data)) {
58 this->status_set_warning();
59 return;
60 }
61 uint8_t num_of_touches = data[REG_TOUCH_NUM] & 3;
62 if (num_of_touches == 0) {
63 return;
64 }
65
66 uint16_t x = encode_uint16(data[REG_XPOS_HIGH] & 0xF, data[REG_XPOS_LOW]);
67 uint16_t y = encode_uint16(data[REG_YPOS_HIGH] & 0xF, data[REG_YPOS_LOW]);
68 ESP_LOGV(TAG, "Read touch %d/%d", x, y);
69 this->add_raw_touch_position_(0, x, y);
70}
71
73 ESP_LOGCONFIG(TAG, "CST816 Touchscreen:");
74 LOG_I2C_DEVICE(this);
75 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
76 LOG_PIN(" Reset Pin: ", this->reset_pin_);
77 ESP_LOGCONFIG(TAG, " X Raw Min: %d, X Raw Max: %d", this->x_raw_min_, this->x_raw_max_);
78 ESP_LOGCONFIG(TAG, " Y Raw Min: %d, Y Raw Max: %d", this->y_raw_min_, this->y_raw_max_);
79 const char *name;
80 switch (this->chip_id_) {
81 case CST820_CHIP_ID:
82 name = "CST820";
83 break;
84 case CST826_CHIP_ID:
85 name = "CST826";
86 break;
87 case CST816S_CHIP_ID:
88 name = "CST816S";
89 break;
90 case CST816D_CHIP_ID:
91 name = "CST816D";
92 break;
93 case CST716_CHIP_ID:
94 name = "CST716";
95 break;
96 case CST816T_CHIP_ID:
97 name = "CST816T";
98 break;
99 default:
100 name = "Unknown";
101 break;
102 }
103 ESP_LOGCONFIG(TAG, " Chip type: %s", name);
104}
105
106} // namespace cst816
107} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message="unspecified")
void status_set_error(const char *message="unspecified")
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
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:323
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:191
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