12static constexpr uint32_t INFO_TIMEOUT_ID = 0;
14static constexpr uint32_t RESET_STARTUP_MS = 100;
17static constexpr size_t RUNTIME_ARRAYS_OFFSET = 32;
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;
22static constexpr size_t READ_CHUNK_SIZE = 128;
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"));
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"));
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"));
55 auto info = std::make_unique<uint8_t[]>(
len);
57 this->
mark_failed(LOG_STR(
"Failed to read the runtime information"));
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"));
68 const uint8_t *addr = &info[offset + RUNTIME_TOUCH_ADDRESS_OFFSET];
71 this->
mark_failed(LOG_STR(
"Invalid runtime information"));
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;
104 ESP_LOGV(TAG,
"Bad header checksum");
106 }
else if ((data[0] & GT9895_EVENT_TOUCH) == 0) {
109 }
else if (count != 0) {
110 const size_t needed = GT9895_HEADER_SIZE +
count * GT9895_CONTACT_SIZE + GT9895_CHECKSUM_SIZE;
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");
122 for (uint8_t i = 0; i !=
count; i++) {
123 const uint8_t *p = contacts + i * GT9895_CONTACT_SIZE;
127 ESP_LOGV(TAG,
"Touch %u: x=%u, y=%u, pressure=%u", p[0] >> 4,
x,
y,
pressure);
169 const size_t body =
len - GT9895_CHECKSUM_SIZE;
175 "GT9895 Touchscreen:\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",
182 LOG_I2C_DEVICE(
this);
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.
void status_clear_warning()
void set_timeout(const char *name, uint32_t timeout, SchedulerCallback &&f)
Set a timeout function with a const char* name.
uint32_t touch_data_address_
void update_touches() override
uint32_t firmware_version_
void dump_config() override
bool read_(uint32_t address, uint8_t *data, size_t len)
static bool checksum_ok(const uint8_t *data, size_t len)
InternalGPIOPin * interrupt_pin_
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
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
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.
TouchscreenInterrupt store_
void add_raw_touch_position_(uint8_t id, int16_t x_raw, int16_t y_raw, int16_t z_raw=0)
ESPHOME_LOG_TAG(TAG, "gt9895.touchscreen")
@ ERROR_OK
No error found during execution of method.
T clamp_at_most(T value, U max)
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.
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
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.
constexpr uint8_t * put_be32(uint8_t *dst, uint32_t value)
ESPHOME_ALWAYS_INLINE void pulse_reset(GPIOPin *pin, ResetPulse pulse)
Set up an active-low reset pin and drive it through pulse.