ESPHome  2024.5.0
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  this->reset_pin_->setup();
9  this->reset_pin_->digital_write(true);
10  delay(5);
11  this->reset_pin_->digital_write(false);
12  delay(5);
13  this->reset_pin_->digital_write(true);
14  this->set_timeout(30, [this] { this->continue_setup_(); });
15 }
16 
18  uint8_t data[28];
19  if (!this->read_bytes(CST226_REG_STATUS, data, sizeof data)) {
20  this->status_set_warning();
21  this->skip_update_ = true;
22  return;
23  }
24  this->status_clear_warning();
25  if (data[6] != 0xAB || data[0] == 0xAB || data[5] == 0x80) {
26  this->skip_update_ = true;
27  return;
28  }
29  uint8_t num_of_touches = data[5] & 0x7F;
30  if (num_of_touches == 0 || num_of_touches > 5) {
31  this->write_byte(0, 0xAB);
32  return;
33  }
34 
35  size_t index = 0;
36  for (uint8_t i = 0; i != num_of_touches; i++) {
37  uint8_t id = data[index] >> 4;
38  int16_t x = (data[index + 1] << 4) | ((data[index + 3] >> 4) & 0x0F);
39  int16_t y = (data[index + 2] << 4) | (data[index + 3] & 0x0F);
40  int16_t z = data[index + 4];
41  this->add_raw_touch_position_(id, x, y, z);
42  esph_log_v(TAG, "Read touch %d: %d/%d", id, x, y);
43  index += 5;
44  if (i == 0)
45  index += 2;
46  }
47 }
48 
50  uint8_t buffer[8];
51  if (this->interrupt_pin_ != nullptr) {
52  this->interrupt_pin_->setup();
54  }
55  buffer[0] = 0xD1;
56  if (this->write_register16(0xD1, buffer, 1) != i2c::ERROR_OK) {
57  esph_log_e(TAG, "Write byte to 0xD1 failed");
58  this->mark_failed();
59  return;
60  }
61  delay(10);
62  if (this->read16_(0xD204, buffer, 4)) {
63  uint16_t chip_id = buffer[2] + (buffer[3] << 8);
64  uint16_t project_id = buffer[0] + (buffer[1] << 8);
65  esph_log_config(TAG, "Chip ID %X, project ID %x", chip_id, project_id);
66  }
67  if (this->x_raw_max_ == 0 || this->y_raw_max_ == 0) {
68  if (this->read16_(0xD1F8, buffer, 4)) {
69  this->x_raw_max_ = buffer[0] + (buffer[1] << 8);
70  this->y_raw_max_ = buffer[2] + (buffer[3] << 8);
71  } else {
72  this->x_raw_max_ = this->display_->get_native_width();
73  this->y_raw_max_ = this->display_->get_native_height();
74  }
75  }
76  this->setup_complete_ = true;
77  esph_log_config(TAG, "CST226 Touchscreen setup complete");
78 }
79 
81  ESP_LOGCONFIG(TAG, "CST226 Touchscreen:");
82  LOG_I2C_DEVICE(this);
83  LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
84  LOG_PIN(" Reset Pin: ", this->reset_pin_);
85 }
86 
87 } // namespace cst226
88 } // 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: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
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
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26