ESPHome  2024.10.2
cst816_touchscreen.cpp
Go to the documentation of this file.
1 #include "cst816_touchscreen.h"
2 
3 namespace esphome {
4 namespace 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  if (this->button_touched_ == state)
42  return;
43  this->button_touched_ = state;
44  for (auto *listener : this->button_listeners_)
45  listener->update_button(state);
46 }
47 
49  ESP_LOGCONFIG(TAG, "Setting up CST816 Touchscreen...");
50  if (this->reset_pin_ != nullptr) {
51  this->reset_pin_->setup();
52  this->reset_pin_->digital_write(true);
53  delay(5);
54  this->reset_pin_->digital_write(false);
55  delay(5);
56  this->reset_pin_->digital_write(true);
57  this->set_timeout(30, [this] { this->continue_setup_(); });
58  } else {
59  this->continue_setup_();
60  }
61 }
62 
64  uint8_t data[13];
65  if (!this->read_bytes(REG_STATUS, data, sizeof data)) {
66  this->status_set_warning();
67  return;
68  }
69  uint8_t num_of_touches = data[REG_TOUCH_NUM] & 3;
70  if (num_of_touches == 0) {
71  this->update_button_state_(false);
72  return;
73  }
74 
75  uint16_t x = encode_uint16(data[REG_XPOS_HIGH] & 0xF, data[REG_XPOS_LOW]);
76  uint16_t y = encode_uint16(data[REG_YPOS_HIGH] & 0xF, data[REG_YPOS_LOW]);
77  ESP_LOGV(TAG, "Read touch %d/%d", x, y);
78  if (x >= this->x_raw_max_) {
79  this->update_button_state_(true);
80  } else {
81  this->add_raw_touch_position_(0, x, y);
82  }
83 }
84 
86  ESP_LOGCONFIG(TAG, "CST816 Touchscreen:");
87  LOG_I2C_DEVICE(this);
88  LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
89  LOG_PIN(" Reset Pin: ", this->reset_pin_);
90  const char *name;
91  switch (this->chip_id_) {
92  case CST820_CHIP_ID:
93  name = "CST820";
94  break;
95  case CST826_CHIP_ID:
96  name = "CST826";
97  break;
98  case CST816S_CHIP_ID:
99  name = "CST816S";
100  break;
101  case CST816D_CHIP_ID:
102  name = "CST816D";
103  break;
104  case CST716_CHIP_ID:
105  name = "CST716";
106  break;
107  case CST816T_CHIP_ID:
108  name = "CST816T";
109  break;
110  default:
111  name = "Unknown";
112  break;
113  }
114  ESP_LOGCONFIG(TAG, " Chip type: %s", name);
115 }
116 
117 } // namespace cst816
118 } // namespace esphome
virtual void digital_write(bool value)=0
const char * name
Definition: stm32flash.h:78
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition: i2c.h:235
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
uint16_t x
Definition: tt21100.cpp:17
int get_native_height()
Get the native (original) height of the display in pixels.
Definition: display.h:223
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:69
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:212
virtual void setup()=0
uint16_t y
Definition: tt21100.cpp:18
int get_native_width()
Get the native (original) width of the display in pixels.
Definition: display.h:221
void status_set_error(const char *message="unspecified")
Definition: component.cpp:159
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.
Definition: touchscreen.cpp:12
std::string str_sprintf(const char *fmt,...)
Definition: helpers.cpp:310
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:182
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition: i2c.h:262
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
void add_raw_touch_position_(uint8_t id, int16_t x_raw, int16_t y_raw, int16_t z_raw=0)
Definition: touchscreen.cpp:74
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
std::vector< CST816ButtonListener * > button_listeners_
bool state
Definition: fan.h:34
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26