ESPHome 2026.6.0-dev
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
9static const char *const 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 this->reset_pin_->setup();
24
25 this->hard_reset_();
26 if (!this->soft_reset_()) {
27 ESP_LOGE(TAG, "Failed to soft reset EKT2232!");
29 this->mark_failed();
30 return;
31 }
32
33 // Get touch resolution
34 uint8_t received[4];
35 if (this->x_raw_max_ == 0 || this->y_raw_max_ == 0) {
36 auto err = this->write(GET_X_RES, 4);
37 if (err == i2c::ERROR_OK) {
38 err = this->read(received, 4);
39 if (err == i2c::ERROR_OK) {
40 this->x_raw_max_ = ((received[2])) | ((received[3] & 0xf0) << 4);
41 err = this->write(GET_Y_RES, 4);
42 if (err == i2c::ERROR_OK) {
43 err = this->read(received, 4);
44 if (err == i2c::ERROR_OK) {
45 this->y_raw_max_ = ((received[2])) | ((received[3] & 0xf0) << 4);
46 }
47 }
48 }
49 }
50 if (err != i2c::ERROR_OK) {
51 ESP_LOGE(TAG, "Failed to read calibration values!");
53 this->mark_failed();
54 return;
55 }
56 if (this->swap_x_y_)
57 std::swap(this->x_raw_max_, this->y_raw_max_);
58 }
59 this->set_power_state(true);
60}
61
63 uint8_t touch_count = 0;
64 int16_t x_raw, y_raw;
65
66 uint8_t raw[8];
67 this->read(raw, 8);
68 for (int i = 0; i < 8; i++) {
69 if (raw[7] & (1 << i))
70 touch_count++;
71 }
72
73 touch_count = std::min<uint8_t>(touch_count, 2);
74
75 ESP_LOGV(TAG, "Touch count: %d", touch_count);
76
77 for (int i = 0; i < touch_count; i++) {
78 uint8_t *d = raw + 1 + (i * 3);
79 x_raw = (d[0] & 0xF0) << 4 | d[1];
80 y_raw = (d[0] & 0x0F) << 8 | d[2];
81 this->add_raw_touch_position_(i, x_raw, y_raw);
82 }
83}
84
86 uint8_t data[] = {0x54, 0x50, 0x00, 0x01};
87 data[1] |= (enable << 3);
88 this->write(data, 4);
89}
90
92 uint8_t received[4];
93 this->write(GET_POWER_STATE_CMD, 4);
94 this->store_.touched = false;
95 this->read(received, 4);
96 return (received[1] >> 3) & 1;
97}
98
100 this->reset_pin_->digital_write(false);
101 delay(15);
102 this->reset_pin_->digital_write(true);
103 delay(15);
104}
105
107 auto err = this->write(SOFT_RESET_CMD, 4);
108 if (err != i2c::ERROR_OK)
109 return false;
110
111 uint8_t received[4];
112 uint16_t timeout = 1000;
113 while (!this->store_.touched && timeout > 0) {
114 delay(1);
115 timeout--;
116 }
117 if (timeout > 0)
118 this->store_.touched = true;
119 this->read(received, 4);
120 this->store_.touched = false;
121
122 return !memcmp(received, HELLO, 4);
123}
124
126 ESP_LOGCONFIG(TAG, "EKT2232 Touchscreen:");
127 LOG_I2C_DEVICE(this);
128 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
129 LOG_PIN(" Reset Pin: ", this->reset_pin_);
130}
131
132} // namespace esphome::ektf2232
uint8_t raw[35]
Definition bl0939.h:0
void mark_failed()
Mark this component as failed.
virtual void pin_mode(gpio::Flags flags)=0
virtual void setup()=0
virtual void digital_write(bool value)=0
virtual void detach_interrupt() const =0
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:183
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:163
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:51
@ FLAG_PULLUP
Definition gpio.h:30
@ FLAG_INPUT
Definition gpio.h:27
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14
void HOT delay(uint32_t ms)
Definition hal.cpp:85