ESPHome 2025.5.0
Loading...
Searching...
No Matches
lilygo_t5_47_touchscreen.cpp
Go to the documentation of this file.
2
4#include "esphome/core/log.h"
5
6namespace esphome {
7namespace lilygo_t5_47 {
8
9static const char *const TAG = "lilygo_t5_47.touchscreen";
10
11static const uint8_t POWER_REGISTER = 0xD6;
12static const uint8_t TOUCH_REGISTER = 0xD0;
13
14static const uint8_t WAKEUP_CMD[1] = {0x06};
15static const uint8_t READ_FLAGS[1] = {0x00};
16static const uint8_t CLEAR_FLAGS[2] = {0x00, 0xAB};
17static 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
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
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
virtual void mark_failed()
Mark this component as failed.
void status_clear_warning()
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:221
int get_native_height()
Get the native (original) height of the display in pixels.
Definition display.h:223
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 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:190
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
Definition i2c.h:164
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)
@ INTERRUPT_FALLING_EDGE
Definition gpio.h:42
@ FLAG_PULLUP
Definition gpio.h:21
@ FLAG_INPUT
Definition gpio.h:18
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:11
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
T id(T value)
Helper function to make id(var) known from lambdas work in custom components.
Definition helpers.h:798