7 namespace lilygo_t5_47 {
9 static const char *
const TAG =
"lilygo_t5_47.touchscreen";
11 static const uint8_t POWER_REGISTER = 0xD6;
12 static const uint8_t TOUCH_REGISTER = 0xD0;
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};
19 #define ERROR_CHECK(err) \ 20 if ((err) != i2c::ERROR_OK) { \ 21 ESP_LOGE(TAG, "Failed to communicate!"); \ 22 this->status_set_warning(); \ 29 ESP_LOGCONFIG(TAG,
"Setting up Lilygo T5 4.7 Touchscreen...");
31 this->interrupt_pin_->setup();
33 this->store_.pin = this->interrupt_pin_->to_isr();
37 ESP_LOGE(TAG,
"Failed to communicate!");
38 this->interrupt_pin_->detach_interrupt();
43 this->write_register(POWER_REGISTER, WAKEUP_CMD, 1);
47 if (!this->store_.touch) {
48 for (
auto *listener : this->touch_listeners_)
52 this->store_.touch =
false;
55 uint8_t buffer[40] = {0};
56 uint32_t sum_l = 0, sum_h = 0;
59 err = this->write_register(TOUCH_REGISTER, READ_FLAGS, 1);
62 err = this->read(buffer, 7);
65 if (buffer[0] == 0xAB) {
66 this->write_register(TOUCH_REGISTER, CLEAR_FLAGS, 2);
70 point = buffer[5] & 0xF;
73 for (
auto *listener : this->touch_listeners_)
76 }
else if (point == 1) {
77 err = this->write_register(TOUCH_REGISTER, READ_TOUCH, 1);
79 err = this->read(&buffer[5], 2);
82 sum_l = buffer[5] << 8 | buffer[6];
83 }
else if (point > 1) {
84 err = this->write_register(TOUCH_REGISTER, READ_TOUCH, 1);
86 err = this->read(&buffer[5], 5 * (point - 1) + 3);
89 sum_l = buffer[5 * point + 1] << 8 | buffer[5 * point + 2];
92 this->write_register(TOUCH_REGISTER, CLEAR_FLAGS, 2);
94 for (
int i = 0; i < 5 * point; i++)
102 for (
int i = 0; i < point; i++) {
111 tp.
id = (buffer[i * 5 + offset] >> 4) & 0x0F;
112 tp.
state = buffer[i * 5 + offset] & 0x0F;
113 if (tp.
state == 0x06)
116 uint16_t y = (uint16_t)((buffer[i * 5 + 1 + offset] << 4) | ((buffer[i * 5 + 3 + offset] >> 4) & 0x0F));
117 uint16_t x = (uint16_t)((buffer[i * 5 + 2 + offset] << 4) | (buffer[i * 5 + 3 + offset] & 0x0F));
119 switch (this->rotation_) {
121 tp.
y = this->display_height_ - y;
125 tp.
x = this->display_height_ - y;
126 tp.
y = this->display_width_ - x;
130 tp.
x = this->display_width_ - x;
138 this->defer([
this, tp]() { this->send_touch_(tp); });
142 tp.
id = (buffer[0] >> 4) & 0x0F;
145 uint16_t y = (uint16_t)((buffer[0 * 5 + 1] << 4) | ((buffer[0 * 5 + 3] >> 4) & 0x0F));
146 uint16_t x = (uint16_t)((buffer[0 * 5 + 2] << 4) | (buffer[0 * 5 + 3] & 0x0F));
148 switch (this->rotation_) {
150 tp.
y = this->display_height_ - y;
154 tp.
x = this->display_height_ - y;
155 tp.
y = this->display_width_ - x;
159 tp.
x = this->display_width_ - x;
167 this->defer([
this, tp]() { this->send_touch_(tp); });
170 this->status_clear_warning();
174 ESP_LOGCONFIG(TAG,
"Lilygo T5 47 Touchscreen:");
175 LOG_I2C_DEVICE(
this);
176 LOG_PIN(
" Interrupt Pin: ", this->interrupt_pin_);
void dump_config() override
static void gpio_intr(Store *store)