ESPHome 2025.5.0
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
7namespace esphome {
8namespace ektf2232 {
9
10static const char *const TAG = "ektf2232";
11
12static const uint8_t SOFT_RESET_CMD[4] = {0x77, 0x77, 0x77, 0x77};
13static const uint8_t HELLO[4] = {0x55, 0x55, 0x55, 0x55};
14static const uint8_t GET_X_RES[4] = {0x53, 0x60, 0x00, 0x00};
15static const uint8_t GET_Y_RES[4] = {0x53, 0x63, 0x00, 0x00};
16static const uint8_t GET_POWER_STATE_CMD[4] = {0x53, 0x50, 0x00, 0x01};
17
19 ESP_LOGCONFIG(TAG, "Setting up EKT2232 Touchscreen...");
21 this->interrupt_pin_->setup();
22
24
25 this->rts_pin_->setup();
26
27 this->hard_reset_();
28 if (!this->soft_reset_()) {
29 ESP_LOGE(TAG, "Failed to soft reset EKT2232!");
31 this->mark_failed();
32 return;
33 }
34
35 // Get touch resolution
36 uint8_t received[4];
37 if (this->x_raw_max_ == 0 || this->y_raw_max_ == 0) {
38 auto err = this->write(GET_X_RES, 4);
39 if (err == i2c::ERROR_OK) {
40 err = this->read(received, 4);
41 if (err == i2c::ERROR_OK) {
42 this->x_raw_max_ = ((received[2])) | ((received[3] & 0xf0) << 4);
43 err = this->write(GET_Y_RES, 4);
44 if (err == i2c::ERROR_OK) {
45 err = this->read(received, 4);
46 if (err == i2c::ERROR_OK) {
47 this->y_raw_max_ = ((received[2])) | ((received[3] & 0xf0) << 4);
48 }
49 }
50 }
51 }
52 if (err != i2c::ERROR_OK) {
53 ESP_LOGE(TAG, "Failed to read calibration values!");
55 this->mark_failed();
56 return;
57 }
58 if (this->swap_x_y_)
59 std::swap(this->x_raw_max_, this->y_raw_max_);
60 }
61 this->set_power_state(true);
62}
63
65 uint8_t touch_count = 0;
66 int16_t x_raw, y_raw;
67
68 uint8_t raw[8];
69 this->read(raw, 8);
70 for (int i = 0; i < 8; i++) {
71 if (raw[7] & (1 << i))
72 touch_count++;
73 }
74
75 touch_count = std::min<uint8_t>(touch_count, 2);
76
77 ESP_LOGV(TAG, "Touch count: %d", touch_count);
78
79 for (int i = 0; i < touch_count; i++) {
80 uint8_t *d = raw + 1 + (i * 3);
81 x_raw = (d[0] & 0xF0) << 4 | d[1];
82 y_raw = (d[0] & 0x0F) << 8 | d[2];
83 this->add_raw_touch_position_(i, x_raw, y_raw);
84 }
85}
86
88 uint8_t data[] = {0x54, 0x50, 0x00, 0x01};
89 data[1] |= (enable << 3);
90 this->write(data, 4);
91}
92
94 uint8_t received[4];
95 this->write(GET_POWER_STATE_CMD, 4);
96 this->store_.touched = false;
97 this->read(received, 4);
98 return (received[1] >> 3) & 1;
99}
100
102 this->rts_pin_->digital_write(false);
103 delay(15);
104 this->rts_pin_->digital_write(true);
105 delay(15);
106}
107
109 auto err = this->write(SOFT_RESET_CMD, 4);
110 if (err != i2c::ERROR_OK)
111 return false;
112
113 uint8_t received[4];
114 uint16_t timeout = 1000;
115 while (!this->store_.touched && timeout > 0) {
116 delay(1);
117 timeout--;
118 }
119 if (timeout > 0)
120 this->store_.touched = true;
121 this->read(received, 4);
122 this->store_.touched = false;
123
124 return !memcmp(received, HELLO, 4);
125}
126
128 ESP_LOGCONFIG(TAG, "EKT2232 Touchscreen:");
129 LOG_I2C_DEVICE(this);
130 LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
131 LOG_PIN(" RTS Pin: ", this->rts_pin_);
132}
133
134} // namespace ektf2232
135} // namespace esphome
uint8_t raw[35]
Definition bl0939.h:0
virtual 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, 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
@ 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
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:28