ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
gt9895_touchscreen.cpp
Go to the documentation of this file.
2
3#include <memory>
4
6#include "esphome/core/log.h"
7
8namespace esphome::gt9895 {
9
10ESPHOME_LOG_TAG(TAG, "gt9895.touchscreen");
11
12static constexpr uint32_t INFO_TIMEOUT_ID = 0;
13// The controller answers about 100 ms after its reset is released
14static constexpr uint32_t RESET_STARTUP_MS = 100;
15
16// Offsets in the runtime information block
17static constexpr size_t RUNTIME_ARRAYS_OFFSET = 32; // length, version, features and fixed parameters
18static constexpr size_t RUNTIME_ARRAY_COUNT = 5;
19static constexpr size_t RUNTIME_MISC_SIZE = 48;
20static constexpr size_t RUNTIME_TOUCH_ADDRESS_OFFSET = 44; // in the miscellaneous part after the arrays
21// The Wire buffer on ESP8266; longer reads are split
22static constexpr size_t READ_CHUNK_SIZE = 128;
23
25 if (this->reset_pin_ != nullptr) {
26 pulse_reset(this->reset_pin_, {.high_before_ms = 1, .low_ms = 10});
27 this->set_timeout(INFO_TIMEOUT_ID, RESET_STARTUP_MS, [this]() { this->read_info_(); });
28 } else {
29 this->read_info_();
30 }
31}
32
34 uint8_t fw[GT9895_FW_VERSION_SIZE];
35 if (!this->read_(GT9895_REG_FW_VERSION, fw, sizeof(fw)) || !checksum_ok(fw, sizeof(fw))) {
36 this->mark_failed(LOG_STR("Failed to read the firmware version"));
37 return;
38 }
39 memcpy(this->product_id_, fw + 10, 8);
40 this->firmware_version_ = encode_uint32(fw[21], fw[20], fw[19], fw[18]);
41
42 // The runtime information gives the address of the touch reports
43 uint8_t len_bytes[2];
44 if (!this->read_(GT9895_REG_RUNTIME_INFO, len_bytes, sizeof(len_bytes))) {
45 this->mark_failed(LOG_STR("Failed to read the runtime information"));
46 return;
47 }
48 const size_t len = encode_uint16(len_bytes[1], len_bytes[0]);
49 if (len < RUNTIME_ARRAYS_OFFSET + RUNTIME_ARRAY_COUNT + RUNTIME_MISC_SIZE + GT9895_CHECKSUM_SIZE ||
50 len > GT9895_RUNTIME_INFO_MAX_SIZE) {
51 ESP_LOGE(TAG, "Runtime information length %u", (unsigned) len);
52 this->mark_failed(LOG_STR("Invalid runtime information"));
53 return;
54 }
55 auto info = std::make_unique<uint8_t[]>(len);
56 if (!this->read_(GT9895_REG_RUNTIME_INFO, info.get(), len) || !checksum_ok(info.get(), len)) {
57 this->mark_failed(LOG_STR("Failed to read the runtime information"));
58 return;
59 }
60 const size_t end = len - GT9895_CHECKSUM_SIZE;
61 size_t offset = RUNTIME_ARRAYS_OFFSET;
62 for (size_t i = 0; i != RUNTIME_ARRAY_COUNT && offset < end; i++)
63 offset += 1 + info[offset] * 2;
64 if (offset + RUNTIME_MISC_SIZE > end) {
65 this->mark_failed(LOG_STR("Invalid runtime information"));
66 return;
67 }
68 const uint8_t *addr = &info[offset + RUNTIME_TOUCH_ADDRESS_OFFSET];
69 this->touch_data_address_ = encode_uint32(addr[3], addr[2], addr[1], addr[0]);
70 if (this->touch_data_address_ == 0) {
71 this->mark_failed(LOG_STR("Invalid runtime information"));
72 return;
73 }
74
75 if (this->interrupt_pin_ != nullptr) {
76 this->interrupt_pin_->setup();
78 // A report written before the interrupt was attached sends no further interrupt until it is cleared
79 this->store_.touched = true;
80 }
81}
82
84 if (this->touch_data_address_ == 0) {
85 this->skip_update_ = true;
86 return;
87 }
88 uint8_t data[GT9895_HEADER_SIZE + GT9895_MAX_TOUCHES * GT9895_CONTACT_SIZE + GT9895_CHECKSUM_SIZE];
89 size_t len = GT9895_HEADER_SIZE + GT9895_FIRST_READ_CONTACTS * GT9895_CONTACT_SIZE + GT9895_CHECKSUM_SIZE;
90 if (!this->status_comm_check(this->read_(this->touch_data_address_, data, len))) {
91 this->comm_failed_();
92 return;
93 }
94 // No new report since the last one was cleared: keep the current touches
95 if (data[0] == 0) {
97 this->skip_update_ = true;
98 return;
99 }
100
101 const uint8_t count = clamp_at_most<uint8_t>(data[2] & 0x0F, GT9895_MAX_TOUCHES);
102 if (!checksum_ok(data, GT9895_HEADER_SIZE)) {
103 // Still clear the report below, or the controller will not write the next one
104 ESP_LOGV(TAG, "Bad header checksum");
105 this->skip_update_ = true;
106 } else if ((data[0] & GT9895_EVENT_TOUCH) == 0) {
107 // Another kind of event: it has no touch state, so it must not end the current touches
108 this->skip_update_ = true;
109 } else if (count != 0) {
110 const size_t needed = GT9895_HEADER_SIZE + count * GT9895_CONTACT_SIZE + GT9895_CHECKSUM_SIZE;
111 if (needed > len) {
112 if (!this->status_comm_check(this->read_(this->touch_data_address_ + len, data + len, needed - len))) {
113 this->comm_failed_();
114 return;
115 }
116 }
117 const uint8_t *contacts = data + GT9895_HEADER_SIZE;
118 if (!checksum_ok(contacts, count * GT9895_CONTACT_SIZE + GT9895_CHECKSUM_SIZE)) {
119 ESP_LOGV(TAG, "Bad contact checksum");
120 this->skip_update_ = true;
121 } else {
122 for (uint8_t i = 0; i != count; i++) {
123 const uint8_t *p = contacts + i * GT9895_CONTACT_SIZE;
124 const uint16_t x = encode_uint16(p[3], p[2]);
125 const uint16_t y = encode_uint16(p[5], p[4]);
126 const uint16_t pressure = encode_uint16(p[7], p[6]);
127 ESP_LOGV(TAG, "Touch %u: x=%u, y=%u, pressure=%u", p[0] >> 4, x, y, pressure);
128 this->add_raw_touch_position_(p[0] >> 4, x, y, pressure);
129 }
130 }
131 }
132
133 // Clear the report so the controller can write the next one
134 uint8_t clear[5];
135 put_be32(clear, this->touch_data_address_);
136 clear[4] = 0;
137 // The contacts are still delivered; the uncleared report is read again on the next loop
138 if (!this->status_comm_result(this->write(clear, sizeof(clear)) == i2c::ERROR_OK))
139 this->read_again_();
140}
141
143 this->skip_update_ = true;
144 this->read_again_();
145}
146
148 // With an interrupt the base class stops polling, and the controller sends no new interrupt while a report is
149 // pending
150 if (this->store_.init)
151 this->store_.touched = true;
152}
153
154bool GT9895Touchscreen::read_(uint32_t address, uint8_t *data, size_t len) {
155 while (len != 0) {
156 const size_t chunk = clamp_at_most(len, READ_CHUNK_SIZE);
157 uint8_t addr[4];
158 put_be32(addr, address);
159 if (this->write_read(addr, sizeof(addr), data, chunk) != i2c::ERROR_OK)
160 return false;
161 address += chunk;
162 data += chunk;
163 len -= chunk;
164 }
165 return true;
166}
167
168bool GT9895Touchscreen::checksum_ok(const uint8_t *data, size_t len) {
169 const size_t body = len - GT9895_CHECKSUM_SIZE;
170 return sum16_bytes(data, body) == encode_uint16(data[body + 1], data[body]);
171}
172
174 ESP_LOGCONFIG(TAG,
175 "GT9895 Touchscreen:\n"
176 " Product ID: %s\n"
177 " Firmware version: 0x%08" PRIX32 "\n"
178 " X Raw Min: %d, X Raw Max: %d\n"
179 " Y Raw Min: %d, Y Raw Max: %d",
180 this->product_id_, this->firmware_version_, this->x_raw_min_, this->x_raw_max_, this->y_raw_min_,
181 this->y_raw_max_);
182 LOG_I2C_DEVICE(this);
183 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
184 LOG_PIN(" Reset Pin: ", this->reset_pin_);
185}
186
187} // namespace esphome::gt9895
void mark_failed()
Mark this component as failed.
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
bool status_comm_check(bool ok)
Sets the warning ("Communication failed") when not ok but never clears it; returns ok.
Definition component.h:309
void status_clear_warning()
Definition component.h:318
void set_timeout(const char *name, uint32_t timeout, SchedulerCallback &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
bool read_(uint32_t address, uint8_t *data, size_t len)
static bool checksum_ok(const uint8_t *data, size_t len)
ErrorCode write_read(const uint8_t *write_data, size_t write_len, uint8_t *read_data, size_t read_len) const
writes an array of bytes to a device, then reads an array, as a single transaction
Definition i2c.h:205
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:195
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)
unsigned count
Definition gif.cpp:51
@ INTERRUPT_FALLING_EDGE
Definition gpio_pin.h:53
ESPHOME_LOG_TAG(TAG, "gt9895.touchscreen")
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
T clamp_at_most(T value, U max)
Definition helpers.h:2859
const void size_t len
Definition hal.h:68
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
Definition helpers.h:1235
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:1227
constexpr uint16_t sum16_bytes(const uint8_t *data, size_t len, uint16_t sum=0)
Add up the bytes of data with size len, truncated to 16 bits.
Definition helpers.h:1091
constexpr uint8_t * put_be32(uint8_t *dst, uint32_t value)
Definition helpers.h:1307
ESPHOME_ALWAYS_INLINE void pulse_reset(GPIOPin *pin, ResetPulse pulse)
Set up an active-low reset pin and drive it through pulse.
Definition gpio_pin.h:214
static void uint32_t
uint8_t end[39]
Definition sun_gtil2.cpp:17
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6
uint8_t pressure
Definition tt21100.cpp:7