ESPHome  2024.7.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  this->mark_failed();
13  esph_log_e(TAG, "Failed to read chip id");
14  return;
15  }
16  switch (this->chip_id_) {
17  case CST820_CHIP_ID:
18  case CST826_CHIP_ID:
19  case CST716_CHIP_ID:
20  case CST816S_CHIP_ID:
21  case CST816D_CHIP_ID:
22  case CST816T_CHIP_ID:
23  break;
24  default:
25  this->mark_failed();
26  esph_log_e(TAG, "Unknown chip ID 0x%02X", this->chip_id_);
27  return;
28  }
29  this->write_byte(REG_IRQ_CTL, IRQ_EN_MOTION);
30  if (this->x_raw_max_ == this->x_raw_min_) {
31  this->x_raw_max_ = this->display_->get_native_width();
32  }
33  if (this->y_raw_max_ == this->y_raw_min_) {
34  this->y_raw_max_ = this->display_->get_native_height();
35  }
36  esph_log_config(TAG, "CST816 Touchscreen setup complete");
37 }
38 
40  if (this->button_touched_ == state)
41  return;
42  this->button_touched_ = state;
43  for (auto *listener : this->button_listeners_)
44  listener->update_button(state);
45 }
46 
48  esph_log_config(TAG, "Setting up CST816 Touchscreen...");
49  if (this->reset_pin_ != nullptr) {
50  this->reset_pin_->setup();
51  this->reset_pin_->digital_write(true);
52  delay(5);
53  this->reset_pin_->digital_write(false);
54  delay(5);
55  this->reset_pin_->digital_write(true);
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  this->update_button_state_(false);
71  return;
72  }
73 
74  uint16_t x = encode_uint16(data[REG_XPOS_HIGH] & 0xF, data[REG_XPOS_LOW]);
75  uint16_t y = encode_uint16(data[REG_YPOS_HIGH] & 0xF, data[REG_YPOS_LOW]);
76  esph_log_v(TAG, "Read touch %d/%d", x, y);
77  if (x >= this->x_raw_max_) {
78  this->update_button_state_(true);
79  } else {
80  this->add_raw_touch_position_(0, x, y);
81  }
82 }
83 
85  ESP_LOGCONFIG(TAG, "CST816 Touchscreen:");
86  LOG_I2C_DEVICE(this);
87  LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
88  LOG_PIN(" Reset Pin: ", this->reset_pin_);
89  const char *name;
90  switch (this->chip_id_) {
91  case CST820_CHIP_ID:
92  name = "CST820";
93  break;
94  case CST826_CHIP_ID:
95  name = "CST826";
96  break;
97  case CST816S_CHIP_ID:
98  name = "CST816S";
99  break;
100  case CST816D_CHIP_ID:
101  name = "CST816D";
102  break;
103  case CST716_CHIP_ID:
104  name = "CST716";
105  break;
106  case CST816T_CHIP_ID:
107  name = "CST816T";
108  break;
109  default:
110  name = "Unknown";
111  break;
112  }
113  ESP_LOGCONFIG(TAG, " Chip type: %s", name);
114 }
115 
116 } // namespace cst816
117 } // 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:222
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:220
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
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