ESPHome  2024.12.2
cst226_touchscreen.cpp
Go to the documentation of this file.
1 #include "cst226_touchscreen.h"
2 
3 namespace esphome {
4 namespace cst226 {
5 
7  esph_log_config(TAG, "Setting up CST226 Touchscreen...");
8  if (this->reset_pin_ != nullptr) {
9  this->reset_pin_->setup();
10  this->reset_pin_->digital_write(true);
11  delay(5);
12  this->reset_pin_->digital_write(false);
13  delay(5);
14  this->reset_pin_->digital_write(true);
15  this->set_timeout(30, [this] { this->continue_setup_(); });
16  } else {
17  this->continue_setup_();
18  }
19 }
20 
22  uint8_t data[28];
23  if (!this->read_bytes(CST226_REG_STATUS, data, sizeof data)) {
24  this->status_set_warning();
25  this->skip_update_ = true;
26  return;
27  }
28  this->status_clear_warning();
29  if (data[6] != 0xAB || data[0] == 0xAB || data[5] == 0x80) {
30  this->skip_update_ = true;
31  return;
32  }
33  uint8_t num_of_touches = data[5] & 0x7F;
34  if (num_of_touches == 0 || num_of_touches > 5) {
35  this->write_byte(0, 0xAB);
36  return;
37  }
38 
39  size_t index = 0;
40  for (uint8_t i = 0; i != num_of_touches; i++) {
41  uint8_t id = data[index] >> 4;
42  int16_t x = (data[index + 1] << 4) | ((data[index + 3] >> 4) & 0x0F);
43  int16_t y = (data[index + 2] << 4) | (data[index + 3] & 0x0F);
44  int16_t z = data[index + 4];
45  this->add_raw_touch_position_(id, x, y, z);
46  esph_log_v(TAG, "Read touch %d: %d/%d", id, x, y);
47  index += 5;
48  if (i == 0)
49  index += 2;
50  }
51 }
52 
54  uint8_t buffer[8];
55  if (this->interrupt_pin_ != nullptr) {
56  this->interrupt_pin_->setup();
58  }
59  buffer[0] = 0xD1;
60  if (this->write_register16(0xD1, buffer, 1) != i2c::ERROR_OK) {
61  esph_log_e(TAG, "Write byte to 0xD1 failed");
62  this->mark_failed();
63  return;
64  }
65  delay(10);
66  if (this->read16_(0xD204, buffer, 4)) {
67  uint16_t chip_id = buffer[2] + (buffer[3] << 8);
68  uint16_t project_id = buffer[0] + (buffer[1] << 8);
69  esph_log_config(TAG, "Chip ID %X, project ID %x", chip_id, project_id);
70  }
71  if (this->x_raw_max_ == 0 || this->y_raw_max_ == 0) {
72  if (this->read16_(0xD1F8, buffer, 4)) {
73  this->x_raw_max_ = buffer[0] + (buffer[1] << 8);
74  this->y_raw_max_ = buffer[2] + (buffer[3] << 8);
75  } else {
76  this->x_raw_max_ = this->display_->get_native_width();
77  this->y_raw_max_ = this->display_->get_native_height();
78  }
79  }
80  this->setup_complete_ = true;
81  esph_log_config(TAG, "CST226 Touchscreen setup complete");
82 }
83 
85  ESP_LOGCONFIG(TAG, "CST226 Touchscreen:");
86  LOG_I2C_DEVICE(this);
87  LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
88  LOG_PIN(" Reset Pin: ", this->reset_pin_);
89 }
90 
91 } // namespace cst226
92 } // namespace esphome
virtual void digital_write(bool value)=0
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
bool read16_(uint16_t addr, uint8_t *data, size_t len)
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 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
No error found during execution of method.
Definition: i2c_bus.h:13
void status_clear_warning()
Definition: component.cpp:166
ErrorCode write_register16(uint16_t a_register, const uint8_t *data, size_t len, bool stop=true)
write an array of bytes to a specific register in the I²C device
Definition: i2c.cpp:34
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
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26