ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ektf2232.cpp
Go to the documentation of this file.
1#include "ektf2232.h"
3#include "esphome/core/log.h"
4
5#include <vector>
6
8
9ESPHOME_LOG_TAG(TAG, "ektf2232");
10
11static const uint8_t SOFT_RESET_CMD[4] = {0x77, 0x77, 0x77, 0x77};
12static const uint8_t HELLO[4] = {0x55, 0x55, 0x55, 0x55};
13static const uint8_t GET_X_RES[4] = {0x53, 0x60, 0x00, 0x00};
14static const uint8_t GET_Y_RES[4] = {0x53, 0x63, 0x00, 0x00};
15static const uint8_t GET_POWER_STATE_CMD[4] = {0x53, 0x50, 0x00, 0x01};
16
19 this->interrupt_pin_->setup();
20
22
23 pulse_reset(this->reset_pin_, {.low_ms = 15, .high_after_ms = 15});
24 if (!this->soft_reset_()) {
25 ESP_LOGE(TAG, "Failed to soft reset EKT2232!");
27 this->mark_failed();
28 return;
29 }
30
31 // Get touch resolution
32 uint8_t received[4];
33 if (this->x_raw_max_ == 0 || this->y_raw_max_ == 0) {
34 auto err = this->write(GET_X_RES, 4);
35 if (err == i2c::ERROR_OK) {
36 err = this->read(received, 4);
37 if (err == i2c::ERROR_OK) {
38 this->x_raw_max_ = ((received[2])) | ((received[3] & 0xf0) << 4);
39 err = this->write(GET_Y_RES, 4);
40 if (err == i2c::ERROR_OK) {
41 err = this->read(received, 4);
42 if (err == i2c::ERROR_OK) {
43 this->y_raw_max_ = ((received[2])) | ((received[3] & 0xf0) << 4);
44 }
45 }
46 }
47 }
48 if (err != i2c::ERROR_OK) {
49 ESP_LOGE(TAG, "Failed to read calibration values!");
51 this->mark_failed();
52 return;
53 }
54 if (this->swap_x_y_)
55 std::swap(this->x_raw_max_, this->y_raw_max_);
56 }
57 this->set_power_state(true);
58}
59
61 uint8_t touch_count = 0;
62 int16_t x_raw, y_raw;
63
64 uint8_t raw[8];
65 this->read(raw, 8);
66 for (int i = 0; i < 8; i++) {
67 if (raw[7] & (1 << i))
68 touch_count++;
69 }
70
71 touch_count = std::min<uint8_t>(touch_count, 2);
72
73 ESP_LOGV(TAG, "Touch count: %d", touch_count);
74
75 for (int i = 0; i < touch_count; i++) {
76 uint8_t *d = raw + 1 + (i * 3);
77 x_raw = (d[0] & 0xF0) << 4 | d[1];
78 y_raw = (d[0] & 0x0F) << 8 | d[2];
79 this->add_raw_touch_position_(i, x_raw, y_raw);
80 }
81}
82
84 uint8_t data[] = {0x54, 0x50, 0x00, 0x01};
85 data[1] |= (enable << 3);
86 this->write(data, 4);
87}
88
90 uint8_t received[4];
91 this->write(GET_POWER_STATE_CMD, 4);
92 this->store_.touched = false;
93 this->read(received, 4);
94 return (received[1] >> 3) & 1;
95}
96
98 auto err = this->write(SOFT_RESET_CMD, 4);
99 if (err != i2c::ERROR_OK)
100 return false;
101
102 uint8_t received[4];
103 uint16_t timeout = 1000;
104 while (!this->store_.touched && timeout > 0) {
105 delay(1);
106 timeout--;
107 }
108 if (timeout > 0)
109 this->store_.touched = true;
110 this->read(received, 4);
111 this->store_.touched = false;
112
113 return !memcmp(received, HELLO, 4);
114}
115
117 ESP_LOGCONFIG(TAG, "EKT2232 Touchscreen:");
118 LOG_I2C_DEVICE(this);
119 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
120 LOG_PIN(" Reset Pin: ", this->reset_pin_);
121}
122
123} // namespace esphome::ektf2232
uint8_t raw[35]
Definition bl0939.h:0
void mark_failed()
Mark this component as failed.
void pin_mode(gpio::Flags flags) override
Definition gpio.cpp:195
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
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:175
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)
ESPHOME_LOG_TAG(TAG, "ektf2232")
@ INTERRUPT_FALLING_EDGE
Definition gpio_pin.h:53
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
void HOT delay(uint32_t ms)
Definition hal.cpp:85
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:163