ESPHome 2025.9.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
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
28 this->interrupt_pin_->setup();
29
31
32 if (this->write(nullptr, 0) != i2c::ERROR_OK) {
33 ESP_LOGE(TAG, "Failed to communicate!");
35 this->mark_failed();
36 return;
37 }
38
39 this->write_register(POWER_REGISTER, WAKEUP_CMD, 1);
40 if (this->display_ != nullptr) {
41 if (this->x_raw_max_ == this->x_raw_min_) {
42 this->x_raw_max_ = this->display_->get_native_width();
43 }
44 if (this->y_raw_max_ == this->y_raw_min_) {
45 this->x_raw_max_ = this->display_->get_native_height();
46 }
47 }
48}
49
51 uint8_t point = 0;
52 uint8_t buffer[40] = {0};
53
55 err = this->write_register(TOUCH_REGISTER, READ_FLAGS, 1);
56 ERROR_CHECK(err);
57
58 err = this->read(buffer, 7);
59 ERROR_CHECK(err);
60
61 if (buffer[0] == 0xAB) {
62 this->write_register(TOUCH_REGISTER, CLEAR_FLAGS, 2);
63 return;
64 }
65
66 point = buffer[5] & 0xF;
67
68 if (point == 1) {
69 err = this->write_register(TOUCH_REGISTER, READ_TOUCH, 1);
70 ERROR_CHECK(err);
71 err = this->read(&buffer[5], 2);
72 ERROR_CHECK(err);
73
74 } else if (point > 1) {
75 err = this->write_register(TOUCH_REGISTER, READ_TOUCH, 1);
76 ERROR_CHECK(err);
77 err = this->read(&buffer[5], 5 * (point - 1) + 3);
78 ERROR_CHECK(err);
79 }
80
81 this->write_register(TOUCH_REGISTER, CLEAR_FLAGS, 2);
82
83 if (point == 0)
84 point = 1;
85
86 uint16_t id, x_raw, y_raw;
87 for (uint8_t i = 0; i < point; i++) {
88 id = (buffer[i * 5] >> 4) & 0x0F;
89 y_raw = (uint16_t) ((buffer[i * 5 + 1] << 4) | ((buffer[i * 5 + 3] >> 4) & 0x0F));
90 x_raw = (uint16_t) ((buffer[i * 5 + 2] << 4) | (buffer[i * 5 + 3] & 0x0F));
91 this->add_raw_touch_position_(id, x_raw, y_raw);
92 }
93
95}
96
98 ESP_LOGCONFIG(TAG, "Lilygo T5 47 Touchscreen:");
99 LOG_I2C_DEVICE(this);
100 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
101}
102
103} // namespace lilygo_t5_47
104} // 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:933