ESPHome  2024.11.0
lilygo_t5_47_touchscreen.cpp
Go to the documentation of this file.
2 
3 #include "esphome/core/helpers.h"
4 #include "esphome/core/log.h"
5 
6 namespace esphome {
7 namespace lilygo_t5_47 {
8 
9 static const char *const TAG = "lilygo_t5_47.touchscreen";
10 
11 static const uint8_t POWER_REGISTER = 0xD6;
12 static const uint8_t TOUCH_REGISTER = 0xD0;
13 
14 static const uint8_t WAKEUP_CMD[1] = {0x06};
15 static const uint8_t READ_FLAGS[1] = {0x00};
16 static const uint8_t CLEAR_FLAGS[2] = {0x00, 0xAB};
17 static const uint8_t READ_TOUCH[1] = {0x07};
18 
19 #define ERROR_CHECK(err) \
20  if ((err) != i2c::ERROR_OK) { \
21  ESP_LOGE(TAG, "Failed to communicate!"); \
22  this->status_set_warning(); \
23  return; \
24  }
25 
27  ESP_LOGCONFIG(TAG, "Setting up Lilygo T5 4.7 Touchscreen...");
29  this->interrupt_pin_->setup();
30 
32 
33  if (this->write(nullptr, 0) != i2c::ERROR_OK) {
34  ESP_LOGE(TAG, "Failed to communicate!");
36  this->mark_failed();
37  return;
38  }
39 
40  this->write_register(POWER_REGISTER, WAKEUP_CMD, 1);
41  if (this->display_ != nullptr) {
42  if (this->x_raw_max_ == this->x_raw_min_) {
43  this->x_raw_max_ = this->display_->get_native_width();
44  }
45  if (this->y_raw_max_ == this->y_raw_min_) {
46  this->x_raw_max_ = this->display_->get_native_height();
47  }
48  }
49 }
50 
52  uint8_t point = 0;
53  uint8_t buffer[40] = {0};
54 
55  i2c::ErrorCode err;
56  err = this->write_register(TOUCH_REGISTER, READ_FLAGS, 1);
57  ERROR_CHECK(err);
58 
59  err = this->read(buffer, 7);
60  ERROR_CHECK(err);
61 
62  if (buffer[0] == 0xAB) {
63  this->write_register(TOUCH_REGISTER, CLEAR_FLAGS, 2);
64  return;
65  }
66 
67  point = buffer[5] & 0xF;
68 
69  if (point == 1) {
70  err = this->write_register(TOUCH_REGISTER, READ_TOUCH, 1);
71  ERROR_CHECK(err);
72  err = this->read(&buffer[5], 2);
73  ERROR_CHECK(err);
74 
75  } else if (point > 1) {
76  err = this->write_register(TOUCH_REGISTER, READ_TOUCH, 1);
77  ERROR_CHECK(err);
78  err = this->read(&buffer[5], 5 * (point - 1) + 3);
79  ERROR_CHECK(err);
80  }
81 
82  this->write_register(TOUCH_REGISTER, CLEAR_FLAGS, 2);
83 
84  if (point == 0)
85  point = 1;
86 
87  uint16_t id, x_raw, y_raw;
88  for (uint8_t i = 0; i < point; i++) {
89  id = (buffer[i * 5] >> 4) & 0x0F;
90  y_raw = (uint16_t) ((buffer[i * 5 + 1] << 4) | ((buffer[i * 5 + 3] >> 4) & 0x0F));
91  x_raw = (uint16_t) ((buffer[i * 5 + 2] << 4) | (buffer[i * 5 + 3] & 0x0F));
92  this->add_raw_touch_position_(id, x_raw, y_raw);
93  }
94 
95  this->status_clear_warning();
96 }
97 
99  ESP_LOGCONFIG(TAG, "Lilygo T5 47 Touchscreen:");
100  LOG_I2C_DEVICE(this);
101  LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
102 }
103 
104 } // namespace lilygo_t5_47
105 } // namespace esphome
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
Definition: i2c.h:160
int get_native_height()
Get the native (original) height of the display in pixels.
Definition: display.h:223
virtual void pin_mode(gpio::Flags flags)=0
T id(T value)
Helper function to make id(var) known from lambdas work in custom components.
Definition: helpers.h:719
virtual void setup()=0
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
Definition: i2c.h:186
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
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
virtual void detach_interrupt() const =0
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a specific register in the I²C device
Definition: i2c.cpp:25
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition: i2c_bus.h:11