ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
lilygo_t5_47_touchscreen.cpp
Go to the documentation of this file.
2
4#include "esphome/core/log.h"
5
7
8static const char *const TAG = "lilygo_t5_47.touchscreen";
9
10static const uint8_t POWER_REGISTER = 0xD6;
11static const uint8_t TOUCH_REGISTER = 0xD0;
12
13static const uint8_t WAKEUP_CMD[1] = {0x06};
14static const uint8_t READ_FLAGS[1] = {0x00};
15static const uint8_t CLEAR_FLAGS[2] = {0x00, 0xAB};
16static const uint8_t READ_TOUCH[1] = {0x07};
17
18#define ERROR_CHECK(err) \
19 if ((err) != i2c::ERROR_OK) { \
20 ESP_LOGE(TAG, "Failed to communicate!"); \
21 this->status_set_warning(); \
22 return; \
23 }
24
27 this->interrupt_pin_->setup();
28
30
31 if (this->write(nullptr, 0) != i2c::ERROR_OK) {
32 ESP_LOGE(TAG, "Failed to communicate!");
34 this->mark_failed();
35 return;
36 }
37
38 this->write_register(POWER_REGISTER, WAKEUP_CMD, 1);
39 if (this->display_ != nullptr) {
40 if (this->x_raw_max_ == this->x_raw_min_) {
41 this->x_raw_max_ = this->display_->get_native_width();
42 }
43 if (this->y_raw_max_ == this->y_raw_min_) {
44 this->y_raw_max_ = this->display_->get_native_height();
45 }
46 }
47}
48
50 uint8_t point = 0;
51 uint8_t buffer[40] = {0};
52
54 err = this->write_register(TOUCH_REGISTER, READ_FLAGS, 1);
55 ERROR_CHECK(err);
56
57 err = this->read(buffer, 7);
58 ERROR_CHECK(err);
59
60 if (buffer[0] == 0xAB) {
61 this->write_register(TOUCH_REGISTER, CLEAR_FLAGS, 2);
62 return;
63 }
64
65 point = buffer[5] & 0xF;
66 if (point > 2) {
67 ESP_LOGW(TAG, "Invalid touch point count: %d", point);
68 point = 2;
69 }
70
71 if (point == 1) {
72 err = this->write_register(TOUCH_REGISTER, READ_TOUCH, 1);
73 ERROR_CHECK(err);
74 err = this->read(&buffer[5], 2);
75 ERROR_CHECK(err);
76
77 } else if (point > 1) {
78 err = this->write_register(TOUCH_REGISTER, READ_TOUCH, 1);
79 ERROR_CHECK(err);
80 err = this->read(&buffer[5], 5 * (point - 1) + 3);
81 ERROR_CHECK(err);
82 }
83
84 this->write_register(TOUCH_REGISTER, CLEAR_FLAGS, 2);
85
86 if (point == 0)
87 point = 1;
88
89 uint16_t id, x_raw, y_raw;
90 for (uint8_t i = 0; i < point; i++) {
91 id = (buffer[i * 5] >> 4) & 0x0F;
92 y_raw = (uint16_t) ((buffer[i * 5 + 1] << 4) | ((buffer[i * 5 + 3] >> 4) & 0x0F));
93 x_raw = (uint16_t) ((buffer[i * 5 + 2] << 4) | (buffer[i * 5 + 3] & 0x0F));
94 this->add_raw_touch_position_(id, x_raw, y_raw);
95 }
96
98}
99
101 ESP_LOGCONFIG(TAG, "Lilygo T5 47 Touchscreen:");
102 LOG_I2C_DEVICE(this);
103 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
104}
105
106} // namespace esphome::lilygo_t5_47
void mark_failed()
Mark this component as failed.
void status_clear_warning()
Definition component.h:289
virtual void pin_mode(gpio::Flags flags)=0
virtual void setup()=0
virtual void detach_interrupt() const =0
int get_native_width()
Get the native (original) width of the display in pixels.
Definition display.h:330
int get_native_height()
Get the native (original) height of the display in pixels.
Definition display.h:332
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) const
writes an array of bytes to a specific register in the I²C device
Definition i2c.cpp:34
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:183
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:163
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.
void add_raw_touch_position_(uint8_t id, int16_t x_raw, int16_t y_raw, int16_t z_raw=0)
uint16_t id
@ INTERRUPT_FALLING_EDGE
Definition gpio.h:51
@ FLAG_PULLUP
Definition gpio.h:30
@ FLAG_INPUT
Definition gpio.h:27
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:12
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14